ソースの整理中ですが、利用はできます。 大きなファイルはできないかもしれません。
Dependencies: EthernetInterface HttpServer TextLCD expatlib mbed-rpc mbed-rtos mbed Socket lwip-eth lwip-sys lwip
Fork of giken9_HTMLServer_Sample by
HttpServer/HTTPRequestHandler.cpp@2:6f25f8327180, 2014-03-12 (annotated)
- Committer:
- yueee_yt
- Date:
- Wed Mar 12 06:30:30 2014 +0000
- Revision:
- 2:6f25f8327180
- Parent:
- 0:7766f6712673
debug comment
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yueee_yt | 0:7766f6712673 | 1 | /* |
yueee_yt | 0:7766f6712673 | 2 | Permission is hereby granted, free of charge, to any person obtaining a copy |
yueee_yt | 0:7766f6712673 | 3 | of this software and associated documentation files (the "Software"), to deal |
yueee_yt | 0:7766f6712673 | 4 | in the Software without restriction, including without limitation the rights |
yueee_yt | 0:7766f6712673 | 5 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
yueee_yt | 0:7766f6712673 | 6 | copies of the Software, and to permit persons to whom the Software is |
yueee_yt | 0:7766f6712673 | 7 | furnished to do so, subject to the following conditions: |
yueee_yt | 0:7766f6712673 | 8 | |
yueee_yt | 0:7766f6712673 | 9 | The above copyright notice and this permission notice shall be included in |
yueee_yt | 0:7766f6712673 | 10 | all copies or substantial portions of the Software. |
yueee_yt | 0:7766f6712673 | 11 | |
yueee_yt | 0:7766f6712673 | 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
yueee_yt | 0:7766f6712673 | 13 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
yueee_yt | 0:7766f6712673 | 14 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
yueee_yt | 0:7766f6712673 | 15 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
yueee_yt | 0:7766f6712673 | 16 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
yueee_yt | 0:7766f6712673 | 17 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
yueee_yt | 0:7766f6712673 | 18 | THE SOFTWARE. |
yueee_yt | 0:7766f6712673 | 19 | */ |
yueee_yt | 0:7766f6712673 | 20 | //#define _DEBUG_REQUEST_HANDLER |
yueee_yt | 0:7766f6712673 | 21 | //#pragma O0 |
yueee_yt | 0:7766f6712673 | 22 | #include "HTTPRequestHandler.h" |
yueee_yt | 0:7766f6712673 | 23 | |
yueee_yt | 0:7766f6712673 | 24 | #include <string.h> |
yueee_yt | 0:7766f6712673 | 25 | |
yueee_yt | 0:7766f6712673 | 26 | //#define HTTP_REQUEST_TIMEOUT 5000 |
yueee_yt | 0:7766f6712673 | 27 | //#define HTTP_POST_REQUEST_TIMEOUT 2000 |
yueee_yt | 0:7766f6712673 | 28 | #define READ_SIZE 128 |
yueee_yt | 0:7766f6712673 | 29 | //HTTPRequestHandler::HTTPRequestHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : NetService(), |
yueee_yt | 0:7766f6712673 | 30 | // m_pTCPSocketConnection(pTCPSocketConnection), m_reqHeaders(), m_respHeaders(), |
yueee_yt | 0:7766f6712673 | 31 | // m_rootPath(rootPath), m_path(path), m_errc(200), |
yueee_yt | 0:7766f6712673 | 32 | // m_watchdog(), m_timeout(0),**/ m_closed(false), m_headersSent(false) //OK |
yueee_yt | 0:7766f6712673 | 33 | HTTPRequestHandler::HTTPRequestHandler(const char* rootPath, const char* path, TCPSocketConnection* pTCPSocketConnection) : |
yueee_yt | 0:7766f6712673 | 34 | m_pTCPSocketConnection(pTCPSocketConnection), /*m_reqHeaders(), m_respHeaders(),*/ |
yueee_yt | 0:7766f6712673 | 35 | m_rootPath(rootPath), m_path(path), m_errc(200), m_closed(false), m_headersSent(false) |
yueee_yt | 0:7766f6712673 | 36 | { |
yueee_yt | 0:7766f6712673 | 37 | #ifdef _DEBUG_REQUEST_HANDLER |
yueee_yt | 0:7766f6712673 | 38 | printf("+++(HTTPRequestHandler) init \r\n"); |
yueee_yt | 0:7766f6712673 | 39 | #endif |
yueee_yt | 0:7766f6712673 | 40 | req_headers_count=0; |
yueee_yt | 0:7766f6712673 | 41 | resp_headers_count=0; |
yueee_yt | 0:7766f6712673 | 42 | //Read & parse headers |
yueee_yt | 0:7766f6712673 | 43 | readHeaders(); |
yueee_yt | 0:7766f6712673 | 44 | //* m_pTCPSocket->setOnEvent(this, &HTTPRequestHandler::onTCPSocketEvent); |
yueee_yt | 0:7766f6712673 | 45 | //* setTimeout(HTTP_REQUEST_TIMEOUT); |
yueee_yt | 0:7766f6712673 | 46 | #ifdef _DEBUG_REQUEST_HANDLER |
yueee_yt | 0:7766f6712673 | 47 | printf("+++(HTTPRequestHandler) init end \r\n"); |
yueee_yt | 0:7766f6712673 | 48 | #endif |
yueee_yt | 0:7766f6712673 | 49 | } |
yueee_yt | 0:7766f6712673 | 50 | |
yueee_yt | 0:7766f6712673 | 51 | HTTPRequestHandler::~HTTPRequestHandler() |
yueee_yt | 0:7766f6712673 | 52 | { |
yueee_yt | 0:7766f6712673 | 53 | close(); |
yueee_yt | 0:7766f6712673 | 54 | #ifdef _DEBUG_REQUEST_HANDLER |
yueee_yt | 0:7766f6712673 | 55 | printf("+++(HTTPRequestHandler) Destroy end\r\n"); |
yueee_yt | 0:7766f6712673 | 56 | #endif |
yueee_yt | 0:7766f6712673 | 57 | } |
yueee_yt | 0:7766f6712673 | 58 | |
yueee_yt | 0:7766f6712673 | 59 | void HTTPRequestHandler::onTimeout() //Connection has timed out |
yueee_yt | 0:7766f6712673 | 60 | { |
yueee_yt | 0:7766f6712673 | 61 | close(); |
yueee_yt | 0:7766f6712673 | 62 | } |
yueee_yt | 0:7766f6712673 | 63 | |
yueee_yt | 0:7766f6712673 | 64 | void HTTPRequestHandler::close() //Close socket and destroy data |
yueee_yt | 0:7766f6712673 | 65 | { |
yueee_yt | 0:7766f6712673 | 66 | if(m_closed) |
yueee_yt | 0:7766f6712673 | 67 | return; |
yueee_yt | 0:7766f6712673 | 68 | m_closed = true; //Prevent recursive calling or calling on an object being destructed by someone else |
yueee_yt | 0:7766f6712673 | 69 | /** m_watchdog.detach(); **/ |
yueee_yt | 0:7766f6712673 | 70 | //* onClose(); |
yueee_yt | 0:7766f6712673 | 71 | //* m_pTCPSocket->resetOnEvent(); |
yueee_yt | 0:7766f6712673 | 72 | //*m_pTCPSocketConnection->close(); |
yueee_yt | 0:7766f6712673 | 73 | //* delete m_pTCPSocketConnection; //Can safely destroy socket |
yueee_yt | 0:7766f6712673 | 74 | //* NetService::close(); |
yueee_yt | 0:7766f6712673 | 75 | } |
yueee_yt | 0:7766f6712673 | 76 | |
yueee_yt | 0:7766f6712673 | 77 | //map<string, string>& HTTPRequestHandler::reqHeaders() //const |
yueee_yt | 0:7766f6712673 | 78 | //{ |
yueee_yt | 0:7766f6712673 | 79 | // return m_reqHeaders; |
yueee_yt | 0:7766f6712673 | 80 | //} |
yueee_yt | 0:7766f6712673 | 81 | void HTTPRequestHandler::reqHeaders(string *key,string *value,unsigned char *count) //const |
yueee_yt | 0:7766f6712673 | 82 | { |
yueee_yt | 0:7766f6712673 | 83 | for(int i=0; i<10; i++) { |
yueee_yt | 0:7766f6712673 | 84 | key[i]=req_headers_key[i]; |
yueee_yt | 0:7766f6712673 | 85 | value[i]=req_headers_value[i]; |
yueee_yt | 0:7766f6712673 | 86 | } |
yueee_yt | 0:7766f6712673 | 87 | *count=req_headers_count; |
yueee_yt | 0:7766f6712673 | 88 | // return m_reqHeaders; |
yueee_yt | 0:7766f6712673 | 89 | } |
yueee_yt | 0:7766f6712673 | 90 | |
yueee_yt | 0:7766f6712673 | 91 | string& HTTPRequestHandler::path() //const |
yueee_yt | 0:7766f6712673 | 92 | { |
yueee_yt | 0:7766f6712673 | 93 | return m_path; |
yueee_yt | 0:7766f6712673 | 94 | } |
yueee_yt | 0:7766f6712673 | 95 | |
yueee_yt | 0:7766f6712673 | 96 | int HTTPRequestHandler::dataLen() const |
yueee_yt | 0:7766f6712673 | 97 | { |
yueee_yt | 0:7766f6712673 | 98 | int ret=0; |
yueee_yt | 0:7766f6712673 | 99 | if (clength>0) ret=clength; |
yueee_yt | 0:7766f6712673 | 100 | // map<string,string>::iterator it; |
yueee_yt | 0:7766f6712673 | 101 | // it = m_reqHeaders.find("Content-Length"); |
yueee_yt | 0:7766f6712673 | 102 | // if( it == m_reqHeaders.end() ) { |
yueee_yt | 0:7766f6712673 | 103 | // return 0; |
yueee_yt | 0:7766f6712673 | 104 | // } |
yueee_yt | 0:7766f6712673 | 105 | // return atoi((*it).second.c_str()); //return 0 if parse fails, so that's fine |
yueee_yt | 0:7766f6712673 | 106 | return ret; |
yueee_yt | 0:7766f6712673 | 107 | } |
yueee_yt | 0:7766f6712673 | 108 | |
yueee_yt | 0:7766f6712673 | 109 | int HTTPRequestHandler::readData(char* buf, int len) |
yueee_yt | 0:7766f6712673 | 110 | { |
yueee_yt | 0:7766f6712673 | 111 | return m_pTCPSocketConnection->receive(buf, len); |
yueee_yt | 0:7766f6712673 | 112 | } |
yueee_yt | 0:7766f6712673 | 113 | |
yueee_yt | 0:7766f6712673 | 114 | string& HTTPRequestHandler::rootPath() //const |
yueee_yt | 0:7766f6712673 | 115 | { |
yueee_yt | 0:7766f6712673 | 116 | return m_rootPath; |
yueee_yt | 0:7766f6712673 | 117 | } |
yueee_yt | 0:7766f6712673 | 118 | |
yueee_yt | 0:7766f6712673 | 119 | void HTTPRequestHandler::setErrCode(int errc) |
yueee_yt | 0:7766f6712673 | 120 | { |
yueee_yt | 0:7766f6712673 | 121 | m_errc = errc; |
yueee_yt | 0:7766f6712673 | 122 | } |
yueee_yt | 0:7766f6712673 | 123 | |
yueee_yt | 0:7766f6712673 | 124 | void HTTPRequestHandler::setContentLen(int len) |
yueee_yt | 0:7766f6712673 | 125 | { |
yueee_yt | 0:7766f6712673 | 126 | char len_str[7] = {0}; |
yueee_yt | 0:7766f6712673 | 127 | //#ifdef _DEBUG_REQUEST_HANDLER |
yueee_yt | 0:7766f6712673 | 128 | printf( "+++(HTTPRequestHandler)Content-Length %d \r\n", len); |
yueee_yt | 0:7766f6712673 | 129 | //#endif |
yueee_yt | 0:7766f6712673 | 130 | sprintf(len_str, "%d", len); |
yueee_yt | 0:7766f6712673 | 131 | // respHeaders()["Content-Length"] = len_str; |
yueee_yt | 0:7766f6712673 | 132 | addRespHeaders("Content-Length",string(len_str)); |
yueee_yt | 0:7766f6712673 | 133 | } |
yueee_yt | 0:7766f6712673 | 134 | |
yueee_yt | 0:7766f6712673 | 135 | //map<string, string>& HTTPRequestHandler::respHeaders() |
yueee_yt | 0:7766f6712673 | 136 | //{ |
yueee_yt | 0:7766f6712673 | 137 | // return m_respHeaders; |
yueee_yt | 0:7766f6712673 | 138 | //} |
yueee_yt | 0:7766f6712673 | 139 | |
yueee_yt | 0:7766f6712673 | 140 | void HTTPRequestHandler::respHeaders(string *key,string *value,unsigned char *count ) |
yueee_yt | 0:7766f6712673 | 141 | { |
yueee_yt | 0:7766f6712673 | 142 | for(int i=0; i<10; i++) { |
yueee_yt | 0:7766f6712673 | 143 | key[i]=resp_headers_key[i]; |
yueee_yt | 0:7766f6712673 | 144 | value[i]=resp_headers_value[i]; |
yueee_yt | 0:7766f6712673 | 145 | } |
yueee_yt | 0:7766f6712673 | 146 | *count=resp_headers_count; |
yueee_yt | 0:7766f6712673 | 147 | } |
yueee_yt | 0:7766f6712673 | 148 | |
yueee_yt | 0:7766f6712673 | 149 | |
yueee_yt | 0:7766f6712673 | 150 | int HTTPRequestHandler::writeData(const char* buf, int len) |
yueee_yt | 0:7766f6712673 | 151 | { |
yueee_yt | 0:7766f6712673 | 152 | if(!m_headersSent) { |
yueee_yt | 0:7766f6712673 | 153 | m_headersSent = true; |
yueee_yt | 0:7766f6712673 | 154 | writeHeaders(); |
yueee_yt | 0:7766f6712673 | 155 | } |
yueee_yt | 0:7766f6712673 | 156 | return m_pTCPSocketConnection->send((char *)buf, len); |
yueee_yt | 0:7766f6712673 | 157 | } |
yueee_yt | 0:7766f6712673 | 158 | /** |
yueee_yt | 0:7766f6712673 | 159 | void HTTPRequestHandler::setTimeout(int ms) |
yueee_yt | 0:7766f6712673 | 160 | { |
yueee_yt | 0:7766f6712673 | 161 | m_timeout = 1000*ms; |
yueee_yt | 0:7766f6712673 | 162 | resetTimeout(); |
yueee_yt | 0:7766f6712673 | 163 | } |
yueee_yt | 0:7766f6712673 | 164 | **/ |
yueee_yt | 0:7766f6712673 | 165 | /** |
yueee_yt | 0:7766f6712673 | 166 | void HTTPRequestHandler::resetTimeout() |
yueee_yt | 0:7766f6712673 | 167 | { |
yueee_yt | 0:7766f6712673 | 168 | m_watchdog.detach(); |
yueee_yt | 0:7766f6712673 | 169 | m_watchdog.attach_us<HTTPRequestHandler>(this, &HTTPRequestHandler::onTimeout, m_timeout); |
yueee_yt | 0:7766f6712673 | 170 | } |
yueee_yt | 0:7766f6712673 | 171 | **/ |
yueee_yt | 0:7766f6712673 | 172 | |
yueee_yt | 0:7766f6712673 | 173 | void HTTPRequestHandler::readHeaders() |
yueee_yt | 0:7766f6712673 | 174 | { |
yueee_yt | 0:7766f6712673 | 175 | static char line[128]; |
yueee_yt | 0:7766f6712673 | 176 | static char key[128]; |
yueee_yt | 0:7766f6712673 | 177 | static char value[128]; |
yueee_yt | 0:7766f6712673 | 178 | int i; |
yueee_yt | 0:7766f6712673 | 179 | char *p; |
yueee_yt | 0:7766f6712673 | 180 | req_headers_count=0; |
yueee_yt | 0:7766f6712673 | 181 | resp_headers_count=0; |
yueee_yt | 0:7766f6712673 | 182 | while( readLine(line, 128) > 0) { //if == 0, it is an empty line = end of headers |
yueee_yt | 0:7766f6712673 | 183 | int n = sscanf(line, "%[^:]: %[^\n]", key, value); |
yueee_yt | 0:7766f6712673 | 184 | if ( n == 2 ) { |
yueee_yt | 0:7766f6712673 | 185 | #ifdef _DEBUG_REQUEST_HANDLER |
yueee_yt | 0:7766f6712673 | 186 | printf("+++(HTTPRequestHandler)Read header : %s : %s\r\n", key, value); |
yueee_yt | 0:7766f6712673 | 187 | #endif |
yueee_yt | 0:7766f6712673 | 188 | // m_reqHeaders[key] = value; |
yueee_yt | 0:7766f6712673 | 189 | if(req_headers_count<10) { |
yueee_yt | 0:7766f6712673 | 190 | req_headers_key[req_headers_count]=string(key); |
yueee_yt | 0:7766f6712673 | 191 | req_headers_value[req_headers_count]=string(value); |
yueee_yt | 0:7766f6712673 | 192 | req_headers_count++; |
yueee_yt | 0:7766f6712673 | 193 | } |
yueee_yt | 0:7766f6712673 | 194 | //Check Content Length |
yueee_yt | 0:7766f6712673 | 195 | for(p=&key[0]; p<(&key[0]+strlen(key)); p++) { |
yueee_yt | 0:7766f6712673 | 196 | if((*p>0x60)&&(*p<0x7b))*p=*p-0x20; |
yueee_yt | 0:7766f6712673 | 197 | } |
yueee_yt | 0:7766f6712673 | 198 | #ifdef _DEBUG_REQUEST_SERVER_H |
yueee_yt | 0:7766f6712673 | 199 | printf("+++(HTTPRequestHandler) HEADER %s\r\n",key); |
yueee_yt | 0:7766f6712673 | 200 | #endif |
yueee_yt | 0:7766f6712673 | 201 | if(strcmp(key,"CONTENT-LENGTH")==0) { |
yueee_yt | 0:7766f6712673 | 202 | sscanf(value,"%d",&clength); |
yueee_yt | 0:7766f6712673 | 203 | } |
yueee_yt | 0:7766f6712673 | 204 | } |
yueee_yt | 0:7766f6712673 | 205 | //TODO: Impl n==1 case (part 2 of previous header) |
yueee_yt | 0:7766f6712673 | 206 | } |
yueee_yt | 0:7766f6712673 | 207 | } |
yueee_yt | 0:7766f6712673 | 208 | |
yueee_yt | 0:7766f6712673 | 209 | void HTTPRequestHandler::readReqData() |
yueee_yt | 0:7766f6712673 | 210 | { |
yueee_yt | 0:7766f6712673 | 211 | static char key[128]; |
yueee_yt | 0:7766f6712673 | 212 | static char value[128]; |
yueee_yt | 0:7766f6712673 | 213 | int cont_length=0; |
yueee_yt | 0:7766f6712673 | 214 | bool chunk=false; |
yueee_yt | 0:7766f6712673 | 215 | char *p; |
yueee_yt | 0:7766f6712673 | 216 | int i; |
yueee_yt | 0:7766f6712673 | 217 | //ReadHeader |
yueee_yt | 0:7766f6712673 | 218 | // map< string, string >::iterator it; |
yueee_yt | 0:7766f6712673 | 219 | // for (it = m_reqHeaders.begin(); it != m_reqHeaders.end(); it++) { |
yueee_yt | 0:7766f6712673 | 220 | for (i=0; i<req_headers_count; i++) { |
yueee_yt | 0:7766f6712673 | 221 | // sprintf(key,"%s",(*it).first.c_str()); |
yueee_yt | 0:7766f6712673 | 222 | // sprintf(value,"%s",(*it).second.c_str()); |
yueee_yt | 0:7766f6712673 | 223 | sprintf(key,"%s",req_headers_key[i].c_str()); |
yueee_yt | 0:7766f6712673 | 224 | sprintf(value,"%s",req_headers_value[i].c_str()); |
yueee_yt | 0:7766f6712673 | 225 | for(p=&key[0]; p<(&key[0]+strlen(key)); p++) { |
yueee_yt | 0:7766f6712673 | 226 | if((*p>0x60)&&(*p<0x7b))*p=*p-0x20; |
yueee_yt | 0:7766f6712673 | 227 | } |
yueee_yt | 0:7766f6712673 | 228 | #ifdef _DEBUG_REQUEST_SERVER_H |
yueee_yt | 0:7766f6712673 | 229 | printf("+++(HTTPRequestHandler) HEADER %s\r\n",key); |
yueee_yt | 0:7766f6712673 | 230 | #endif |
yueee_yt | 0:7766f6712673 | 231 | if(strcmp(key,"CONTENT-LENGTH")==0) { |
yueee_yt | 0:7766f6712673 | 232 | sscanf(value,"%d",&cont_length); |
yueee_yt | 0:7766f6712673 | 233 | } else if(strcmp(key,"TRANSFER-ENCODING")==0) { |
yueee_yt | 0:7766f6712673 | 234 | for(p=&value[0]; p<(&value[0]+strlen(value)); p++) { |
yueee_yt | 0:7766f6712673 | 235 | if((*p>0x60)&&(*p<0x7b))*p=*p-0x20; |
yueee_yt | 0:7766f6712673 | 236 | } |
yueee_yt | 0:7766f6712673 | 237 | if(strcmp(value,"CHUNKED")==0)chunk=true; |
yueee_yt | 0:7766f6712673 | 238 | } |
yueee_yt | 0:7766f6712673 | 239 | } |
yueee_yt | 0:7766f6712673 | 240 | #ifdef _DEBUG_REQUEST_SERVER_H |
yueee_yt | 0:7766f6712673 | 241 | printf("+++(HTTPRequestHandler)content-length: %d \r\n",cont_length); |
yueee_yt | 0:7766f6712673 | 242 | if(chunk==true)printf("+++(HTTPRequestHandler)CHUNK Transfer \r\n"); |
yueee_yt | 0:7766f6712673 | 243 | else printf("+++(HTTPRequestHandler)NO CHUNK Transfer \r\n"); |
yueee_yt | 0:7766f6712673 | 244 | #endif |
yueee_yt | 0:7766f6712673 | 245 | // m_pTCPSocketConnection->set_blocking(true,HTTP_POST_REQUEST_TIMEOUT); |
yueee_yt | 0:7766f6712673 | 246 | char buffer[READ_SIZE+1]; |
yueee_yt | 0:7766f6712673 | 247 | if(chunk==false) { |
yueee_yt | 0:7766f6712673 | 248 | //no chunked mode |
yueee_yt | 0:7766f6712673 | 249 | for(;;) { |
yueee_yt | 0:7766f6712673 | 250 | if(cont_length>READ_SIZE) { |
yueee_yt | 0:7766f6712673 | 251 | i=m_pTCPSocketConnection->receive(buffer,READ_SIZE); |
yueee_yt | 0:7766f6712673 | 252 | buffer[READ_SIZE]=0x0; |
yueee_yt | 0:7766f6712673 | 253 | m_reqData.append(string(buffer)); |
yueee_yt | 0:7766f6712673 | 254 | cont_length-=READ_SIZE; |
yueee_yt | 0:7766f6712673 | 255 | } else { |
yueee_yt | 0:7766f6712673 | 256 | i=m_pTCPSocketConnection->receive(buffer, cont_length); |
yueee_yt | 0:7766f6712673 | 257 | buffer[cont_length]=0x0; |
yueee_yt | 0:7766f6712673 | 258 | m_reqData.append(string(buffer)); |
yueee_yt | 0:7766f6712673 | 259 | break; |
yueee_yt | 0:7766f6712673 | 260 | } |
yueee_yt | 0:7766f6712673 | 261 | } |
yueee_yt | 0:7766f6712673 | 262 | if(i>0)printf("Success \r\n"); |
yueee_yt | 0:7766f6712673 | 263 | } else { |
yueee_yt | 0:7766f6712673 | 264 | //chunked mode |
yueee_yt | 0:7766f6712673 | 265 | int chunk_size; |
yueee_yt | 0:7766f6712673 | 266 | |
yueee_yt | 0:7766f6712673 | 267 | for(;;) { |
yueee_yt | 0:7766f6712673 | 268 | i=readLine(buffer,20); |
yueee_yt | 0:7766f6712673 | 269 | printf("chunk :%s:\r\n",buffer); |
yueee_yt | 0:7766f6712673 | 270 | sscanf(buffer,"%x",&chunk_size); |
yueee_yt | 0:7766f6712673 | 271 | printf("chunk_size = %d \r\n",chunk_size); |
yueee_yt | 0:7766f6712673 | 272 | if(chunk_size==0)break; |
yueee_yt | 0:7766f6712673 | 273 | for(;;) { |
yueee_yt | 0:7766f6712673 | 274 | if(chunk_size>READ_SIZE) { |
yueee_yt | 0:7766f6712673 | 275 | i=m_pTCPSocketConnection->receive(buffer, READ_SIZE); |
yueee_yt | 0:7766f6712673 | 276 | buffer[READ_SIZE]=0x0; |
yueee_yt | 0:7766f6712673 | 277 | m_reqData.append(string(buffer)); |
yueee_yt | 0:7766f6712673 | 278 | chunk_size-=READ_SIZE; |
yueee_yt | 0:7766f6712673 | 279 | } else { |
yueee_yt | 0:7766f6712673 | 280 | i=m_pTCPSocketConnection->receive(buffer, chunk_size); |
yueee_yt | 0:7766f6712673 | 281 | buffer[chunk_size]=0x0; |
yueee_yt | 0:7766f6712673 | 282 | m_reqData.append(string(buffer)); |
yueee_yt | 0:7766f6712673 | 283 | break; |
yueee_yt | 0:7766f6712673 | 284 | } |
yueee_yt | 0:7766f6712673 | 285 | } |
yueee_yt | 0:7766f6712673 | 286 | i=m_pTCPSocketConnection->receive(buffer,2); //\r\n ro read |
yueee_yt | 0:7766f6712673 | 287 | } |
yueee_yt | 0:7766f6712673 | 288 | // i=readLine(buffer,20);//dummy |
yueee_yt | 0:7766f6712673 | 289 | } |
yueee_yt | 0:7766f6712673 | 290 | // m_pTCPSocketConnection->set_blocking(false); |
yueee_yt | 0:7766f6712673 | 291 | // while(m_pTCPSocketConnection->receive(buffer,1)>0){} |
yueee_yt | 0:7766f6712673 | 292 | // m_pTCPSocketConnection->set_blocking(false); |
yueee_yt | 0:7766f6712673 | 293 | // printf("%s \r\n",m_reqData.c_str()); |
yueee_yt | 0:7766f6712673 | 294 | } |
yueee_yt | 0:7766f6712673 | 295 | |
yueee_yt | 0:7766f6712673 | 296 | void HTTPRequestHandler::writeHeaders() //Called at the first writeData call |
yueee_yt | 0:7766f6712673 | 297 | { |
yueee_yt | 0:7766f6712673 | 298 | char line[128]= {0}; |
yueee_yt | 0:7766f6712673 | 299 | int i; |
yueee_yt | 0:7766f6712673 | 300 | //Response line |
yueee_yt | 0:7766f6712673 | 301 | //printf("Hdebug e1\r\n"); |
yueee_yt | 0:7766f6712673 | 302 | //m_pTCPSocketConnection->receive(line, 1); |
yueee_yt | 0:7766f6712673 | 303 | //printf("%d \r\n",line[0]); |
yueee_yt | 0:7766f6712673 | 304 | |
yueee_yt | 0:7766f6712673 | 305 | sprintf(line, "HTTP/1.1 %d OK\r\n", m_errc); //Not a violation of the standard not to include the descriptive text |
yueee_yt | 0:7766f6712673 | 306 | //printf("debug e2 %d:%s \r\n",strlen(line),line); |
yueee_yt | 0:7766f6712673 | 307 | //if(m_pTCPSocketConnection->is_connected()==true) { |
yueee_yt | 0:7766f6712673 | 308 | // printf("Connect \r\n"); |
yueee_yt | 0:7766f6712673 | 309 | //} else { |
yueee_yt | 0:7766f6712673 | 310 | // printf("disconnected \r\n"); |
yueee_yt | 0:7766f6712673 | 311 | //} |
yueee_yt | 0:7766f6712673 | 312 | //return; |
yueee_yt | 0:7766f6712673 | 313 | m_pTCPSocketConnection->send(line, strlen(line)); |
yueee_yt | 0:7766f6712673 | 314 | // map<string,string>::iterator it; |
yueee_yt | 0:7766f6712673 | 315 | //printf("%d debug e3 \r\n",resp_headers_count); |
yueee_yt | 0:7766f6712673 | 316 | for(i=0; i<resp_headers_count; i++) { |
yueee_yt | 0:7766f6712673 | 317 | // while( !m_respHeaders.empty() ) { |
yueee_yt | 0:7766f6712673 | 318 | // it = m_respHeaders.begin(); |
yueee_yt | 0:7766f6712673 | 319 | // sprintf(line, "%s: %s\r\n", (*it).first.c_str(), (*it).second.c_str() ); |
yueee_yt | 0:7766f6712673 | 320 | // printf("debug e\r\n"); |
yueee_yt | 0:7766f6712673 | 321 | sprintf(line, "%s: %s\r\n", resp_headers_key[i].c_str(),resp_headers_value[i].c_str()); |
yueee_yt | 0:7766f6712673 | 322 | // printf("debug e2 %s %d\r\n",line,strlen(line)); |
yueee_yt | 0:7766f6712673 | 323 | #ifdef _DEBUG_REQUEST_HANDLER |
yueee_yt | 0:7766f6712673 | 324 | printf("\r\n+++(HTTPRequestHandler)Write header %s \r\n", line); |
yueee_yt | 0:7766f6712673 | 325 | #endif |
yueee_yt | 0:7766f6712673 | 326 | m_pTCPSocketConnection->send(line, strlen(line)); |
yueee_yt | 0:7766f6712673 | 327 | // m_respHeaders.erase(it); |
yueee_yt | 0:7766f6712673 | 328 | } |
yueee_yt | 0:7766f6712673 | 329 | m_pTCPSocketConnection->send("\r\n",2); //End of head |
yueee_yt | 0:7766f6712673 | 330 | } |
yueee_yt | 0:7766f6712673 | 331 | |
yueee_yt | 0:7766f6712673 | 332 | void HTTPRequestHandler::addRespHeaders(string key,string value) |
yueee_yt | 0:7766f6712673 | 333 | { |
yueee_yt | 0:7766f6712673 | 334 | if(resp_headers_count<10) { |
yueee_yt | 0:7766f6712673 | 335 | resp_headers_key[resp_headers_count]=string(key); |
yueee_yt | 0:7766f6712673 | 336 | resp_headers_value[resp_headers_count]=string(value); |
yueee_yt | 0:7766f6712673 | 337 | resp_headers_count++; |
yueee_yt | 0:7766f6712673 | 338 | } else { |
yueee_yt | 0:7766f6712673 | 339 | printf("MAX over resp_headers_num \r\n"); |
yueee_yt | 0:7766f6712673 | 340 | } |
yueee_yt | 0:7766f6712673 | 341 | } |
yueee_yt | 0:7766f6712673 | 342 | |
yueee_yt | 0:7766f6712673 | 343 | int HTTPRequestHandler::readLine(char* str, int maxLen) |
yueee_yt | 0:7766f6712673 | 344 | { |
yueee_yt | 0:7766f6712673 | 345 | int ret; |
yueee_yt | 0:7766f6712673 | 346 | int len = 0; |
yueee_yt | 0:7766f6712673 | 347 | for(int i = 0; i < maxLen - 1; i++) { |
yueee_yt | 0:7766f6712673 | 348 | ret = m_pTCPSocketConnection->receive(str, 1); |
yueee_yt | 0:7766f6712673 | 349 | if(!ret) { |
yueee_yt | 0:7766f6712673 | 350 | break; |
yueee_yt | 0:7766f6712673 | 351 | } |
yueee_yt | 0:7766f6712673 | 352 | if( (len > 1) && *(str-1)=='\r' && *str=='\n' ) { |
yueee_yt | 0:7766f6712673 | 353 | str--; |
yueee_yt | 0:7766f6712673 | 354 | len-=2; |
yueee_yt | 0:7766f6712673 | 355 | break; |
yueee_yt | 0:7766f6712673 | 356 | } else if( *str=='\n' ) { |
yueee_yt | 0:7766f6712673 | 357 | len--; |
yueee_yt | 0:7766f6712673 | 358 | break; |
yueee_yt | 0:7766f6712673 | 359 | } |
yueee_yt | 0:7766f6712673 | 360 | str++; |
yueee_yt | 0:7766f6712673 | 361 | len++; |
yueee_yt | 0:7766f6712673 | 362 | } |
yueee_yt | 0:7766f6712673 | 363 | *str = 0; |
yueee_yt | 0:7766f6712673 | 364 | return len; |
yueee_yt | 0:7766f6712673 | 365 | } |
yueee_yt | 0:7766f6712673 | 366 | /** |
yueee_yt | 0:7766f6712673 | 367 | void HTTPRequestHandler::onTCPSocketEvent(TCPSocketEvent e) |
yueee_yt | 0:7766f6712673 | 368 | { |
yueee_yt | 0:7766f6712673 | 369 | //printf("\r\nEvent %d in HTTPRequestHandler\r\n", e); |
yueee_yt | 0:7766f6712673 | 370 | printf("\r\n+++(HTTPRequestHandler)Event in HTTPRequestHandler\r\n"); |
yueee_yt | 0:7766f6712673 | 371 | |
yueee_yt | 0:7766f6712673 | 372 | if(m_closed) |
yueee_yt | 0:7766f6712673 | 373 | { |
yueee_yt | 0:7766f6712673 | 374 | printf("\r\n+++(HTTPRequestHandler)WARN: Discarded\r\n"); |
yueee_yt | 0:7766f6712673 | 375 | return; |
yueee_yt | 0:7766f6712673 | 376 | } |
yueee_yt | 0:7766f6712673 | 377 | |
yueee_yt | 0:7766f6712673 | 378 | switch(e) |
yueee_yt | 0:7766f6712673 | 379 | { |
yueee_yt | 0:7766f6712673 | 380 | case TCPSOCKET_READABLE: |
yueee_yt | 0:7766f6712673 | 381 | resetTimeout(); |
yueee_yt | 0:7766f6712673 | 382 | onReadable(); |
yueee_yt | 0:7766f6712673 | 383 | break; |
yueee_yt | 0:7766f6712673 | 384 | case TCPSOCKET_WRITEABLE: |
yueee_yt | 0:7766f6712673 | 385 | resetTimeout(); |
yueee_yt | 0:7766f6712673 | 386 | onWriteable(); |
yueee_yt | 0:7766f6712673 | 387 | break; |
yueee_yt | 0:7766f6712673 | 388 | case TCPSOCKET_CONTIMEOUT: |
yueee_yt | 0:7766f6712673 | 389 | case TCPSOCKET_CONRST: |
yueee_yt | 0:7766f6712673 | 390 | case TCPSOCKET_CONABRT: |
yueee_yt | 0:7766f6712673 | 391 | case TCPSOCKET_ERROR: |
yueee_yt | 0:7766f6712673 | 392 | case TCPSOCKET_DISCONNECTED: |
yueee_yt | 0:7766f6712673 | 393 | DBG("\r\nConnection error in handler\r\n"); |
yueee_yt | 0:7766f6712673 | 394 | close(); |
yueee_yt | 0:7766f6712673 | 395 | break; |
yueee_yt | 0:7766f6712673 | 396 | } |
yueee_yt | 0:7766f6712673 | 397 | } |
yueee_yt | 0:7766f6712673 | 398 | **/ |
yueee_yt | 0:7766f6712673 | 399 | char* HTTPRequestHandler::getAddress(void) |
yueee_yt | 0:7766f6712673 | 400 | { |
yueee_yt | 0:7766f6712673 | 401 | return m_pTCPSocketConnection->get_address(); |
yueee_yt | 0:7766f6712673 | 402 | } |
yueee_yt | 0:7766f6712673 | 403 | |
yueee_yt | 0:7766f6712673 | 404 | string& HTTPRequestHandler::getRequestData(void) |
yueee_yt | 0:7766f6712673 | 405 | { |
yueee_yt | 0:7766f6712673 | 406 | return m_reqData; |
yueee_yt | 0:7766f6712673 | 407 | } |
yueee_yt | 0:7766f6712673 | 408 |