Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c4bbd5c
Showing
4 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include "httpHeader.h" | ||
#include "strutils.h" | ||
|
||
HTTPHeader::HTTPHeader() { | ||
} | ||
|
||
void HTTPHeader::consumeLine(char* line) { | ||
int nTokens; | ||
char** tokens = stringSplit(line, " :", &nTokens); | ||
|
||
if (strcmp(tokens[0], "GET")==0 || strcmp(tokens[0], "PUT")==0 || strcmp(tokens[0], "POST")==0 || strcmp(tokens[0], "DELETE")==0) { | ||
if (strcmp(tokens[0], "GET")==0) | ||
verb = GET; | ||
else if (strcmp(tokens[0], "PUT")==0) | ||
verb = PUT; | ||
else if (strcmp(tokens[0], "POST")==0) | ||
verb = POST; | ||
else if(strcmp(tokens[0], "DELETE")==0) | ||
verb = DELETE; | ||
|
||
if (stringContains(tokens[1], '?')) { | ||
int nQTokens; | ||
char** qTokens = stringSplit(tokens[1], "?&", &nQTokens); | ||
|
||
for (int i = 0; i < nQTokens; i++) { | ||
int nElements; | ||
char** qElements = stringSplit(qTokens[i], "=", &nElements); | ||
|
||
if (nElements == 2 && nPairs < MAX_PAIRS) { | ||
strcpy(this->pairs[nPairs].key, qElements[0]); | ||
strcpy(this->pairs[nPairs++].value, qElements[1]); | ||
} | ||
|
||
free(qElements); | ||
} | ||
|
||
free(qTokens); | ||
} | ||
|
||
strcpy(this->resource, tokens[1]); | ||
} | ||
else if (strcmp(tokens[0], "Content-Length") == 0) { | ||
contentLength = atoi(tokens[1]); | ||
} | ||
|
||
free(tokens); | ||
} | ||
|
||
char* HTTPHeader::fetchQuery (char* key) { | ||
for (int i=0; i < this->nPairs; i++) { | ||
if (strcmp(this->pairs[i].key, key) == 0) { | ||
return this->pairs[i].value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include "Arduino.h" | ||
#include "stdlib.h" | ||
#include "httpQuery.h" | ||
|
||
#ifndef HTTPHEADER_H | ||
#define HTTPHEADER_H | ||
|
||
#ifndef NULL | ||
#define NULL 0 | ||
#endif | ||
|
||
#define GET 0 | ||
#define PUT 1 | ||
#define POST 2 | ||
#define DELETE 3 | ||
|
||
#define MAX_PAIRS 3 | ||
|
||
class HTTPHeader { | ||
private: | ||
int verb; | ||
char resource[64]; | ||
int nPairs; | ||
HTTPQuery pairs[MAX_PAIRS]; | ||
unsigned int contentLength; | ||
public: | ||
HTTPHeader(); | ||
void reset() {nPairs = 0;} | ||
void consumeLine(char* line); | ||
int getContentLength() {return contentLength;} | ||
//char* getHost() {return host;} | ||
char* getResource() {return resource;} | ||
//char* getVersion() {return "VERSION 1.0";} | ||
char* fetchQuery (char* key); | ||
}; | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#ifndef HTTPQUERY_H | ||
#define HTTPQUERY_H | ||
|
||
struct HTTPQuery { | ||
char key[16], value[32]; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include "Arduino.h" | ||
#include "stdlib.h" | ||
|
||
#ifndef STRUTILS_H | ||
#define STRUTILS_H | ||
|
||
bool stringContains(char* string, char c) { | ||
int len = strlen(string); | ||
|
||
for (int i = 0; i < len; i++) { | ||
if (string[i] == c) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
bool isDelimiter(char c, char* delimiters) { | ||
int nDelimiters = strlen(delimiters); | ||
|
||
for (int i = 0; i < nDelimiters; i++) { | ||
if (delimiters[i] == c) | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
char** stringSplit(char* string, char* delimiters, int* nTokens) { | ||
char* p = string; | ||
char* token; | ||
char** tokens; | ||
int count = 0; | ||
bool delimiterFound = false; | ||
|
||
while(*p != 0) { | ||
if (isDelimiter(*p, delimiters) && !delimiterFound) { | ||
count++; | ||
delimiterFound = true; | ||
} | ||
else { | ||
delimiterFound = false; | ||
} | ||
|
||
p++; | ||
} | ||
|
||
count++; | ||
|
||
tokens = (char**)malloc(count*sizeof(char*)); | ||
*nTokens = count; | ||
delimiterFound = false; | ||
token = p = string; | ||
count = 0; | ||
|
||
while(*p != 0) { | ||
if (isDelimiter(*p, delimiters) && !delimiterFound) { | ||
*p = 0; | ||
tokens[count++] = token; | ||
token = p + 1; | ||
delimiterFound = true; | ||
} | ||
else if (isDelimiter(*p, delimiters) && delimiterFound) { | ||
token++; | ||
} | ||
else { | ||
delimiterFound = false; | ||
} | ||
|
||
p++; | ||
} | ||
|
||
tokens[count] = token; | ||
|
||
return tokens; | ||
} | ||
#endif |