John Lowe
/
WebSockets2
Embedded WebSockets Experiment
HTTPServer/HTTPServer.cpp@0:6dee052a3fa4, 2011-07-26 (annotated)
- Committer:
- nandgate
- Date:
- Tue Jul 26 05:30:53 2011 +0000
- Revision:
- 0:6dee052a3fa4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nandgate | 0:6dee052a3fa4 | 1 | #include "HTTPServer.h" |
nandgate | 0:6dee052a3fa4 | 2 | #include "NetServer.h" |
nandgate | 0:6dee052a3fa4 | 3 | |
nandgate | 0:6dee052a3fa4 | 4 | using namespace std; |
nandgate | 0:6dee052a3fa4 | 5 | using namespace mbed; |
nandgate | 0:6dee052a3fa4 | 6 | |
nandgate | 0:6dee052a3fa4 | 7 | //unsigned int gconnections = 0; |
nandgate | 0:6dee052a3fa4 | 8 | |
nandgate | 0:6dee052a3fa4 | 9 | unsigned int mbed::hash(unsigned char *str) { |
nandgate | 0:6dee052a3fa4 | 10 | unsigned int hash = 5381; |
nandgate | 0:6dee052a3fa4 | 11 | int c; |
nandgate | 0:6dee052a3fa4 | 12 | while((c = *(str++))!=(unsigned char)'\0') { |
nandgate | 0:6dee052a3fa4 | 13 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ |
nandgate | 0:6dee052a3fa4 | 14 | } |
nandgate | 0:6dee052a3fa4 | 15 | return hash; |
nandgate | 0:6dee052a3fa4 | 16 | } |
nandgate | 0:6dee052a3fa4 | 17 | |
nandgate | 0:6dee052a3fa4 | 18 | HTTPConnection::HTTPConnection(HTTPServer *pparent, struct tcp_pcb *pcb) |
nandgate | 0:6dee052a3fa4 | 19 | : TCPConnection(pparent, pcb), request_incomplete(true), data(NULL), |
nandgate | 0:6dee052a3fa4 | 20 | request_handler(NULL), request_status(HTTP_NotFound), parent(pparent), |
nandgate | 0:6dee052a3fa4 | 21 | _request_url(NULL), _request_type(0), _request_headerfields(NULL), |
nandgate | 0:6dee052a3fa4 | 22 | _request_length(0), _request_arg_key(NULL), _request_arg_value(NULL), |
nandgate | 0:6dee052a3fa4 | 23 | _request_arg_state(0), emptypolls(0), webSocket(false) { |
nandgate | 0:6dee052a3fa4 | 24 | _timeout_max = pparent->timeout(); |
nandgate | 0:6dee052a3fa4 | 25 | } |
nandgate | 0:6dee052a3fa4 | 26 | |
nandgate | 0:6dee052a3fa4 | 27 | HTTPConnection::~HTTPConnection() { |
nandgate | 0:6dee052a3fa4 | 28 | deleteRequest(); |
nandgate | 0:6dee052a3fa4 | 29 | emptypolls = NetServer::time(); |
nandgate | 0:6dee052a3fa4 | 30 | } |
nandgate | 0:6dee052a3fa4 | 31 | |
nandgate | 0:6dee052a3fa4 | 32 | const char *HTTPConnection::getField(char *key) const { |
nandgate | 0:6dee052a3fa4 | 33 | unsigned int h = hash((unsigned char *)key); |
nandgate | 0:6dee052a3fa4 | 34 | return _request_fields.find(h)->second; |
nandgate | 0:6dee052a3fa4 | 35 | } |
nandgate | 0:6dee052a3fa4 | 36 | |
nandgate | 0:6dee052a3fa4 | 37 | void HTTPConnection::addField(char *key, char *value) { |
nandgate | 0:6dee052a3fa4 | 38 | unsigned int h = hash((unsigned char *)key); |
nandgate | 0:6dee052a3fa4 | 39 | if(parent->isField(h)) { |
nandgate | 0:6dee052a3fa4 | 40 | _request_fields.insert( make_pair(h, value)); |
nandgate | 0:6dee052a3fa4 | 41 | } |
nandgate | 0:6dee052a3fa4 | 42 | } |
nandgate | 0:6dee052a3fa4 | 43 | |
nandgate | 0:6dee052a3fa4 | 44 | void HTTPConnection::send() { |
nandgate | 0:6dee052a3fa4 | 45 | int i = sndbuf(); |
nandgate | 0:6dee052a3fa4 | 46 | |
nandgate | 0:6dee052a3fa4 | 47 | if(!request_incomplete&&i) { |
nandgate | 0:6dee052a3fa4 | 48 | switch(request_handler->send(this, i)) { |
nandgate | 0:6dee052a3fa4 | 49 | case HTTP_SuccessEnded: |
nandgate | 0:6dee052a3fa4 | 50 | case HTTP_Failed: { |
nandgate | 0:6dee052a3fa4 | 51 | deleteRequest(); |
nandgate | 0:6dee052a3fa4 | 52 | release_callbacks(); |
nandgate | 0:6dee052a3fa4 | 53 | NetServer::get()->free(this); |
nandgate | 0:6dee052a3fa4 | 54 | close(); |
nandgate | 0:6dee052a3fa4 | 55 | } break; |
nandgate | 0:6dee052a3fa4 | 56 | default: |
nandgate | 0:6dee052a3fa4 | 57 | emptypolls = NetServer::time(); |
nandgate | 0:6dee052a3fa4 | 58 | break; |
nandgate | 0:6dee052a3fa4 | 59 | } |
nandgate | 0:6dee052a3fa4 | 60 | } else { |
nandgate | 0:6dee052a3fa4 | 61 | if(NetServer::time() - emptypolls > _timeout_max) { |
nandgate | 0:6dee052a3fa4 | 62 | release_callbacks(); |
nandgate | 0:6dee052a3fa4 | 63 | NetServer::get()->free(this); |
nandgate | 0:6dee052a3fa4 | 64 | close(); |
nandgate | 0:6dee052a3fa4 | 65 | } |
nandgate | 0:6dee052a3fa4 | 66 | } |
nandgate | 0:6dee052a3fa4 | 67 | } |
nandgate | 0:6dee052a3fa4 | 68 | |
nandgate | 0:6dee052a3fa4 | 69 | void HTTPConnection::store(void *d, struct pbuf *p) { |
nandgate | 0:6dee052a3fa4 | 70 | int len = p->len-(((int)d)-((int)p->payload)); |
nandgate | 0:6dee052a3fa4 | 71 | do { |
nandgate | 0:6dee052a3fa4 | 72 | switch(request_handler->data(this, d, len)) { |
nandgate | 0:6dee052a3fa4 | 73 | case HTTP_SuccessEnded: |
nandgate | 0:6dee052a3fa4 | 74 | case HTTP_Failed: { |
nandgate | 0:6dee052a3fa4 | 75 | deleteRequest(); |
nandgate | 0:6dee052a3fa4 | 76 | release_callbacks(); |
nandgate | 0:6dee052a3fa4 | 77 | NetServer::get()->free(this); |
nandgate | 0:6dee052a3fa4 | 78 | close(); |
nandgate | 0:6dee052a3fa4 | 79 | } break; |
nandgate | 0:6dee052a3fa4 | 80 | default: |
nandgate | 0:6dee052a3fa4 | 81 | break; |
nandgate | 0:6dee052a3fa4 | 82 | } |
nandgate | 0:6dee052a3fa4 | 83 | p = p->next; |
nandgate | 0:6dee052a3fa4 | 84 | if(p) { |
nandgate | 0:6dee052a3fa4 | 85 | len = p->len; |
nandgate | 0:6dee052a3fa4 | 86 | d = static_cast<char *>(p->payload); |
nandgate | 0:6dee052a3fa4 | 87 | } |
nandgate | 0:6dee052a3fa4 | 88 | } while(_request_type&&p); |
nandgate | 0:6dee052a3fa4 | 89 | } |
nandgate | 0:6dee052a3fa4 | 90 | |
nandgate | 0:6dee052a3fa4 | 91 | void HTTPConnection::err(err_t err) { |
nandgate | 0:6dee052a3fa4 | 92 | release_callbacks(); |
nandgate | 0:6dee052a3fa4 | 93 | NetServer::get()->free(this); |
nandgate | 0:6dee052a3fa4 | 94 | } |
nandgate | 0:6dee052a3fa4 | 95 | |
nandgate | 0:6dee052a3fa4 | 96 | err_t HTTPConnection::poll() { |
nandgate | 0:6dee052a3fa4 | 97 | send(); |
nandgate | 0:6dee052a3fa4 | 98 | |
nandgate | 0:6dee052a3fa4 | 99 | return ERR_OK; |
nandgate | 0:6dee052a3fa4 | 100 | } |
nandgate | 0:6dee052a3fa4 | 101 | |
nandgate | 0:6dee052a3fa4 | 102 | err_t HTTPConnection::sent(u16_t len) { |
nandgate | 0:6dee052a3fa4 | 103 | return poll(); |
nandgate | 0:6dee052a3fa4 | 104 | } |
nandgate | 0:6dee052a3fa4 | 105 | |
nandgate | 0:6dee052a3fa4 | 106 | err_t HTTPConnection::recv(struct pbuf *q, err_t err) { |
nandgate | 0:6dee052a3fa4 | 107 | |
nandgate | 0:6dee052a3fa4 | 108 | if(webSocket) { // JDL |
nandgate | 0:6dee052a3fa4 | 109 | switch(request_handler->data(this, q->payload, q->len)) { |
nandgate | 0:6dee052a3fa4 | 110 | case HTTP_SuccessEnded: |
nandgate | 0:6dee052a3fa4 | 111 | case HTTP_Failed: { |
nandgate | 0:6dee052a3fa4 | 112 | deleteRequest(); |
nandgate | 0:6dee052a3fa4 | 113 | release_callbacks(); |
nandgate | 0:6dee052a3fa4 | 114 | NetServer::get()->free(this); |
nandgate | 0:6dee052a3fa4 | 115 | close(); |
nandgate | 0:6dee052a3fa4 | 116 | } break; |
nandgate | 0:6dee052a3fa4 | 117 | default: |
nandgate | 0:6dee052a3fa4 | 118 | break; |
nandgate | 0:6dee052a3fa4 | 119 | } |
nandgate | 0:6dee052a3fa4 | 120 | return ERR_OK; |
nandgate | 0:6dee052a3fa4 | 121 | } |
nandgate | 0:6dee052a3fa4 | 122 | |
nandgate | 0:6dee052a3fa4 | 123 | |
nandgate | 0:6dee052a3fa4 | 124 | struct pbuf *p = q; |
nandgate | 0:6dee052a3fa4 | 125 | int i; |
nandgate | 0:6dee052a3fa4 | 126 | char *data; |
nandgate | 0:6dee052a3fa4 | 127 | if(err == ERR_OK && p != NULL) { |
nandgate | 0:6dee052a3fa4 | 128 | /* Inform TCP that we have taken the data. */ |
nandgate | 0:6dee052a3fa4 | 129 | recved(p->tot_len); |
nandgate | 0:6dee052a3fa4 | 130 | data = static_cast<char *>(p->payload); |
nandgate | 0:6dee052a3fa4 | 131 | // :1 |
nandgate | 0:6dee052a3fa4 | 132 | // Looking if it's GET, POST, |
nandgate | 0:6dee052a3fa4 | 133 | // Followup from an incomplete request Header, |
nandgate | 0:6dee052a3fa4 | 134 | // POST data or just crap (DEL, HEAD ...). |
nandgate | 0:6dee052a3fa4 | 135 | if(!_request_type&&(strncmp(data, "GET ", 4) == 0)) { |
nandgate | 0:6dee052a3fa4 | 136 | _request_type = GET; // Need :2 |
nandgate | 0:6dee052a3fa4 | 137 | } else if(!_request_type&&(strncmp(data, "POST ", 5) == 0)) { |
nandgate | 0:6dee052a3fa4 | 138 | _request_type = POST; // Need :2 |
nandgate | 0:6dee052a3fa4 | 139 | } else if(_request_type&&request_incomplete) { |
nandgate | 0:6dee052a3fa4 | 140 | getFields(&p, &data); // Need :3 |
nandgate | 0:6dee052a3fa4 | 141 | } else if(_request_type == POST) { |
nandgate | 0:6dee052a3fa4 | 142 | // Followup (Data) // Exits |
nandgate | 0:6dee052a3fa4 | 143 | data = static_cast<char *>(p->payload); |
nandgate | 0:6dee052a3fa4 | 144 | store(data, p); |
nandgate | 0:6dee052a3fa4 | 145 | emptypolls = NetServer::time(); |
nandgate | 0:6dee052a3fa4 | 146 | pbuf_free(q); |
nandgate | 0:6dee052a3fa4 | 147 | data = NULL; |
nandgate | 0:6dee052a3fa4 | 148 | return ERR_OK; |
nandgate | 0:6dee052a3fa4 | 149 | } else { |
nandgate | 0:6dee052a3fa4 | 150 | pbuf_free(q); // Exits |
nandgate | 0:6dee052a3fa4 | 151 | data = NULL; |
nandgate | 0:6dee052a3fa4 | 152 | return ERR_OK; |
nandgate | 0:6dee052a3fa4 | 153 | } |
nandgate | 0:6dee052a3fa4 | 154 | |
nandgate | 0:6dee052a3fa4 | 155 | // :2 |
nandgate | 0:6dee052a3fa4 | 156 | // Processing first POST or GET Packet |
nandgate | 0:6dee052a3fa4 | 157 | // need :3 v--- If its 0 we have followup header data. |
nandgate | 0:6dee052a3fa4 | 158 | if(_request_type&&!_request_url) { |
nandgate | 0:6dee052a3fa4 | 159 | char *pagename = (char *)(data + _request_type); |
nandgate | 0:6dee052a3fa4 | 160 | for(i = _request_type; i < p->len; i++) { |
nandgate | 0:6dee052a3fa4 | 161 | if((data[i] == ' ') || (data[i] == '\r') || (data[i] == '\n')) { |
nandgate | 0:6dee052a3fa4 | 162 | data[i] = 0; |
nandgate | 0:6dee052a3fa4 | 163 | data = &data[i+1]; |
nandgate | 0:6dee052a3fa4 | 164 | break; |
nandgate | 0:6dee052a3fa4 | 165 | } |
nandgate | 0:6dee052a3fa4 | 166 | } |
nandgate | 0:6dee052a3fa4 | 167 | emptypolls = NetServer::time(); |
nandgate | 0:6dee052a3fa4 | 168 | |
nandgate | 0:6dee052a3fa4 | 169 | if((pagename[0] == '/') && (pagename[1] == 0)) { |
nandgate | 0:6dee052a3fa4 | 170 | pagename = "/index.htm"; |
nandgate | 0:6dee052a3fa4 | 171 | } |
nandgate | 0:6dee052a3fa4 | 172 | |
nandgate | 0:6dee052a3fa4 | 173 | i = strlen(pagename); |
nandgate | 0:6dee052a3fa4 | 174 | _request_url = new char[i+1]; |
nandgate | 0:6dee052a3fa4 | 175 | memcpy(_request_url, pagename, i); |
nandgate | 0:6dee052a3fa4 | 176 | _request_url[i] = '\0'; |
nandgate | 0:6dee052a3fa4 | 177 | getFields(&p, &data); |
nandgate | 0:6dee052a3fa4 | 178 | } |
nandgate | 0:6dee052a3fa4 | 179 | // :3 |
nandgate | 0:6dee052a3fa4 | 180 | // Send or store the first amoungh of data. |
nandgate | 0:6dee052a3fa4 | 181 | // Only when the message is complete. |
nandgate | 0:6dee052a3fa4 | 182 | if(!request_incomplete) { |
nandgate | 0:6dee052a3fa4 | 183 | emptypolls = NetServer::time(); |
nandgate | 0:6dee052a3fa4 | 184 | // Find the right handler |
nandgate | 0:6dee052a3fa4 | 185 | if(!request_handler) { |
nandgate | 0:6dee052a3fa4 | 186 | request_handler = parent->handle(this); |
nandgate | 0:6dee052a3fa4 | 187 | request_status = request_handler->init(this); |
nandgate | 0:6dee052a3fa4 | 188 | } |
nandgate | 0:6dee052a3fa4 | 189 | i = strlen(_request_headerfields) + 120; |
nandgate | 0:6dee052a3fa4 | 190 | char *buf = new char[i]; |
nandgate | 0:6dee052a3fa4 | 191 | sprintf(buf, "HTTP/1.1 %d OK\r\nServer: mbed embedded\r\nContent-Length: %d\r\n%s\r\n", |
nandgate | 0:6dee052a3fa4 | 192 | request_status, _request_length, getHeaderFields()); |
nandgate | 0:6dee052a3fa4 | 193 | i = strlen(buf); |
nandgate | 0:6dee052a3fa4 | 194 | if(sndbuf()>i) { |
nandgate | 0:6dee052a3fa4 | 195 | if(request_status==HTTP_NotFound) { |
nandgate | 0:6dee052a3fa4 | 196 | const char *msg = { |
nandgate | 0:6dee052a3fa4 | 197 | "HTTP/1.1 404 Not Found\r\nServer:mbed embedded\r\n" |
nandgate | 0:6dee052a3fa4 | 198 | "Content-Type: text/html\r\n" |
nandgate | 0:6dee052a3fa4 | 199 | "Content-Length: 163\r\n" |
nandgate | 0:6dee052a3fa4 | 200 | "\r\n" |
nandgate | 0:6dee052a3fa4 | 201 | "<html>\r\n" |
nandgate | 0:6dee052a3fa4 | 202 | "<header>\r\n" |
nandgate | 0:6dee052a3fa4 | 203 | "<title>File not found<title>\r\n" |
nandgate | 0:6dee052a3fa4 | 204 | "</header>\r\n" |
nandgate | 0:6dee052a3fa4 | 205 | "<body>\r\n" |
nandgate | 0:6dee052a3fa4 | 206 | "<h1>HTTP 404</h1>\r\n" |
nandgate | 0:6dee052a3fa4 | 207 | "<p>The file you requested was not found on this mbed. </p>\r\n" |
nandgate | 0:6dee052a3fa4 | 208 | "</body>\r\n" |
nandgate | 0:6dee052a3fa4 | 209 | "</html>\r\n" |
nandgate | 0:6dee052a3fa4 | 210 | }; |
nandgate | 0:6dee052a3fa4 | 211 | |
nandgate | 0:6dee052a3fa4 | 212 | write((void *)msg, strlen(msg), 0); |
nandgate | 0:6dee052a3fa4 | 213 | deleteRequest(); |
nandgate | 0:6dee052a3fa4 | 214 | } else { |
nandgate | 0:6dee052a3fa4 | 215 | write(buf, i, (TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE)); |
nandgate | 0:6dee052a3fa4 | 216 | if(request_status != HTTP_SwitchProtocols) { |
nandgate | 0:6dee052a3fa4 | 217 | if(_request_type == POST) { |
nandgate | 0:6dee052a3fa4 | 218 | store(data, p); |
nandgate | 0:6dee052a3fa4 | 219 | } else { |
nandgate | 0:6dee052a3fa4 | 220 | send(); |
nandgate | 0:6dee052a3fa4 | 221 | } |
nandgate | 0:6dee052a3fa4 | 222 | } else { |
nandgate | 0:6dee052a3fa4 | 223 | store(data, p); |
nandgate | 0:6dee052a3fa4 | 224 | send(); |
nandgate | 0:6dee052a3fa4 | 225 | webSocket= true; |
nandgate | 0:6dee052a3fa4 | 226 | } |
nandgate | 0:6dee052a3fa4 | 227 | } |
nandgate | 0:6dee052a3fa4 | 228 | } |
nandgate | 0:6dee052a3fa4 | 229 | delete buf; |
nandgate | 0:6dee052a3fa4 | 230 | } |
nandgate | 0:6dee052a3fa4 | 231 | |
nandgate | 0:6dee052a3fa4 | 232 | // Exits |
nandgate | 0:6dee052a3fa4 | 233 | pbuf_free(q); |
nandgate | 0:6dee052a3fa4 | 234 | data = NULL; |
nandgate | 0:6dee052a3fa4 | 235 | } else { |
nandgate | 0:6dee052a3fa4 | 236 | release_callbacks(); |
nandgate | 0:6dee052a3fa4 | 237 | NetServer::get()->free(this); |
nandgate | 0:6dee052a3fa4 | 238 | close(); |
nandgate | 0:6dee052a3fa4 | 239 | } |
nandgate | 0:6dee052a3fa4 | 240 | return ERR_OK; |
nandgate | 0:6dee052a3fa4 | 241 | } |
nandgate | 0:6dee052a3fa4 | 242 | |
nandgate | 0:6dee052a3fa4 | 243 | void HTTPConnection::getFields(struct pbuf **q, char **d) { |
nandgate | 0:6dee052a3fa4 | 244 | if(parent->fields.empty()) { |
nandgate | 0:6dee052a3fa4 | 245 | while((*q)&&request_incomplete) { |
nandgate | 0:6dee052a3fa4 | 246 | unsigned int end = ((unsigned int)((*q)->payload)+(unsigned int)((*q)->len)); |
nandgate | 0:6dee052a3fa4 | 247 | for(; request_incomplete && ((unsigned int)(*d) < end); (*d)++) { |
nandgate | 0:6dee052a3fa4 | 248 | if((*((char *)((*d)-0))=='\n')&&(*((char *)((*d)-1))=='\r')&& |
nandgate | 0:6dee052a3fa4 | 249 | (*((char *)((*d)-2))=='\n')&&(*((char *)((*d)-3))=='\r')) { |
nandgate | 0:6dee052a3fa4 | 250 | request_incomplete = false; |
nandgate | 0:6dee052a3fa4 | 251 | (*d) += 1; |
nandgate | 0:6dee052a3fa4 | 252 | break; |
nandgate | 0:6dee052a3fa4 | 253 | } |
nandgate | 0:6dee052a3fa4 | 254 | } |
nandgate | 0:6dee052a3fa4 | 255 | if(request_incomplete) { |
nandgate | 0:6dee052a3fa4 | 256 | (*q) = (*q)->next; |
nandgate | 0:6dee052a3fa4 | 257 | if((*q)) { |
nandgate | 0:6dee052a3fa4 | 258 | (*d) = static_cast<char *>((*q)->payload); |
nandgate | 0:6dee052a3fa4 | 259 | } |
nandgate | 0:6dee052a3fa4 | 260 | } |
nandgate | 0:6dee052a3fa4 | 261 | } |
nandgate | 0:6dee052a3fa4 | 262 | } else { |
nandgate | 0:6dee052a3fa4 | 263 | char *kb = *d, *ke = NULL, *vb = *d, *ve = NULL; |
nandgate | 0:6dee052a3fa4 | 264 | while((*q)&&request_incomplete) { |
nandgate | 0:6dee052a3fa4 | 265 | unsigned int end = ((unsigned int)((*q)->payload)+(unsigned int)((*q)->len)); |
nandgate | 0:6dee052a3fa4 | 266 | for(; request_incomplete && ((unsigned int)(*d) < end); (*d)++) { |
nandgate | 0:6dee052a3fa4 | 267 | switch(**d) { |
nandgate | 0:6dee052a3fa4 | 268 | case ' ': switch(_request_arg_state) { |
nandgate | 0:6dee052a3fa4 | 269 | case 1: case 2: _request_arg_state = 2; break; |
nandgate | 0:6dee052a3fa4 | 270 | case 3: _request_arg_state = 3; break; |
nandgate | 0:6dee052a3fa4 | 271 | default: _request_arg_state = 0; break; |
nandgate | 0:6dee052a3fa4 | 272 | } break; |
nandgate | 0:6dee052a3fa4 | 273 | case ':': switch(_request_arg_state) { |
nandgate | 0:6dee052a3fa4 | 274 | default: _request_arg_state = 2; break; |
nandgate | 0:6dee052a3fa4 | 275 | case 3: _request_arg_state = 3; break; |
nandgate | 0:6dee052a3fa4 | 276 | } break; |
nandgate | 0:6dee052a3fa4 | 277 | case '\r': switch(_request_arg_state) { |
nandgate | 0:6dee052a3fa4 | 278 | default: _request_arg_state = 4; break; |
nandgate | 0:6dee052a3fa4 | 279 | case 5: case 6: _request_arg_state = 6; break; |
nandgate | 0:6dee052a3fa4 | 280 | } break; |
nandgate | 0:6dee052a3fa4 | 281 | case '\n': switch(_request_arg_state) { |
nandgate | 0:6dee052a3fa4 | 282 | default: _request_arg_state = 4; break; |
nandgate | 0:6dee052a3fa4 | 283 | case 4: case 5: _request_arg_state = 5; break; |
nandgate | 0:6dee052a3fa4 | 284 | case 6: _request_arg_state = 7; break; |
nandgate | 0:6dee052a3fa4 | 285 | } break; |
nandgate | 0:6dee052a3fa4 | 286 | default: switch(_request_arg_state) { |
nandgate | 0:6dee052a3fa4 | 287 | default: _request_arg_state = 1; break; |
nandgate | 0:6dee052a3fa4 | 288 | case 2: case 3: _request_arg_state = 3; break; |
nandgate | 0:6dee052a3fa4 | 289 | } break; |
nandgate | 0:6dee052a3fa4 | 290 | } |
nandgate | 0:6dee052a3fa4 | 291 | switch(_request_arg_state) { |
nandgate | 0:6dee052a3fa4 | 292 | case 0: kb = (*d)+1; break; //PreKey |
nandgate | 0:6dee052a3fa4 | 293 | case 1: ke = (*d); break; //Key |
nandgate | 0:6dee052a3fa4 | 294 | case 2: vb = (*d)+1; break; //PreValue |
nandgate | 0:6dee052a3fa4 | 295 | case 3: ve = (*d); break; //Value |
nandgate | 0:6dee052a3fa4 | 296 | default: break; |
nandgate | 0:6dee052a3fa4 | 297 | case 7: request_incomplete = false; break; |
nandgate | 0:6dee052a3fa4 | 298 | case 5: { |
nandgate | 0:6dee052a3fa4 | 299 | int oldkey = (_request_arg_key)?strlen(_request_arg_key):0; |
nandgate | 0:6dee052a3fa4 | 300 | int oldval = (_request_arg_value)?strlen(_request_arg_value):0; |
nandgate | 0:6dee052a3fa4 | 301 | int keylen =(ke&&kb)?ke-kb+1:0; |
nandgate | 0:6dee052a3fa4 | 302 | int vallen = (ve&&vb)?ve-vb+1:0; |
nandgate | 0:6dee052a3fa4 | 303 | char *key = new char[oldkey+keylen]; |
nandgate | 0:6dee052a3fa4 | 304 | char *val = new char[oldval+vallen]; |
nandgate | 0:6dee052a3fa4 | 305 | if(_request_arg_key&&oldkey) { |
nandgate | 0:6dee052a3fa4 | 306 | strncpy(key, _request_arg_key, oldkey); |
nandgate | 0:6dee052a3fa4 | 307 | } |
nandgate | 0:6dee052a3fa4 | 308 | if(_request_arg_value&&oldval) { |
nandgate | 0:6dee052a3fa4 | 309 | strncpy(val, _request_arg_value, oldval); |
nandgate | 0:6dee052a3fa4 | 310 | } |
nandgate | 0:6dee052a3fa4 | 311 | if(kb&&keylen) { |
nandgate | 0:6dee052a3fa4 | 312 | strncpy(&key[oldkey], kb, keylen); |
nandgate | 0:6dee052a3fa4 | 313 | } |
nandgate | 0:6dee052a3fa4 | 314 | if(vb&&vallen) { |
nandgate | 0:6dee052a3fa4 | 315 | strncpy(&val[oldval], vb, vallen); |
nandgate | 0:6dee052a3fa4 | 316 | } |
nandgate | 0:6dee052a3fa4 | 317 | key[oldkey+keylen] = 0; |
nandgate | 0:6dee052a3fa4 | 318 | val[oldval+vallen] = 0; |
nandgate | 0:6dee052a3fa4 | 319 | // printf("'%s':='%s'\n", key, val); |
nandgate | 0:6dee052a3fa4 | 320 | addField(key, val); |
nandgate | 0:6dee052a3fa4 | 321 | kb = vb = (*d)+1; |
nandgate | 0:6dee052a3fa4 | 322 | ke = ve = NULL; |
nandgate | 0:6dee052a3fa4 | 323 | if(_request_arg_key) { |
nandgate | 0:6dee052a3fa4 | 324 | delete _request_arg_key; |
nandgate | 0:6dee052a3fa4 | 325 | _request_arg_key = NULL; |
nandgate | 0:6dee052a3fa4 | 326 | } |
nandgate | 0:6dee052a3fa4 | 327 | if(_request_arg_value) { |
nandgate | 0:6dee052a3fa4 | 328 | delete _request_arg_value; |
nandgate | 0:6dee052a3fa4 | 329 | _request_arg_value = NULL; |
nandgate | 0:6dee052a3fa4 | 330 | } |
nandgate | 0:6dee052a3fa4 | 331 | delete key; |
nandgate | 0:6dee052a3fa4 | 332 | } break; |
nandgate | 0:6dee052a3fa4 | 333 | } |
nandgate | 0:6dee052a3fa4 | 334 | } |
nandgate | 0:6dee052a3fa4 | 335 | } |
nandgate | 0:6dee052a3fa4 | 336 | switch(_request_arg_state) { |
nandgate | 0:6dee052a3fa4 | 337 | case 0: break; // PreKey |
nandgate | 0:6dee052a3fa4 | 338 | case 5: break; // n-rec |
nandgate | 0:6dee052a3fa4 | 339 | case 6: break; // 2r-rec |
nandgate | 0:6dee052a3fa4 | 340 | default: break; |
nandgate | 0:6dee052a3fa4 | 341 | case 1: case 2: { // Key // PreValue |
nandgate | 0:6dee052a3fa4 | 342 | int keylen =(kb)?(*d)-kb+1:0; |
nandgate | 0:6dee052a3fa4 | 343 | _request_arg_key = new char[keylen]; |
nandgate | 0:6dee052a3fa4 | 344 | strncpy(_request_arg_key, kb, keylen+1); |
nandgate | 0:6dee052a3fa4 | 345 | _request_arg_key[keylen] = 0; |
nandgate | 0:6dee052a3fa4 | 346 | } break; |
nandgate | 0:6dee052a3fa4 | 347 | case 3: case 4: { // Value // r-rec |
nandgate | 0:6dee052a3fa4 | 348 | int keylen =(ke&&kb)?ke-kb+1:0; |
nandgate | 0:6dee052a3fa4 | 349 | int vallen = (vb)?(*d)-vb+1:0; |
nandgate | 0:6dee052a3fa4 | 350 | _request_arg_key = new char[keylen]; |
nandgate | 0:6dee052a3fa4 | 351 | _request_arg_value = new char[vallen]; |
nandgate | 0:6dee052a3fa4 | 352 | strncpy(_request_arg_key, kb, keylen+1); |
nandgate | 0:6dee052a3fa4 | 353 | strncpy(_request_arg_value, vb, vallen+1); |
nandgate | 0:6dee052a3fa4 | 354 | _request_arg_key[keylen] = 0; |
nandgate | 0:6dee052a3fa4 | 355 | _request_arg_value[vallen] = 0; |
nandgate | 0:6dee052a3fa4 | 356 | } break; |
nandgate | 0:6dee052a3fa4 | 357 | } |
nandgate | 0:6dee052a3fa4 | 358 | if(request_incomplete) { |
nandgate | 0:6dee052a3fa4 | 359 | (*q) = (*q)->next; |
nandgate | 0:6dee052a3fa4 | 360 | if((*q)) { |
nandgate | 0:6dee052a3fa4 | 361 | (*d) = static_cast<char *>((*q)->payload); |
nandgate | 0:6dee052a3fa4 | 362 | } |
nandgate | 0:6dee052a3fa4 | 363 | } |
nandgate | 0:6dee052a3fa4 | 364 | } |
nandgate | 0:6dee052a3fa4 | 365 | } |
nandgate | 0:6dee052a3fa4 | 366 | |
nandgate | 0:6dee052a3fa4 | 367 | HTTPServer::HTTPServer(unsigned short port) |
nandgate | 0:6dee052a3fa4 | 368 | : TCPListener(port), _timeout_max(60000) { |
nandgate | 0:6dee052a3fa4 | 369 | } |
nandgate | 0:6dee052a3fa4 | 370 | |
nandgate | 0:6dee052a3fa4 | 371 | HTTPServer::HTTPServer(const char *hostname, struct ip_addr ip, struct ip_addr nm, struct ip_addr gw, struct ip_addr dns, unsigned short port) |
nandgate | 0:6dee052a3fa4 | 372 | : TCPListener(port), _timeout_max(60000) { |
nandgate | 0:6dee052a3fa4 | 373 | NetServer *net = NULL; |
nandgate | 0:6dee052a3fa4 | 374 | if(ip.addr != ip_addr_any.addr && nm.addr != ip_addr_any.addr && gw.addr != ip_addr_any.addr) { |
nandgate | 0:6dee052a3fa4 | 375 | net = NetServer::create(ip, nm, gw); |
nandgate | 0:6dee052a3fa4 | 376 | if(dns.addr != ip_addr_any.addr) { |
nandgate | 0:6dee052a3fa4 | 377 | net->setDNS1(dns); |
nandgate | 0:6dee052a3fa4 | 378 | } |
nandgate | 0:6dee052a3fa4 | 379 | } else if(hostname) { |
nandgate | 0:6dee052a3fa4 | 380 | net = NetServer::create(); |
nandgate | 0:6dee052a3fa4 | 381 | } |
nandgate | 0:6dee052a3fa4 | 382 | if(hostname) { |
nandgate | 0:6dee052a3fa4 | 383 | net->setHostname(hostname); |
nandgate | 0:6dee052a3fa4 | 384 | } |
nandgate | 0:6dee052a3fa4 | 385 | } |
nandgate | 0:6dee052a3fa4 | 386 | |
nandgate | 0:6dee052a3fa4 | 387 | void HTTPConnection::deleteRequest() { |
nandgate | 0:6dee052a3fa4 | 388 | for(map<unsigned int, char *>::iterator iter = _request_fields.begin(); |
nandgate | 0:6dee052a3fa4 | 389 | iter!=_request_fields.end();iter++) { |
nandgate | 0:6dee052a3fa4 | 390 | delete iter->second; |
nandgate | 0:6dee052a3fa4 | 391 | } |
nandgate | 0:6dee052a3fa4 | 392 | _request_fields.clear(); |
nandgate | 0:6dee052a3fa4 | 393 | if(data) {delete data; data = NULL; }; |
nandgate | 0:6dee052a3fa4 | 394 | |
nandgate | 0:6dee052a3fa4 | 395 | if(_request_type) { |
nandgate | 0:6dee052a3fa4 | 396 | delete _request_headerfields; |
nandgate | 0:6dee052a3fa4 | 397 | delete _request_arg_key; |
nandgate | 0:6dee052a3fa4 | 398 | delete _request_arg_value; |
nandgate | 0:6dee052a3fa4 | 399 | if(_request_url) { |
nandgate | 0:6dee052a3fa4 | 400 | delete _request_url; |
nandgate | 0:6dee052a3fa4 | 401 | _request_url = NULL; |
nandgate | 0:6dee052a3fa4 | 402 | } |
nandgate | 0:6dee052a3fa4 | 403 | } |
nandgate | 0:6dee052a3fa4 | 404 | _request_type = 0; |
nandgate | 0:6dee052a3fa4 | 405 | request_incomplete = true; |
nandgate | 0:6dee052a3fa4 | 406 | } |
nandgate | 0:6dee052a3fa4 | 407 |