diff --git a/src/main.c b/src/main.c index 77d5fd6..768ac77 100644 --- a/src/main.c +++ b/src/main.c @@ -5,7 +5,7 @@ #define EPILOG "Made by simon987 . Released under GPL-3.0" -static const char *const Version = "1.0.6"; +static const char *const Version = "1.0.7"; static const char *const usage[] = { "sist2 scan [OPTION]... PATH", "sist2 index [OPTION]... INDEX", diff --git a/src/util.c b/src/util.c index a73706d..e81d208 100644 --- a/src/util.c +++ b/src/util.c @@ -5,6 +5,24 @@ #define PBSTR "========================================" #define PBWIDTH 40 +dyn_buffer_t url_escape(char *str) { + + dyn_buffer_t text = dyn_buffer_create(); + + char * ptr = str; + while (*ptr) { + if (*ptr == '#') { + dyn_buffer_write(&text, "%23", 3); + ptr++; + } + + dyn_buffer_write_char(&text, *ptr++); + } + dyn_buffer_write_char(&text, '\0'); + + return text; +} + char *abspath(const char *path) { wordexp_t w; wordexp(path, &w, 0); diff --git a/src/util.h b/src/util.h index a868602..c04fc21 100644 --- a/src/util.h +++ b/src/util.h @@ -149,6 +149,7 @@ int text_buffer_append_char(text_buffer_t *buf, int c) { char *abspath(const char * path); char *expandpath(const char *path); +dyn_buffer_t url_escape(char *str); void progress_bar_print(double percentage, size_t tn_size, size_t index_size); diff --git a/src/web/serve.c b/src/web/serve.c index cc500dd..912e910 100644 --- a/src/web/serve.c +++ b/src/web/serve.c @@ -287,7 +287,10 @@ int serve_file_from_url(cJSON *json, index_t *idx, onion_request *req, onion_res "%s%s/%s%s%s", idx->desc.rewrite_url, path, name, strlen(ext) == 0 ? "" : ".", ext); - return onion_shortcut_redirect(url, req, res); + dyn_buffer_t encoded = url_escape(url); + int ret = onion_shortcut_redirect(encoded.buf, req, res); + dyn_buffer_destroy(&encoded); + return ret; } int serve_file_from_disk(cJSON *json, index_t *idx, onion_request *req, onion_response *res) { @@ -301,6 +304,11 @@ int serve_file_from_disk(cJSON *json, index_t *idx, onion_request *req, onion_re snprintf(full_path, PATH_MAX, "%s%s/%s%s%s", idx->desc.root, path, name, strlen(ext) == 0 ? "" : ".", ext); + char disposition[8196]; + snprintf(disposition, sizeof(disposition), "inline; filename=\"%s%s%s\"", + name, strlen(ext) == 0 ? "" : ".", ext); + onion_response_set_header(res, "Content-Disposition", disposition); + return chunked_response_file(full_path, mime, 1, req, res); } @@ -350,13 +358,6 @@ int file(void *p, onion_request *req, onion_response *res) { return OCS_NOT_PROCESSED; } - const char *name = cJSON_GetObjectItem(source, "name")->valuestring; - const char *ext = cJSON_GetObjectItem(source, "extension")->valuestring; - char disposition[8196]; - snprintf(disposition, sizeof(disposition), "inline; filename=\"%s%s%s\"", - name, strlen(ext) == 0 ? "" : ".", ext); - onion_response_set_header(res, "Content-Disposition", disposition); - if (strlen(idx->desc.rewrite_url) == 0) { return serve_file_from_disk(source, idx, req, res); } else {