Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
deusprogrammer committed Jun 18, 2012
0 parents commit c4bbd5c
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 0 deletions.
55 changes: 55 additions & 0 deletions httpHeader.cpp
@@ -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;
}
}
}
36 changes: 36 additions & 0 deletions httpHeader.h
@@ -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
8 changes: 8 additions & 0 deletions httpQuery.h
@@ -0,0 +1,8 @@
#ifndef HTTPQUERY_H
#define HTTPQUERY_H

struct HTTPQuery {
char key[16], value[32];
};

#endif
78 changes: 78 additions & 0 deletions strutils.h
@@ -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

0 comments on commit c4bbd5c

Please sign in to comment.