diff options
author | Omar Polo <op@omarpolo.com> | 2022-07-04 09:31:36 +0000 |
---|---|---|
committer | Omar Polo <op@omarpolo.com> | 2022-07-04 09:31:36 +0000 |
commit | f2f8eb35c86c4e1c1d858e782c864deac0511cd3 (patch) | |
tree | 93fa5ddc61c2ade321e1eeeabfe0d50d8b938b4b | |
parent | 3bd4a6dea08fc977e314877cefed1c6fdd6b1613 (diff) |
encode file names in the directory index
Spotted the hard way by cage
-rw-r--r-- | gmid.h | 1 | ||||
-rw-r--r-- | iri.c | 37 | ||||
-rw-r--r-- | server.c | 4 |
3 files changed, 41 insertions, 1 deletions
@@ -427,6 +427,7 @@ char *utf8_nth(char*, size_t); /* iri.c */ int parse_iri(char*, struct iri*, const char**); int serialize_iri(struct iri*, char*, size_t); +int encode_path(char *, size_t, const char *); char *pct_decode_str(char *); /* proxy.c */ @@ -444,6 +444,43 @@ serialize_iri(struct iri *i, char *buf, size_t len) return l < len; } +int +encode_path(char *buf, size_t len, const char *path) +{ + char *p = buf; + int a, b; + + memset(buf, 0, len); + while (*path != '\0') { + if (len == 1) /* NUL */ + return -1; + + if (unreserved(*path) || + sub_delimiters(*path) || + *path == '@' || + *path == ':' || + *path == '/') { + *p++ = *path++; + len--; + } else if (len < 4) + return -1; + else { + a = (*path & 0xF0) >> 4; + b = (*path & 0x0F); + + p[0] = '%'; + p[1] = a <= 9 ? ('0' + a) : ('7' + a); + p[2] = b <= 9 ? ('0' + b) : ('7' + b); + + path++; + p += 3; + len -= 3; + } + } + + return 0; +} + char * pct_decode_str(char *s) { @@ -1070,6 +1070,7 @@ client_write(struct bufferevent *bev, void *d) { struct client *c = d; struct evbuffer *out = EVBUFFER_OUTPUT(bev); + char nam[PATH_MAX]; char buf[BUFSIZ]; ssize_t r; @@ -1097,8 +1098,9 @@ client_write(struct bufferevent *bev, void *d) case REQUEST_DIR: /* TODO: handle big big directories better */ for (c->diroff = 0; c->diroff < c->dirlen; ++c->diroff) { - evbuffer_add_printf(out, "=> %s\n", + encode_path(nam, sizeof(nam), c->dir[c->diroff]->d_name); + evbuffer_add_printf(out, "=> ./%s\n", nam); free(c->dir[c->diroff]); } free(c->dir); |