A simple web server that can be bound to either the EthernetInterface or the WiflyInterface.
Dependents: Smart-WiFly-WebServer WattEye X10Svr SSDP_Server
Revision 53:e5d96abe5e9b, committed 2018-06-10
- Comitter:
- WiredHome
- Date:
- Sun Jun 10 22:24:54 2018 +0000
- Parent:
- 51:758601b9bacd
- Child:
- 55:238dd90b6fb3
- Commit message:
- Intermediate check-in
Changed in this revision
SW_HTTPServer.cpp | Show annotated file Show diff for this revision Revisions of this file |
SW_HTTPServer.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/SW_HTTPServer.cpp Tue Apr 11 18:53:04 2017 +0000 +++ b/SW_HTTPServer.cpp Sun Jun 10 22:24:54 2018 +0000 @@ -317,17 +317,15 @@ } -void HTTPServer::send(const char * msg, int bytes) +int HTTPServer::send(const char * msg, int bytes) { if (bytes == -1) bytes = strlen(msg); INFO("Sending %d bytes", bytes); //INFO("send:\r\n%s", msg); - #ifdef DEBUG - int r = - #endif - client.send((char *)msg, bytes); + int r = client.send((char *)msg, bytes); INFO("client.send returned: %d", r); + return r; } const char * HTTPServer::FindAlias(const HTTPServer::namevalue * haystack, const char * needle) @@ -367,15 +365,23 @@ char *fbuffer = (char *)mymalloc(FILESEND_BUF_SIZE); int bytes; + client.set_blocking(true, 450); + //server->set_blocking(true, 240); if (fbuffer) { char ContentLen[30]; snprintf(ContentLen, sizeof(ContentLen), "Content-Length: %u\r\n", FileSize(filename)); header(OK, "OK", filetype, ContentLen); header(""); bytes = fread(fbuffer,sizeof(char),FILESEND_BUF_SIZE,fp); + INFO("Start with %d bytes", bytes); while (bytes > 0) { - send(fbuffer, bytes); - bytes = fread(fbuffer,sizeof(char),FILESEND_BUF_SIZE,fp); + int r = send(fbuffer, bytes); + INFO("sent %d", r); + if (r >= 0) { + bytes = fread(fbuffer,sizeof(char),FILESEND_BUF_SIZE,fp); + INFO(" Next %d bytes", bytes); + //INFO("::%s", fbuffer); + } } myfree(fbuffer); } else { @@ -523,7 +529,7 @@ if (optional_text == NULL) optional_text = ""; - INFO("header(%d, %s, %s, %s)", code, code_text, content_type); + INFO("header(%d, %s, %s, %s)", code, code_text, content_type, optional_text); snprintf(http, sizeof(http), "%s %i %s\r\n", hdr_httpver, code, code_text); send(http); if (content_type) { @@ -645,7 +651,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) { - INFO("CheckDynamicHandlers - SEND_PAGE"); + INFO("CheckDynamicHandlers - SEND_PAGE %s", handlers[i].path); (*handlers[i].callback)(this, SEND_PAGE, queryString, queryParams, queryParamCount); regHandled = true; break; // we only execute the first one
--- a/SW_HTTPServer.h Tue Apr 11 18:53:04 2017 +0000 +++ b/SW_HTTPServer.h Sun Jun 10 22:24:54 2018 +0000 @@ -288,7 +288,7 @@ * number of bytes being passed in the path parameters. * @return one of the @see CallBackResults signals indicating error or successes */ - typedef CallBackResults (* Handler)(HTTPServer * svr, CallBackType type, const char *path, + typedef CallBackResults (* Handler)(HTTPServer * svr, CallBackType type, char *path, const namevalue *queryParams, int queryParamCount); /** @@ -444,8 +444,9 @@ * * @param[in] msg is the text string to send * @param[in] bytes is the number of bytes to send. If not set, then strlen is calculated. + * @returns # of bytes sent, and < 0 for various error codes. */ - void send(const char * msg, int bytes = -1); + int send(const char * msg, int bytes = -1); /** * Get the size of the file @@ -482,7 +483,7 @@ * svr.RegisterHandler("/dyn1", SimpleDynamicPage); * ... * - * HTTPServer::CallBackResults SimpleDynamicPage(HTTPServer *svr, HTTPServer::CallBackType type, const char * path, + * HTTPServer::CallBackResults SimpleDynamicPage(HTTPServer *svr, HTTPServer::CallBackType type, char * path, * const HTTPServer::namevalue *queryParams, int queryParamCount) { * char buf[100]; * HTTPServer::CallBackResults ret = HTTPServer::ACCEPT_ERROR;