mirror of
https://github.com/tsl0922/ttyd.git
synced 2025-12-25 13:14:18 +01:00
Improvements for --base-path usage (#292)
* Trim trailing slashes in --base-path option * Redirect `/base-path` to `/base-path/` * Log endpoints if --base-path is provided * Use predefined token for standard header 'location'
This commit is contained in:
committed by
GitHub
parent
ea27950594
commit
398bebf091
15
src/http.c
15
src/http.c
@@ -160,6 +160,21 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
|
||||
break;
|
||||
}
|
||||
|
||||
// accessing `/base-path` redirects 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
|
||||
)
|
||||
return 1;
|
||||
goto try_to_reuse;
|
||||
}
|
||||
|
||||
if (strcmp(pss->path, endpoints.index) != 0) {
|
||||
lws_return_http_status(wsi, HTTP_STATUS_NOT_FOUND, NULL);
|
||||
goto try_to_reuse;
|
||||
|
||||
13
src/server.c
13
src/server.c
@@ -20,7 +20,7 @@
|
||||
volatile bool force_exit = false;
|
||||
struct lws_context *context;
|
||||
struct server *server;
|
||||
struct endpoints endpoints = {"/ws", "/", "/token"};
|
||||
struct endpoints endpoints = {"/ws", "/", "/token", ""};
|
||||
|
||||
extern int callback_http(struct lws *wsi, enum lws_callback_reasons reason,
|
||||
void *user, void *in, size_t len);
|
||||
@@ -370,10 +370,12 @@ 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 /
|
||||
if (!len) break;
|
||||
#define sc(f) \
|
||||
strncpy(path + len, endpoints.f, 128 - len); \
|
||||
endpoints.f = strdup(path);
|
||||
sc(ws) sc(index) sc(token)
|
||||
sc(ws) sc(index) sc(token) sc(parent)
|
||||
#undef sc
|
||||
} break;
|
||||
case '6':
|
||||
@@ -484,6 +486,13 @@ int main(int argc, char **argv) {
|
||||
lwsl_notice(" start command: %s\n", server->command);
|
||||
lwsl_notice(" close signal: %s (%d)\n", server->sig_name, server->sig_code);
|
||||
lwsl_notice(" terminal type: %s\n", server->terminal_type);
|
||||
if (endpoints.parent[0]) {
|
||||
lwsl_notice("endpoints:\n");
|
||||
lwsl_notice(" base-path: %s\n", endpoints.parent);
|
||||
lwsl_notice(" index : %s\n", endpoints.index);
|
||||
lwsl_notice(" token : %s\n", endpoints.token);
|
||||
lwsl_notice(" websocket: %s\n", endpoints.ws);
|
||||
}
|
||||
if (server->check_origin) lwsl_notice(" check origin: true\n");
|
||||
if (server->url_arg) lwsl_notice(" allow url arg: true\n");
|
||||
if (server->readonly) lwsl_notice(" readonly: true\n");
|
||||
|
||||
@@ -18,6 +18,7 @@ struct endpoints {
|
||||
char *ws;
|
||||
char *index;
|
||||
char *token;
|
||||
char *parent;
|
||||
};
|
||||
|
||||
extern volatile bool force_exit;
|
||||
|
||||
Reference in New Issue
Block a user