This is about a project I've started last year and had to freeze for some time. Now I revisited the code and gave it the last adjustments.
I've built a library to parse HTTP requests received by Arduino and convert the arguments into accessible variables/functions.
**This is NOT a web server / tiny web server *** It's just a set of functions and methods to parse the HTTP request arguments and make them available through functions (library).
It has many opportunities for improvement, but for those (like myself) who need a quick way to interpreter HTTP requests on small projects, this library is very handy.
I hope you find it useful and fell free to enhance it.
Francisco Paletta.
BASIC INSTRUCTIONS:
void setup() {
//Create an object to handle the HTTP request
HttpRequest httpReq;
}
void loop(){
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//parse the received caracter
httpReq.parseRequest(c);
//IF request has ended -> handle response
if (httpReq.endOfRequest()) {
//handle response to requesting client and access arguments
//access object properties
client.print("Method: ");
client.print(httpReq.method);
client.println("
");
client.print("Uri: ");
client.print(httpReq.uri);
client.println("
");
client.print("Version: ");
client.print(httpReq.version);
client.println("
");
client.print("paramCount: ");
client.print(httpReq.paramCount);
client.println("
");
//list received parameters GET and POST
client.println("Parameters:
");
for(int i=1;i<=httpReq.paramCount;i++){
httpReq.getParam(i,name,value);
client.print(name);
client.print("-");
client.print(value);
client.println("
");
}
//list received cookies
client.println("Cookies:
");
for(int i=1;i<=httpReq.cookieCount;i++){
httpReq.getCookie(i,name,value);
client.print(name);
client.print(" - ");
client.print(value);
client.println("
");
}
//Reset object and free dynamic allocated memory
httpReq.resetRequest();
}
}
}
}
Our friend xfileslv has found a bug on the data send through POST method: the very last character in the post message was not being processed, so the last value posted was wrong.
I have fixed the bug and reposted the Zip file with the correct library.
Yes, I can add that field to the HTTP parser library. Let me know how urgent you need it because I can write one especial modification with that field so you can use right away.
But, if it's not too urgent, I already have in mind developing a mechanism to allow all the fields without having to create a variable to each (it would go like a dynamic chained list - as I already use for the parameters). So it would, at once, parse any HTTP request field with it's related data. The only constraint on that is I need some time, and it may be done only later in January.
So let me know whether you can wait a few more weeks or prefer just to have that one hard-coded into the object.
I know of several other libraries doing more or less the same, each with its plus and minus, and I thank you for sharing yours.
I tried Webduino and appreciated its overall structure, and I also developed one from scratch that I called HttpSvr (Google Code Archive - Long-term storage for Google Code Project Hosting.) inspired by Webduino, in the attempt of serving file upload and download, together with AJAX POST requests.
If you find it useful, you can give HttpSvr a try, or make comments if you deem it useful.
I've seen, you are open for wishes???
It would be great to have Referer available as parameter?
By the way....maybe not really for this library, but a possibility for simple (maybe sessionbased) password protection would be great! (have searched for it, but only found many requests but no "simple" solution appart from some large webserver libs.