Skip to content
This repository has been archived by the owner on May 9, 2024. It is now read-only.

Commit

Permalink
URI encode filenames
Browse files Browse the repository at this point in the history
So that they can work, if the filename contains reserved characters
  • Loading branch information
ltworf committed Feb 11, 2017
1 parent 75e6a61 commit b2a86c0
Showing 1 changed file with 52 additions and 3 deletions.
55 changes: 52 additions & 3 deletions utils.c
Expand Up @@ -40,6 +40,52 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "utils.h"
#include "embedded_auth.h"

/**
* This function creates an URI encoded version of the string origin
**/
static void uri_encode(char *dest, size_t size, char *origin) {
if (size == 0 || dest == NULL || origin == NULL)
return;

dest[0] = '\0';

size_t len = strlen(origin);
size_t rlen = 0;

char *format;
for (size_t i = 0; i < len; i++) {
//Encode forbidden characters
if (
origin[i] == '!' ||
origin[i] == '*' ||
origin[i] == '\'' ||
origin[i] == '(' ||
origin[i] == ')' ||
origin[i] == ';' ||
origin[i] == ':' ||
origin[i] == '@' ||
origin[i] == '&' ||
origin[i] == '=' ||
origin[i] == '+' ||
origin[i] == '$' ||
origin[i] == ',' ||
origin[i] == '/' ||
origin[i] == '?' ||
origin[i] == '#' ||
origin[i] == '[' ||
origin[i] == ']' ||
origin[i] == '%'
) {
format = "%%%02x";
} else {
format = "%c";
}
if (size - rlen <= 2)
return;
rlen += sprintf(dest + rlen, format, origin[i]);
}
}

/**
This function reads the directory dir, putting inside the html string an html
page with links to all the files within the directory.
Expand Down Expand Up @@ -67,6 +113,7 @@ int list_dir(connection_t *connection_prop, char *html, unsigned int bufsize, bo

struct dirent **namelist = NULL;
counter = scandir(connection_prop->strfile, &namelist, 0, alphasort);
char *escaped_dname = malloc(ESCAPED_FNAME_LEN);


if (counter <0) { //Open not succesfull
Expand Down Expand Up @@ -108,6 +155,8 @@ int list_dir(connection_t *connection_prop, char *html, unsigned int bufsize, bo
localtime_r(&f_prop.st_mtime,&ts);
strftime(last_modified,URI_LEN, "%a, %d %b %Y %H:%M:%S", &ts);

uri_encode(escaped_dname, ESCAPED_FNAME_LEN, namelist[i]->d_name);

if (S_ISREG(f_mode)) { //Regular file

//Table row for the file
Expand All @@ -129,18 +178,17 @@ int list_dir(connection_t *connection_prop, char *html, unsigned int bufsize, bo
color = "white";
else
color = "#EAEAEA";

printf_s=snprintf(html+pagesize,maxsize,
"<tr style=\"background-color: %s;\"><td>f</td><td><a href=\"%s\">%s</a></td><td>%lld%s</td><td>%s</td></tr>\n",
color, namelist[i]->d_name, namelist[i]->d_name, (long long int)size, measure,last_modified);
color, escaped_dname, namelist[i]->d_name, (long long int)size, measure,last_modified);
maxsize-=printf_s;
pagesize+=printf_s;

} else if (S_ISDIR(f_mode)) { //Directory entry
//Table row for the dir
printf_s=snprintf(html+pagesize,maxsize,
"<tr style=\"background-color: #DFDFDF;\"><td>d</td><td><a href=\"%s/\">%s/</a></td><td>-</td><td>%s</td></tr>\n",
namelist[i]->d_name, namelist[i]->d_name,last_modified);
escaped_dname, namelist[i]->d_name,last_modified);
maxsize-=printf_s;
pagesize+=printf_s;
}
Expand All @@ -152,6 +200,7 @@ int list_dir(connection_t *connection_prop, char *html, unsigned int bufsize, bo
}

escape:
free(escaped_dname);
free(namelist);
if (errcode == 0) {
printf_s=snprintf(html+pagesize,maxsize,"</table>%s",HTMLFOOT);
Expand Down

0 comments on commit b2a86c0

Please sign in to comment.