ソースの整理中ですが、利用はできます。 大きなファイルはできないかもしれません。
Dependencies: EthernetInterface HttpServer TextLCD expatlib mbed-rpc mbed-rtos mbed Socket lwip-eth lwip-sys lwip
Fork of giken9_HTMLServer_Sample by
HttpServer/HTTPServer.h@2:6f25f8327180, 2014-03-12 (annotated)
- Committer:
- yueee_yt
- Date:
- Wed Mar 12 06:30:30 2014 +0000
- Revision:
- 2:6f25f8327180
- Parent:
- 1:bd7da995f192
debug comment
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yueee_yt | 0:7766f6712673 | 1 | //#define _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 2 | |
yueee_yt | 0:7766f6712673 | 3 | #ifndef HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 4 | #define HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 5 | |
yueee_yt | 0:7766f6712673 | 6 | #define HELLO_PAGE "/hello" |
yueee_yt | 0:7766f6712673 | 7 | #define RPC_PAGE "/rpc" |
yueee_yt | 0:7766f6712673 | 8 | //#define IEEE1888WSDL_PAGE "/IEEE1888?wsdl" |
yueee_yt | 0:7766f6712673 | 9 | //#define IEEE1888_PAGE "/IEEE1888" |
yueee_yt | 0:7766f6712673 | 10 | #define FS_PAGE "/" |
yueee_yt | 0:7766f6712673 | 11 | |
yueee_yt | 0:7766f6712673 | 12 | #include <string> |
yueee_yt | 0:7766f6712673 | 13 | using std::string; |
yueee_yt | 0:7766f6712673 | 14 | |
yueee_yt | 0:7766f6712673 | 15 | #include <map> |
yueee_yt | 0:7766f6712673 | 16 | using std::map; |
yueee_yt | 0:7766f6712673 | 17 | |
yueee_yt | 0:7766f6712673 | 18 | #include "HTTPRequestHandler.h" |
yueee_yt | 0:7766f6712673 | 19 | #include "rtos.h" |
yueee_yt | 0:7766f6712673 | 20 | #include "mbed.h" |
yueee_yt | 0:7766f6712673 | 21 | #include "EthernetInterface.h" |
yueee_yt | 0:7766f6712673 | 22 | |
yueee_yt | 0:7766f6712673 | 23 | #include "Handler/RPCHandler.h" |
yueee_yt | 0:7766f6712673 | 24 | #include "Handler/FSHandler.h" |
yueee_yt | 0:7766f6712673 | 25 | #include "Handler/SimpleHandler.h" |
yueee_yt | 0:7766f6712673 | 26 | |
yueee_yt | 1:bd7da995f192 | 27 | #define THREAD_MAX 5 |
yueee_yt | 0:7766f6712673 | 28 | Thread *threads[THREAD_MAX]; |
yueee_yt | 0:7766f6712673 | 29 | Thread *xthread; |
yueee_yt | 0:7766f6712673 | 30 | |
yueee_yt | 0:7766f6712673 | 31 | /* |
yueee_yt | 0:7766f6712673 | 32 | struct handlersComp { //Used to order handlers in the right way |
yueee_yt | 0:7766f6712673 | 33 | bool operator() (const string& handler1, const string& handler2) const { |
yueee_yt | 0:7766f6712673 | 34 | //The first handler is longer than the second one |
yueee_yt | 0:7766f6712673 | 35 | if (handler1.length() > handler2.length()) |
yueee_yt | 0:7766f6712673 | 36 | return true; //Returns true if handler1 is to appear before handler2 |
yueee_yt | 0:7766f6712673 | 37 | else if (handler1.length() < handler2.length()) |
yueee_yt | 0:7766f6712673 | 38 | return false; |
yueee_yt | 0:7766f6712673 | 39 | else //To avoid the == case, sort now by address |
yueee_yt | 0:7766f6712673 | 40 | return ((&handler1)>(&handler2)); |
yueee_yt | 0:7766f6712673 | 41 | } |
yueee_yt | 0:7766f6712673 | 42 | }; |
yueee_yt | 0:7766f6712673 | 43 | |
yueee_yt | 0:7766f6712673 | 44 | map< string, HTTPRequestHandler*(*)(const char*, const char* , TCPSocketConnection* ), handlersComp > m_lpHandlers; |
yueee_yt | 0:7766f6712673 | 45 | template<typename T> |
yueee_yt | 0:7766f6712673 | 46 | void HTTPServerAddHandler(const char* path) //Template decl in header |
yueee_yt | 0:7766f6712673 | 47 | { |
yueee_yt | 0:7766f6712673 | 48 | m_lpHandlers[path] = &T::inst; |
yueee_yt | 0:7766f6712673 | 49 | } |
yueee_yt | 0:7766f6712673 | 50 | */ |
yueee_yt | 0:7766f6712673 | 51 | |
yueee_yt | 0:7766f6712673 | 52 | void ListenThread(void const *args); |
yueee_yt | 0:7766f6712673 | 53 | enum HTTP_METH { |
yueee_yt | 0:7766f6712673 | 54 | HTTP_GET, |
yueee_yt | 0:7766f6712673 | 55 | HTTP_POST, |
yueee_yt | 0:7766f6712673 | 56 | HTTP_HEAD |
yueee_yt | 0:7766f6712673 | 57 | }; |
yueee_yt | 0:7766f6712673 | 58 | |
yueee_yt | 0:7766f6712673 | 59 | bool getRequest(TCPSocketConnection* client,string* path, string* meth) |
yueee_yt | 0:7766f6712673 | 60 | { |
yueee_yt | 0:7766f6712673 | 61 | char req[128]; |
yueee_yt | 0:7766f6712673 | 62 | char c_path[128]; |
yueee_yt | 0:7766f6712673 | 63 | char c_meth[128]; |
yueee_yt | 0:7766f6712673 | 64 | const int maxLen = 128; |
yueee_yt | 0:7766f6712673 | 65 | char* p = req; |
yueee_yt | 0:7766f6712673 | 66 | //Read Line |
yueee_yt | 0:7766f6712673 | 67 | int ret; |
yueee_yt | 0:7766f6712673 | 68 | int len = 0; |
yueee_yt | 0:7766f6712673 | 69 | for(int i = 0; i < maxLen - 1; i++) { |
yueee_yt | 0:7766f6712673 | 70 | ret = client->receive(p, 1); |
yueee_yt | 0:7766f6712673 | 71 | if(!ret) { |
yueee_yt | 0:7766f6712673 | 72 | break; |
yueee_yt | 0:7766f6712673 | 73 | } |
yueee_yt | 0:7766f6712673 | 74 | if( (len > 1) && *(p-1)=='\r' && *p=='\n' ) { |
yueee_yt | 0:7766f6712673 | 75 | p--; |
yueee_yt | 0:7766f6712673 | 76 | len-=2; |
yueee_yt | 0:7766f6712673 | 77 | break; |
yueee_yt | 0:7766f6712673 | 78 | } else if( *p=='\n' ) { |
yueee_yt | 0:7766f6712673 | 79 | len--; |
yueee_yt | 0:7766f6712673 | 80 | break; |
yueee_yt | 0:7766f6712673 | 81 | } |
yueee_yt | 0:7766f6712673 | 82 | p++; |
yueee_yt | 0:7766f6712673 | 83 | len++; |
yueee_yt | 0:7766f6712673 | 84 | } |
yueee_yt | 0:7766f6712673 | 85 | *p = 0; |
yueee_yt | 0:7766f6712673 | 86 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 87 | printf("Parsing request : %s\r\n", req); |
yueee_yt | 0:7766f6712673 | 88 | #endif |
yueee_yt | 0:7766f6712673 | 89 | ret = sscanf(req, "%s %s HTTP/%*d.%*d", c_meth, c_path); |
yueee_yt | 0:7766f6712673 | 90 | if(ret !=2) return false; |
yueee_yt | 0:7766f6712673 | 91 | *meth = string(c_meth); |
yueee_yt | 0:7766f6712673 | 92 | *path = string(c_path); |
yueee_yt | 0:7766f6712673 | 93 | return true; |
yueee_yt | 0:7766f6712673 | 94 | } |
yueee_yt | 0:7766f6712673 | 95 | |
yueee_yt | 0:7766f6712673 | 96 | void dispatchRequest(TCPSocketConnection* client) |
yueee_yt | 0:7766f6712673 | 97 | { |
yueee_yt | 0:7766f6712673 | 98 | string path; |
yueee_yt | 0:7766f6712673 | 99 | string meth; |
yueee_yt | 0:7766f6712673 | 100 | HTTP_METH methCode; |
yueee_yt | 0:7766f6712673 | 101 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 102 | printf("Dispatching req\r\n"); |
yueee_yt | 0:7766f6712673 | 103 | #endif |
yueee_yt | 0:7766f6712673 | 104 | if( !getRequest(client,&path, &meth ) ) { |
yueee_yt | 0:7766f6712673 | 105 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 106 | printf("dispatchRequest Invalid request\r\n"); |
yueee_yt | 0:7766f6712673 | 107 | #endif |
yueee_yt | 0:7766f6712673 | 108 | //close(); |
yueee_yt | 0:7766f6712673 | 109 | return; //Invalid request |
yueee_yt | 0:7766f6712673 | 110 | } |
yueee_yt | 0:7766f6712673 | 111 | if( !meth.compare("GET") ) { |
yueee_yt | 0:7766f6712673 | 112 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 113 | printf("dispatchRequest HTTP_GET\r\n"); |
yueee_yt | 0:7766f6712673 | 114 | #endif |
yueee_yt | 0:7766f6712673 | 115 | methCode = HTTP_GET; |
yueee_yt | 0:7766f6712673 | 116 | } else if( !meth.compare("POST") ) { |
yueee_yt | 0:7766f6712673 | 117 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 118 | printf("dispatchRequest HTTP_POST\r\n"); |
yueee_yt | 0:7766f6712673 | 119 | #endif |
yueee_yt | 0:7766f6712673 | 120 | methCode = HTTP_POST; |
yueee_yt | 0:7766f6712673 | 121 | } else if( !meth.compare("HEAD") ) { |
yueee_yt | 0:7766f6712673 | 122 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 123 | printf("dispatchRequest HTTP_HEAD\r\n"); |
yueee_yt | 0:7766f6712673 | 124 | #endif |
yueee_yt | 0:7766f6712673 | 125 | methCode = HTTP_HEAD; |
yueee_yt | 0:7766f6712673 | 126 | } else { |
yueee_yt | 0:7766f6712673 | 127 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 128 | printf("dispatchRequest() Parse error\r\n"); |
yueee_yt | 0:7766f6712673 | 129 | #endif |
yueee_yt | 0:7766f6712673 | 130 | //close(); //Parse error |
yueee_yt | 0:7766f6712673 | 131 | return; |
yueee_yt | 0:7766f6712673 | 132 | } |
yueee_yt | 0:7766f6712673 | 133 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 134 | printf("Looking for a handler\r\n"); |
yueee_yt | 0:7766f6712673 | 135 | #endif |
yueee_yt | 0:7766f6712673 | 136 | /* |
yueee_yt | 0:7766f6712673 | 137 | map< string, HTTPRequestHandler*(*)(const char*, const char*, TCPSocketConnection*) >::iterator it; |
yueee_yt | 0:7766f6712673 | 138 | int root_len = 0; |
yueee_yt | 0:7766f6712673 | 139 | for (it = m_lpHandlers.begin(); it != m_lpHandlers.end(); it++) { |
yueee_yt | 0:7766f6712673 | 140 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 141 | printf("Checking %s...\r\n", (*it).first.c_str()); |
yueee_yt | 0:7766f6712673 | 142 | #endif |
yueee_yt | 0:7766f6712673 | 143 | root_len = (*it).first.length(); |
yueee_yt | 0:7766f6712673 | 144 | if ( root_len && |
yueee_yt | 0:7766f6712673 | 145 | !path.compare( 0, root_len, (*it).first ) && |
yueee_yt | 0:7766f6712673 | 146 | (path[root_len] == '/' || path[root_len] == '\0')) { |
yueee_yt | 0:7766f6712673 | 147 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 148 | printf("Found (%s)\r\n", (*it).first.c_str()); |
yueee_yt | 0:7766f6712673 | 149 | #endif |
yueee_yt | 0:7766f6712673 | 150 | // Found! |
yueee_yt | 0:7766f6712673 | 151 | break; // for |
yueee_yt | 0:7766f6712673 | 152 | } |
yueee_yt | 0:7766f6712673 | 153 | } |
yueee_yt | 0:7766f6712673 | 154 | if((it == m_lpHandlers.end()) && !(m_lpHandlers.empty())) { |
yueee_yt | 0:7766f6712673 | 155 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 156 | printf("Using default handler\r\n"); |
yueee_yt | 0:7766f6712673 | 157 | #endif |
yueee_yt | 0:7766f6712673 | 158 | it = m_lpHandlers.end(); |
yueee_yt | 0:7766f6712673 | 159 | it--; //Get the last element |
yueee_yt | 0:7766f6712673 | 160 | if( ! (((*it).first.length() == 0) || !(*it).first.compare("/")) ) //This is not the default handler |
yueee_yt | 0:7766f6712673 | 161 | it = m_lpHandlers.end(); |
yueee_yt | 0:7766f6712673 | 162 | root_len = 0; |
yueee_yt | 0:7766f6712673 | 163 | } |
yueee_yt | 0:7766f6712673 | 164 | if(it == m_lpHandlers.end()) { |
yueee_yt | 0:7766f6712673 | 165 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 166 | printf("No handler found\r\n"); |
yueee_yt | 0:7766f6712673 | 167 | #endif |
yueee_yt | 0:7766f6712673 | 168 | return; |
yueee_yt | 0:7766f6712673 | 169 | } |
yueee_yt | 0:7766f6712673 | 170 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 171 | printf("Handler found.\r\n"); |
yueee_yt | 0:7766f6712673 | 172 | #endif |
yueee_yt | 0:7766f6712673 | 173 | HTTPRequestHandler* pHdlr = (*it).second((*it).first.c_str(), path.c_str() + root_len, client); |
yueee_yt | 0:7766f6712673 | 174 | */ |
yueee_yt | 0:7766f6712673 | 175 | HTTPRequestHandler* pHdlr; |
yueee_yt | 0:7766f6712673 | 176 | if (!path.compare(0,strlen(HELLO_PAGE),HELLO_PAGE)) { |
yueee_yt | 0:7766f6712673 | 177 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 178 | printf("HELLO PAGE CREATE. PATH %s: %s \r\n",HELLO_PAGE, path.c_str() + strlen(HELLO_PAGE)); |
yueee_yt | 0:7766f6712673 | 179 | #endif |
yueee_yt | 0:7766f6712673 | 180 | pHdlr = new SimpleHandler(HELLO_PAGE, path.c_str() + strlen(HELLO_PAGE), client); |
yueee_yt | 0:7766f6712673 | 181 | } |
yueee_yt | 0:7766f6712673 | 182 | |
yueee_yt | 0:7766f6712673 | 183 | #ifdef RPC_PAGE |
yueee_yt | 0:7766f6712673 | 184 | else if (!path.compare(0,strlen(RPC_PAGE),RPC_PAGE)) { |
yueee_yt | 0:7766f6712673 | 185 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 186 | printf("RPC PAGE CREATE. PATH %s: %s \r\n",RPC_PAGE, path.c_str() + strlen(RPC_PAGE)); |
yueee_yt | 0:7766f6712673 | 187 | #endif |
yueee_yt | 0:7766f6712673 | 188 | pHdlr = new RPCHandler(RPC_PAGE, path.c_str() + strlen(RPC_PAGE), client); |
yueee_yt | 0:7766f6712673 | 189 | } |
yueee_yt | 0:7766f6712673 | 190 | #endif |
yueee_yt | 0:7766f6712673 | 191 | |
yueee_yt | 0:7766f6712673 | 192 | #ifdef IEEE1888WSDL_PAGE |
yueee_yt | 0:7766f6712673 | 193 | else if (!path.compare(0,strlen(IEEE1888WSDL_PAGE),IEEE1888WSDL_PAGE)) { |
yueee_yt | 0:7766f6712673 | 194 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 195 | printf("IEEE1888WSDL_PAGE CREATE. PATH %s: %s \r\n",IEEE1888WSDL_PAGE, path.c_str() + strlen(IEEE1888WSDL_PAGE)); |
yueee_yt | 0:7766f6712673 | 196 | #endif |
yueee_yt | 0:7766f6712673 | 197 | pHdlr = new IEEE1888WSDLHandler(IEEE1888WSDL_PAGE, path.c_str() + strlen(IEEE1888WSDL_PAGE)-1, client); |
yueee_yt | 0:7766f6712673 | 198 | } |
yueee_yt | 0:7766f6712673 | 199 | #endif |
yueee_yt | 0:7766f6712673 | 200 | |
yueee_yt | 0:7766f6712673 | 201 | #ifdef IEEE1888_PAGE |
yueee_yt | 0:7766f6712673 | 202 | else if (!path.compare(0,strlen(IEEE1888_PAGE),IEEE1888_PAGE)) { |
yueee_yt | 0:7766f6712673 | 203 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 204 | printf("IEEE1888_PAGE CREATE. PATH %s: %s \r\n",IEEE1888_PAGE, path.c_str() + strlen(IEEE1888_PAGE)); |
yueee_yt | 0:7766f6712673 | 205 | #endif |
yueee_yt | 0:7766f6712673 | 206 | pHdlr = new IEEE1888Handler(IEEE1888_PAGE, path.c_str() + strlen(IEEE1888_PAGE)-1, client); |
yueee_yt | 0:7766f6712673 | 207 | } |
yueee_yt | 0:7766f6712673 | 208 | #endif |
yueee_yt | 0:7766f6712673 | 209 | |
yueee_yt | 0:7766f6712673 | 210 | #ifdef FS_PAGE |
yueee_yt | 0:7766f6712673 | 211 | else if (!path.compare(0,strlen(FS_PAGE),FS_PAGE)) { |
yueee_yt | 0:7766f6712673 | 212 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 213 | printf("FS_PAGE CREATE. PATH %s: %s \r\n",FS_PAGE, path.c_str() + strlen(FS_PAGE)); |
yueee_yt | 0:7766f6712673 | 214 | #endif |
yueee_yt | 0:7766f6712673 | 215 | pHdlr = new FSHandler(FS_PAGE, path.c_str() + strlen(FS_PAGE)-1, client); |
yueee_yt | 0:7766f6712673 | 216 | } |
yueee_yt | 0:7766f6712673 | 217 | #endif |
yueee_yt | 0:7766f6712673 | 218 | |
yueee_yt | 0:7766f6712673 | 219 | else { |
yueee_yt | 0:7766f6712673 | 220 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 221 | printf("No handler found\r\n"); |
yueee_yt | 0:7766f6712673 | 222 | #endif |
yueee_yt | 0:7766f6712673 | 223 | pHdlr = new SimpleHandler(HELLO_PAGE, path.c_str() + strlen(HELLO_PAGE), client); |
yueee_yt | 0:7766f6712673 | 224 | return; |
yueee_yt | 0:7766f6712673 | 225 | } |
yueee_yt | 0:7766f6712673 | 226 | //**** client = NULL; //We don't own it anymore |
yueee_yt | 0:7766f6712673 | 227 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 228 | printf("Handler Created.\r\n"); |
yueee_yt | 0:7766f6712673 | 229 | #endif |
yueee_yt | 0:7766f6712673 | 230 | switch(methCode) { |
yueee_yt | 0:7766f6712673 | 231 | case HTTP_GET: |
yueee_yt | 0:7766f6712673 | 232 | pHdlr->doGet(); |
yueee_yt | 0:7766f6712673 | 233 | break; |
yueee_yt | 0:7766f6712673 | 234 | case HTTP_POST: |
yueee_yt | 0:7766f6712673 | 235 | pHdlr->doPost(); |
yueee_yt | 0:7766f6712673 | 236 | break; |
yueee_yt | 0:7766f6712673 | 237 | case HTTP_HEAD: |
yueee_yt | 0:7766f6712673 | 238 | pHdlr->doHead(); |
yueee_yt | 0:7766f6712673 | 239 | break; |
yueee_yt | 0:7766f6712673 | 240 | } |
yueee_yt | 0:7766f6712673 | 241 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 242 | printf("Handler Delete.\r\n"); |
yueee_yt | 0:7766f6712673 | 243 | #endif |
yueee_yt | 0:7766f6712673 | 244 | delete pHdlr; |
yueee_yt | 0:7766f6712673 | 245 | // delete client; |
yueee_yt | 0:7766f6712673 | 246 | //delete pTCPSocketConnection; |
yueee_yt | 0:7766f6712673 | 247 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 248 | printf("(dispatcherRequest)return\r\n"); |
yueee_yt | 0:7766f6712673 | 249 | #endif |
yueee_yt | 0:7766f6712673 | 250 | return ; |
yueee_yt | 0:7766f6712673 | 251 | } |
yueee_yt | 0:7766f6712673 | 252 | |
yueee_yt | 0:7766f6712673 | 253 | void HTTPServerChild (void const *arg) |
yueee_yt | 0:7766f6712673 | 254 | { |
yueee_yt | 0:7766f6712673 | 255 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 256 | printf("HTTPServerChiled Start......\r\n"); |
yueee_yt | 0:7766f6712673 | 257 | #endif |
yueee_yt | 0:7766f6712673 | 258 | TCPSocketConnection* client = (TCPSocketConnection*)arg; |
yueee_yt | 0:7766f6712673 | 259 | |
yueee_yt | 0:7766f6712673 | 260 | for (;;) { |
yueee_yt | 0:7766f6712673 | 261 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 262 | printf("(HTTPServer.h<HTTPServerChild>)Connection from %s\r\n", client->get_address()); |
yueee_yt | 0:7766f6712673 | 263 | #endif |
yueee_yt | 0:7766f6712673 | 264 | dispatchRequest(client); |
yueee_yt | 0:7766f6712673 | 265 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 266 | printf("(HTTPServer.h<HTTPServerChild>)Client->Close %s\r\n", client->get_address()); |
yueee_yt | 0:7766f6712673 | 267 | #endif |
yueee_yt | 0:7766f6712673 | 268 | client->close(); |
yueee_yt | 0:7766f6712673 | 269 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 270 | // printf("(HTTPServer.h<HTTPServerChild>)Client->reset\r\n"); |
yueee_yt | 0:7766f6712673 | 271 | #endif |
yueee_yt | 0:7766f6712673 | 272 | // client->reset_address(); |
yueee_yt | 0:7766f6712673 | 273 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 274 | printf("(HTTPServer.h<HTTPServerChild>)Thread::signal_wait(1)\r\n"); |
yueee_yt | 0:7766f6712673 | 275 | #endif //delete client; |
yueee_yt | 0:7766f6712673 | 276 | Thread::signal_wait(1); |
yueee_yt | 0:7766f6712673 | 277 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 278 | printf("(HTTPServer.h<HTTPServerChild>)Return \r\n"); |
yueee_yt | 0:7766f6712673 | 279 | #endif |
yueee_yt | 0:7766f6712673 | 280 | } |
yueee_yt | 0:7766f6712673 | 281 | } |
yueee_yt | 0:7766f6712673 | 282 | |
yueee_yt | 0:7766f6712673 | 283 | void HTTPServerCloser (void const *arg) |
yueee_yt | 0:7766f6712673 | 284 | { |
yueee_yt | 0:7766f6712673 | 285 | TCPSocketConnection *client = (TCPSocketConnection*)arg; |
yueee_yt | 0:7766f6712673 | 286 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 287 | printf("HTTPCloser start%s\r\n", client->get_address()); |
yueee_yt | 0:7766f6712673 | 288 | #endif |
yueee_yt | 0:7766f6712673 | 289 | |
yueee_yt | 0:7766f6712673 | 290 | for (;;) { |
yueee_yt | 0:7766f6712673 | 291 | client->close(); |
yueee_yt | 0:7766f6712673 | 292 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 293 | printf("Close %s\r\n", client->get_address()); |
yueee_yt | 0:7766f6712673 | 294 | #endif |
yueee_yt | 0:7766f6712673 | 295 | Thread::signal_wait(1); |
yueee_yt | 0:7766f6712673 | 296 | } |
yueee_yt | 0:7766f6712673 | 297 | } |
yueee_yt | 0:7766f6712673 | 298 | |
yueee_yt | 0:7766f6712673 | 299 | void HTTPServerStart(int port = 80) |
yueee_yt | 0:7766f6712673 | 300 | { |
yueee_yt | 0:7766f6712673 | 301 | int i, t = 0; |
yueee_yt | 0:7766f6712673 | 302 | TCPSocketConnection clients[THREAD_MAX]; |
yueee_yt | 0:7766f6712673 | 303 | TCPSocketConnection xclient; |
yueee_yt | 0:7766f6712673 | 304 | |
yueee_yt | 0:7766f6712673 | 305 | for (i = 0; i < THREAD_MAX; i ++) { |
yueee_yt | 0:7766f6712673 | 306 | threads[i] = NULL; |
yueee_yt | 0:7766f6712673 | 307 | } |
yueee_yt | 0:7766f6712673 | 308 | xthread = NULL; |
yueee_yt | 0:7766f6712673 | 309 | |
yueee_yt | 0:7766f6712673 | 310 | TCPSocketServer server; |
yueee_yt | 0:7766f6712673 | 311 | server.bind(port); |
yueee_yt | 0:7766f6712673 | 312 | server.listen(); |
yueee_yt | 0:7766f6712673 | 313 | // server.set_blocking(false); |
yueee_yt | 0:7766f6712673 | 314 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 315 | printf("Wait for new connection...\r\n"); |
yueee_yt | 0:7766f6712673 | 316 | #endif |
yueee_yt | 0:7766f6712673 | 317 | for (;;) { |
yueee_yt | 0:7766f6712673 | 318 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 319 | printf("**Start Loop** \r\n"); |
yueee_yt | 0:7766f6712673 | 320 | #endif |
yueee_yt | 0:7766f6712673 | 321 | if (t >= 0) { |
yueee_yt | 0:7766f6712673 | 322 | if(server.accept(clients[t])==0) { |
yueee_yt | 0:7766f6712673 | 323 | // fork child process |
yueee_yt | 0:7766f6712673 | 324 | if (threads[t]) { |
yueee_yt | 0:7766f6712673 | 325 | threads[t]->signal_set(1); |
yueee_yt | 0:7766f6712673 | 326 | } else { |
yueee_yt | 0:7766f6712673 | 327 | threads[t] = new Thread(HTTPServerChild, (void*)&clients[t]); |
yueee_yt | 0:7766f6712673 | 328 | } |
yueee_yt | 0:7766f6712673 | 329 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 330 | printf("Forked %d\r\n", t); |
yueee_yt | 0:7766f6712673 | 331 | #endif |
yueee_yt | 0:7766f6712673 | 332 | } |
yueee_yt | 0:7766f6712673 | 333 | } else { |
yueee_yt | 0:7766f6712673 | 334 | if(server.accept(xclient)==0) { |
yueee_yt | 0:7766f6712673 | 335 | // closer process |
yueee_yt | 0:7766f6712673 | 336 | if (xthread) { |
yueee_yt | 0:7766f6712673 | 337 | xthread->signal_set(1); |
yueee_yt | 0:7766f6712673 | 338 | } else { |
yueee_yt | 0:7766f6712673 | 339 | xthread = new Thread(HTTPServerCloser, (void*)&xclient); |
yueee_yt | 0:7766f6712673 | 340 | } |
yueee_yt | 0:7766f6712673 | 341 | #ifdef _DEBUG_HTTP_SERVER_H |
yueee_yt | 0:7766f6712673 | 342 | printf("Connection full\r\n"); |
yueee_yt | 0:7766f6712673 | 343 | #endif |
yueee_yt | 0:7766f6712673 | 344 | } |
yueee_yt | 0:7766f6712673 | 345 | } |
yueee_yt | 0:7766f6712673 | 346 | |
yueee_yt | 0:7766f6712673 | 347 | t = -1; |
yueee_yt | 0:7766f6712673 | 348 | for (i = 0; i < THREAD_MAX; i ++) { |
yueee_yt | 0:7766f6712673 | 349 | if (threads[i] == NULL || threads[i]->get_state() == Thread::WaitingAnd) { |
yueee_yt | 0:7766f6712673 | 350 | if (t < 0) t = i; // next empty thread |
yueee_yt | 0:7766f6712673 | 351 | } |
yueee_yt | 0:7766f6712673 | 352 | } |
yueee_yt | 0:7766f6712673 | 353 | // Thread::wait(100); |
yueee_yt | 0:7766f6712673 | 354 | } |
yueee_yt | 0:7766f6712673 | 355 | } |
yueee_yt | 0:7766f6712673 | 356 | |
yueee_yt | 0:7766f6712673 | 357 | |
yueee_yt | 0:7766f6712673 | 358 | #endif |
yueee_yt | 0:7766f6712673 | 359 | |
yueee_yt | 0:7766f6712673 | 360 |