aa
Dependents: Internet-Piano_WIZwiki-W7500
Fork of httpServer by
HTTPServer.cpp@2:45877b9fab33, 2015-08-11 (annotated)
- Committer:
- ysy00700
- Date:
- Tue Aug 11 05:37:32 2015 +0000
- Revision:
- 2:45877b9fab33
- Parent:
- 0:e59cc54df17c
aa
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hjjeon | 0:e59cc54df17c | 1 | #include "mbed.h" |
hjjeon | 0:e59cc54df17c | 2 | #include "HTTPServer.h" |
hjjeon | 0:e59cc54df17c | 3 | //#define DEBUG |
hjjeon | 0:e59cc54df17c | 4 | #include "hl_debug.h" |
hjjeon | 0:e59cc54df17c | 5 | |
hjjeon | 0:e59cc54df17c | 6 | |
hjjeon | 0:e59cc54df17c | 7 | |
hjjeon | 0:e59cc54df17c | 8 | /* Constructor */ |
hjjeon | 0:e59cc54df17c | 9 | /* initialize all members and set the standard error handler. */ |
hjjeon | 0:e59cc54df17c | 10 | HTTPServer::HTTPServer() |
hjjeon | 0:e59cc54df17c | 11 | : m_pEthernet(NULL) |
hjjeon | 0:e59cc54df17c | 12 | { |
hjjeon | 0:e59cc54df17c | 13 | m_pErrorHandler = StdErrorHandler; |
hjjeon | 0:e59cc54df17c | 14 | } |
hjjeon | 0:e59cc54df17c | 15 | |
hjjeon | 0:e59cc54df17c | 16 | HTTPServer::~HTTPServer() |
hjjeon | 0:e59cc54df17c | 17 | { |
hjjeon | 0:e59cc54df17c | 18 | if (m_pEthernet == NULL) { |
hjjeon | 0:e59cc54df17c | 19 | INFO("Deleting EthernetInterface Object.\n"); |
hjjeon | 0:e59cc54df17c | 20 | delete m_pEthernet; |
hjjeon | 0:e59cc54df17c | 21 | } |
hjjeon | 0:e59cc54df17c | 22 | } |
hjjeon | 0:e59cc54df17c | 23 | |
hjjeon | 0:e59cc54df17c | 24 | 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"; |
hjjeon | 0:e59cc54df17c | 25 | |
hjjeon | 0:e59cc54df17c | 26 | void HTTPServer::StdErrorHandler(HTTPConnection::HTTPMessage& msg, TCPSocketConnection& tcp) |
hjjeon | 0:e59cc54df17c | 27 | { |
hjjeon | 0:e59cc54df17c | 28 | char echoHeader[256]; |
hjjeon | 0:e59cc54df17c | 29 | tcp.set_blocking(true, 1500); |
hjjeon | 0:e59cc54df17c | 30 | 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)); |
hjjeon | 0:e59cc54df17c | 31 | tcp.send(echoHeader, strlen(echoHeader)); |
hjjeon | 0:e59cc54df17c | 32 | tcp.send((char*)szStdErrorPage, strlen(szStdErrorPage)); |
hjjeon | 0:e59cc54df17c | 33 | } |
hjjeon | 0:e59cc54df17c | 34 | |
hjjeon | 0:e59cc54df17c | 35 | |
hjjeon | 0:e59cc54df17c | 36 | bool HTTPServer::start(int port, EthernetInterface* pEthernet) |
hjjeon | 0:e59cc54df17c | 37 | { |
hjjeon | 0:e59cc54df17c | 38 | // If no ethernet interface is provided, instantiate own on the heap. This has to be deleted later in the destructor. |
hjjeon | 0:e59cc54df17c | 39 | // If a valid pointer to an thernet interface is proveded, we can simply use it. |
hjjeon | 0:e59cc54df17c | 40 | if (pEthernet == NULL) { |
hjjeon | 0:e59cc54df17c | 41 | ERR("Socket is not opened!!\r\n"); |
hjjeon | 0:e59cc54df17c | 42 | |
hjjeon | 0:e59cc54df17c | 43 | } else { |
hjjeon | 0:e59cc54df17c | 44 | // In the case that the ethernet interface is provided, it is assumed that a connection has already been created. |
hjjeon | 0:e59cc54df17c | 45 | INFO("Using connection IP %s", pEthernet->getIPAddress()); |
hjjeon | 0:e59cc54df17c | 46 | } |
hjjeon | 0:e59cc54df17c | 47 | |
hjjeon | 0:e59cc54df17c | 48 | INFO("Binding to port %d...", port); |
hjjeon | 0:e59cc54df17c | 49 | if (m_Svr.bind(port) < 0) { |
hjjeon | 0:e59cc54df17c | 50 | ERR("Failed to bind to port !\n"); |
hjjeon | 0:e59cc54df17c | 51 | error("Binding"); |
hjjeon | 0:e59cc54df17c | 52 | return false; |
hjjeon | 0:e59cc54df17c | 53 | } |
hjjeon | 0:e59cc54df17c | 54 | |
hjjeon | 0:e59cc54df17c | 55 | INFO("Listening ..."); |
hjjeon | 0:e59cc54df17c | 56 | if (m_Svr.listen(1) < 0) { |
hjjeon | 0:e59cc54df17c | 57 | ERR("Failed to listen !\n"); |
hjjeon | 0:e59cc54df17c | 58 | error("Listening"); |
hjjeon | 0:e59cc54df17c | 59 | return false; |
hjjeon | 0:e59cc54df17c | 60 | } |
hjjeon | 0:e59cc54df17c | 61 | |
hjjeon | 0:e59cc54df17c | 62 | INFO("Connected !"); |
hjjeon | 0:e59cc54df17c | 63 | // set into non blocking operation |
hjjeon | 0:e59cc54df17c | 64 | m_Svr.set_blocking(false, 100); |
hjjeon | 0:e59cc54df17c | 65 | |
hjjeon | 0:e59cc54df17c | 66 | return true; |
hjjeon | 0:e59cc54df17c | 67 | } |
hjjeon | 0:e59cc54df17c | 68 | |
hjjeon | 0:e59cc54df17c | 69 | |
hjjeon | 0:e59cc54df17c | 70 | int HTTPServer::poll(bool blocking) |
hjjeon | 0:e59cc54df17c | 71 | { |
hjjeon | 0:e59cc54df17c | 72 | // This thread basically checks if there is a new incoming connection. |
hjjeon | 0:e59cc54df17c | 73 | // If so , a new HTTPConnection is created and the connection thread is started. |
hjjeon | 0:e59cc54df17c | 74 | TCPSocketConnection Clnt; |
hjjeon | 0:e59cc54df17c | 75 | if (m_Svr.accept(Clnt) < 0) { |
hjjeon | 0:e59cc54df17c | 76 | return -1; |
hjjeon | 0:e59cc54df17c | 77 | } |
hjjeon | 0:e59cc54df17c | 78 | |
hjjeon | 0:e59cc54df17c | 79 | // a new connection was received |
hjjeon | 0:e59cc54df17c | 80 | INFO("Client (IP=%s) is connected !\n", Clnt.get_address()); |
hjjeon | 0:e59cc54df17c | 81 | HTTPConnection con(Clnt); |
hjjeon | 0:e59cc54df17c | 82 | int c = con.poll(); |
hjjeon | 0:e59cc54df17c | 83 | if (c == 0) { |
hjjeon | 0:e59cc54df17c | 84 | // Handle the request |
hjjeon | 0:e59cc54df17c | 85 | INFO("Handling request !"); |
hjjeon | 0:e59cc54df17c | 86 | HandleRequest(con.m_Msg, Clnt); |
hjjeon | 0:e59cc54df17c | 87 | } |
hjjeon | 0:e59cc54df17c | 88 | if (c == -1) { |
hjjeon | 0:e59cc54df17c | 89 | // break; |
hjjeon | 0:e59cc54df17c | 90 | } |
hjjeon | 0:e59cc54df17c | 91 | |
hjjeon | 0:e59cc54df17c | 92 | INFO("Leaving polling thread"); |
hjjeon | 0:e59cc54df17c | 93 | return 0; |
hjjeon | 0:e59cc54df17c | 94 | } |
hjjeon | 0:e59cc54df17c | 95 | |
hjjeon | 0:e59cc54df17c | 96 | void HTTPServer::HandleRequest(HTTPConnection::HTTPMessage& msg, TCPSocketConnection& tcp) |
hjjeon | 0:e59cc54df17c | 97 | { |
hjjeon | 0:e59cc54df17c | 98 | std::string localPath; |
hjjeon | 0:e59cc54df17c | 99 | std::map<std::string, HTTPRequestHandler*(*)(const char*, const char*, HTTPConnection::HTTPMessage&, TCPSocketConnection&), handlersComp>::const_iterator it; |
hjjeon | 0:e59cc54df17c | 100 | |
hjjeon | 0:e59cc54df17c | 101 | // Iterate through registered handlers and check if the handler's path is a subset of the requested uri. |
hjjeon | 0:e59cc54df17c | 102 | for (it = m_lpHandlers.begin() ; it != m_lpHandlers.end() ; it++) { |
hjjeon | 0:e59cc54df17c | 103 | // check if this entries' path is fully contained at the beginning of the requested path |
hjjeon | 0:e59cc54df17c | 104 | std::string curpth = it->first; |
hjjeon | 0:e59cc54df17c | 105 | |
hjjeon | 0:e59cc54df17c | 106 | if (msg.uri.find(curpth) == 0) { |
hjjeon | 0:e59cc54df17c | 107 | // firts matching handler found, we just take it and we'll be happy |
hjjeon | 0:e59cc54df17c | 108 | localPath = msg.uri.substr(curpth.length()); |
hjjeon | 0:e59cc54df17c | 109 | break; |
hjjeon | 0:e59cc54df17c | 110 | } |
hjjeon | 0:e59cc54df17c | 111 | } |
hjjeon | 0:e59cc54df17c | 112 | |
hjjeon | 0:e59cc54df17c | 113 | if (it == m_lpHandlers.end()) { |
hjjeon | 0:e59cc54df17c | 114 | // There is no such handler, so return invalid |
hjjeon | 0:e59cc54df17c | 115 | |
hjjeon | 0:e59cc54df17c | 116 | m_pErrorHandler(msg, tcp); |
hjjeon | 0:e59cc54df17c | 117 | INFO("Webrequest left unhandled."); |
hjjeon | 0:e59cc54df17c | 118 | } else { |
hjjeon | 0:e59cc54df17c | 119 | // Valid handler was found |
hjjeon | 0:e59cc54df17c | 120 | INFO("Routing webrequest !"); |
hjjeon | 0:e59cc54df17c | 121 | // Instantiate the handler object (handling will be done from withing the object's constructor |
hjjeon | 0:e59cc54df17c | 122 | HTTPRequestHandler *phdl = (*it->second)(it->first.c_str(), localPath.c_str(), msg, tcp); |
hjjeon | 0:e59cc54df17c | 123 | // now we can delete the object, because handling is completed. |
hjjeon | 0:e59cc54df17c | 124 | if (phdl != NULL) |
hjjeon | 0:e59cc54df17c | 125 | delete phdl; |
hjjeon | 0:e59cc54df17c | 126 | } |
hjjeon | 0:e59cc54df17c | 127 | } |