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