Library for httpserver

Dependents:   Internet_Piano_v2

Fork of httpServer_with_Ethernt by IOP

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HTTPServer.cpp Source File

HTTPServer.cpp

00001 #include "mbed.h"
00002 #include "HTTPServer.h"
00003 #define DEBUG
00004 #include "hl_debug.h"
00005 
00006 
00007 
00008 /* Constructor */
00009 /* initialize all members and set the standard error handler. */
00010 HTTPServer::HTTPServer()
00011     : m_pEthernet(NULL)
00012 {
00013     m_pErrorHandler = StdErrorHandler;
00014 }
00015 
00016 HTTPServer::~HTTPServer()
00017 {
00018     if (m_pEthernet == NULL) {
00019         INFO("Deleting EthernetInterface Object.\n");
00020         delete m_pEthernet;
00021     }
00022 }
00023 
00024 static const char* szStdErrorPage = "<HTML><HEAD><META content=\"text/html\" http-equiv=Content-Type></HEAD><BODY><h1>Error 404</h1><P>This resource is not available<P></BODY></HTML>\r\n\r\n";
00025 
00026 void HTTPServer::StdErrorHandler(HTTPConnection::HTTPMessage& msg, TCPSocketConnection& tcp)
00027 {
00028     char echoHeader[256];
00029     tcp.set_blocking(false, 1500);
00030     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));
00031     tcp.send(echoHeader, strlen(echoHeader));
00032     tcp.send((char*)szStdErrorPage, strlen(szStdErrorPage));
00033 }
00034 
00035 
00036 bool HTTPServer::start(int port, EthernetInterface* pEthernet)
00037 {
00038     //  If no ethernet interface is provided, instantiate own on the heap. This has to be deleted later in the destructor.
00039     //  If a valid pointer to an thernet interface is proveded, we can simply use it. 
00040     if (pEthernet == NULL) {
00041         ERR("Socket is not opened!!\r\n");
00042         
00043     } else {
00044         //  In the case that the ethernet interface is provided, it is assumed that a connection has already been created.
00045         INFO("Using connection IP %s", pEthernet->getIPAddress());
00046         m_pEthernet = pEthernet;
00047     }
00048     
00049     INFO("Binding to port %d...", port);
00050     if (m_Svr.bind(port) < 0) {
00051         ERR("Failed to bind to port !\n");
00052         error("Binding");
00053         return false;
00054     }
00055 
00056     INFO("Listening ...");
00057     if (m_Svr.listen(1) < 0) {
00058         ERR("Failed to listen !\n");
00059         error("Listening");
00060         return false;
00061     }
00062 
00063     INFO("Connected !");
00064     //  set into non blocking operation
00065     m_Svr.set_blocking(false, 1500); 
00066     
00067     return true;
00068 }
00069 
00070 
00071 int HTTPServer::poll(bool blocking)
00072 {
00073     //  This thread basically checks if there is a new incoming connection.
00074     //  If so , a new HTTPConnection is created and the connection thread is started.
00075     TCPSocketConnection Clnt;
00076     if (m_Svr.accept(Clnt) < 0) {
00077         return -1;
00078     }
00079 
00080     //   a new connection was received
00081     INFO("Client (IP=%s) is connected !\n", Clnt.get_address());
00082     HTTPConnection con(Clnt);
00083     int c = con.poll();
00084     if (c == 0) {
00085         //  Handle the request
00086         INFO("Handling request !");
00087         HandleRequest(con.m_Msg, Clnt);
00088     }
00089     if (c == -1) {
00090 //        break;
00091     }
00092 
00093     INFO("Leaving polling thread");
00094     return 0;
00095 }
00096 
00097 void HTTPServer::HandleRequest(HTTPConnection::HTTPMessage& msg, TCPSocketConnection& tcp)
00098 {
00099     std::string localPath;
00100     std::map<std::string, HTTPRequestHandler*(*)(const char*, const char*, HTTPConnection::HTTPMessage&, TCPSocketConnection&), handlersComp>::const_iterator it;
00101 
00102     //  Iterate through registered handlers and check if the handler's path is a subset of the requested uri.
00103     for (it = m_lpHandlers.begin() ; it != m_lpHandlers.end() ; it++) {
00104         //  check if this entries' path is fully contained at the beginning of the requested path
00105         std::string curpth = it->first;
00106 
00107         if (msg.uri.find(curpth) == 0) {
00108             // firts matching handler found, we just take it and we'll be happy
00109             localPath = msg.uri.substr(curpth.length());
00110             break;
00111         }
00112     }
00113 
00114     if (it == m_lpHandlers.end()) {
00115         //  There is no such handler, so return invalid
00116 
00117         m_pErrorHandler(msg, tcp);
00118         INFO("Webrequest left unhandled.");
00119     } else {
00120         //  Valid handler was found
00121         INFO("Routing webrequest !");
00122         //  Instantiate the handler object (handling will be done from withing the object's constructor
00123         HTTPRequestHandler *phdl = (*it->second)(it->first.c_str(), localPath.c_str(), msg, tcp);
00124         //  now we can delete the object, because handling is completed.
00125         if (phdl != NULL)
00126             delete phdl;
00127     }
00128 }
00129 
00130