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:
- 22:55974de690c1
- Parent:
- 21:660143f20b04
- Child:
- 23:6963f45e950a
--- a/SW_HTTPServer.cpp Thu Sep 26 23:47:50 2013 +0000 +++ b/SW_HTTPServer.cpp Thu Oct 10 13:44:07 2013 +0000 @@ -87,6 +87,8 @@ wifly = _wf; webroot = (char *)malloc(strlen(_webroot)+1); strcpy(webroot, _webroot); + if (strlen(webroot)>1 && webroot[strlen(webroot)-1] == '/') // remove trailing '/' + webroot[strlen(webroot)-1] = '\0'; maxqueryParams = _maxqueryParams; maxdynamicpages = _maxdynamicpages; headerParams = (namevalue *)malloc(maxheaderParams * sizeof(namevalue)); @@ -457,13 +459,17 @@ char * HTTPServer::rewritePrependWebroot(char * queryString) { - char * temp = (char *)mymalloc(strlen(webroot) + strlen(queryString) + 1); + char * temp = (char *)mymalloc(strlen(webroot) + strlen(queryString) + 2); // room for / if (temp) { *temp = '\0'; + char lastchar; strcpy(temp, webroot); - if (temp[strlen(temp)-1] == '/' && *queryString == '/') - temp[strlen(temp)-1] = '\0'; + lastchar = temp[strlen(temp)-1]; + if (lastchar != '/' && *queryString != '/') + strcat(temp, "/"); + else if (lastchar == '/' && *queryString == '/') + queryString++; strcat(temp, queryString); myfree(queryString); return temp; @@ -637,18 +643,26 @@ bool acceptIt = false; if (strcmp(queryType, "POST") == 0 && postBytes > 0 ) { if (postBytes) { + int ndxHandler = 0; 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, queryParams, queryParamCount); + for (ndxHandler=0; ndxHandler<handlercount; ndxHandler++) { + if (strcmp(handlers[ndxHandler].path, queryString) == 0) { + acceptIt = (*handlers[ndxHandler].callback)(this, CONTENT_LENGTH_REQUEST, queryString, queryParams, queryParamCount); regHandled = true; break; // we only execute the first one } } if (regHandled && acceptIt) { + // @todo need to refactor - if the thing is bigger than the buffer, + // then we can receive it a chunk at a time, and hand off + // the chunks to the callback. May need callbacks that + // are: START: extract the filename/object name, + // NEXT: a chunk of data, + // END: signals that all chunks were delivered. + // // If so, we'll make space for it postQueryString = (char *)mymalloc(postBytes + 1); if (postQueryString) { @@ -665,12 +679,22 @@ n = client.receive(offset, postBytes - len); if (n >=0) { offset[n] = '\0'; + //printf("HTTPd: %d of %d: [%s]\r\n", len, postBytes, offset); + } else if (n < 0) { + //printf("HTTPd: n=%d\r\n", n); + break; // no more data, before the plan } } if (len >= 0) { - UnescapeString(postQueryString); - ParseParameters(postQueryString); +// UnescapeString(postQueryString); +// ParseParameters(postQueryString); + // use the same handler as for the length check + acceptIt = (*handlers[ndxHandler].callback)(this, DATA_TRANSFER, postQueryString, NULL, 0); + } else { + pc->printf("HTTPd: len error.\r\n"); } + } else { + pc->printf("HTTPd: attempt to allocate %d failed.\r\n", postBytes+1); } } else { // Simply copy it to the bitbucket