URL-encode filenames in generated links

This commit is contained in:
Thomas Kolb 2013-01-17 22:42:31 +01:00
parent 2bb32b1cf9
commit b77e4dcc8b
5 changed files with 61 additions and 13 deletions

View file

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

View file

@ -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, "<li><a href=\"/%s/\">%s/</a></li>\n",
entries[i].name, entries[i].name);
encName, entries[i].name);
} else {
sprintf(buf, "<li><a href=\"%s/%s/\">%s/</a></li>\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, "<li><a href=\"/%s\">%s</a> [%s]</li>\n",
entries[i].name, entries[i].name, format_size(entries[i].size));
encName, entries[i].name, format_size(entries[i].size));
} else {
sprintf(buf, "<li><a href=\"%s/%s\">%s</a> [%s]</li>\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);
}

8
main.c
View file

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

41
util.c Normal file
View file

@ -0,0 +1,41 @@
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#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++;
}
}

7
util.h Normal file
View file

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