A simple web server that can be bound to either the EthernetInterface or the WiflyInterface.

Dependents:   Smart-WiFly-WebServer WattEye X10Svr SSDP_Server

Committer:
WiredHome
Date:
Mon Aug 12 11:26:59 2013 +0000
Revision:
13:8975d7928678
Parent:
12:109bf1558300
Child:
14:19c5f6151319
Added Base64 Encode/Decode for simple password management

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 0:729320f63c5c 1 //
WiredHome 2:a29c32190037 2 // @note Copyright © 2013 by Smartware Computing, all rights reserved.
WiredHome 0:729320f63c5c 3 // Individuals may use this application for evaluation or non-commercial
WiredHome 0:729320f63c5c 4 // purposes. Within this restriction, changes may be made to this application
WiredHome 0:729320f63c5c 5 // as long as this copyright notice is retained. The user shall make
WiredHome 0:729320f63c5c 6 // clear that their work is a derived work, and not the original.
WiredHome 0:729320f63c5c 7 // Users of this application and sources accept this application "as is" and
WiredHome 0:729320f63c5c 8 // shall hold harmless Smartware Computing, for any undesired results while
WiredHome 0:729320f63c5c 9 // using this application - whether real or imagined.
WiredHome 0:729320f63c5c 10 //
WiredHome 0:729320f63c5c 11 // author David Smart, Smartware Computing
WiredHome 0:729320f63c5c 12 //
WiredHome 0:729320f63c5c 13 #include "mbed.h"
WiredHome 0:729320f63c5c 14 #include "SW_HTTPServer.h"
WiredHome 0:729320f63c5c 15 #include "Utility.h"
WiredHome 0:729320f63c5c 16
WiredHome 12:109bf1558300 17 #define DEBUG
WiredHome 0:729320f63c5c 18
WiredHome 0:729320f63c5c 19 const char * DEFAULT_FILENAME = "index.htm";
WiredHome 0:729320f63c5c 20
WiredHome 8:262583f054f6 21 // Header information to always send (must be \r\n terminated)
WiredHome 8:262583f054f6 22 const char hdr_httpver[] = "HTTP/1.0"; // typically HTTP/1.0 or HTTP/1.1
WiredHome 8:262583f054f6 23 const char hdr_age[] = "Max-age: 0\r\n"; // expires right away
WiredHome 8:262583f054f6 24 const char hdr_server[] = "Server: Smart_Server v0.1\r\n"; // Server
WiredHome 8:262583f054f6 25 const char hdr_dnt[] = "DNT: 1\r\n"; // Do Not Track
WiredHome 8:262583f054f6 26 const char hdr_close[] = "Connection: close\r\n"; // tell the client to close the connection
WiredHome 8:262583f054f6 27 const char nl[] = "\r\n"; // final \r\n for the termination of the header
WiredHome 0:729320f63c5c 28
WiredHome 0:729320f63c5c 29 static const struct {
WiredHome 0:729320f63c5c 30 char *ext;
WiredHome 0:729320f63c5c 31 char *filetype;
WiredHome 0:729320f63c5c 32 } extensions [] = {
WiredHome 3:17928786bdb5 33 {".gif", "Content-Type: image/gif\r\n" },
WiredHome 3:17928786bdb5 34 {".jpg", "Content-Type: image/jpeg\r\n" },
WiredHome 3:17928786bdb5 35 {".jpeg","Content-Type: image/jpeg\r\n" },
WiredHome 3:17928786bdb5 36 {".ico", "Content-Type: image/x-icon\r\n" },
WiredHome 3:17928786bdb5 37 {".png", "Content-Type: image/png\r\n" },
WiredHome 3:17928786bdb5 38 {".zip", "Content-Type: image/zip\r\n" },
WiredHome 3:17928786bdb5 39 {".gz", "Content-Type: image/gz\r\n" },
WiredHome 3:17928786bdb5 40 {".tar", "Content-Type: image/tar\r\n" },
WiredHome 3:17928786bdb5 41 {".txt", "Content-Type: plain/text\r\n" },
WiredHome 3:17928786bdb5 42 {".pdf", "Content-Type: application/pdf\r\n" },
WiredHome 3:17928786bdb5 43 {".htm", "Content-Type: text/html\r\n" },
WiredHome 3:17928786bdb5 44 {".html","Content-Type: text/html\r\n" },
WiredHome 0:729320f63c5c 45 {0,0}
WiredHome 0:729320f63c5c 46 };
WiredHome 0:729320f63c5c 47
WiredHome 8:262583f054f6 48 #ifdef DEBUG
WiredHome 12:109bf1558300 49 // This uses standard library dynamic memory management, but for an
WiredHome 9:2ea342765c9d 50 // embedded system there are alternates that may make better sense -
WiredHome 9:2ea342765c9d 51 // search the web for embedded system malloc alternates.
WiredHome 8:262583f054f6 52 static void * MyMalloc(int x, int y)
WiredHome 8:262583f054f6 53 {
WiredHome 8:262583f054f6 54 std::printf("[%04d] malloc(%d)\r\n", y, x);
WiredHome 8:262583f054f6 55 return malloc(x);
WiredHome 8:262583f054f6 56 }
WiredHome 11:17d84c41a7b3 57 static char toP(void * x)
WiredHome 11:17d84c41a7b3 58 {
WiredHome 11:17d84c41a7b3 59 char * c = (char *) x;
WiredHome 11:17d84c41a7b3 60 if (*c >= ' ' && *c < 0x7F)
WiredHome 11:17d84c41a7b3 61 return *c;
WiredHome 11:17d84c41a7b3 62 else
WiredHome 11:17d84c41a7b3 63 return '.';
WiredHome 11:17d84c41a7b3 64 }
WiredHome 8:262583f054f6 65 #define mymalloc(x) MyMalloc(x, __LINE__)
WiredHome 8:262583f054f6 66 #define myfree(x) \
WiredHome 11:17d84c41a7b3 67 pc->printf("[%4d] free(%02x %02x %02x %02x %02x ... %c%c%c%c%c)\r\n", __LINE__, \
WiredHome 11:17d84c41a7b3 68 *x, *(x+1), *(x+2), *(x+3), *(x+4), \
WiredHome 11:17d84c41a7b3 69 toP(x), toP(x+1), toP(x+2), toP(x+3), toP(x+4) ); \
WiredHome 8:262583f054f6 70 free(x);
WiredHome 8:262583f054f6 71 #else
WiredHome 8:262583f054f6 72 #define mymalloc(x) malloc(x)
WiredHome 8:262583f054f6 73 #define myfree(x) free(x)
WiredHome 8:262583f054f6 74 #endif
WiredHome 8:262583f054f6 75
WiredHome 3:17928786bdb5 76 HTTPServer::HTTPServer(
WiredHome 7:99ad7a67f05e 77 Wifly * _wf,
WiredHome 7:99ad7a67f05e 78 int port,
WiredHome 7:99ad7a67f05e 79 const char * _webroot,
WiredHome 13:8975d7928678 80 int maxheaderParams,
WiredHome 13:8975d7928678 81 int _maxqueryParams,
WiredHome 7:99ad7a67f05e 82 int _maxdynamicpages,
WiredHome 7:99ad7a67f05e 83 PC * _pc,
WiredHome 7:99ad7a67f05e 84 int _allocforheader,
WiredHome 3:17928786bdb5 85 int _allocforfile)
WiredHome 0:729320f63c5c 86 {
WiredHome 0:729320f63c5c 87 wifly = _wf;
WiredHome 0:729320f63c5c 88 webroot = (char *)malloc(strlen(_webroot)+1);
WiredHome 0:729320f63c5c 89 strcpy(webroot, _webroot);
WiredHome 13:8975d7928678 90 maxqueryParams = _maxqueryParams;
WiredHome 0:729320f63c5c 91 maxdynamicpages = _maxdynamicpages;
WiredHome 13:8975d7928678 92 headerParams = (namevalue *)malloc(maxheaderParams * sizeof(namevalue));
WiredHome 13:8975d7928678 93 queryParams = (namevalue *)malloc(maxqueryParams * sizeof(namevalue));
WiredHome 0:729320f63c5c 94 handlers = (handler *)malloc(maxdynamicpages * sizeof(handler));
WiredHome 3:17928786bdb5 95 headerbuffersize = _allocforheader;
WiredHome 3:17928786bdb5 96 headerbuffer = (char *)malloc(headerbuffersize);
WiredHome 0:729320f63c5c 97 pc = _pc;
WiredHome 3:17928786bdb5 98 queryType = NULL;
WiredHome 3:17928786bdb5 99 queryString = NULL;
WiredHome 13:8975d7928678 100 // hostString = NULL;
WiredHome 13:8975d7928678 101 // contentLength = NULL;
WiredHome 13:8975d7928678 102 // contentType = NULL;
WiredHome 13:8975d7928678 103 // authorization = NULL;
WiredHome 3:17928786bdb5 104 postQueryString = NULL;
WiredHome 13:8975d7928678 105 queryParamCount = 0;
WiredHome 0:729320f63c5c 106 handlercount = 0;
WiredHome 3:17928786bdb5 107 maxheaderbytes = 0;
WiredHome 0:729320f63c5c 108 server = new TCPSocketServer();
WiredHome 0:729320f63c5c 109 server->bind(port);
WiredHome 0:729320f63c5c 110 server->listen();
WiredHome 8:262583f054f6 111 // server->set_blocking(false, 0);
WiredHome 8:262583f054f6 112 // client.set_blocking(false, 0);
WiredHome 8:262583f054f6 113 // server->accept(client);
WiredHome 3:17928786bdb5 114 ResetPerformanceData();
WiredHome 10:9c8d2c6a3469 115 PerformanceTimer.start();
WiredHome 0:729320f63c5c 116 }
WiredHome 0:729320f63c5c 117
WiredHome 0:729320f63c5c 118 HTTPServer::~HTTPServer()
WiredHome 0:729320f63c5c 119 {
WiredHome 8:262583f054f6 120 int i;
WiredHome 8:262583f054f6 121
WiredHome 8:262583f054f6 122 for (i=0; i<handlercount; i++)
WiredHome 8:262583f054f6 123 myfree(handlers[i].path);
WiredHome 8:262583f054f6 124 myfree(headerbuffer);
WiredHome 8:262583f054f6 125 myfree(handlers);
WiredHome 13:8975d7928678 126 myfree(queryParams);
WiredHome 8:262583f054f6 127 myfree(webroot);
WiredHome 0:729320f63c5c 128 webroot = NULL;
WiredHome 0:729320f63c5c 129 }
WiredHome 0:729320f63c5c 130
WiredHome 3:17928786bdb5 131 int HTTPServer::GetMaxHeaderSize()
WiredHome 3:17928786bdb5 132 {
WiredHome 3:17928786bdb5 133 return maxheaderbytes;
WiredHome 3:17928786bdb5 134 }
WiredHome 3:17928786bdb5 135
WiredHome 0:729320f63c5c 136 bool HTTPServer::RegisterHandler(const char * path, Handler callback)
WiredHome 0:729320f63c5c 137 {
WiredHome 0:729320f63c5c 138 if (handlercount < maxdynamicpages && path && callback) {
WiredHome 8:262583f054f6 139 handlers[handlercount].path = (char *)mymalloc(strlen(path)+1);
WiredHome 0:729320f63c5c 140 memcpy(handlers[handlercount].path, path, strlen(path)+1);
WiredHome 0:729320f63c5c 141 handlers[handlercount].callback = callback;
WiredHome 0:729320f63c5c 142 handlercount++;
WiredHome 0:729320f63c5c 143 return true;
WiredHome 0:729320f63c5c 144 } else {
WiredHome 0:729320f63c5c 145 return false;
WiredHome 0:729320f63c5c 146 }
WiredHome 0:729320f63c5c 147 }
WiredHome 0:729320f63c5c 148
WiredHome 2:a29c32190037 149 // Poll()
WiredHome 0:729320f63c5c 150 //
WiredHome 0:729320f63c5c 151 // *OPEN*GET /x=1 HTTP/1.1
WiredHome 0:729320f63c5c 152 // Host: 192.168.1.140
WiredHome 0:729320f63c5c 153 // Connection: keep-alive
WiredHome 0:729320f63c5c 154 // Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
WiredHome 0:729320f63c5c 155 // User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36
WiredHome 0:729320f63c5c 156 // Accept-Encoding: gzip,deflate,sdch
WiredHome 0:729320f63c5c 157 // Accept-Language: en-US,en;q=0.8
WiredHome 0:729320f63c5c 158 //
WiredHome 2:a29c32190037 159 void HTTPServer::Poll()
WiredHome 0:729320f63c5c 160 {
WiredHome 3:17928786bdb5 161 typedef enum {
WiredHome 3:17928786bdb5 162 Idle, // waiting for a connection
WiredHome 3:17928786bdb5 163 Receiving, // receiving data
WiredHome 3:17928786bdb5 164 Sending, // send the response
WiredHome 7:99ad7a67f05e 165 WaitingToClose, // small timeout to close
WiredHome 7:99ad7a67f05e 166 Reset
WiredHome 3:17928786bdb5 167 } state;
WiredHome 0:729320f63c5c 168 static state op = Idle;
WiredHome 3:17928786bdb5 169 static char * bPtr = headerbuffer;
WiredHome 0:729320f63c5c 170 int n;
WiredHome 10:9c8d2c6a3469 171 static int t_ref; // reference point for the PerformanceTimer
WiredHome 0:729320f63c5c 172
WiredHome 8:262583f054f6 173 #ifdef DEBUG
WiredHome 8:262583f054f6 174 static state lastOp = Reset;
WiredHome 7:99ad7a67f05e 175 if (lastOp != op) {
WiredHome 9:2ea342765c9d 176 const char *states[] = {"Idle", "Receiving", "Sending", "WaitingToClose", "Reset"};
WiredHome 8:262583f054f6 177 pc->printf("Poll: %s\r\n", states[op]);
WiredHome 7:99ad7a67f05e 178 lastOp = op;
WiredHome 7:99ad7a67f05e 179 }
WiredHome 8:262583f054f6 180 #endif
WiredHome 0:729320f63c5c 181 switch(op) {
WiredHome 3:17928786bdb5 182 default: // not expected to arrive here
WiredHome 3:17928786bdb5 183 op = Idle;
WiredHome 3:17928786bdb5 184 break;
WiredHome 8:262583f054f6 185
WiredHome 3:17928786bdb5 186 case Idle:
WiredHome 10:9c8d2c6a3469 187 PerformanceTimer.reset();
WiredHome 3:17928786bdb5 188 bPtr = headerbuffer;
WiredHome 11:17d84c41a7b3 189 if (0 == server->accept(client)) {
WiredHome 3:17928786bdb5 190 op = Receiving;
WiredHome 10:9c8d2c6a3469 191 t_ref = PerformanceTimer.read_us();
WiredHome 8:262583f054f6 192 #ifdef DEBUG
WiredHome 8:262583f054f6 193 pc->printf("Accept at %d\r\n", t_ref);
WiredHome 8:262583f054f6 194 #endif
WiredHome 3:17928786bdb5 195 }
WiredHome 0:729320f63c5c 196 break;
WiredHome 8:262583f054f6 197
WiredHome 3:17928786bdb5 198 case Receiving:
WiredHome 3:17928786bdb5 199 n = client.receive(bPtr, headerbuffersize - (bPtr - headerbuffer));
WiredHome 3:17928786bdb5 200 if (n < 0) {
WiredHome 9:2ea342765c9d 201 #ifdef DEBUG
WiredHome 8:262583f054f6 202 pc->printf("*** client.receive() => %d\r\n", n);
WiredHome 9:2ea342765c9d 203 #endif
WiredHome 3:17928786bdb5 204 } else if (n) {
WiredHome 3:17928786bdb5 205 bPtr[n] = '\0';
WiredHome 3:17928786bdb5 206 if (ParseHeader(headerbuffer)) {
WiredHome 3:17928786bdb5 207 op = Sending;
WiredHome 3:17928786bdb5 208 t_ref = RecordPerformanceData(&perfData.Header, t_ref);
WiredHome 3:17928786bdb5 209 }
WiredHome 3:17928786bdb5 210 bPtr += n;
WiredHome 0:729320f63c5c 211 }
WiredHome 0:729320f63c5c 212 break;
WiredHome 8:262583f054f6 213
WiredHome 0:729320f63c5c 214 case Sending:
WiredHome 3:17928786bdb5 215 SendResponse();
WiredHome 0:729320f63c5c 216 op = WaitingToClose;
WiredHome 3:17928786bdb5 217 RecordPerformanceData(&perfData.SendData, t_ref);
WiredHome 0:729320f63c5c 218 break;
WiredHome 9:2ea342765c9d 219
WiredHome 0:729320f63c5c 220 case WaitingToClose:
WiredHome 3:17928786bdb5 221 close_connection();
WiredHome 0:729320f63c5c 222 op = Idle;
WiredHome 0:729320f63c5c 223 break;
WiredHome 0:729320f63c5c 224 }
WiredHome 0:729320f63c5c 225 }
WiredHome 0:729320f63c5c 226
WiredHome 0:729320f63c5c 227
WiredHome 0:729320f63c5c 228 const char * HTTPServer::GetSupportedType(const char * filename)
WiredHome 0:729320f63c5c 229 {
WiredHome 0:729320f63c5c 230 int i;
WiredHome 0:729320f63c5c 231 int buflen = strlen(filename);
WiredHome 0:729320f63c5c 232 int extlen;
WiredHome 0:729320f63c5c 233
WiredHome 0:729320f63c5c 234 for (i=0; extensions[i].ext != 0; i++) {
WiredHome 0:729320f63c5c 235 extlen = strlen(extensions[i].ext);
WiredHome 0:729320f63c5c 236 if ( !strncmp(&filename[buflen-extlen], extensions[i].ext, extlen)) {
WiredHome 0:729320f63c5c 237 return extensions[i].filetype;
WiredHome 0:729320f63c5c 238 }
WiredHome 0:729320f63c5c 239 }
WiredHome 0:729320f63c5c 240 return NULL;
WiredHome 0:729320f63c5c 241 }
WiredHome 0:729320f63c5c 242
WiredHome 3:17928786bdb5 243
WiredHome 0:729320f63c5c 244 void HTTPServer::send(const char * msg, int bytes)
WiredHome 0:729320f63c5c 245 {
WiredHome 0:729320f63c5c 246 if (bytes == -1)
WiredHome 0:729320f63c5c 247 bytes = strlen(msg);
WiredHome 0:729320f63c5c 248 wifly->send(msg, bytes);
WiredHome 0:729320f63c5c 249 }
WiredHome 0:729320f63c5c 250
WiredHome 3:17928786bdb5 251
WiredHome 0:729320f63c5c 252 bool HTTPServer::SendFile(const char * filename, const char * filetype)
WiredHome 0:729320f63c5c 253 {
WiredHome 0:729320f63c5c 254 FILE * fp;
WiredHome 3:17928786bdb5 255
WiredHome 0:729320f63c5c 256 fp = fopen(filename,"rb");
WiredHome 0:729320f63c5c 257 if (fp) { // can open it
WiredHome 8:262583f054f6 258 char *fbuffer = (char *)mymalloc(FILESEND_BUF_SIZE);
WiredHome 0:729320f63c5c 259 int bytes;
WiredHome 0:729320f63c5c 260
WiredHome 3:17928786bdb5 261 if (fbuffer) {
WiredHome 3:17928786bdb5 262 header(200, "OK", filetype);
WiredHome 0:729320f63c5c 263 bytes = fread(fbuffer,sizeof(char),FILESEND_BUF_SIZE,fp);
WiredHome 3:17928786bdb5 264 while (bytes > 0) {
WiredHome 3:17928786bdb5 265 send(fbuffer, bytes);
WiredHome 3:17928786bdb5 266 bytes = fread(fbuffer,sizeof(char),FILESEND_BUF_SIZE,fp);
WiredHome 3:17928786bdb5 267 }
WiredHome 8:262583f054f6 268 myfree(fbuffer);
WiredHome 3:17928786bdb5 269 } else {
WiredHome 3:17928786bdb5 270 header(500, "Server Error", "Pragma: err - insufficient memory\r\n");
WiredHome 0:729320f63c5c 271 }
WiredHome 0:729320f63c5c 272 fclose(fp);
WiredHome 0:729320f63c5c 273 return true;
WiredHome 0:729320f63c5c 274 } else {
WiredHome 3:17928786bdb5 275 header(404, "Not Found", "Pragma: err - Can't open file\r\n");
WiredHome 0:729320f63c5c 276 return false;
WiredHome 0:729320f63c5c 277 }
WiredHome 0:729320f63c5c 278 }
WiredHome 0:729320f63c5c 279
WiredHome 0:729320f63c5c 280 int HTTPServer::HexCharToInt(char c)
WiredHome 0:729320f63c5c 281 {
WiredHome 0:729320f63c5c 282 if (c >= 'a' && c <= 'f')
WiredHome 0:729320f63c5c 283 return (c - 'a' + 10);
WiredHome 0:729320f63c5c 284 else if (c >= 'A' && c <= 'F')
WiredHome 0:729320f63c5c 285 return (c - 'A' + 10);
WiredHome 0:729320f63c5c 286 else if (c >= '0' && c <= '9')
WiredHome 0:729320f63c5c 287 return c - '0';
WiredHome 0:729320f63c5c 288 else
WiredHome 0:729320f63c5c 289 return 0;
WiredHome 0:729320f63c5c 290 }
WiredHome 0:729320f63c5c 291
WiredHome 0:729320f63c5c 292 char HTTPServer::HexPairToChar(char * p)
WiredHome 0:729320f63c5c 293 {
WiredHome 0:729320f63c5c 294 return 16 * HexCharToInt(*p) + HexCharToInt(*(p+1));
WiredHome 0:729320f63c5c 295 }
WiredHome 0:729320f63c5c 296
WiredHome 0:729320f63c5c 297 void HTTPServer::UnescapeString(char * encoded)
WiredHome 0:729320f63c5c 298 {
WiredHome 0:729320f63c5c 299 char *p;
WiredHome 0:729320f63c5c 300
WiredHome 0:729320f63c5c 301 // first convert '+' to ' '
WiredHome 0:729320f63c5c 302 p = strchr(encoded, '+');
WiredHome 0:729320f63c5c 303 while (p) {
WiredHome 0:729320f63c5c 304 *p = ' ';
WiredHome 0:729320f63c5c 305 p = strchr(encoded, '+');
WiredHome 0:729320f63c5c 306 }
WiredHome 0:729320f63c5c 307 // then convert hex '%xx' to char 'x'
WiredHome 0:729320f63c5c 308 p = strchr(encoded, '%');
WiredHome 0:729320f63c5c 309 while (p) {
WiredHome 0:729320f63c5c 310 if (strchr("0123456789ABCDEFabcdef", *(p+1))
WiredHome 0:729320f63c5c 311 && strchr("0123456789ABCDEFabcdef", *(p+2)) ) {
WiredHome 0:729320f63c5c 312 *p = HexPairToChar(p+1);
WiredHome 0:729320f63c5c 313 p++; // advance past the %
WiredHome 0:729320f63c5c 314 char * a = p;
WiredHome 0:729320f63c5c 315 char * b = p + 2;
WiredHome 0:729320f63c5c 316 do {
WiredHome 0:729320f63c5c 317 *a++ = *b++;
WiredHome 0:729320f63c5c 318 } while (*b);
WiredHome 0:729320f63c5c 319 *a = '\0';
WiredHome 0:729320f63c5c 320 }
WiredHome 0:729320f63c5c 321 p = strchr(p, '%');
WiredHome 0:729320f63c5c 322 }
WiredHome 0:729320f63c5c 323 }
WiredHome 0:729320f63c5c 324
WiredHome 0:729320f63c5c 325 const char * HTTPServer::GetParameter(const char * name)
WiredHome 0:729320f63c5c 326 {
WiredHome 13:8975d7928678 327 for (int i=0; i<queryParamCount; i++) {
WiredHome 13:8975d7928678 328 if (strcmp(queryParams[i].name, name) == 0) {
WiredHome 13:8975d7928678 329 return queryParams[i].value;
WiredHome 0:729320f63c5c 330 }
WiredHome 0:729320f63c5c 331 }
WiredHome 0:729320f63c5c 332 return NULL;
WiredHome 0:729320f63c5c 333 }
WiredHome 0:729320f63c5c 334
WiredHome 0:729320f63c5c 335 // this=that&who=what&more=stuff...
WiredHome 0:729320f63c5c 336 // ^ ^ ^
WiredHome 3:17928786bdb5 337 void HTTPServer::ParseParameters(char * pName)
WiredHome 0:729320f63c5c 338 {
WiredHome 0:729320f63c5c 339 char * pVal;
WiredHome 0:729320f63c5c 340 char * pNextName;
WiredHome 0:729320f63c5c 341
WiredHome 13:8975d7928678 342 // Parse queryParams
WiredHome 0:729320f63c5c 343 pVal = strchr(pName, '#'); // If there is a '#fragment_id', we can ignore it
WiredHome 0:729320f63c5c 344 if (pVal)
WiredHome 3:17928786bdb5 345 *pVal = '\0';
WiredHome 0:729320f63c5c 346 do {
WiredHome 0:729320f63c5c 347 //pc->printf("Parse(%s)\r\n", pName);
WiredHome 0:729320f63c5c 348 //*pName++ = '\0'; // already '\0' on the first entry
WiredHome 13:8975d7928678 349 queryParams[queryParamCount].name = pName;
WiredHome 0:729320f63c5c 350 pVal = strchr(pName, '=');
WiredHome 0:729320f63c5c 351 pNextName = strchr(pName,'&');
WiredHome 0:729320f63c5c 352 if (pVal) {
WiredHome 0:729320f63c5c 353 if (pNextName == NULL || (pNextName && pNextName > pVal)) {
WiredHome 0:729320f63c5c 354 *pVal++ = '\0';
WiredHome 13:8975d7928678 355 queryParams[queryParamCount].value = pVal;
WiredHome 0:729320f63c5c 356 pName = pVal;
WiredHome 0:729320f63c5c 357 }
WiredHome 0:729320f63c5c 358 }
WiredHome 13:8975d7928678 359 //pc->printf(" [%s]=[%s]\r\n", queryParams[queryParamCount].name, queryParams[queryParamCount].value);
WiredHome 13:8975d7928678 360 queryParamCount++;
WiredHome 0:729320f63c5c 361 if (pNextName) {
WiredHome 0:729320f63c5c 362 pName = pNextName;
WiredHome 0:729320f63c5c 363 *pName++ = '\0';
WiredHome 0:729320f63c5c 364 } else {
WiredHome 0:729320f63c5c 365 pName = NULL;
WiredHome 0:729320f63c5c 366 }
WiredHome 13:8975d7928678 367 } while (pName && queryParamCount < maxqueryParams);
WiredHome 0:729320f63c5c 368 }
WiredHome 0:729320f63c5c 369
WiredHome 0:729320f63c5c 370
WiredHome 5:c9b27e718054 371 bool HTTPServer::GetRemoteAddr(char * str, int strSize)
WiredHome 0:729320f63c5c 372 {
WiredHome 0:729320f63c5c 373 bool res = false;
WiredHome 0:729320f63c5c 374 char *p;
WiredHome 0:729320f63c5c 375
WiredHome 4:f34642902056 376 if (strSize < 16) { // Can only guard it here w/o modifying Wifly class
WiredHome 3:17928786bdb5 377 *str = '\0';
WiredHome 5:c9b27e718054 378 return res;
WiredHome 3:17928786bdb5 379 }
WiredHome 5:c9b27e718054 380 res = wifly->sendCommand("show z\r", NULL, str, strSize);
WiredHome 0:729320f63c5c 381 if (res) {
WiredHome 0:729320f63c5c 382 p = strchr(str, '\n'); // truncate after the octets.
WiredHome 0:729320f63c5c 383 if (p) *p = '\0';
WiredHome 0:729320f63c5c 384 p = strchr(str, ' '); // or a space
WiredHome 0:729320f63c5c 385 if (p) *p = '\0';
WiredHome 0:729320f63c5c 386 p = strchr(str, '<'); // or a <
WiredHome 0:729320f63c5c 387 if (p) *p = '\0';
WiredHome 5:c9b27e718054 388 res = true;
WiredHome 0:729320f63c5c 389 }
WiredHome 0:729320f63c5c 390 wifly->exit();
WiredHome 5:c9b27e718054 391 return res;
WiredHome 0:729320f63c5c 392 }
WiredHome 0:729320f63c5c 393
WiredHome 0:729320f63c5c 394
WiredHome 0:729320f63c5c 395 void HTTPServer::header(int code, const char * code_text, const char * content_type, const char * optional_text)
WiredHome 0:729320f63c5c 396 {
WiredHome 0:729320f63c5c 397 char http[100];
WiredHome 0:729320f63c5c 398
WiredHome 0:729320f63c5c 399 sprintf(http, "%s %i %s\r\n", hdr_httpver, code, code_text);
WiredHome 0:729320f63c5c 400 send(http);
WiredHome 0:729320f63c5c 401 send(hdr_age);
WiredHome 0:729320f63c5c 402 send(hdr_server);
WiredHome 0:729320f63c5c 403 if (content_type) {
WiredHome 0:729320f63c5c 404 send(content_type);
WiredHome 0:729320f63c5c 405 }
WiredHome 0:729320f63c5c 406 if (optional_text) {
WiredHome 0:729320f63c5c 407 send(optional_text);
WiredHome 0:729320f63c5c 408 }
WiredHome 2:a29c32190037 409 send(hdr_dnt);
WiredHome 0:729320f63c5c 410 send(hdr_close);
WiredHome 0:729320f63c5c 411 send(nl);
WiredHome 0:729320f63c5c 412 }
WiredHome 0:729320f63c5c 413
WiredHome 7:99ad7a67f05e 414 bool HTTPServer::close_connection()
WiredHome 0:729320f63c5c 415 {
WiredHome 7:99ad7a67f05e 416 bool res;
WiredHome 7:99ad7a67f05e 417
WiredHome 10:9c8d2c6a3469 418 res = server->close(); //wifly->close();
WiredHome 7:99ad7a67f05e 419 #ifdef DEBUG
WiredHome 7:99ad7a67f05e 420 pc->printf("close connection returned %d\r\n", res);
WiredHome 0:729320f63c5c 421 #endif
WiredHome 7:99ad7a67f05e 422 return res;
WiredHome 0:729320f63c5c 423 }
WiredHome 0:729320f63c5c 424
WiredHome 0:729320f63c5c 425 bool HTTPServer::Extract(char * haystack, char * needle, char ** string)
WiredHome 0:729320f63c5c 426 {
WiredHome 0:729320f63c5c 427 bool ret = false; // assume failure until proven otherwise
WiredHome 0:729320f63c5c 428 char * qs = NULL;
WiredHome 0:729320f63c5c 429 char * eqs = NULL;
WiredHome 0:729320f63c5c 430 char * container = NULL;
WiredHome 0:729320f63c5c 431 char * get = strstr(haystack, needle); // what if not at the front?
WiredHome 0:729320f63c5c 432 if (get) {
WiredHome 0:729320f63c5c 433 // Seems to be a valid "...GET /QueryString HTTP/1.1"
WiredHome 8:262583f054f6 434 // or "...<needle>param..."
WiredHome 0:729320f63c5c 435 qs = get + strlen(needle); // in case the needle didn't have space delimiters
WiredHome 0:729320f63c5c 436 while (*qs == ' ')
WiredHome 0:729320f63c5c 437 qs++;
WiredHome 0:729320f63c5c 438 // /QueryString\0HTTP/1.1\0\0
WiredHome 0:729320f63c5c 439 if (*string) // recycle old string when working a new one
WiredHome 8:262583f054f6 440 myfree(*string);
WiredHome 8:262583f054f6 441 container = (char *)mymalloc(strlen(qs));
WiredHome 0:729320f63c5c 442 if (container) {
WiredHome 0:729320f63c5c 443 strcpy(container, qs);
WiredHome 0:729320f63c5c 444 eqs = strchr(container, ' ');
WiredHome 0:729320f63c5c 445 if (eqs)
WiredHome 0:729320f63c5c 446 *eqs = '\0';
WiredHome 0:729320f63c5c 447 *string = container;
WiredHome 8:262583f054f6 448 #ifdef DEBUG
WiredHome 8:262583f054f6 449 pc->printf("Extract(%s) = %s\r\n", needle, container);
WiredHome 8:262583f054f6 450 #endif
WiredHome 0:729320f63c5c 451 ret = true;
WiredHome 0:729320f63c5c 452 } else {
WiredHome 0:729320f63c5c 453 *string = NULL; // something bad happened... no memory
WiredHome 0:729320f63c5c 454 }
WiredHome 0:729320f63c5c 455 }
WiredHome 0:729320f63c5c 456 return ret;
WiredHome 0:729320f63c5c 457 }
WiredHome 0:729320f63c5c 458
WiredHome 0:729320f63c5c 459 char * HTTPServer::rewriteWithDefaultFile(char * queryString)
WiredHome 0:729320f63c5c 460 {
WiredHome 8:262583f054f6 461 char * temp = (char *)mymalloc(strlen(queryString) + strlen(DEFAULT_FILENAME) + 1);
WiredHome 0:729320f63c5c 462
WiredHome 0:729320f63c5c 463 if (temp) {
WiredHome 0:729320f63c5c 464 *temp = '\0';
WiredHome 0:729320f63c5c 465 strcpy(temp, queryString);
WiredHome 0:729320f63c5c 466 strcat(temp, DEFAULT_FILENAME);
WiredHome 8:262583f054f6 467 myfree(queryString);
WiredHome 0:729320f63c5c 468 return temp;
WiredHome 0:729320f63c5c 469 } else {
WiredHome 0:729320f63c5c 470 return queryString;
WiredHome 0:729320f63c5c 471 }
WiredHome 0:729320f63c5c 472 }
WiredHome 0:729320f63c5c 473
WiredHome 0:729320f63c5c 474 char * HTTPServer::rewritePrependWebroot(char * queryString)
WiredHome 0:729320f63c5c 475 {
WiredHome 8:262583f054f6 476 char * temp = (char *)mymalloc(strlen(webroot) + strlen(queryString) + 1);
WiredHome 0:729320f63c5c 477
WiredHome 0:729320f63c5c 478 if (temp) {
WiredHome 0:729320f63c5c 479 *temp = '\0';
WiredHome 0:729320f63c5c 480 strcpy(temp, webroot);
WiredHome 0:729320f63c5c 481 if (temp[strlen(temp)-1] == '/' && *queryString == '/')
WiredHome 0:729320f63c5c 482 temp[strlen(temp)-1] = '\0';
WiredHome 0:729320f63c5c 483 strcat(temp, queryString);
WiredHome 8:262583f054f6 484 myfree(queryString);
WiredHome 0:729320f63c5c 485 return temp;
WiredHome 0:729320f63c5c 486 } else {
WiredHome 0:729320f63c5c 487 return queryString;
WiredHome 0:729320f63c5c 488 }
WiredHome 0:729320f63c5c 489 }
WiredHome 0:729320f63c5c 490
WiredHome 3:17928786bdb5 491 bool HTTPServer::CheckDynamicHandlers()
WiredHome 3:17928786bdb5 492 {
WiredHome 3:17928786bdb5 493 bool regHandled = false;
WiredHome 0:729320f63c5c 494
WiredHome 3:17928786bdb5 495 // If this queryString is in the list of registered handlers, call that
WiredHome 3:17928786bdb5 496 for (int i=0; i<handlercount; i++) {
WiredHome 3:17928786bdb5 497 if (strcmp(handlers[i].path, queryString) == 0) {
WiredHome 13:8975d7928678 498 (*handlers[i].callback)(this, SEND_PAGE, queryString, queryParams, queryParamCount);
WiredHome 3:17928786bdb5 499 regHandled = true;
WiredHome 3:17928786bdb5 500 break; // we only execute the first one
WiredHome 3:17928786bdb5 501 }
WiredHome 3:17928786bdb5 502 }
WiredHome 3:17928786bdb5 503 return regHandled;
WiredHome 3:17928786bdb5 504 }
WiredHome 3:17928786bdb5 505
WiredHome 3:17928786bdb5 506 void HTTPServer::SendResponse()
WiredHome 3:17928786bdb5 507 {
WiredHome 8:262583f054f6 508 #ifdef DEBUG
WiredHome 8:262583f054f6 509 pc->printf("SendResponse(%s) [%d]\r\n", queryType, __LINE__);
WiredHome 8:262583f054f6 510 #endif
WiredHome 3:17928786bdb5 511 if (strcmp(queryType, "GET") == 0 || strcmp(queryType, "POST") == 0) {
WiredHome 3:17928786bdb5 512 if (!(queryString[0] == '.' && queryString[1] == '.')) {
WiredHome 3:17928786bdb5 513 const char * fType;
WiredHome 3:17928786bdb5 514
WiredHome 8:262583f054f6 515 #ifdef DEBUG
WiredHome 8:262583f054f6 516 pc->printf(" SendResponse() [%d]\r\n", __LINE__);
WiredHome 8:262583f054f6 517 #endif
WiredHome 3:17928786bdb5 518 if (!CheckDynamicHandlers()) {
WiredHome 3:17928786bdb5 519 // Otherwise, this queryString must be trying to reference a static file
WiredHome 3:17928786bdb5 520 if (queryString[strlen(queryString)-1] == '/') {
WiredHome 3:17928786bdb5 521 queryString = rewriteWithDefaultFile(queryString);
WiredHome 3:17928786bdb5 522 }
WiredHome 3:17928786bdb5 523 // see if we support this file type
WiredHome 3:17928786bdb5 524 fType = GetSupportedType(queryString);
WiredHome 3:17928786bdb5 525 if (fType) {
WiredHome 3:17928786bdb5 526 queryString = rewritePrependWebroot(queryString);
WiredHome 3:17928786bdb5 527 SendFile(queryString, fType);
WiredHome 3:17928786bdb5 528 } else {
WiredHome 3:17928786bdb5 529 //pc->printf("Unsupported file type %s\r\n", queryString);
WiredHome 3:17928786bdb5 530 header(404, "Not Found", "Pragma: err - Unsupported type\r\n");
WiredHome 3:17928786bdb5 531 }
WiredHome 3:17928786bdb5 532 }
WiredHome 3:17928786bdb5 533 } else {
WiredHome 3:17928786bdb5 534 //pc->printf("Unsupported path %s\r\n", queryString);
WiredHome 3:17928786bdb5 535 header(400, "Bad Request", "Pragma: err - Unsupported path\r\n");
WiredHome 3:17928786bdb5 536 }
WiredHome 3:17928786bdb5 537 } else {
WiredHome 3:17928786bdb5 538 //pc->printf("Unsupported query type %s\r\n", queryType);
WiredHome 3:17928786bdb5 539 header(400, "Bad Request", "Pragma: err - Unsupported query type\r\n");
WiredHome 3:17928786bdb5 540 }
WiredHome 3:17928786bdb5 541 if (queryType) {
WiredHome 8:262583f054f6 542 myfree(queryType);
WiredHome 3:17928786bdb5 543 queryType = NULL;
WiredHome 3:17928786bdb5 544 }
WiredHome 3:17928786bdb5 545 if (queryString) {
WiredHome 8:262583f054f6 546 myfree(queryString);
WiredHome 3:17928786bdb5 547 queryString = NULL;
WiredHome 3:17928786bdb5 548 }
WiredHome 13:8975d7928678 549 // if (hostString) {
WiredHome 13:8975d7928678 550 // myfree(hostString);
WiredHome 13:8975d7928678 551 // hostString = NULL;
WiredHome 13:8975d7928678 552 // }
WiredHome 13:8975d7928678 553 // if (contentLength) {
WiredHome 13:8975d7928678 554 // myfree(contentLength);
WiredHome 13:8975d7928678 555 // contentLength = NULL;
WiredHome 13:8975d7928678 556 // }
WiredHome 13:8975d7928678 557 // if (contentType) {
WiredHome 13:8975d7928678 558 // myfree(contentType);
WiredHome 13:8975d7928678 559 // contentType = NULL;
WiredHome 13:8975d7928678 560 // }
WiredHome 13:8975d7928678 561 // if (authorization) {
WiredHome 13:8975d7928678 562 // myfree(authorization);
WiredHome 13:8975d7928678 563 // authorization = NULL;
WiredHome 13:8975d7928678 564 // }
WiredHome 3:17928786bdb5 565 if (postQueryString) {
WiredHome 8:262583f054f6 566 myfree(postQueryString);
WiredHome 3:17928786bdb5 567 postQueryString = NULL;
WiredHome 3:17928786bdb5 568 }
WiredHome 3:17928786bdb5 569 }
WiredHome 3:17928786bdb5 570
WiredHome 3:17928786bdb5 571 bool HTTPServer::ParseHeader(char * buffer)
WiredHome 3:17928786bdb5 572 {
WiredHome 3:17928786bdb5 573 char * dblCR;
WiredHome 3:17928786bdb5 574 bool advanceState = false;
WiredHome 3:17928786bdb5 575 int bytecount;
WiredHome 7:99ad7a67f05e 576
WiredHome 3:17928786bdb5 577 // Buffer could have partial, but the double \r\n is the key
WiredHome 13:8975d7928678 578 // GET /QueryString?this=that&sky=blue HTTP/1.1\r\n
WiredHome 8:262583f054f6 579 // GET /QueryString HTTP/1.1\r\nHost: 192.168.1.140\r\nCache-Con
WiredHome 8:262583f054f6 580 // GET /QueryString HTTP/1.1\r\nHost: 192.168.1.140\r\nCache-Control: max-age=0\r\n\r\n
WiredHome 3:17928786bdb5 581 dblCR = strstr(buffer,"\r\n\r\n");
WiredHome 3:17928786bdb5 582 if (dblCR) { // Have to scan from the beginning in case split on \r
WiredHome 8:262583f054f6 583 #ifdef DEBUG
WiredHome 8:262583f054f6 584 pc->printf("==\r\n%s==\r\n", buffer);
WiredHome 3:17928786bdb5 585 #endif
WiredHome 3:17928786bdb5 586 char * soRec = buffer; // start of the next record of text
WiredHome 13:8975d7928678 587 char * eoRec = strchr(soRec, '\n'); // search for end of the current record
WiredHome 7:99ad7a67f05e 588
WiredHome 13:8975d7928678 589 headerParamCount = 0;
WiredHome 3:17928786bdb5 590 bytecount = strlen(buffer);
WiredHome 3:17928786bdb5 591 if (bytecount > maxheaderbytes)
WiredHome 3:17928786bdb5 592 maxheaderbytes = bytecount;
WiredHome 3:17928786bdb5 593 while (eoRec) {
WiredHome 3:17928786bdb5 594 *eoRec = '\0';
WiredHome 3:17928786bdb5 595 if (*(eoRec-1) == '\r')
WiredHome 3:17928786bdb5 596 *(eoRec-1) = '\0';
WiredHome 12:109bf1558300 597 #ifdef DEBUG
WiredHome 8:262583f054f6 598 pc->printf("rec {%s}\r\n", soRec);
WiredHome 12:109bf1558300 599 #endif
WiredHome 8:262583f054f6 600 // Inspect the supported query types (GET, POST) and ignore (HEAD, PUT, OPTION, DELETE, TRACE, CONNECT]
WiredHome 8:262583f054f6 601 // This is very clumsy
WiredHome 8:262583f054f6 602 if (strstr(soRec, "GET ") == soRec) {
WiredHome 8:262583f054f6 603 Extract(soRec, "GET", &queryString);
WiredHome 8:262583f054f6 604 if (queryString) {
WiredHome 8:262583f054f6 605 queryType = (char *)mymalloc(strlen("GET")+1);
WiredHome 8:262583f054f6 606 strcpy(queryType, "GET");
WiredHome 8:262583f054f6 607 }
WiredHome 10:9c8d2c6a3469 608 //printf("GET: %s\r\n", queryString);
WiredHome 8:262583f054f6 609 } else if (strstr(soRec, "POST ") == soRec) {
WiredHome 8:262583f054f6 610 Extract(soRec, "POST", &queryString);
WiredHome 8:262583f054f6 611 if (queryString) {
WiredHome 8:262583f054f6 612 queryType = (char *)mymalloc(strlen("POST")+1);
WiredHome 8:262583f054f6 613 strcpy(queryType, "POST");
WiredHome 8:262583f054f6 614 }
WiredHome 10:9c8d2c6a3469 615 //printf("POST: %s\r\n", queryString);
WiredHome 10:9c8d2c6a3469 616 }
WiredHome 13:8975d7928678 617
WiredHome 13:8975d7928678 618 // if there is a ": " delimiter, we have a header item to parse into name,value pair
WiredHome 13:8975d7928678 619 // "Connection: keep-alive" becomes "Connection" "keep-alive"
WiredHome 13:8975d7928678 620 char *delim = strstr(soRec, ": ");
WiredHome 13:8975d7928678 621 char *chkSpace = strchr(soRec, ' '); // a field-name has no space ahead of the ":"
WiredHome 13:8975d7928678 622 if (delim
WiredHome 13:8975d7928678 623 && (!chkSpace || (chkSpace && delim < chkSpace))
WiredHome 13:8975d7928678 624 && headerParamCount < maxheaderParams) {
WiredHome 13:8975d7928678 625 *delim++ = '\0';
WiredHome 13:8975d7928678 626 *delim++ = '\0';
WiredHome 13:8975d7928678 627 headerParams[headerParamCount].name = soRec;
WiredHome 13:8975d7928678 628 headerParams[headerParamCount].value = delim;
WiredHome 13:8975d7928678 629 #ifdef DEBUG
WiredHome 13:8975d7928678 630 pc->printf("%d: headerParams[%s] = {%s}\r\n", headerParamCount,
WiredHome 13:8975d7928678 631 headerParams[headerParamCount].name, headerParams[headerParamCount].value);
WiredHome 13:8975d7928678 632 #endif
WiredHome 13:8975d7928678 633 headerParamCount++;
WiredHome 13:8975d7928678 634 }
WiredHome 13:8975d7928678 635 // Extract(soRec, "Host: ", &hostString);
WiredHome 13:8975d7928678 636 // Extract(soRec, "Content-Length: ", &contentLength);
WiredHome 13:8975d7928678 637 // Extract(soRec, "Content-Type: ", &contentType);
WiredHome 13:8975d7928678 638 // Extract(soRec, "Authorization: ", &authorization);
WiredHome 3:17928786bdb5 639 soRec = eoRec + 1;
WiredHome 3:17928786bdb5 640 eoRec = strchr(soRec, '\n');
WiredHome 3:17928786bdb5 641 }
WiredHome 3:17928786bdb5 642 if (queryString) {
WiredHome 3:17928786bdb5 643 // We have enough to try to reply
WiredHome 12:109bf1558300 644 #ifdef DEBUG
WiredHome 8:262583f054f6 645 pc->printf("create reply queryType{%s}, queryString{%s}\r\n", "GET", queryString);
WiredHome 12:109bf1558300 646 #endif
WiredHome 13:8975d7928678 647 // parse queryParams - if any
WiredHome 3:17928786bdb5 648 // /file.htm?name1=value1&name2=value2...
WiredHome 3:17928786bdb5 649 // /file.htm?name1&name2=value2...
WiredHome 13:8975d7928678 650 queryParamCount = 0;
WiredHome 3:17928786bdb5 651 char * paramDelim = strchr(queryString, '?');
WiredHome 3:17928786bdb5 652 if (paramDelim) {
WiredHome 3:17928786bdb5 653 *paramDelim++ = '\0';
WiredHome 3:17928786bdb5 654 UnescapeString(paramDelim); // everything after the '?'
WiredHome 13:8975d7928678 655 ParseParameters(paramDelim); // pointing at the NULL, but there are queryParams beyond
WiredHome 3:17928786bdb5 656 }
WiredHome 13:8975d7928678 657 //for (int i=0; i<queryParamCount; i++)
WiredHome 13:8975d7928678 658 // pc->printf("param %d '%s'='%s'\r\n", i, queryParams[i].name, queryParams[i].value);
WiredHome 3:17928786bdb5 659 } else {
WiredHome 10:9c8d2c6a3469 660 pc->printf("ERROR: queryString not found in (%s)\r\n", soRec);
WiredHome 3:17928786bdb5 661 }
WiredHome 3:17928786bdb5 662 advanceState = true;
WiredHome 3:17928786bdb5 663 buffer[0] = 0;
WiredHome 3:17928786bdb5 664
WiredHome 3:17928786bdb5 665 // This part parses the extra data on a POST method.
WiredHome 3:17928786bdb5 666 // Since there has to be a dynamic handler registered for this
WiredHome 3:17928786bdb5 667 // it would make sense to move some of this responsibility to
WiredHome 3:17928786bdb5 668 // that handler. It could then choose if it wanted to allocate
WiredHome 3:17928786bdb5 669 // the requested 'Content-Length' amount of memory.
WiredHome 3:17928786bdb5 670 // Should we check the 'Content-Type' to see if it is
WiredHome 3:17928786bdb5 671 // 'application/x-www-form-urlencoded'?
WiredHome 13:8975d7928678 672 int postBytes = atoi(GetHeaderValue("Content-Length"));
WiredHome 3:17928786bdb5 673 bool acceptIt = false;
WiredHome 3:17928786bdb5 674 //pc->printf("Content-Length = %d\r\n", postBytes);
WiredHome 3:17928786bdb5 675 if (strcmp(queryType, "POST") == 0 && postBytes > 0 ) {
WiredHome 3:17928786bdb5 676 if (postBytes) {
WiredHome 3:17928786bdb5 677 bool regHandled = false;
WiredHome 3:17928786bdb5 678 // Registered Dynamic Handler
WiredHome 3:17928786bdb5 679 // Callback and ask if they want to accept this data
WiredHome 3:17928786bdb5 680 for (int i=0; i<handlercount; i++) {
WiredHome 3:17928786bdb5 681 if (strcmp(handlers[i].path, queryString) == 0) {
WiredHome 13:8975d7928678 682 acceptIt = (*handlers[i].callback)(this, CONTENT_LENGTH_REQUEST, queryString, queryParams, queryParamCount);
WiredHome 3:17928786bdb5 683 regHandled = true;
WiredHome 3:17928786bdb5 684 break; // we only execute the first one
WiredHome 3:17928786bdb5 685 }
WiredHome 3:17928786bdb5 686 }
WiredHome 3:17928786bdb5 687
WiredHome 3:17928786bdb5 688 if (regHandled && acceptIt) {
WiredHome 3:17928786bdb5 689 // If so, we'll make space for it
WiredHome 8:262583f054f6 690 postQueryString = (char *)mymalloc(postBytes + 1);
WiredHome 3:17928786bdb5 691 if (postQueryString) {
WiredHome 3:17928786bdb5 692 char * offset;
WiredHome 3:17928786bdb5 693 int len;
WiredHome 3:17928786bdb5 694
WiredHome 3:17928786bdb5 695 dblCR += 4; // If we slurped up any of the POST,
WiredHome 3:17928786bdb5 696 while (*dblCR && *dblCR <= ' ')
WiredHome 3:17928786bdb5 697 dblCR++;
WiredHome 3:17928786bdb5 698 strcpy(postQueryString, dblCR); // copy that in and then get the rest
WiredHome 3:17928786bdb5 699 while ((len = strlen(postQueryString)) < postBytes) {
WiredHome 3:17928786bdb5 700 int n;
WiredHome 3:17928786bdb5 701 offset = postQueryString + len;
WiredHome 3:17928786bdb5 702 n = client.receive(offset, postBytes - len);
WiredHome 3:17928786bdb5 703 if (n >=0) {
WiredHome 3:17928786bdb5 704 offset[n] = '\0';
WiredHome 3:17928786bdb5 705 }
WiredHome 3:17928786bdb5 706 }
WiredHome 3:17928786bdb5 707 if (len >= 0) {
WiredHome 3:17928786bdb5 708 UnescapeString(postQueryString);
WiredHome 3:17928786bdb5 709 ParseParameters(postQueryString);
WiredHome 3:17928786bdb5 710 }
WiredHome 3:17928786bdb5 711 }
WiredHome 3:17928786bdb5 712 } else {
WiredHome 3:17928786bdb5 713 // Simply copy it to the bitbucket
WiredHome 3:17928786bdb5 714 int bytesToDump = postBytes;
WiredHome 8:262583f054f6 715 char * bitbucket = (char *)mymalloc(201);
WiredHome 3:17928786bdb5 716 dblCR += 4;
WiredHome 3:17928786bdb5 717 while (*dblCR && *dblCR <= ' ')
WiredHome 3:17928786bdb5 718 dblCR++;
WiredHome 3:17928786bdb5 719 bytesToDump -= strlen(dblCR);
WiredHome 3:17928786bdb5 720 while (bytesToDump > 0) {
WiredHome 3:17928786bdb5 721 int n = (bytesToDump > 200) ? 200 : bytesToDump;
WiredHome 3:17928786bdb5 722 n = client.receive(bitbucket, n);
WiredHome 3:17928786bdb5 723 bytesToDump -= n;
WiredHome 3:17928786bdb5 724 }
WiredHome 8:262583f054f6 725 myfree(bitbucket);
WiredHome 3:17928786bdb5 726 }
WiredHome 3:17928786bdb5 727 }
WiredHome 3:17928786bdb5 728 }
WiredHome 3:17928786bdb5 729 }
WiredHome 3:17928786bdb5 730 return advanceState;
WiredHome 3:17928786bdb5 731 }
WiredHome 3:17928786bdb5 732
WiredHome 13:8975d7928678 733 const char * HTTPServer::GetHeaderValue(const char * hdr)
WiredHome 13:8975d7928678 734 {
WiredHome 13:8975d7928678 735 int i;
WiredHome 13:8975d7928678 736
WiredHome 13:8975d7928678 737 for (i=0; i<headerParamCount; i++)
WiredHome 13:8975d7928678 738 {
WiredHome 13:8975d7928678 739 if (strcmp(hdr, headerParams[i].name) == 0)
WiredHome 13:8975d7928678 740 return headerParams[i].value;
WiredHome 13:8975d7928678 741 }
WiredHome 13:8975d7928678 742 return NULL;
WiredHome 13:8975d7928678 743 }
WiredHome 13:8975d7928678 744
WiredHome 12:109bf1558300 745
WiredHome 7:99ad7a67f05e 746 void HTTPServer::GetPerformanceData(SW_PerformanceData * p)
WiredHome 7:99ad7a67f05e 747 {
WiredHome 3:17928786bdb5 748 memcpy(p, &perfData, sizeof(perfData));
WiredHome 3:17928786bdb5 749 }
WiredHome 3:17928786bdb5 750
WiredHome 7:99ad7a67f05e 751 int HTTPServer::RecordPerformanceData(SW_PerformanceParam * param, int refTime)
WiredHome 7:99ad7a67f05e 752 {
WiredHome 10:9c8d2c6a3469 753 int t_now = PerformanceTimer.read_us();
WiredHome 3:17928786bdb5 754 param->TotalTime_us += (t_now - refTime);
WiredHome 3:17928786bdb5 755 param->Samples++;
WiredHome 3:17928786bdb5 756 if ((t_now - refTime) > param->MaxTime_us)
WiredHome 3:17928786bdb5 757 param->MaxTime_us = (t_now - refTime);
WiredHome 3:17928786bdb5 758 return t_now;
WiredHome 3:17928786bdb5 759 }
WiredHome 3:17928786bdb5 760
WiredHome 7:99ad7a67f05e 761 void HTTPServer::ResetPerformanceData()
WiredHome 7:99ad7a67f05e 762 {
WiredHome 3:17928786bdb5 763 memset(&perfData, 0, sizeof(perfData));
WiredHome 3:17928786bdb5 764 }
WiredHome 3:17928786bdb5 765
WiredHome 4:f34642902056 766