diff --git a/Makefile b/Makefile index cbc84cd..81425c1 100644 --- a/Makefile +++ b/Makefile @@ -5,8 +5,8 @@ CFLAGS+=-g -Wall -pedantic -std=c99 -D_POSIX_C_SOURCE=20120607L LIBS=-lmicrohttpd TARGET=fileshare -SOURCE=main.c logger.c dirlisting.c -DEPS=logger.h templates.h dirlisting.h +SOURCE=main.c logger.c dirlisting.c util.c +DEPS=logger.h templates.h dirlisting.h util.h OBJ=$(patsubst %.c, %.o, $(SOURCE)) diff --git a/dirlisting.c b/dirlisting.c index 32c3ec0..dd54924 100644 --- a/dirlisting.c +++ b/dirlisting.c @@ -14,6 +14,7 @@ #include "logger.h" #include "templates.h" +#include "util.h" #define FLG_USED 1 #define FLG_ISDIR 2 @@ -58,6 +59,7 @@ char* gen_html(const char *url, struct Entry *entries, size_t numentries) { size_t i; char buf[256]; char fullpath[PATH_MAX]; + char encName[2*PATH_MAX]; // allocate a buffer for the result string allocated = strlen(HEADER1 HEADER2 FOOTER) + 64 * numentries + 4096; @@ -134,12 +136,14 @@ char* gen_html(const char *url, struct Entry *entries, size_t numentries) { result = tmpresult; } + urlencode(entries[i].name, encName); + if(isrootdir) { sprintf(buf, "
  • %s/
  • \n", - entries[i].name, entries[i].name); + encName, entries[i].name); } else { sprintf(buf, "
  • %s/
  • \n", - url, entries[i].name, entries[i].name); + url, encName, entries[i].name); } strcat(result, buf); } @@ -171,12 +175,14 @@ char* gen_html(const char *url, struct Entry *entries, size_t numentries) { result = tmpresult; } + urlencode(entries[i].name, encName); + if(isrootdir) { sprintf(buf, "
  • %s [%s]
  • \n", - entries[i].name, entries[i].name, format_size(entries[i].size)); + encName, entries[i].name, format_size(entries[i].size)); } else { sprintf(buf, "
  • %s [%s]
  • \n", - url, entries[i].name, entries[i].name, format_size(entries[i].size)); + url, encName, entries[i].name, format_size(entries[i].size)); } strcat(result, buf); } diff --git a/main.c b/main.c index 14ac652..100aac6 100644 --- a/main.c +++ b/main.c @@ -16,6 +16,7 @@ #include "logger.h" #include "templates.h" #include "dirlisting.h" +#include "util.h" #define DEFAULT_PORT 8888 @@ -32,13 +33,6 @@ struct MHD_Response *error500Response; int running = 1; -void remove_trailing_slash(char *str) { - size_t offset = strlen(str)-1; - if((offset != 0) && (str[offset] == '/')) { - str[offset] = '\0'; - } -} - void request_completed(void *cls, struct MHD_Connection * connection, void **ptr, diff --git a/util.c b/util.c new file mode 100644 index 0000000..226fd3d --- /dev/null +++ b/util.c @@ -0,0 +1,41 @@ +#include +#include +#include + +#include "util.h" + +void remove_trailing_slash(char *str) { + size_t offset = strlen(str)-1; + if((offset != 0) && (str[offset] == '/')) { + str[offset] = '\0'; + } +} + +void urlencode(const char *str, char *result) { + uint8_t c; + while(*str) { + c = *str; + + if((c >= 'a' && c <= 'z') || + (c >= 'A' && c <= 'Z') || + (c >= '0' && c <= '9') || + c == '-' || + c == '_' || + c == '!' || + c == '(' || + c == ')' || + c == '$' || + c == '*' || + c == '\'' || + c == ',' || + c == '.') { + // simply print safe characters + result += sprintf(result, "%c", c); + } else { + // encode all others + result += sprintf(result, "%%%02X", c); + } + + str++; + } +} diff --git a/util.h b/util.h new file mode 100644 index 0000000..9ef3599 --- /dev/null +++ b/util.h @@ -0,0 +1,7 @@ +#ifndef UTIL_H +#define UTIL_H + +void remove_trailing_slash(char *str); +void urlencode(const char *str, char *result); + +#endif // UTIL_H