Skip to content

Commit

Permalink
Add argument for temp file path/prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
vaishalikumanan committed Aug 6, 2021
1 parent cde0982 commit 8a7db24
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ OPTIONS:
-g, --gid Group id to run with
-s, --signal Signal to send to the command when exit it (default: 1, SIGHUP)
-a, --url-arg Allow client to send command line arguments in URL (eg: http://localhost:7681?arg=foo&arg=bar)
-f, --arg-file Temporary file template to write URL arguments to (ex. /tmp/prefix); the temporary file's full path is then passed in as a command line argument
-R, --readonly Do not allow clients to write to the TTY
-t, --client-option Send option to client (format: key=value), repeat to add more options
-T, --terminal-type Terminal type to report, default: xterm-256color
Expand Down
2 changes: 1 addition & 1 deletion man/ttyd.1
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Cross platform: macOS, Linux, FreeBSD/OpenBSD, OpenWrt/LEDE, Windows

.PP
\-f, \-\-arg\-file
Allow client to write URL arguments to a temporary file; the file name is then passed in as a command line argument
Temporary file template to write URL arguments to (ex. /tmp/prefix); the temporary file's full path is then passed in as a command line argument

.PP
\-R, \-\-readonly
Expand Down
2 changes: 1 addition & 1 deletion man/ttyd.man.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ ttyd 1 "September 2016" ttyd "User Manual"
Allow client to send command line arguments in URL (eg: http://localhost:7681?arg=foo&arg=bar)

-f, --arg-file
Allow client to write URL arguments to a temporary file; the file name is then passed in as a command line argument
Temporary file template to write URL arguments to (ex. /tmp/prefix); the temporary file's full path is then passed in as a command line argument

-R, --readonly
Do not allow clients to write to the TTY
Expand Down
6 changes: 3 additions & 3 deletions src/protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ static bool spawn_process(struct pss_tty *pss, uint16_t columns, uint16_t rows)
argv[n++] = pss->args[i];
}
}
else if (server->arg_file) {
else if (server->arg_file != NULL) {
int fd = -1;
char filePath[] = "/tmp/XXXXXX";
char *filePath = strcat(server->arg_file, "XXXXXX");

if ((fd = mkstemp(filePath)) == -1) {
lwsl_err("Creation of temp file failed with error: %d (%s)\n", errno, strerror(errno));
Expand Down Expand Up @@ -200,7 +200,7 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user,
pss->wsi = wsi;
pss->lws_close_status = LWS_CLOSE_STATUS_NOSTATUS;

if (server->url_arg || server->arg_file) {
if (server->url_arg || server->arg_file != NULL) {
while (lws_hdr_copy_fragment(wsi, buf, sizeof(buf), WSI_TOKEN_HTTP_URI_ARGS, n++) > 0) {
if (strncmp(buf, "arg=", 4) == 0) {
pss->args = xrealloc(pss->args, (pss->argc + 1) * sizeof(char *));
Expand Down
15 changes: 8 additions & 7 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static const struct option options[] = {
{"ssl-key", required_argument, NULL, 'K'},
{"ssl-ca", required_argument, NULL, 'A'},
{"url-arg", no_argument, NULL, 'a'},
{"arg-file", no_argument, NULL, 'f'},
{"arg-file", required_argument, NULL, 'f'},
{"readonly", no_argument, NULL, 'R'},
{"terminal-type", required_argument, NULL, 'T'},
{"client-option", required_argument, NULL, 't'},
Expand All @@ -87,10 +87,10 @@ static const struct option options[] = {
{NULL, 0, 0, 0}};

#if LWS_LIBRARY_VERSION_NUMBER < 4000000
static const char *opt_string = "p:i:c:u:g:s:I:b:6afSC:K:A:Rt:T:Om:oBd:vh";
static const char *opt_string = "p:i:c:u:g:s:I:b:6af:SC:K:A:Rt:T:Om:oBd:vh";
#endif
#if LWS_LIBRARY_VERSION_NUMBER >= 4000000
static const char *opt_string = "p:i:c:u:g:s:I:b:P:6afSC:K:A:Rt:T:Om:oBd:vh";
static const char *opt_string = "p:i:c:u:g:s:I:b:P:6af:SC:K:A:Rt:T:Om:oBd:vh";
#endif

static void print_help() {
Expand All @@ -108,7 +108,7 @@ static void print_help() {
" -g, --gid Group id to run with\n"
" -s, --signal Signal to send to the command when exit it (default: 1, SIGHUP)\n"
" -a, --url-arg Allow client to send command line arguments in URL (eg: http://localhost:7681?arg=foo&arg=bar)\n"
" -f, --arg-file Allow client to write URL arguments to a temporary file; the file name is then passed in as a command line argument\n"
" -f, --arg-file Temporary file template to write URL arguments to (ex. /tmp/prefix); the temporary file's full path is then passed in as a command line argument\n"
" -R, --readonly Do not allow clients to write to the TTY\n"
" -t, --client-option Send option to client (format: key=value), repeat to add more options\n"
" -T, --terminal-type Terminal type to report, default: xterm-256color\n"
Expand Down Expand Up @@ -184,6 +184,7 @@ static struct server *server_new(int argc, char **argv, int start) {

static void server_free(struct server *ts) {
if (ts == NULL) return;
if (ts->arg_file != NULL) free(ts->arg_file);
if (ts->credential != NULL) free(ts->credential);
if (ts->index != NULL) free(ts->index);
free(ts->command);
Expand Down Expand Up @@ -323,10 +324,10 @@ int main(int argc, char **argv) {
break;
case 'a':
server->url_arg = true;
server->arg_file = false;
server->arg_file = NULL;
break;
case 'f':
server->arg_file = true;
server->arg_file = strdup(optarg);
server->url_arg = false;
break;
case 'R':
Expand Down Expand Up @@ -545,7 +546,7 @@ int main(int argc, char **argv) {
}
if (server->check_origin) lwsl_notice(" check origin: true\n");
if (server->url_arg) lwsl_notice(" allow url arg to cli arg: true\n");
if (server->arg_file) lwsl_notice(" allow url arg to tmp file: true\n");
if (server->arg_file != NULL) lwsl_notice(" temp file template: %s\n", server->arg_file);
if (server->readonly) lwsl_notice(" readonly: true\n");
if (server->max_clients > 0)
lwsl_notice(" max clients: %d\n", server->max_clients);
Expand Down
2 changes: 1 addition & 1 deletion src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ struct server {
int sig_code; // close signal
char sig_name[20]; // human readable signal string
bool url_arg; // allow client to send cli arguments in URL
bool arg_file; // allow client to write to a temp file through URL arguments
char *arg_file; // temp file template to write URL arguments
bool readonly; // whether not allow clients to write to the TTY
bool check_origin; // whether allow websocket connection from different origin
int max_clients; // maximum clients to support
Expand Down

0 comments on commit 8a7db24

Please sign in to comment.