This is a HTTP Server example using the new EthernetInterface and new mbed-rpc libraries.
Dependents: HTTP-RPCServerExample
Fork of HTTPServer by
Revision 11:3943841e1798, committed 2013-06-02
- Comitter:
- leihen
- Date:
- Sun Jun 02 22:59:51 2013 +0000
- Parent:
- 10:cbde7929db7f
- Child:
- 12:ba81cc117fb6
- Commit message:
- A few bugfixes
Changed in this revision
--- a/HTTPConnection.cpp Sun Jun 02 00:37:38 2013 +0000 +++ b/HTTPConnection.cpp Sun Jun 02 22:59:51 2013 +0000 @@ -73,7 +73,7 @@ if (rcvd == -1) { // Invalid content received, so close the connection INFO("Invalid message received, so sending negative response and closing connection !"); - sprintf(buffer,"HTTP/1.0 400 BadRequest\n\rContent-Length: %d\n\rContent-Type: text\n\rConnection: Close\n\r\n\r",0); + sprintf(buffer,"HTTP/1.1 400 BadRequest\n\rContent-Length: %d\n\rContent-Type: text\n\r\n\r\n\r",0); m_Tcp.set_blocking(true, 1500); m_Tcp.send(buffer,strlen(buffer)); close(); @@ -116,7 +116,7 @@ // Try to receive up to the max number of characters for (i = 0 ; i < nMaxLen-1 ; i++) { int c; - c = m_Tcp.receive_all( szLine + i, 1 ); + c = m_Tcp.receive( szLine + i, 1 ); // Check that - if no character was currently received - the timeout period is reached. if ((c == 0) || (c==-1)) { // no character was read, so check if operation timed out
--- a/HTTPRequestHandler.cpp Sun Jun 02 00:37:38 2013 +0000 +++ b/HTTPRequestHandler.cpp Sun Jun 02 22:59:51 2013 +0000 @@ -2,7 +2,7 @@ #include "mbed.h" #include "HTTPRequestHandler.h" -#define _DEBUG 1 +#define _DEBUG 0 #if (_DEBUG && !defined(TARGET_LPC11U24)) #define INFO(x, ...) std::printf("[HTTPRequestHandler : DBG]"x"\r\n", ##__VA_ARGS__); @@ -16,6 +16,14 @@ static char buffer[128]; + +const char hdrDNT[] = "DNT: 1\r\n"; +const char hdrMaxAge[] = "MaxAge: 0\r\n"; +const char hdrConClose[] = "Connection: Keep-Alive\r\n"; +const char hdrContent[] = "Content-Type: text/html\r\n"; +const char hdrServer[] = "Server: mbed embedded\r\n"; +const char hdrEndl[] = "\r\n"; + HTTPRequestHandler::HTTPRequestHandler(HTTPConnection::HTTPMessage& Msg, TCPSocketConnection& Tcp) : msg(Msg), tcp(Tcp) { @@ -65,7 +73,7 @@ { INFO("Handling error !"); tcp.set_blocking(true, 1500); - sprintf(buffer,"HTTP/1.0 %d Error\r\n", errorCode); + sprintf(buffer,"HTTP/1.1 %d Error\r\n", errorCode); tcp.send(buffer, strlen(buffer)); sprintf(buffer, "Content-Length: %d\r\n", strlen(szErrorPage)); tcp.send(buffer, strlen(buffer)); @@ -90,13 +98,17 @@ { INFO("Starting response (%ld bytes in total)!", nLen); tcp.set_blocking(true, 1500); - sprintf(buffer, "HTTP/1.0 %d OK\r\n", returnCode); - tcp.send(buffer, strlen(buffer)); + sprintf(buffer, "HTTP/1.1 %d OK\r\n", returnCode); + tcp.send_all(buffer, strlen(buffer)); + tcp.send_all((char*)hdrConClose, strlen(hdrConClose)); sprintf(buffer, "Content-Length: %ld\r\n", nLen); // Add 2 chars for the terminating CR+LF - tcp.send(buffer, strlen(buffer)); + tcp.send_all(buffer, strlen(buffer)); if (header == NULL) { - sprintf(buffer, "Content-Type: text/html\r\nServer: mbed embedded\r\n\r\n"); - tcp.send(buffer, strlen(buffer)); + tcp.send_all((char*)hdrDNT, strlen(hdrDNT)); + tcp.send_all((char*)hdrMaxAge, strlen(hdrMaxAge)); + tcp.send_all((char*)hdrContent, strlen(hdrContent)); + tcp.send_all((char*)hdrServer, strlen(hdrServer)); + tcp.send_all((char*)hdrEndl, strlen(hdrEndl)); } else { for ( map<const char*, const char*>::iterator cIter = header->begin() ; cIter != header->end() ; cIter ++) { @@ -105,7 +117,7 @@ tcp.send((char*)cIter->second, strlen(cIter->second)); tcp.send("\r\n\r\n",2); } - tcp.send("\r\n", 2); + tcp.send_all("\r\n", 2); } // other content must be sent using the 'processResponse' function } @@ -113,11 +125,11 @@ void HTTPRequestHandler::processResponse(int nLen, char* body) { INFO("Processing Response (%d bytes)!\n",nLen); - tcp.send(body, nLen); + tcp.send_all(body, nLen); } void HTTPRequestHandler::endResponse() { INFO("Ending Response !"); - tcp.send("\r\n", 2); +// tcp.send("\r\n\r\n", 4); }
--- a/HTTPServer.cpp Sun Jun 02 00:37:38 2013 +0000 +++ b/HTTPServer.cpp Sun Jun 02 22:59:51 2013 +0000 @@ -1,7 +1,7 @@ #include "mbed.h" #include "HTTPServer.h" -#define _DEBUG 1 +#define _DEBUG 0 #ifdef _DEBUG DigitalOut led1(LED1); @@ -45,7 +45,7 @@ { char echoHeader[256]; tcp.set_blocking(true, 1500); - sprintf(echoHeader,"HTTP/1.1 404 Fail\r\nContent-Length: %d\r\nContent-Type: text/html\r\nServer: mbed embedded\r\n\n\r",strlen(szStdErrorPage)); + sprintf(echoHeader,"HTTP/1.0 404 Fail\r\nConnection: close\r\nContent-Length: %d\r\nContent-Type: text/html\r\nServer: mbed embedded\r\n\n\r",strlen(szStdErrorPage)); tcp.send(echoHeader, strlen(echoHeader)); tcp.send((char*)szStdErrorPage, strlen(szStdErrorPage)); } @@ -64,7 +64,10 @@ INFO("Connecting to network..."); // Try join the network - while(!m_wifly.connect()); + while(!m_wifly.connect()) { + INFO("Failed to connect. Trying again\n"); + m_wifly.reset(); + } INFO("connected\n"); // check if the start member was called already once @@ -150,20 +153,31 @@ // a new connection was received INFO("Client (IP=%s) is connected !\n", Clnt.get_address()); // Start the main connection thread -#ifdef _DEBUG - led3 = 1; - led2 = 1; -#endif - HTTPConnection con; - int c = con.poll(); - if (c == 0) { - // Handle the request - HandleRequest(con.m_Msg, Clnt); - } -#ifdef _DEBUG - led2 = 0; - led3 = 0; -#endif +// while(1) { + + #ifdef _DEBUG + led3 = 1; + led2 = 1; + #endif + HTTPConnection con; + int c = con.poll(); + if (c == 0) { + // Handle the request + HandleRequest(con.m_Msg, Clnt); + INFO("Closing connection.\n"); +// if (!m_wifly.close()) { +// ERR("Failed to close connection !\n"); +// } + } + if (c == -1) { + // No more data available or error + // break; + } + #ifdef _DEBUG + led2 = 0; + led3 = 0; + #endif +// } } INFO("Leaving polling thread");