Skip to content

Commit

Permalink
clang-format: udpate ColumnLimit to 100 and reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Mar 25, 2020
1 parent 398bebf commit c38cdc3
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 132 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
BasedOnStyle: Google
Language: Cpp
ColumnLimit: 100
87 changes: 35 additions & 52 deletions src/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ static int check_auth(struct lws *wsi, struct pss_http *pss) {
break;
}
}
if (b64_text != NULL && !strcmp(b64_text, server->credential))
return AUTH_OK;
if (b64_text != NULL && !strcmp(b64_text, server->credential)) return AUTH_OK;
}

unsigned char buffer[1024 + LWS_PRE], *p, *end;
Expand All @@ -41,16 +40,12 @@ static int check_auth(struct lws *wsi, struct pss_http *pss) {
char *body = strdup("401 Unauthorized\n");
size_t n = strlen(body);

if (lws_add_http_header_status(wsi, HTTP_STATUS_UNAUTHORIZED, &p, end))
return AUTH_ERROR;
if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_WWW_AUTHENTICATE,
(unsigned char *)"Basic realm=\"ttyd\"", 18,
&p, end))
return AUTH_ERROR;
if (lws_add_http_header_content_length(wsi, n, &p, end)) return AUTH_ERROR;
if (lws_finalize_http_header(wsi, &p, end)) return AUTH_ERROR;
if (lws_write(wsi, buffer + LWS_PRE, p - (buffer + LWS_PRE),
LWS_WRITE_HTTP_HEADERS) < 0)
if (lws_add_http_header_status(wsi, HTTP_STATUS_UNAUTHORIZED, &p, end) ||
lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_WWW_AUTHENTICATE,
(unsigned char *)"Basic realm=\"ttyd\"", 18, &p, end) ||
lws_add_http_header_content_length(wsi, n, &p, end) ||
lws_finalize_http_header(wsi, &p, end) ||
lws_write(wsi, buffer + LWS_PRE, p - (buffer + LWS_PRE), LWS_WRITE_HTTP_HEADERS) < 0)
return AUTH_ERROR;

pss->buffer = pss->ptr = body;
Expand Down Expand Up @@ -98,8 +93,7 @@ static bool uncompress_html(char **output, size_t *output_len) {
}

static void pss_buffer_free(struct pss_http *pss) {
if (pss->buffer != (char *)index_html && pss->buffer != html_cache)
free(pss->buffer);
if (pss->buffer != (char *)index_html && pss->buffer != html_cache) free(pss->buffer);
}

static void access_log(struct lws *wsi, const char *path) {
Expand All @@ -109,14 +103,13 @@ static void access_log(struct lws *wsi, const char *path) {
lws_get_peer_simple(lws_get_network_wsi(wsi), rip, sizeof(rip));
#else
char name[100];
lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), name, sizeof(name), rip,
sizeof(rip));
lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), name, sizeof(name), rip, sizeof(rip));
#endif
lwsl_notice("HTTP %s - %s\n", path, rip);
}

int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
void *in, size_t len) {
int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in,
size_t len) {
struct pss_http *pss = (struct pss_http *)user;
unsigned char buffer[4096 + LWS_PRE], *p, *end;
char buf[256];
Expand All @@ -140,37 +133,32 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
end = p + sizeof(buffer) - LWS_PRE;

if (strcmp(pss->path, endpoints.token) == 0) {
const char *credential =
server->credential != NULL ? server->credential : "";
const char *credential = server->credential != NULL ? server->credential : "";
size_t n = sprintf(buf, "{\"token\": \"%s\"}", credential);
if (lws_add_http_header_status(wsi, HTTP_STATUS_OK, &p, end)) return 1;
if (lws_add_http_header_by_token(
wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
(unsigned char *)"application/json;charset=utf-8", 30, &p, end))
return 1;
if (lws_add_http_header_content_length(wsi, (unsigned long)n, &p, end))
return 1;
if (lws_finalize_http_header(wsi, &p, end)) return 1;
if (lws_write(wsi, buffer + LWS_PRE, p - (buffer + LWS_PRE),
LWS_WRITE_HTTP_HEADERS) < 0)
if (lws_add_http_header_status(wsi, HTTP_STATUS_OK, &p, end) ||
lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
(unsigned char *)"application/json;charset=utf-8", 30, &p,
end) ||
lws_add_http_header_content_length(wsi, (unsigned long)n, &p, end) ||
lws_finalize_http_header(wsi, &p, end) ||
lws_write(wsi, buffer + LWS_PRE, p - (buffer + LWS_PRE), LWS_WRITE_HTTP_HEADERS) < 0)
return 1;

pss->buffer = pss->ptr = strdup(buf);
pss->len = n;
lws_callback_on_writable(wsi);
break;
}

