전시회용

Fork of httpServer-orgel by justin kim

Committer:
justinkim
Date:
Fri Oct 21 04:01:13 2016 +0000
Revision:
7:d19f02b618d0
Parent:
3:87bec0b34de7
????
;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hjjeon 0:e59cc54df17c 1 #include "mbed.h"
hjjeon 0:e59cc54df17c 2 #include "HTTPServer.h"
hjjeon 3:87bec0b34de7 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 1:772534e3b627 29 tcp.set_blocking(false, 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());
justinkim 2:dd293a10a772 46 m_pEthernet = pEthernet;
hjjeon 0:e59cc54df17c 47 }
hjjeon 0:e59cc54df17c 48
hjjeon 0:e59cc54df17c 49 INFO("Binding to port %d...", port);
hjjeon 0:e59cc54df17c 50 if (m_Svr.bind(port) < 0) {
hjjeon 0:e59cc54df17c 51 ERR("Failed to bind to port !\n");
hjjeon 0:e59cc54df17c 52 error("Binding");
hjjeon 0:e59cc54df17c 53 return false;
hjjeon 0:e59cc54df17c 54 }
hjjeon 0:e59cc54df17c 55
hjjeon 0:e59cc54df17c 56 INFO("Listening ...");
hjjeon 0:e59cc54df17c 57 if (m_Svr.listen(1) < 0) {
hjjeon 0:e59cc54df17c 58 ERR("Failed to listen !\n");
hjjeon 0:e59cc54df17c 59 error("Listening");
hjjeon 0:e59cc54df17c 60 return false;
hjjeon 0:e59cc54df17c 61 }
hjjeon 0:e59cc54df17c 62
hjjeon 0:e59cc54df17c 63 INFO("Connected !");
hjjeon 0:e59cc54df17c 64 // set into non blocking operation
hjjeon 1:772534e3b627 65 m_Svr.set_blocking(false, 1500);
hjjeon 1:772534e3b627 66
hjjeon 0:e59cc54df17c 67 return true;
hjjeon 0:e59cc54df17c 68 }
hjjeon 0:e59cc54df17c 69
hjjeon 0:e59cc54df17c 70
hjjeon 0:e59cc54df17c 71 int HTTPServer::poll(bool blocking)
hjjeon 0:e59cc54df17c 72 {
hjjeon 0:e59cc54df17c 73 // This thread basically checks if there is a new incoming connection.
hjjeon 0:e59cc54df17c 74 // If so , a new HTTPConnection is created and the connection thread is started.
hjjeon 0:e59cc54df17c 75 TCPSocketConnection Clnt;
hjjeon 0:e59cc54df17c 76 if (m_Svr.accept(Clnt) < 0) {
hjjeon 0:e59cc54df17c 77 return -1;
hjjeon 0:e59cc54df17c 78 }
hjjeon 0:e59cc54df17c 79
hjjeon 0:e59cc54df17c 80 // a new connection was received
hjjeon 0:e59cc54df17c 81 INFO("Client (IP=%s) is connected !\n", Clnt.get_address());
hjjeon 0:e59cc54df17c 82 HTTPConnection con(Clnt);
hjjeon 0:e59cc54df17c 83 int c = con.poll();
hjjeon 0:e59cc54df17c 84 if (c == 0) {
hjjeon 0:e59cc54df17c 85 // Handle the request
hjjeon 0:e59cc54df17c 86 INFO("Handling request !");
hjjeon 0:e59cc54df17c 87 HandleRequest(con.m_Msg, Clnt);
hjjeon 0:e59cc54df17c 88 }
hjjeon 0:e59cc54df17c 89 if (c == -1) {
hjjeon 0:e59cc54df17c 90 // break;
hjjeon 0:e59cc54df17c 91 }
hjjeon 0:e59cc54df17c 92
hjjeon 0:e59cc54df17c 93 INFO("Leaving polling thread");
hjjeon 0:e59cc54df17c 94 return 0;
hjjeon 0:e59cc54df17c 95 }
hjjeon 0:e59cc54df17c 96
hjjeon 0:e59cc54df17c 97 void HTTPServer::HandleRequest(HTTPConnection::HTTPMessage& msg, TCPSocketConnection& tcp)
hjjeon 0:e59cc54df17c 98 {
hjjeon 0:e59cc54df17c 99 std::string localPath;
hjjeon 0:e59cc54df17c 100 std::map<std::string, HTTPRequestHandler*(*)(const char*, const char*, HTTPConnection::HTTPMessage&, TCPSocketConnection&), handlersComp>::const_iterator it;
hjjeon 0:e59cc54df17c 101
hjjeon 0:e59cc54df17c 102 // Iterate through registered handlers and check if the handler's path is a subset of the requested uri.
hjjeon 0:e59cc54df17c 103 for (it = m_lpHandlers.begin() ; it != m_lpHandlers.end() ; it++) {
hjjeon 0:e59cc54df17c 104 // check if this entries' path is fully contained at the beginning of the requested path
hjjeon 0:e59cc54df17c 105 std::string curpth = it->first;
hjjeon 0:e59cc54df17c 106
hjjeon 0:e59cc54df17c 107 if (msg.uri.find(curpth) == 0) {
hjjeon 0:e59cc54df17c 108 // firts matching handler found, we just take it and we'll be happy
hjjeon 0:e59cc54df17c 109 localPath = msg.uri.substr(curpth.length());
hjjeon 0:e59cc54df17c 110 break;
hjjeon 0:e59cc54df17c 111 }
hjjeon 0:e59cc54df17c 112 }
hjjeon 0:e59cc54df17c 113
hjjeon 0:e59cc54df17c 114 if (it == m_lpHandlers.end()) {
hjjeon 0:e59cc54df17c 115 // There is no such handler, so return invalid
hjjeon 0:e59cc54df17c 116
hjjeon 0:e59cc54df17c 117 m_pErrorHandler(msg, tcp);
hjjeon 0:e59cc54df17c 118 INFO("Webrequest left unhandled.");
hjjeon 0:e59cc54df17c 119 } else {
hjjeon 0:e59cc54df17c 120 // Valid handler was found
hjjeon 0:e59cc54df17c 121 INFO("Routing webrequest !");
hjjeon 0:e59cc54df17c 122 // Instantiate the handler object (handling will be done from withing the object's constructor
hjjeon 0:e59cc54df17c 123 HTTPRequestHandler *phdl = (*it->second)(it->first.c_str(), localPath.c_str(), msg, tcp);
hjjeon 0:e59cc54df17c 124 // now we can delete the object, because handling is completed.
hjjeon 0:e59cc54df17c 125 if (phdl != NULL)
hjjeon 0:e59cc54df17c 126 delete phdl;
hjjeon 0:e59cc54df17c 127 }
hjjeon 0:e59cc54df17c 128 }
justinkim 2:dd293a10a772 129
justinkim 2:dd293a10a772 130