Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add URI decode prior to the REST #18

Merged
merged 3 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 49 additions & 6 deletions src/http_rest.c
Original file line number Diff line number Diff line change
Expand Up @@ -2772,7 +2772,9 @@ http_set_cgi_handlers(const tCGI *cgis, int num_handlers)



// ####################################################################################################################
/////////////////////////////////////
// Open Trickler Modified Code //
// //////////////////////////////////

#include "eeprom.h"

Expand Down Expand Up @@ -2807,21 +2809,62 @@ rest_handler_t rest_get_handler(const char *uri) {
return target_handler;
}

/*
Decode special characters in URI into the regular ASCII characters

Reference: https://stackoverflow.com/a/14530993
*/
void decode_uri(char * dst, const char * src) {
char a, b;
while (*src) {
if ((*src == '%') &&
((a = src[1]) && (b = src[2])) &&
(isxdigit(a) && isxdigit(b))) {
if (a >= 'a')
a -= 'a'-'A';
if (a >= 'A')
a -= ('A' - 10);
else
a -= '0';
if (b >= 'a')
b -= 'a'-'A';
if (b >= 'A')
b -= ('A' - 10);
else
b -= '0';
*dst++ = 16*a+b;
src+=3;
} else if (*src == '+') {
*dst++ = ' ';
src++;
} else {
*dst++ = *src++;
}
}
*dst++ = '\0';
}


static err_t http_find_file(struct http_state * hs, const char * uri, int is_09) {
struct fs_file * file = NULL;
char * params = NULL;

// The decoded URI will be fed to the parameter and REST handler loopup
// decoded_uri will be within the scope of `http_find_file` exclusively
char decoded_uri[strlen(uri) + 1];
memset(decoded_uri, 0x0, sizeof(decoded_uri));
decode_uri(decoded_uri, uri);

// First, isolate the base URI (without any parameters)
params = (char *) strchr(uri, '?');
params = (char *) strchr(decoded_uri, '?');
if (params != NULL) {
// uri includes parameters, we need to terminate the base URI
*params = 0;
params += 1;
}

// Look for handler
rest_handler_t rest_handler = rest_get_handler(uri);
rest_handler_t rest_handler = rest_get_handler(decoded_uri);

if (rest_handler) {
// Extract parameters from the uri
Expand All @@ -2830,15 +2873,15 @@ static err_t http_find_file(struct http_state * hs, const char * uri, int is_09)
rest_handler(&hs->file_handle, http_cgi_paramcount, hs->params, hs->param_vals);
file = &hs->file_handle;
}

if (file == NULL) {
rest_handler = rest_get_handler("/404");
LWIP_ASSERT("Missing 404 handler", file == NULL);

rest_handler(&hs->file_handle, http_cgi_paramcount, hs->params, hs->param_vals);

file = &hs->file_handle;

}

uint8_t tag_check = 0;
Expand Down Expand Up @@ -2870,4 +2913,4 @@ fs_close(struct fs_file *file)
fs_state_free(file, file->state);
#endif /* #if LWIP_HTTPD_FILE_STATE */
LWIP_UNUSED_ARG(file);
}
}
11 changes: 4 additions & 7 deletions src/neopixel_led.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,10 @@ bool neopixel_led_config_save() {
uint32_t _to_hex_colour(char * string) {
uint32_t value = 0;

if (string) {
// If the string is an escaped character, then skip the next 3 characters
if (string[0] == '%') {
string += 3;
}

value = strtol(string, NULL, 16);
// Valid hex decimal starts with #
// For example, #ff00ff
if (string && string[0] == '#') {
value = strtol(string + 1, NULL, 16);
}

return value;
Expand Down