window
Fork of httpServer by
Embed:
(wiki syntax)
Show/hide line numbers
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 } 00047 00048 INFO("Binding to port %d...", port); 00049 if (m_Svr.bind(port) < 0) { 00050 ERR("Failed to bind to port !\n"); 00051 error("Binding"); 00052 return false; 00053 } 00054 00055 INFO("Listening ..."); 00056 if (m_Svr.listen(1) < 0) { 00057 ERR("Failed to listen !\n"); 00058 error("Listening"); 00059 return false; 00060 } 00061 00062 INFO("Connected !"); 00063 // set into non blocking operation 00064 m_Svr.set_blocking(false, 1500); 00065 00066 return true; 00067 } 00068 00069 00070 int HTTPServer::poll(bool blocking) 00071 { 00072 // This thread basically checks if there is a new incoming connection. 00073 // If so , a new HTTPConnection is created and the connection thread is started. 00074 TCPSocketConnection Clnt; 00075 if (m_Svr.accept(Clnt) < 0) { 00076 return -1; 00077 } 00078 00079 // a new connection was received 00080 INFO("Client (IP=%s) is connected !\n", Clnt.get_address()); 00081 HTTPConnection con(Clnt); 00082 int c = con.poll(); 00083 if (c == 0) { 00084 // Handle the request 00085 INFO("Handling request !"); 00086 HandleRequest(con.m_Msg, Clnt); 00087 } 00088 if (c == -1) { 00089 // break; 00090 } 00091 00092 INFO("Leaving polling thread"); 00093 return 0; 00094 } 00095 00096 void HTTPServer::HandleRequest(HTTPConnection::HTTPMessage& msg, TCPSocketConnection& tcp) 00097 { 00098 std::string localPath; 00099 std::map<std::string, HTTPRequestHandler*(*)(const char*, const char*, HTTPConnection::HTTPMessage&, TCPSocketConnection&), handlersComp>::const_iterator it; 00100 00101 // Iterate through registered handlers and check if the handler's path is a subset of the requested uri. 00102 for (it = m_lpHandlers.begin() ; it != m_lpHandlers.end() ; it++) { 00103 // check if this entries' path is fully contained at the beginning of the requested path 00104 std::string curpth = it->first; 00105 00106 if (msg.uri.find(curpth) == 0) { 00107 // firts matching handler found, we just take it and we'll be happy 00108 localPath = msg.uri.substr(curpth.length()); 00109 break; 00110 } 00111 } 00112 00113 if (it == m_lpHandlers.end()) { 00114 // There is no such handler, so return invalid 00115 00116 m_pErrorHandler(msg, tcp); 00117 INFO("Webrequest left unhandled."); 00118 } else { 00119 // Valid handler was found 00120 INFO("Routing webrequest !"); 00121 // Instantiate the handler object (handling will be done from withing the object's constructor 00122 HTTPRequestHandler *phdl = (*it->second)(it->first.c_str(), localPath.c_str(), msg, tcp); 00123 // now we can delete the object, because handling is completed. 00124 if (phdl != NULL) 00125 delete phdl; 00126 } 00127 }
Generated on Mon Jul 18 2022 01:17:42 by 1.7.2