A simple web server that can be bound to either the EthernetInterface or the WiflyInterface.
Dependents: Smart-WiFly-WebServer WattEye X10Svr SSDP_Server
Diff: SW_HTTPServer.cpp
- Revision:
- 2:a29c32190037
- Parent:
- 0:729320f63c5c
- Child:
- 3:17928786bdb5
diff -r 54353af0d20a -r a29c32190037 SW_HTTPServer.cpp --- a/SW_HTTPServer.cpp Fri May 31 03:46:58 2013 +0000 +++ b/SW_HTTPServer.cpp Sun Jun 02 18:32:05 2013 +0000 @@ -1,5 +1,5 @@ // -// note Copyright © 2013 by Smartware Computing, all rights reserved. +// @note Copyright © 2013 by Smartware Computing, all rights reserved. // Individuals may use this application for evaluation or non-commercial // purposes. Within this restriction, changes may be made to this application // as long as this copyright notice is retained. The user shall make @@ -24,6 +24,7 @@ const char hdr_httpver[] = "HTTP/1.1"; // typically HTTP/1.0 or HTTP/1.1 const char hdr_age[] = "Max-age: 0\r\n"; // expires right away (must be \r\n terminated) const char hdr_server[] = "Server: Smart_Server v0.1\r\n"; // Server (must be \r\n terminated) +const char hdr_dnt[] = "DNT: 1\r\n"; // Do Not Track const char hdr_close[] = "Connection: close\r\n"; // tell the client to close the connection (must be \r\n terminated) const char nl[] = "\r\n"; // final \r\n for the termination of the header @@ -39,6 +40,8 @@ {".zip", "Content-Type: image/zip\r\n" }, {".gz", "Content-Type: image/gz\r\n" }, {".tar", "Content-Type: image/tar\r\n" }, + {".txt", "Content-Type: plain/text\r\n" }, + {".pdf", "Content-Type: application/pdf\r\n" }, {".htm", "Content-Type: text/html\r\n" }, {".html","Content-Type: text/html\r\n" }, {0,0} @@ -84,7 +87,7 @@ } } -// ip_process() +// Poll() // // *OPEN*GET /x=1 HTTP/1.1 // Host: 192.168.1.140 @@ -94,9 +97,9 @@ // Accept-Encoding: gzip,deflate,sdch // Accept-Language: en-US,en;q=0.8 // -void HTTPServer::ip_process() +void HTTPServer::Poll() { -#define MAXRECSIZE 2000 +#define MAXRECSIZE 4000 static char buffer[MAXRECSIZE]; static char * bPtr = buffer; @@ -134,7 +137,7 @@ // If this queryString is in the list of registered handlers, call that for (int i=0; i<handlercount; i++) { if (strcmp(handlers[i].path, queryString) == 0) { - (*handlers[i].callback)(this, queryString, params, paramcount); + (*handlers[i].callback)(this, SEND_PAGE, queryString, params, paramcount); regHandled = true; break; // we only execute the first one } @@ -220,7 +223,9 @@ // *OPEN*GET /QueryString HTTP/1.1\r\nHeader stuff...\r\n\r\n dblCR = strstr(buffer,"\r\n\r\n"); if (dblCR) { // Have to scan from the beginning in case split on \r - //pc->printf("\r\n\r\nThe Whole Header:\r\n%s\r\n\r\n", buffer); + #ifdef DEBUG + pc->printf("\r\n\r\nThe Header:\r\n%s\r\n\r\n", buffer); + #endif char * soRec = buffer; // start of the next record of text char * eoRec = strchr(soRec, '\n'); // search for end of a record while (eoRec) { @@ -267,29 +272,62 @@ // Should we check the 'Content-Type' to see if it is // 'application/x-www-form-urlencoded'? int postBytes = atoi(contentLength); + bool acceptIt = false; //pc->printf("Content-Length = %d\r\n", postBytes); if (strcmp(queryType, "POST") == 0 && postBytes > 0 ) { if (postBytes) { - postQueryString = (char *)malloc(postBytes + 1); - if (postQueryString) { - char * offset; - int len; - - dblCR += 4; // If we slurped up any of the POST, + bool regHandled = false; + // Registered Dynamic Handler + // Callback and ask if they want to accept this data + for (int i=0; i<handlercount; i++) { + if (strcmp(handlers[i].path, queryString) == 0) { + acceptIt = (*handlers[i].callback)(this, CONTENT_LENGTH_REQUEST, queryString, params, paramcount); + regHandled = true; + break; // we only execute the first one + } + } + + if (regHandled && acceptIt) { + // If so, we'll make space for it + postQueryString = (char *)malloc(postBytes + 1); + if (postQueryString) { + char * offset; + int len; + + dblCR += 4; // If we slurped up any of the POST, + while (*dblCR && *dblCR <= ' ') + dblCR++; + strcpy(postQueryString, dblCR); // copy that in and then get the rest + while ((len = strlen(postQueryString)) < postBytes) { + int n; + offset = postQueryString + len; + n = client.receive(offset, postBytes - len); + if (n >=0) { + offset[n] = '\0'; + } + } + if (len >= 0) { + #ifdef DEBUG + pc->printf("POSTDATA: %s\r\n", postQueryString); + #endif + UnescapeString(postQueryString); + ParseParameters(postQueryString); + } + } + } else { + // Simply copy it to the bitbucket + int bytesToDump = postBytes; + char * bitbucket = (char *)malloc(201); + dblCR += 4; while (*dblCR && *dblCR <= ' ') - dblCR++; - strcpy(postQueryString, dblCR); // copy that in and then get the rest - while ((len = strlen(postQueryString)) < postBytes) { - int n; - offset = postQueryString + len; - n = client.receive(offset, postBytes - len); - if (n >=0) - offset[n] = '\0'; + dblCR++; + bytesToDump -= strlen(dblCR); + while (bytesToDump > 0) { + int n = (bytesToDump > 200) ? 200 : bytesToDump; + n = client.receive(bitbucket, n); + bytesToDump -= n; } - if (len >= 0) { - UnescapeString(postQueryString); - ParseParameters(postQueryString); - } + free(bitbucket); } } } @@ -484,6 +522,7 @@ if (optional_text) { send(optional_text); } + send(hdr_dnt); send(hdr_close); send(nl); }