Skip to content

Commit

Permalink
Decode URL query parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Derpthemeus committed Jan 16, 2020
1 parent c51e6e1 commit ebab5aa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Request.cpp
Expand Up @@ -175,6 +175,7 @@ char Request::parseQuery(char *str, size_t length, size_t &index) {
value = &str[index];
}

decodeQueryParameter(value);
Serial.printf((char *) F("Found query parameter '%s' with value '%s'.\n"), key, value);

queryParams.add(key, value);
Expand Down Expand Up @@ -256,6 +257,33 @@ bool Request::parseHeader(char *str, size_t length, size_t &index, LinkedMap<cha
}
}

void Request::decodeQueryParameter(char *value) {
unsigned int offset = 0;
unsigned int index = 0;
while (value[index + offset] != '\0') {
Serial.printf((char *) F("Index is %d and offset is %d\n"), index, offset);
char character = value[index + offset];
if (character == '+') {
character = ' ';
} else if (character == '%') {
char highDigit = value[index + ++offset];
char lowDigit = value[index + ++offset];
if (highDigit == '\0' || lowDigit == '\0') {
// Abort decoding because the query string is illegally formatted.
return;
}

char hex[3] = {highDigit, lowDigit, '\0'};
character = strtol(hex, nullptr, 16);
}

value[index] = character;

index++;
}
value[index] = '\0';
}

char *Request::getPath() const { return path; }

char *Request::getQueryParameter(const __FlashStringHelper *key) const { return queryParams.find(key); }
Expand Down
6 changes: 6 additions & 0 deletions src/Request.h
Expand Up @@ -55,6 +55,12 @@ namespace OTF {
*/
bool parseHeader(char *str, size_t length, size_t &index, LinkedMap<char *> &headers);

/**
* Decodes the specified query string, updating and null-terminating the string in-place.
* @param value a null-terminated query value.
*/
static void decodeQueryParameter(char *value);

public:

/** Returns the path of the request (not including the query) as a null-terminated string. */
Expand Down

0 comments on commit ebab5aa

Please sign in to comment.