// accessing `/base-path` redirects to `/base-path/`
// redirects `/base-path` to `/base-path/`
if (strcmp(pss->path, endpoints.parent) == 0) {
if (lws_add_http_header_status(wsi, HTTP_STATUS_FOUND, &p, end)
|| lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_LOCATION,
endpoints.index,
strlen(endpoints.index), &p, end)
|| lws_add_http_header_content_length(wsi, 0, &p, end)
|| lws_finalize_http_header(wsi, &p, end)
|| lws_write(wsi, buffer + LWS_PRE, p - (buffer + LWS_PRE),
LWS_WRITE_HTTP_HEADERS) < 0
)
if (lws_add_http_header_status(wsi, HTTP_STATUS_FOUND, &p, end) ||
lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_LOCATION,
(unsigned char *)endpoints.index,
(int)strlen(endpoints.index), &p, end) ||
lws_add_http_header_content_length(wsi, 0, &p, end) ||
lws_finalize_http_header(wsi, &p, end) ||
lws_write(wsi, buffer + LWS_PRE, p - (buffer + LWS_PRE), LWS_WRITE_HTTP_HEADERS) < 0)
return 1;
goto try_to_reuse;
}
Expand All @@ -187,10 +175,9 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
} else {
char *output = (char *)index_html;
size_t output_len = index_html_len;
if (lws_add_http_header_status(wsi, HTTP_STATUS_OK, &p, end)) return 1;
if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
(const unsigned char *)content_type, 9,
&p, end))
if (lws_add_http_header_status(wsi, HTTP_STATUS_OK, &p, end) ||
lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
(const unsigned char *)content_type, 9, &p, end))
return 1;
#ifdef LWS_WITH_HTTP_STREAM_COMPRESSION
if (!uncompress_html(&output, &output_len)) return 1;
Expand All @@ -204,14 +191,11 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
}
#endif

if (lws_add_http_header_content_length(wsi, (unsigned long)output_len,
&p, end))
if (lws_add_http_header_content_length(wsi, (unsigned long)output_len, &p, end) ||
lws_finalize_http_header(wsi, &p, end) ||
lws_write(wsi, buffer + LWS_PRE, p - (buffer + LWS_PRE), LWS_WRITE_HTTP_HEADERS) < 0)
return 1;

if (lws_finalize_http_header(wsi, &p, end)) return 1;
if (lws_write(wsi, buffer + LWS_PRE, p - (buffer + LWS_PRE),
LWS_WRITE_HTTP_HEADERS) < 0)
return 1;
#if LWS_LIBRARY_VERSION_MAJOR < 2
if (lws_write_http(wsi, output, output_len) < 0) return 1;
goto try_to_reuse;
Expand Down Expand Up @@ -265,8 +249,7 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
int err = X509_STORE_CTX_get_error((X509_STORE_CTX *)user);
int depth = X509_STORE_CTX_get_error_depth((X509_STORE_CTX *)user);
const char *msg = X509_verify_cert_error_string(err);
lwsl_err("client certificate verification error: %s (%d), depth: %d\n",
msg, err, depth);
lwsl_err("client certificate verification error: %s (%d), depth: %d\n", msg, err, depth);
return 1;
}
break;
Expand Down
50 changes: 18 additions & 32 deletions src/protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ static void pty_proc_free(struct pty_proc *proc) {
free(proc);
}

