Fix files with # character in url redirect

This commit is contained in:
simon 2019-10-27 08:30:47 -04:00
parent 69f0c1f2cf
commit 4109ba6d34
4 changed files with 29 additions and 9 deletions

View File

@ -5,7 +5,7 @@
#define EPILOG "Made by simon987 <me@simon987.net>. 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",

View File

@ -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);

View File

@ -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);

View File

@ -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 {