| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 32 | kaklik | /********************************************************************* |
| 2 | * |
||
| 3 | * HyperText Transfer Protocol (HTTP) Server |
||
| 4 | * Module for Microchip TCP/IP Stack |
||
| 5 | * -Serves dynamic pages to web browsers such as Microsoft Internet |
||
| 6 | * Explorer, Mozilla Firefox, etc. |
||
| 7 | * -Reference: RFC 2068 |
||
| 8 | * |
||
| 9 | ********************************************************************** |
||
| 10 | * FileName: HTTP.c |
||
| 11 | * Dependencies: TCP, MPFS, HTTPGetVar() callback, HTTPExecCmd() callback |
||
| 12 | * Processor: PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F, PIC32 |
||
| 13 | * Compiler: Microchip C32 v1.05 or higher |
||
| 14 | * Microchip C30 v3.12 or higher |
||
| 15 | * Microchip C18 v3.30 or higher |
||
| 16 | * HI-TECH PICC-18 PRO 9.63PL2 or higher |
||
| 17 | * Company: Microchip Technology, Inc. |
||
| 18 | * |
||
| 19 | * Software License Agreement |
||
| 20 | * |
||
| 21 | * Copyright (C) 2002-2009 Microchip Technology Inc. All rights |
||
| 22 | * reserved. |
||
| 23 | * |
||
| 24 | * Microchip licenses to you the right to use, modify, copy, and |
||
| 25 | * distribute: |
||
| 26 | * (i) the Software when embedded on a Microchip microcontroller or |
||
| 27 | * digital signal controller product ("Device") which is |
||
| 28 | * integrated into Licensee's product; or |
||
| 29 | * (ii) ONLY the Software driver source files ENC28J60.c, ENC28J60.h, |
||
| 30 | * ENCX24J600.c and ENCX24J600.h ported to a non-Microchip device |
||
| 31 | * used in conjunction with a Microchip ethernet controller for |
||
| 32 | * the sole purpose of interfacing with the ethernet controller. |
||
| 33 | * |
||
| 34 | * You should refer to the license agreement accompanying this |
||
| 35 | * Software for additional information regarding your rights and |
||
| 36 | * obligations. |
||
| 37 | * |
||
| 38 | * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT |
||
| 39 | * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT |
||
| 40 | * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A |
||
| 41 | * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
||
| 42 | * MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR |
||
| 43 | * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF |
||
| 44 | * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS |
||
| 45 | * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE |
||
| 46 | * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER |
||
| 47 | * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT |
||
| 48 | * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE. |
||
| 49 | * |
||
| 50 | * |
||
| 51 | * Author Date Comment |
||
| 52 | *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||
| 53 | * Nilesh Rajbharti 8/14/01 Original |
||
| 54 | * Nilesh Rajbharti 9/12/01 Released (Rev. 1.0) |
||
| 55 | * Nilesh Rajbharti 2/9/02 Cleanup |
||
| 56 | * Nilesh Rajbharti 5/22/02 Rev 2.0 (See version.log for detail) |
||
| 57 | * Nilesh Rajbharti 7/9/02 Rev 2.1 (Fixed HTTPParse bug) |
||
| 58 | * Howard Schlunder 2/9/05 Fixed variable substitution |
||
| 59 | * parsing (uses hex now) |
||
| 60 | ********************************************************************/ |
||
| 61 | #define __HTTP_C |
||
| 62 | |||
| 63 | #include "TCPIPConfig.h" |
||
| 64 | |||
| 65 | #if defined(STACK_USE_HTTP_SERVER) |
||
| 66 | |||
| 67 | #include "TCPIP Stack/TCPIP.h" |
||
| 68 | #include "ctype.h" |
||
| 69 | |||
| 70 | |||
| 71 | // Each dynamic variable within a CGI file should be preceeded with this character. |
||
| 72 | #define HTTP_VAR_ESC_CHAR '%' |
||
| 73 | #define HTTP_DYNAMIC_FILE_TYPE (HTTP_CGI) |
||
| 74 | |||
| 75 | // HTTP File Types |
||
| 76 | #define HTTP_TXT (0u) |
||
| 77 | #define HTTP_HTML (1u) |
||
| 78 | #define HTTP_CGI (2u) |
||
| 79 | #define HTTP_XML (3u) |
||
| 80 | #define HTTP_GIF (4u) |
||
| 81 | #define HTTP_PNG (5u) |
||
| 82 | #define HTTP_JPG (6u) |
||
| 83 | #define HTTP_JAVA (7u) |
||
| 84 | #define HTTP_WAV (8u) |
||
| 85 | #define HTTP_UNKNOWN (9u) |
||
| 86 | |||
| 87 | |||
| 88 | #define FILE_EXT_LEN (3u) |
||
| 89 | typedef struct _FILE_TYPES |
||
| 90 | { |
||
| 91 | BYTE fileExt[FILE_EXT_LEN+1]; |
||
| 92 | } FILE_TYPES; |
||
| 93 | |||
| 94 | // Each entry in this structure must be in UPPER case. |
||
| 95 | // Order of these entries must match with those defined by "HTTP File Types" defines. |
||
| 96 | static ROM FILE_TYPES httpFiles[] = |
||
| 97 | { |
||
| 98 | { "TXT" }, // HTTP_TXT |
||
| 99 | { "HTM" }, // HTTP_HTML |
||
| 100 | { "CGI" }, // HTTP_CGI |
||
| 101 | { "XML" }, // HTTP_XML |
||
| 102 | { "GIF" }, // HTTP_GIF |
||
| 103 | { "PNG" }, // HTTP_PNG |
||
| 104 | { "JPG" }, // HTTP_JPG |
||
| 105 | { "CLA" }, // HTTP_JAVA |
||
| 106 | { "WAV" }, // HTTP_WAV |
||
| 107 | { "" } // HTTP_UNKNOWN |
||
| 108 | }; |
||
| 109 | #define TOTAL_FILE_TYPES ( sizeof(httpFiles)/sizeof(httpFiles[0]) ) |
||
| 110 | |||
| 111 | |||
| 112 | typedef struct |
||
| 113 | { |
||
| 114 | ROM BYTE typeString[20]; |
||
| 115 | } HTTP_CONTENT; |
||
| 116 | |||
| 117 | // Content entry order must match with those "HTTP File Types" define's. |
||
| 118 | static ROM HTTP_CONTENT httpContents[] = |
||
| 119 | { |
||
| 120 | { "text/plain" }, // HTTP_TXT |
||
| 121 | { "text/html" }, // HTTP_HTML |
||
| 122 | { "text/html" }, // HTTP_CGI |
||
| 123 | { "text/xml" }, // HTTP_XML |
||
| 124 | { "image/gif" }, // HTTP_GIF |
||
| 125 | { "image/png" }, // HTTP_PNG |
||
| 126 | { "image/jpeg" }, // HTTP_JPG |
||
| 127 | { "application/java-vm" }, // HTTP_JAVA |
||
| 128 | { "audio/x-wave" }, // HTTP_WAV |
||
| 129 | { "" } // HTTP_UNKNOWN |
||
| 130 | }; |
||
| 131 | #define TOTAL_HTTP_CONTENTS ( sizeof(httpContents)/sizeof(httpConetents[0]) ) |
||
| 132 | |||
| 133 | // HTTP FSM states for each connection. |
||
| 134 | typedef enum |
||
| 135 | { |
||
| 136 | SM_HTTP_IDLE = 0u, |
||
| 137 | SM_HTTP_GET, |
||
| 138 | SM_HTTP_NOT_FOUND, |
||
| 139 | SM_HTTP_GET_READ, |
||
| 140 | SM_HTTP_GET_PASS, |
||
| 141 | SM_HTTP_GET_DLE, |
||
| 142 | SM_HTTP_GET_HANDLE, |
||
| 143 | SM_HTTP_GET_HANDLE_NEXT, |
||
| 144 | SM_HTTP_GET_VAR, |
||
| 145 | SM_HTTP_HEADER, |
||
| 146 | SM_HTTP_DISCARD |
||
| 147 | } SM_HTTP; |
||
| 148 | |||
| 149 | // Supported HTTP Commands |
||
| 150 | typedef enum |
||
| 151 | { |
||
| 152 | HTTP_GET = 0u, |
||
| 153 | HTTP_POST, |
||
| 154 | HTTP_NOT_SUPPORTED, |
||
| 155 | HTTP_INVALID_COMMAND |
||
| 156 | } HTTP_COMMAND; |
||
| 157 | |||
| 158 | // HTTP Connection Info - one for each connection. |
||
| 159 | typedef struct |
||
| 160 | { |
||
| 161 | TCP_SOCKET socket; |
||
| 162 | MPFS file; |
||
| 163 | SM_HTTP smHTTP; |
||
| 164 | BYTE smHTTPGet; |
||
| 165 | WORD VarRef; |
||
| 166 | BYTE bProcess; |
||
| 167 | BYTE Variable; |
||
| 168 | BYTE fileType; |
||
| 169 | } HTTP_INFO; |
||
| 170 | typedef BYTE HTTP_HANDLE; |
||
| 171 | |||
| 172 | |||
| 173 | typedef enum |
||
| 174 | { |
||
| 175 | HTTP_NOT_FOUND = 0u, |
||
| 176 | HTTP_NOT_AVAILABLE |
||
| 177 | } HTTP_MESSAGES; |
||
| 178 | |||
| 179 | // Following message order must match with that of HTTP_MESSAGES enum. |
||
| 180 | static ROM BYTE * ROM HTTPMessages[] = |
||
| 181 | { |
||
| 182 | (ROM BYTE*)"HTTP/1.1 404 Not found\r\n\r\nNot found.\r\n", |
||
| 183 | (ROM BYTE*)"HTTP/1.1 503 \r\n\r\nService Unavailable\r\n" |
||
| 184 | }; |
||
| 185 | |||
| 186 | // Standard HTTP messages. |
||
| 187 | static ROM BYTE HTTP_OK_STRING[] = "HTTP/1.1 200 OK\r\nContent-type: "; |
||
| 188 | static ROM BYTE HTTP_OK_NO_CACHE_STRING[] = "HTTP/1.1 200 OK\r\nDate: Wed, 05 Apr 2006 02:53:05 GMT\r\nExpires: Wed, 05 Apr 2006 02:52:05 GMT\r\nCache-control: private\r\nContent-type: "; |
||
| 189 | #define HTTP_OK_STRING_LEN (sizeof(HTTP_OK_STRING)-1) |
||
| 190 | #define HTTP_OK_NO_CACHE_STRING_LEN (sizeof(HTTP_OK_NO_CACHE_STRING)-1) |
||
| 191 | |||
| 192 | static ROM BYTE HTTP_HEADER_END_STRING[] = "\r\n\r\n"; |
||
| 193 | #define HTTP_HEADER_END_STRING_LEN (sizeof(HTTP_HEADER_END_STRING)-1) |
||
| 194 | |||
| 195 | // HTTP Command Strings |
||
| 196 | static ROM BYTE HTTP_GET_STRING[] = "GET"; |
||
| 197 | #define HTTP_GET_STRING_LEN (sizeof(HTTP_GET_STRING)-1) |
||
| 198 | |||
| 199 | // Default HTML file. |
||
| 200 | static ROM BYTE HTTP_DEFAULT_FILE_STRING[] = "INDEX.HTM"; |
||
| 201 | #define HTTP_DEFAULT_FILE_STRING_LEN (sizeof(HTTP_DEFAULT_FILE_STRING)-1) |
||
| 202 | |||
| 203 | |||
| 204 | // Maximum nuber of arguments supported by this HTTP Server. |
||
| 205 | #define MAX_HTTP_ARGS (11u) |
||
| 206 | |||
| 207 | // Maximum HTML Command String length. |
||
| 208 | #define MAX_HTML_CMD_LEN (100u) |
||
| 209 | |||
| 210 | |||
| 211 | static HTTP_INFO HCB[MAX_HTTP_CONNECTIONS]; |
||
| 212 | |||
| 213 | |||
| 214 | static void HTTPProcess(HTTP_HANDLE h); |
||
| 215 | static HTTP_COMMAND HTTPParse(BYTE *string, |
||
| 216 | BYTE** arg, |
||
| 217 | BYTE* argc, |
||
| 218 | BYTE* type); |
||
| 219 | static BOOL SendFile(HTTP_INFO* ph); |
||
| 220 | |||
| 221 | |||
| 222 | |||
| 223 | |||
| 224 | /********************************************************************* |
||
| 225 | * Function: void HTTPInit(void) |
||
| 226 | * |
||
| 227 | * PreCondition: TCP must already be initialized. |
||
| 228 | * |
||
| 229 | * Input: None |
||
| 230 | * |
||
| 231 | * Output: HTTP FSM and connections are initialized |
||
| 232 | * |
||
| 233 | * Side Effects: None |
||
| 234 | * |
||
| 235 | * Overview: Set all HTTP connections to Listening state. |
||
| 236 | * Initialize FSM for each connection. |
||
| 237 | * |
||
| 238 | * Note: This function is called only one during lifetime |
||
| 239 | * of the application. |
||
| 240 | ********************************************************************/ |
||
| 241 | void HTTPInit(void) |
||
| 242 | { |
||
| 243 | BYTE i; |
||
| 244 | |||
| 245 | for ( i = 0; i < MAX_HTTP_CONNECTIONS; i++ ) |
||
| 246 | { |
||
| 247 | HCB[i].socket = TCPOpen(0, TCP_OPEN_SERVER, HTTP_PORT, TCP_PURPOSE_HTTP_SERVER); |
||
| 248 | |||
| 249 | HCB[i].smHTTP = SM_HTTP_IDLE; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | |||
| 254 | |||
| 255 | /********************************************************************* |
||
| 256 | * Function: void HTTPServer(void) |
||
| 257 | * |
||
| 258 | * PreCondition: HTTPInit() must already be called. |
||
| 259 | * |
||
| 260 | * Input: None |
||
| 261 | * |
||
| 262 | * Output: Opened HTTP connections are served. |
||
| 263 | * |
||
| 264 | * Side Effects: None |
||
| 265 | * |
||
| 266 | * Overview: Browse through each connections and let it |
||
| 267 | * handle its connection. |
||
| 268 | * If a connection is not finished, do not process |
||
| 269 | * next connections. This must be done, all |
||
| 270 | * connections use some static variables that are |
||
| 271 | * common. |
||
| 272 | * |
||
| 273 | * Note: This function acts as a task (similar to one in |
||
| 274 | * RTOS). This function performs its task in |
||
| 275 | * co-operative manner. Main application must call |
||
| 276 | * this function repeatdly to ensure all open |
||
| 277 | * or new connections are served on time. |
||
| 278 | ********************************************************************/ |
||
| 279 | void HTTPServer(void) |
||
| 280 | { |
||
| 281 | BYTE conn; |
||
| 282 | |||
| 283 | for ( conn = 0; conn < MAX_HTTP_CONNECTIONS; conn++ ) |
||
| 284 | HTTPProcess(conn); |
||
| 285 | } |
||
| 286 | |||
| 287 | |||
| 288 | /********************************************************************* |
||
| 289 | * Function: static BOOL HTTPProcess(HTTP_HANDLE h) |
||
| 290 | * |
||
| 291 | * PreCondition: HTTPInit() called. |
||
| 292 | * |
||
| 293 | * Input: h - Index to the handle which needs to be |
||
| 294 | * processed. |
||
| 295 | * |
||
| 296 | * Output: Connection referred by 'h' is served. |
||
| 297 | * |
||
| 298 | * Side Effects: None |
||
| 299 | * |
||
| 300 | * Overview: |
||
| 301 | * |
||
| 302 | * Note: None. |
||
| 303 | ********************************************************************/ |
||
| 304 | static void HTTPProcess(HTTP_HANDLE h) |
||
| 305 | { |
||
| 306 | BYTE httpData[MAX_HTML_CMD_LEN+1]; |
||
| 307 | HTTP_COMMAND httpCommand; |
||
| 308 | BOOL lbContinue; |
||
| 309 | BYTE *arg[MAX_HTTP_ARGS]; |
||
| 310 | BYTE argc; |
||
| 311 | HTTP_INFO* ph; |
||
| 312 | WORD w; |
||
| 313 | |||
| 314 | ph = &HCB[h]; |
||
| 315 | |||
| 316 | |||
| 317 | do |
||
| 318 | { |
||
| 319 | lbContinue = FALSE; |
||
| 320 | |||
| 321 | // If during handling of HTTP socket, it gets disconnected, |
||
| 322 | // forget about previous processing and return to idle state. |
||
| 323 | if(!TCPIsConnected(ph->socket)) |
||
| 324 | { |
||
| 325 | ph->smHTTP = SM_HTTP_IDLE; |
||
| 326 | break; |
||
| 327 | } |
||
| 328 | |||
| 329 | |||
| 330 | switch(ph->smHTTP) |
||
| 331 | { |
||
| 332 | case SM_HTTP_IDLE: |
||
| 333 | // Search for the CRLF deliminating the end of the first GET/HEAD/POST request |
||
| 334 | w = TCPFindROMArray(ph->socket, (ROM BYTE*)"\r\n", 2, 0, FALSE); |
||
| 335 | if(w == 0xFFFFu) |
||
| 336 | { |
||
| 337 | if(TCPGetRxFIFOFree(ph->socket) == 0u) |
||
| 338 | { |
||
| 339 | // Request is too big, we can't support it. |
||
| 340 | TCPDisconnect(ph->socket); |
||
| 341 | } |
||
| 342 | break; |
||
| 343 | } |
||
| 344 | |||
| 345 | lbContinue = TRUE; |
||
| 346 | |||
| 347 | if(w > sizeof(httpData)-1) |
||
| 348 | w = sizeof(httpData)-1; |
||
| 349 | |||
| 350 | TCPGetArray(ph->socket, httpData, w); |
||
| 351 | httpData[w] = '\0'; |
||
| 352 | TCPDiscard(ph->socket); |
||
| 353 | |||
| 354 | ph->smHTTP = SM_HTTP_NOT_FOUND; |
||
| 355 | argc = MAX_HTTP_ARGS; |
||
| 356 | httpCommand = HTTPParse(httpData, arg, &argc, &ph->fileType); |
||
| 357 | if ( httpCommand == HTTP_GET ) |
||
| 358 | { |
||
| 359 | // If there are any arguments, this must be a remote command. |
||
| 360 | // Execute it and then send the file. |
||
| 361 | // The file name may be modified by command handler. |
||
| 362 | if ( argc > 1u ) |
||
| 363 | { |
||
| 364 | // Let main application handle this remote command. |
||
| 365 | HTTPExecCmd(&arg[0], argc); |
||
| 366 | |||
| 367 | // Command handler must have modified arg[0] which now |
||
| 368 | // points to actual file that will be sent as a result of |
||
| 369 | // this remote command. |
||
| 370 | |||
| 371 | // Assume that Web author will only use CGI or HTML |
||
| 372 | // file for remote command. |
||
| 373 | ph->fileType = HTTP_CGI; |
||
| 374 | } |
||
| 375 | |||
| 376 | ph->file = MPFSOpen(arg[0]); |
||
| 377 | if ( ph->file == MPFS_INVALID ) |
||
| 378 | { |
||
| 379 | ph->Variable = HTTP_NOT_FOUND; |
||
| 380 | ph->smHTTP = SM_HTTP_NOT_FOUND; |
||
| 381 | } |
||
| 382 | else if ( ph->file == MPFS_NOT_AVAILABLE ) |
||
| 383 | { |
||
| 384 | ph->Variable = HTTP_NOT_AVAILABLE; |
||
| 385 | ph->smHTTP = SM_HTTP_NOT_FOUND; |
||
| 386 | } |
||
| 387 | else |
||
| 388 | { |
||
| 389 | ph->smHTTP = SM_HTTP_HEADER; |
||
| 390 | } |
||
| 391 | } |
||
| 392 | break; |
||
| 393 | |||
| 394 | case SM_HTTP_NOT_FOUND: |
||
| 395 | if(TCPIsPutReady(ph->socket) >= strlenpgm((ROM char*)HTTPMessages[ph->Variable])) |
||
| 396 | { |
||
| 397 | TCPPutROMString(ph->socket, HTTPMessages[ph->Variable]); |
||
| 398 | TCPFlush(ph->socket); |
||
| 399 | TCPDisconnect(ph->socket); |
||
| 400 | ph->smHTTP = SM_HTTP_IDLE; |
||
| 401 | } |
||
| 402 | break; |
||
| 403 | |||
| 404 | case SM_HTTP_HEADER: |
||
| 405 | if ( TCPIsPutReady(ph->socket) ) |
||
| 406 | { |
||
| 407 | lbContinue = TRUE; |
||
| 408 | |||
| 409 | if ( ph->fileType == HTTP_DYNAMIC_FILE_TYPE ) |
||
| 410 | { |
||
| 411 | ph->bProcess = TRUE; |
||
| 412 | TCPPutROMArray(ph->socket, (ROM BYTE*)HTTP_OK_NO_CACHE_STRING, HTTP_OK_NO_CACHE_STRING_LEN); |
||
| 413 | } |
||
| 414 | else |
||
| 415 | { |
||
| 416 | ph->bProcess = FALSE; |
||
| 417 | TCPPutROMArray(ph->socket, (ROM BYTE*)HTTP_OK_STRING, HTTP_OK_STRING_LEN); |
||
| 418 | } |
||
| 419 | |||
| 420 | TCPPutROMString(ph->socket, httpContents[ph->fileType].typeString); |
||
| 421 | TCPPutROMArray(ph->socket, (ROM BYTE*)HTTP_HEADER_END_STRING, HTTP_HEADER_END_STRING_LEN); |
||
| 422 | |||
| 423 | ph->smHTTPGet = SM_HTTP_GET_READ; |
||
| 424 | ph->smHTTP = SM_HTTP_GET; |
||
| 425 | } |
||
| 426 | break; |
||
| 427 | |||
| 428 | case SM_HTTP_GET: |
||
| 429 | // Throw away any more data receieved - we aren't going to use it. |
||
| 430 | TCPDiscard(ph->socket); |
||
| 431 | |||
| 432 | if(SendFile(ph)) |
||
| 433 | { |
||
| 434 | MPFSClose(); |
||
| 435 | TCPDisconnect(ph->socket); |
||
| 436 | ph->smHTTP = SM_HTTP_IDLE; |
||
| 437 | } |
||
| 438 | break; |
||
| 439 | |||
| 440 | default: |
||
| 441 | break; |
||
| 442 | } |
||
| 443 | } while( lbContinue ); |
||
| 444 | } |
||
| 445 | |||
| 446 | |||
| 447 | /********************************************************************* |
||
| 448 | * Function: static BOOL SendFile(HTTP_INFO* ph) |
||
| 449 | * |
||
| 450 | * PreCondition: None |
||
| 451 | * |
||
| 452 | * Input: ph - A HTTP connection info. |
||
| 453 | * |
||
| 454 | * Output: File reference by this connection is served. |
||
| 455 | * |
||
| 456 | * Side Effects: None |
||
| 457 | * |
||
| 458 | * Overview: None |
||
| 459 | * |
||
| 460 | * Note: None. |
||
| 461 | ********************************************************************/ |
||
| 462 | static BOOL SendFile(HTTP_INFO* ph) |
||
| 463 | { |
||
| 464 | BOOL lbTransmit; |
||
| 465 | BYTE c; |
||
| 466 | BYTE buffer[8]; |
||
| 467 | WORD w; |
||
| 468 | WORD_VAL HexNumber; |
||
| 469 | |||
| 470 | MPFSGetBegin(ph->file); |
||
| 471 | |||
| 472 | // Check if file is dynamic (.cgi) -- need to look for and |
||
| 473 | // process escape sequences |
||
| 474 | if(ph->bProcess) |
||
| 475 | { |
||
| 476 | w = TCPIsPutReady(ph->socket); |
||
| 477 | while(w) |
||
| 478 | { |
||
| 479 | lbTransmit = FALSE; |
||
| 480 | |||
| 481 | if(ph->smHTTPGet != (BYTE)SM_HTTP_GET_VAR) |
||
| 482 | { |
||
| 483 | c = MPFSGet(); |
||
| 484 | if(MPFSIsEOF()) |
||
| 485 | { |
||
| 486 | MPFSGetEnd(); |
||
| 487 | TCPFlush(ph->socket); |
||
| 488 | return TRUE; |
||
| 489 | } |
||
| 490 | } |
||
| 491 | |||
| 492 | switch(ph->smHTTPGet) |
||
| 493 | { |
||
| 494 | case SM_HTTP_GET_READ: |
||
| 495 | if ( c == HTTP_VAR_ESC_CHAR ) |
||
| 496 | ph->smHTTPGet = SM_HTTP_GET_DLE; |
||
| 497 | else |
||
| 498 | lbTransmit = TRUE; |
||
| 499 | break; |
||
| 500 | |||
| 501 | case SM_HTTP_GET_DLE: |
||
| 502 | if ( c == HTTP_VAR_ESC_CHAR ) |
||
| 503 | { |
||
| 504 | lbTransmit = TRUE; |
||
| 505 | ph->smHTTPGet = SM_HTTP_GET_READ; |
||
| 506 | } |
||
| 507 | else |
||
| 508 | { |
||
| 509 | HexNumber.v[1] = c; |
||
| 510 | ph->smHTTPGet = SM_HTTP_GET_HANDLE; |
||
| 511 | } |
||
| 512 | break; |
||
| 513 | |||
| 514 | case SM_HTTP_GET_HANDLE: |
||
| 515 | HexNumber.v[0] = c; |
||
| 516 | ph->Variable = hexatob(HexNumber); |
||
| 517 | |||
| 518 | ph->smHTTPGet = SM_HTTP_GET_VAR; |
||
| 519 | ph->VarRef = HTTP_START_OF_VAR; |
||
| 520 | break; |
||
| 521 | |||
| 522 | case SM_HTTP_GET_VAR: |
||
| 523 | ph->VarRef = HTTPGetVar(ph->Variable, ph->VarRef, &c); |
||
| 524 | lbTransmit = TRUE; |
||
| 525 | if ( ph->VarRef == HTTP_END_OF_VAR ) |
||
| 526 | ph->smHTTPGet = SM_HTTP_GET_READ; |
||
| 527 | break; |
||
| 528 | } |
||
| 529 | |||
| 530 | if(lbTransmit) |
||
| 531 | { |
||
| 532 | TCPPut(ph->socket, c); |
||
| 533 | w--; |
||
| 534 | } |
||
| 535 | } |
||
| 536 | } |
||
| 537 | else // Static page content -- no processing required |
||
| 538 | { |
||
| 539 | w = TCPIsPutReady(ph->socket); |
||
| 540 | while(w >= sizeof(buffer)) |
||
| 541 | { |
||
| 542 | for(c = 0; c < sizeof(buffer); c++) |
||
| 543 | { |
||
| 544 | buffer[c] = MPFSGet(); |
||
| 545 | if(MPFSIsEOF()) |
||
| 546 | { |
||
| 547 | MPFSGetEnd(); |
||
| 548 | TCPPutArray(ph->socket, buffer, c); |
||
| 549 | TCPFlush(ph->socket); |
||
| 550 | return TRUE; |
||
| 551 | } |
||
| 552 | } |
||
| 553 | |||
| 554 | TCPPutArray(ph->socket, buffer, sizeof(buffer)); |
||
| 555 | w -= sizeof(buffer); |
||
| 556 | lbTransmit = TRUE; |
||
| 557 | } |
||
| 558 | if(lbTransmit) |
||
| 559 | TCPFlush(ph->socket); |
||
| 560 | } |
||
| 561 | ph->file = MPFSGetEnd(); |
||
| 562 | |||
| 563 | // We are not done sending a file yet... |
||
| 564 | return FALSE; |
||
| 565 | } |
||
| 566 | |||
| 567 | |||
| 568 | /********************************************************************* |
||
| 569 | * Function: static HTTP_COMMAND HTTPParse(BYTE *string, |
||
| 570 | * BYTE** arg, |
||
| 571 | * BYTE* argc, |
||
| 572 | * BYTE* type) |
||
| 573 | * |
||
| 574 | * PreCondition: None |
||
| 575 | * |
||
| 576 | * Input: string - HTTP Command String |
||
| 577 | * arg - List of string pointer to hold |
||
| 578 | * HTTP arguments. |
||
| 579 | * argc - Pointer to hold total number of |
||
| 580 | * arguments in this command string/ |
||
| 581 | * type - Pointer to hold type of file |
||
| 582 | * received. |
||
| 583 | * Valid values are: |
||
| 584 | * HTTP_TXT |
||
| 585 | * HTTP_HTML |
||
| 586 | * HTTP_GIF |
||
| 587 | * HTTP_CGI |
||
| 588 | * HTTP_UNKNOWN |
||
| 589 | * |
||
| 590 | * Output: HTTP FSM and connections are initialized |
||
| 591 | * |
||
| 592 | * Side Effects: None |
||
| 593 | * |
||
| 594 | * Overview: None |
||
| 595 | * |
||
| 596 | * Note: This function parses URL that may or may not |
||
| 597 | * contain arguments. |
||
| 598 | * e.g. "GET HTTP/1.0 thank.htm?name=MCHP&age=12" |
||
| 599 | * would be returned as below: |
||
| 600 | * arg[0] => GET |
||
| 601 | * arg[1] => thank.htm |
||
| 602 | * arg[2] => name |
||
| 603 | * arg[3] => MCHP |
||
| 604 | * arg[4] => 12 |
||
| 605 | * argc = 5 |
||
| 606 | * |
||
| 607 | * This parses does not "de-escape" URL string. |
||
| 608 | ********************************************************************/ |
||
| 609 | static HTTP_COMMAND HTTPParse(BYTE *string, |
||
| 610 | BYTE** arg, |
||
| 611 | BYTE* argc, |
||
| 612 | BYTE* type) |
||
| 613 | { |
||
| 614 | BYTE i; |
||
| 615 | BYTE smParse; |
||
| 616 | HTTP_COMMAND cmd; |
||
| 617 | BYTE *ext; |
||
| 618 | BYTE c; |
||
| 619 | ROM BYTE *fileType; |
||
| 620 | |||
| 621 | enum |
||
| 622 | { |
||
| 623 | SM_PARSE_IDLE, |
||
| 624 | SM_PARSE_ARG, |
||
| 625 | SM_PARSE_ARG_FORMAT |
||
| 626 | }; |
||
| 627 | |||
| 628 | smParse = SM_PARSE_IDLE; |
||
| 629 | ext = NULL; |
||
| 630 | i = 0; |
||
| 631 | |||
| 632 | // Only "GET" is supported for time being. |
||
| 633 | if ( !memcmppgm2ram(string, (ROM void*) HTTP_GET_STRING, HTTP_GET_STRING_LEN) ) |
||
| 634 | { |
||
| 635 | string += (HTTP_GET_STRING_LEN + 1); |
||
| 636 | cmd = HTTP_GET; |
||
| 637 | } |
||
| 638 | else |
||
| 639 | { |
||
| 640 | return HTTP_NOT_SUPPORTED; |
||
| 641 | } |
||
| 642 | |||
| 643 | // Skip white spaces. |
||
| 644 | while( *string == ' ' ) |
||
| 645 | string++; |
||
| 646 | |||
| 647 | c = *string; |
||
| 648 | |||
| 649 | while ( c != ' ' && c != '\0' && c != '\r' && c != '\n' ) |
||
| 650 | |||
| 651 | { |
||
| 652 | // Do not accept any more arguments than we haved designed to. |
||
| 653 | if ( i >= *argc ) |
||
| 654 | break; |
||
| 655 | |||
| 656 | switch(smParse) |
||
| 657 | { |
||
| 658 | case SM_PARSE_IDLE: |
||
| 659 | arg[i] = string; |
||
| 660 | c = *string; |
||
| 661 | if ( c == '/' || c == '\\' ) |
||
| 662 | smParse = SM_PARSE_ARG; |
||
| 663 | break; |
||
| 664 | |||
| 665 | case SM_PARSE_ARG: |
||
| 666 | arg[i++] = string; |
||
| 667 | smParse = SM_PARSE_ARG_FORMAT; |
||
| 668 | /* |
||
| 669 | * Do not break. |
||
| 670 | * Parameter may be empty. |
||
| 671 | */ |
||
| 672 | |||
| 673 | case SM_PARSE_ARG_FORMAT: |
||
| 674 | c = *string; |
||
| 675 | if ( c == '?' || c == '&' ) |
||
| 676 | { |
||
| 677 | *string = '\0'; |
||
| 678 | smParse = SM_PARSE_ARG; |
||
| 679 | } |
||
| 680 | else |
||
| 681 | { |
||
| 682 | // Recover space characters. |
||
| 683 | if ( c == '+' ) |
||
| 684 | *string = ' '; |
||
| 685 | |||
| 686 | // Remember where file extension starts. |
||
| 687 | else if ( c == '.' && i == 1u ) |
||
| 688 | { |
||
| 689 | ext = ++string; |
||
| 690 | } |
||
| 691 | |||
| 692 | else if ( c == '=' ) |
||
| 693 | { |
||
| 694 | *string = '\0'; |
||
| 695 | smParse = SM_PARSE_ARG; |
||
| 696 | } |
||
| 697 | |||
| 698 | // Only interested in file name - not a path. |
||
| 699 | else if ( c == '/' || c == '\\' ) |
||
| 700 | arg[i-1] = string+1; |
||
| 701 | |||
| 702 | } |
||
| 703 | break; |
||
| 704 | } |
||
| 705 | string++; |
||
| 706 | c = *string; |
||
| 707 | } |
||
| 708 | *string = '\0'; |
||
| 709 | |||
| 710 | *type = HTTP_UNKNOWN; |
||
| 711 | if ( ext != NULL ) |
||
| 712 | { |
||
| 713 | ext = (BYTE*)strupr((char*)ext); |
||
| 714 | |||
| 715 | fileType = httpFiles[0].fileExt; |
||
| 716 | for ( c = 0; c < TOTAL_FILE_TYPES; c++ ) |
||
| 717 | { |
||
| 718 | if ( !memcmppgm2ram((void*)ext, (ROM void*)fileType, FILE_EXT_LEN) ) |
||
| 719 | { |
||
| 720 | *type = c; |
||
| 721 | break; |
||
| 722 | } |
||
| 723 | fileType += sizeof(FILE_TYPES); |
||
| 724 | |||
| 725 | } |
||
| 726 | } |
||
| 727 | |||
| 728 | if ( i == 0u ) |
||
| 729 | { |
||
| 730 | memcpypgm2ram(arg[0], (ROM void*)HTTP_DEFAULT_FILE_STRING, |
||
| 731 | HTTP_DEFAULT_FILE_STRING_LEN); |
||
| 732 | arg[0][HTTP_DEFAULT_FILE_STRING_LEN] = '\0'; |
||
| 733 | *type = HTTP_HTML; |
||
| 734 | i++; |
||
| 735 | } |
||
| 736 | *argc = i; |
||
| 737 | |||
| 738 | return cmd; |
||
| 739 | } |
||
| 740 | |||
| 741 | |||
| 742 | #endif //#if defined(STACK_USE_HTTP_SERVER) |
Powered by WebSVN v2.8.3