static void alloc_cb(uv_handle_t *handle, size_t suggested_size,
uv_buf_t *buf) {
static void alloc_cb(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
buf->base = xmalloc(suggested_size);
buf->len = suggested_size;
}
Expand Down Expand Up @@ -149,16 +148,14 @@ static void child_cb(uv_signal_t *handle, int signum) {

if (WIFEXITED(stat)) {
proc->status = WEXITSTATUS(stat);
lwsl_notice("process exited with code %d, pid: %d\n", proc->status,
proc->pid);
lwsl_notice("process exited with code %d, pid: %d\n", proc->status, proc->pid);
} else if (WIFSIGNALED(stat)) {
int sig = WTERMSIG(stat);
char sig_name[20];

proc->status = 128 + sig;
get_sig_name(sig, sig_name, sizeof(sig_name));
lwsl_notice("process killed with signal %d (%s), pid: %d\n", sig,
sig_name, proc->pid);
lwsl_notice("process killed with signal %d (%s), pid: %d\n", sig, sig_name, proc->pid);
}

LIST_REMOVE(proc, entry);
Expand Down Expand Up @@ -225,8 +222,8 @@ static void write_cb(uv_write_t *req, int status) {
free(req);
}

int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user,
void *in, size_t len) {
int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in,
size_t len) {
struct pss_tty *pss = (struct pss_tty *)user;
struct pty_proc *proc;
char buf[256];
Expand All @@ -238,10 +235,8 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user,
lwsl_warn("refuse to serve WS client due to the --once option.\n");
return 1;
}
if (server->max_clients > 0 &&
server->client_count == server->max_clients) {
lwsl_warn(
"refuse to serve WS client due to the --max-clients option.\n");
if (server->max_clients > 0 && server->client_count == server->max_clients) {
lwsl_warn("refuse to serve WS client due to the --max-clients option.\n");
return 1;
}
if (lws_hdr_copy(wsi, buf, sizeof(buf), WSI_TOKEN_GET_URI) <= 0 ||
Expand Down Expand Up @@ -272,11 +267,9 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user,
uv_pipe_init(server->loop, &proc->pipe, 0);

if (server->url_arg) {
while (lws_hdr_copy_fragment(wsi, buf, sizeof(buf),
WSI_TOKEN_HTTP_URI_ARGS, n++) > 0) {
while (lws_hdr_copy_fragment(wsi, buf, sizeof(buf), WSI_TOKEN_HTTP_URI_ARGS, n++) > 0) {
if (strncmp(buf, "arg=", 4) == 0) {
proc->args =
xrealloc(proc->args, (proc->argc + 1) * sizeof(char *));
proc->args = xrealloc(proc->args, (proc->argc + 1) * sizeof(char *));
proc->args[proc->argc] = strdup(&buf[4]);
proc->argc++;
}
Expand All @@ -289,15 +282,13 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user,
lws_hdr_copy(wsi, buf, sizeof(buf), WSI_TOKEN_GET_URI);

#if LWS_LIBRARY_VERSION_NUMBER >= 2004000
lws_get_peer_simple(lws_get_network_wsi(wsi), pss->address,
sizeof(pss->address));
lws_get_peer_simple(lws_get_network_wsi(wsi), pss->address, sizeof(pss->address));
#else
char name[100];
lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), name, sizeof(name),
pss->address, sizeof(pss->address));
lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), name, sizeof(name), pss->address,
sizeof(pss->address));
#endif
lwsl_notice("WS %s - %s, clients: %d\n", buf, pss->address,
server->client_count);
lwsl_notice("WS %s - %s, clients: %d\n", buf, pss->address, server->client_count);
break;

case LWS_CALLBACK_SERVER_WRITEABLE:
Expand All @@ -309,8 +300,7 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user,
break;
}
if (send_initial_message(wsi, pss->initial_cmd_index) < 0) {
lwsl_err("failed to send initial message, index: %d\n",
pss->initial_cmd_index);
lwsl_err("failed to send initial message, index: %d\n", pss->initial_cmd_index);
lws_close_reason(wsi, LWS_CLOSE_STATUS_UNEXPECTED_CONDITION, NULL, 0);
return -1;
}
Expand All @@ -332,8 +322,7 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user,

proc->pty_buffer[LWS_PRE] = OUTPUT;
n = (size_t)(proc->pty_len + 1);
if (lws_write(wsi, (unsigned char *)proc->pty_buffer + LWS_PRE, n,
LWS_WRITE_BINARY) < n) {
if (lws_write(wsi, (unsigned char *)proc->pty_buffer + LWS_PRE, n, LWS_WRITE_BINARY) < n) {
lwsl_err("write OUTPUT to WS\n");
}
free(proc->pty_buffer);
Expand All @@ -355,15 +344,13 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user,
const char command = pss->buffer[0];

// check auth
if (server->credential != NULL && !pss->authenticated &&
command != JSON_DATA) {
if (server->credential != NULL && !pss->authenticated && command != JSON_DATA) {
lwsl_warn("WS client not authenticated\n");
return 1;
}

// check if there are more fragmented messages
if (lws_remaining_packet_payload(wsi) > 0 ||
!lws_is_final_fragment(wsi)) {
if (lws_remaining_packet_payload(wsi) > 0 || !lws_is_final_fragment(wsi)) {
return 0;
}

Expand Down Expand Up @@ -428,8 +415,7 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user,
if (pss->wsi == NULL) break;

server->client_count--;
lwsl_notice("WS closed from %s, clients: %d\n", pss->address,
server->client_count);
lwsl_notice("WS closed from %s, clients: %d\n", pss->address, server->client_count);
if (pss->buffer != NULL) {
free(pss->buffer);
}
Expand Down
62 changes: 31 additions & 31 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,38 +74,38 @@ static const char *opt_string = "p:i:c:u:g:s:I:b:6aSC:K:A:Rt:T:Om:oBd:vh";
static void print_help() {
// clang-format off
fprintf(stderr, "ttyd is a tool for sharing terminal over the web\n\n"
"USAGE:\n"
" ttyd [options] <command> [<arguments...>]\n\n"
"VERSION:\n"
" %s\n\n"
"OPTIONS:\n"
" -p, --port Port to listen (default: 7681, use `0` for random port)\n"
" -i, --interface Network interface to bind (eg: eth0), or UNIX domain socket path (eg: /var/run/ttyd.sock)\n"
" -c, --credential Credential for Basic Authentication (format: username:password)\n"
" -u, --uid User id to run with\n"
" -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"
" -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"
" -O, --check-origin Do not allow websocket connection from different origin\n"
" -m, --max-clients Maximum clients to support (default: 0, no limit)\n"
" -o, --once Accept only one client and exit on disconnection\n"
" -B, --browser Open terminal with the default system browser\n"
" -I, --index Custom index.html path\n"
" -b, --base-path Expected base path for requests coming from a reverse proxy (eg: /mounted/here)\n"
"USAGE:\n"
" ttyd [options] <command> [<arguments...>]\n\n"
"VERSION:\n"
" %s\n\n"
"OPTIONS:\n"
" -p, --port Port to listen (default: 7681, use `0` for random port)\n"
" -i, --interface Network interface to bind (eg: eth0), or UNIX domain socket path (eg: /var/run/ttyd.sock)\n"
" -c, --credential Credential for Basic Authentication (format: username:password)\n"
" -u, --uid User id to run with\n"
" -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"
" -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"
" -O, --check-origin Do not allow websocket connection from different origin\n"
" -m, --max-clients Maximum clients to support (default: 0, no limit)\n"
" -o, --once Accept only one client and exit on disconnection\n"
" -B, --browser Open terminal with the default system browser\n"
" -I, --index Custom index.html path\n"
" -b, --base-path Expected base path for requests coming from a reverse proxy (eg: /mounted/here)\n"
#ifdef LWS_WITH_IPV6
" -6, --ipv6 Enable IPv6 support\n"
" -6, --ipv6 Enable IPv6 support\n"
#endif
" -S, --ssl Enable SSL\n"
" -C, --ssl-cert SSL certificate file path\n"
" -K, --ssl-key SSL key file path\n"
" -A, --ssl-ca SSL CA file path for client certificate verification\n"
" -d, --debug Set log level (default: 7)\n"
" -v, --version Print the version and exit\n"
" -h, --help Print this text and exit\n\n"
"Visit https://github.com/tsl0922/ttyd to get more information and report bugs.\n",
" -S, --ssl Enable SSL\n"
" -C, --ssl-cert SSL certificate file path\n"
" -K, --ssl-key SSL key file path\n"
" -A, --ssl-ca SSL CA file path for client certificate verification\n"
" -d, --debug Set log level (default: 7)\n"
" -v, --version Print the version and exit\n"
" -h, --help Print this text and exit\n\n"
"Visit https://github.com/tsl0922/ttyd to get more information and report bugs.\n",
TTYD_VERSION
);
// clang-format on
Expand Down Expand Up @@ -370,7 +370,7 @@ int main(int argc, char **argv) {
char path[128];
strncpy(path, optarg, 128);
size_t len = strlen(path);
while (len && path[len - 1] == '/') path[--len] = 0; // trim trailing /
while (len && path[len - 1] == '/') path[--len] = 0; // trim trailing /
if (!len) break;
#define sc(f) \
strncpy(path + len, endpoints.f, 128 - len); \
Expand Down
Loading

0 comments on commit c38cdc3

Please sign in to comment.