Proper url encoding for location field

This commit is contained in:
2020-12-18 22:20:32 +01:00
parent a5c1516468
commit 50f5438654
3 changed files with 33 additions and 4 deletions

View File

@ -38,7 +38,7 @@ int client_websocket_handler() {
int client_request_handler(sock *client, int req_num) { int client_request_handler(sock *client, int req_num) {
struct timespec begin, end; struct timespec begin, end;
int ret, client_keep_alive; int ret, client_keep_alive;
char buf[64]; char buf[1024];
char msg_buf[4096]; char msg_buf[4096];
char msg_pre_buf[4096]; char msg_pre_buf[4096];
char err_msg[256]; char err_msg[256];
@ -127,9 +127,14 @@ int client_request_handler(sock *client, int req_num) {
print("is_dir: %i", uri.is_dir); print("is_dir: %i", uri.is_dir);
*/ */
if (strcmp(uri.uri, req.uri) != 0) { ssize_t size = sizeof(buf);
url_decode(req.uri, buf, &size);
if (strcmp(uri.uri, buf) != 0) {
res.status = http_get_status(308); res.status = http_get_status(308);
http_add_header_field(&res.hdr, "Location", uri.uri); size = sizeof(buf);
encode_url(uri.uri, buf, &size);
print("%s", buf);
http_add_header_field(&res.hdr, "Location", buf);
goto respond; goto respond;
} }

View File

@ -28,7 +28,6 @@ int url_encode(const char *str, char *enc, ssize_t *size) {
memset(enc, 0, *size); memset(enc, 0, *size);
for (int i = 0; i < strlen(str); i++, ptr++) { for (int i = 0; i < strlen(str); i++, ptr++) {
if ((ptr - enc) >= *size) { if ((ptr - enc) >= *size) {
printf("%li %li\n", ptr - enc, *size);
return -1; return -1;
} }
ch = str[i]; ch = str[i];
@ -50,6 +49,29 @@ int url_encode(const char *str, char *enc, ssize_t *size) {
return 0; return 0;
} }
int encode_url(const char *str, char *enc, ssize_t *size) {
char *ptr = enc;
unsigned char ch;
memset(enc, 0, *size);
for (int i = 0; i < strlen(str); i++, ptr++) {
if ((ptr - enc) >= *size) {
return -1;
}
ch = str[i];
if (ch > 0x7F || ch == ' ') {
if ((ptr - enc + 2) >= *size) {
return -1;
}
sprintf(ptr, "%%%02X", ch);
ptr += 2;
} else {
ptr[0] = (char) ch;
}
}
*size = ptr - enc;
return 0;
}
int url_decode(const char *str, char *dec, ssize_t *size) { int url_decode(const char *str, char *dec, ssize_t *size) {
char *ptr = dec; char *ptr = dec;
char ch, buf[3]; char ch, buf[3];

View File

@ -24,6 +24,8 @@ char *format_duration(unsigned long micros, char *buf);
int url_encode(const char *str, char *enc, ssize_t *size); int url_encode(const char *str, char *enc, ssize_t *size);
int encode_url(const char *str, char *enc, ssize_t *size);
int url_decode(const char *str, char *dec, ssize_t *size); int url_decode(const char *str, char *dec, ssize_t *size);
#endif //NECRONDA_SERVER_UTILS_H #endif //NECRONDA_SERVER_UTILS_H