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:
- 54:435bf9b66b2b
- Parent:
- 52:611a3b360f8c
--- a/SW_HTTPServer.cpp Sun Dec 03 23:39:30 2017 +0000 +++ b/SW_HTTPServer.cpp Sun Jun 10 22:40:27 2018 +0000 @@ -12,7 +12,7 @@ // #include "mbed.h" -#define DEBUG "HTTP" +//#define DEBUG "HTTP" #include <cstdio> #if (defined(DEBUG) && !defined(TARGET_LPC11U24)) #define DBG(x, ...) std::printf("[DBG %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); @@ -153,7 +153,7 @@ server->bind(port); server->listen(5); server->set_blocking(false, blockingtime); - client.set_blocking(true, blockingtime); //@TODO client is separate from server. any way to combine? + client.set_blocking(false, blockingtime); //@TODO client is separate from server. any way to combine? ResetPerformanceData(); PerformanceTimer.start(); } @@ -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("Sending %d bytes", bytes); //INFO("send:\r\n%s", msg); - #ifdef DEBUG - int r = - #endif - client.send((char *)msg, bytes); - //INFO("client.send returned: %d", r); + 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 { @@ -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