/
httpServer.c
749 lines (641 loc) · 21.2 KB
/
httpServer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "socket.h"
#include "wizchip_conf.h"
#include "httpServer.h"
#include "httpParser.h"
#include "httpUtil.h"
#ifdef _USE_SDCARD_
#include "ff.h" // header file for FatFs library (FAT file system)
#endif
#ifndef DATA_BUF_SIZE
#define DATA_BUF_SIZE 2048
#endif
/*****************************************************************************
* Private types/enumerations/variables
****************************************************************************/
static uint8_t HTTPSock_Num[_WIZCHIP_SOCK_NUM_] = {0, };
static st_http_request * http_request; /**< Pointer to received HTTP request */
static st_http_request * parsed_http_request; /**< Pointer to parsed HTTP request */
static uint8_t * http_response; /**< Pointer to HTTP response */
// ## For Debugging
//static uint8_t uri_buf[128];
// Number of registered web content in code flash memory
static uint16_t total_content_cnt = 0;
/*****************************************************************************
* Public types/enumerations/variables
****************************************************************************/
uint8_t * pHTTP_TX;
uint8_t * pHTTP_RX;
volatile uint32_t httpServer_tick_1s = 0;
st_http_socket HTTPSock_Status[_WIZCHIP_SOCK_NUM_] = { {STATE_HTTP_IDLE, }, };
httpServer_webContent web_content[MAX_CONTENT_CALLBACK];
#ifdef _USE_SDCARD_
FIL fs; // FatFs: File object
FRESULT fr; // FatFs: File function return code
#endif
/*****************************************************************************
* Private functions
****************************************************************************/
void httpServer_Sockinit(uint8_t cnt, uint8_t * socklist);
static uint8_t getHTTPSocketNum(uint8_t seqnum);
static int8_t getHTTPSequenceNum(uint8_t socket);
static int8_t http_disconnect(uint8_t sn);
static void http_process_handler(uint8_t s, st_http_request * p_http_request);
static void send_http_response_header(uint8_t s, uint8_t content_type, uint32_t body_len, uint16_t http_status);
static void send_http_response_body(uint8_t s, uint8_t * uri_name, uint8_t * buf, uint32_t start_addr, uint32_t file_len);
static void send_http_response_cgi(uint8_t s, uint8_t * buf, uint8_t * http_body, uint16_t file_len);
/*****************************************************************************
* Public functions
****************************************************************************/
// Callback functions definition: MCU Reset / WDT Reset
void default_mcu_reset(void) {;}
void default_wdt_reset(void) {;}
void (*HTTPServer_ReStart)(void) = default_mcu_reset;
void (*HTTPServer_WDT_Reset)(void) = default_wdt_reset;
void httpServer_Sockinit(uint8_t cnt, uint8_t * socklist)
{
uint8_t i;
for(i = 0; i < cnt; i++)
{
// Mapping the H/W socket numbers to the sequential index numbers
HTTPSock_Num[i] = socklist[i];
}
}
static uint8_t getHTTPSocketNum(uint8_t seqnum)
{
// Return the 'H/W socket number' corresponding to the index number
return HTTPSock_Num[seqnum];
}
static int8_t getHTTPSequenceNum(uint8_t socket)
{
uint8_t i;
for(i = 0; i < _WIZCHIP_SOCK_NUM_; i++)
if(HTTPSock_Num[i] == socket) return i;
return -1;
}
void httpServer_init(uint8_t * tx_buf, uint8_t * rx_buf, uint8_t cnt, uint8_t * socklist)
{
// User's shared buffer
pHTTP_TX = tx_buf;
pHTTP_RX = rx_buf;
// H/W Socket number mapping
httpServer_Sockinit(cnt, socklist);
}
/* Register the call back functions for HTTP Server */
void reg_httpServer_cbfunc(void(*mcu_reset)(void), void(*wdt_reset)(void))
{
// Callback: HW Reset and WDT reset function for each MCU platforms
if(mcu_reset) HTTPServer_ReStart = mcu_reset;
if(wdt_reset) HTTPServer_WDT_Reset = wdt_reset;
}
void httpServer_run(uint8_t seqnum)
{
uint8_t s; // socket number
uint16_t len;
uint32_t gettime = 0;
#ifdef _HTTPSERVER_DEBUG_
uint8_t destip[4] = {0, };
uint16_t destport = 0;
#endif
http_request = (st_http_request *)pHTTP_RX; // Structure of HTTP Request
parsed_http_request = (st_http_request *)pHTTP_TX;
// Get the H/W socket number
s = getHTTPSocketNum(seqnum);
/* HTTP Service Start */
switch(getSn_SR(s))
{
case SOCK_ESTABLISHED:
// Interrupt clear
if(getSn_IR(s) & Sn_IR_CON)
{
setSn_IR(s, Sn_IR_CON);
}
// HTTP Process states
switch(HTTPSock_Status[seqnum].sock_status)
{
case STATE_HTTP_IDLE :
if ((len = getSn_RX_RSR(s)) > 0)
{
if (len > DATA_BUF_SIZE) len = DATA_BUF_SIZE;
len = recv(s, (uint8_t *)http_request, len);
*(((uint8_t *)http_request) + len) = '\0';
parse_http_request(parsed_http_request, (uint8_t *)http_request);
#ifdef _HTTPSERVER_DEBUG_
getSn_DIPR(s, destip);
destport = getSn_DPORT(s);
printf("\r\n");
printf("> HTTPSocket[%d] : HTTP Request received ", s);
printf("from %d.%d.%d.%d : %d\r\n", destip[0], destip[1], destip[2], destip[3], destport);
#endif
#ifdef _HTTPSERVER_DEBUG_
printf("> HTTPSocket[%d] : [State] STATE_HTTP_REQ_DONE\r\n", s);
#endif
// HTTP 'response' handler; includes send_http_response_header / body function
http_process_handler(s, parsed_http_request);
gettime = get_httpServer_timecount();
// Check the TX socket buffer for End of HTTP response sends
while(getSn_TX_FSR(s) != (getSn_TXBUF_SIZE(s)*1024))
{
if((get_httpServer_timecount() - gettime) > 3)
{
#ifdef _HTTPSERVER_DEBUG_
printf("> HTTPSocket[%d] : [State] STATE_HTTP_REQ_DONE: TX Buffer clear timeout\r\n", s);
#endif
break;
}
}
if(HTTPSock_Status[seqnum].file_len > 0) HTTPSock_Status[seqnum].sock_status = STATE_HTTP_RES_INPROC;
else HTTPSock_Status[seqnum].sock_status = STATE_HTTP_RES_DONE; // Send the 'HTTP response' end
}
break;
case STATE_HTTP_RES_INPROC :
/* Repeat: Send the remain parts of HTTP responses */
#ifdef _HTTPSERVER_DEBUG_
printf("> HTTPSocket[%d] : [State] STATE_HTTP_RES_INPROC\r\n", s);
#endif
// Repeatedly send remaining data to client
send_http_response_body(s, 0, http_response, 0, 0);
if(HTTPSock_Status[seqnum].file_len == 0) HTTPSock_Status[seqnum].sock_status = STATE_HTTP_RES_DONE;
break;
case STATE_HTTP_RES_DONE :
#ifdef _HTTPSERVER_DEBUG_
printf("> HTTPSocket[%d] : [State] STATE_HTTP_RES_DONE\r\n", s);
#endif
// Socket file info structure re-initialize
HTTPSock_Status[seqnum].file_len = 0;
HTTPSock_Status[seqnum].file_offset = 0;
HTTPSock_Status[seqnum].file_start = 0;
HTTPSock_Status[seqnum].sock_status = STATE_HTTP_IDLE;
//#ifdef _USE_SDCARD_
// f_close(&fs);
//#endif
#ifdef _USE_WATCHDOG_
HTTPServer_WDT_Reset();
#endif
http_disconnect(s);
break;
default :
break;
}
break;
case SOCK_CLOSE_WAIT:
#ifdef _HTTPSERVER_DEBUG_
printf("> HTTPSocket[%d] : ClOSE_WAIT\r\n", s); // if a peer requests to close the current connection
#endif
disconnect(s);
break;
case SOCK_CLOSED:
#ifdef _HTTPSERVER_DEBUG_
printf("> HTTPSocket[%d] : CLOSED\r\n", s);
#endif
if(socket(s, Sn_MR_TCP, HTTP_SERVER_PORT, 0x00) == s) /* Reinitialize the socket */
{
#ifdef _HTTPSERVER_DEBUG_
printf("> HTTPSocket[%d] : OPEN\r\n", s);
#endif
}
break;
case SOCK_INIT:
listen(s);
break;
case SOCK_LISTEN:
break;
default :
break;
} // end of switch
#ifdef _USE_WATCHDOG_
HTTPServer_WDT_Reset();
#endif
}
////////////////////////////////////////////
// Private Functions
////////////////////////////////////////////
static void send_http_response_header(uint8_t s, uint8_t content_type, uint32_t body_len, uint16_t http_status)
{
switch(http_status)
{
case STATUS_OK: // HTTP/1.1 200 OK
if((content_type != PTYPE_CGI) && (content_type != PTYPE_XML)) // CGI/XML type request does not respond HTTP header
{
#ifdef _HTTPSERVER_DEBUG_
printf("> HTTPSocket[%d] : HTTP Response Header - STATUS_OK\r\n", s);
#endif
make_http_response_head((char*)http_response, content_type, body_len);
}
else
{
#ifdef _HTTPSERVER_DEBUG_
printf("> HTTPSocket[%d] : HTTP Response Header - NONE / CGI or XML\r\n", s);
#endif
// CGI/XML type request does not respond HTTP header to client
http_status = 0;
}
break;
case STATUS_BAD_REQ: // HTTP/1.1 400 OK
#ifdef _HTTPSERVER_DEBUG_
printf("> HTTPSocket[%d] : HTTP Response Header - STATUS_BAD_REQ\r\n", s);
#endif
memcpy(http_response, ERROR_REQUEST_PAGE, sizeof(ERROR_REQUEST_PAGE));
break;
case STATUS_NOT_FOUND: // HTTP/1.1 404 Not Found
#ifdef _HTTPSERVER_DEBUG_
printf("> HTTPSocket[%d] : HTTP Response Header - STATUS_NOT_FOUND\r\n", s);
#endif
memcpy(http_response, ERROR_HTML_PAGE, sizeof(ERROR_HTML_PAGE));
break;
default:
break;
}
// Send the HTTP Response 'header'
if(http_status)
{
#ifdef _HTTPSERVER_DEBUG_
printf("> HTTPSocket[%d] : [Send] HTTP Response Header [ %d ]byte\r\n", s, (uint16_t)strlen((char *)http_response));
#endif
send(s, http_response, strlen((char *)http_response));
}
}
static void send_http_response_body(uint8_t s, uint8_t * uri_name, uint8_t * buf, uint32_t start_addr, uint32_t file_len)
{
int8_t get_seqnum;
uint32_t send_len;
uint8_t flag_datasend_end = 0;
#ifdef _USE_SDCARD_
uint16_t blocklen;
#endif
#ifdef _USE_FLASH_
uint32_t addr = 0;
#endif
if((get_seqnum = getHTTPSequenceNum(s)) == -1) return; // exception handling; invalid number
// Send the HTTP Response 'body'; requested file
if(!HTTPSock_Status[get_seqnum].file_len) // ### Send HTTP response body: First part ###
{
if (file_len > DATA_BUF_SIZE - 1)
{
HTTPSock_Status[get_seqnum].file_start = start_addr;
HTTPSock_Status[get_seqnum].file_len = file_len;
send_len = DATA_BUF_SIZE - 1;
/////////////////////////////////////////////////////////////////////////////////////////////////
// ## 20141219 Eric added, for 'File object structure' (fs) allocation reduced (8 -> 1)
memset(HTTPSock_Status[get_seqnum].file_name, 0x00, MAX_CONTENT_NAME_LEN);
strcpy((char *)HTTPSock_Status[get_seqnum].file_name, (char *)uri_name);
#ifdef _HTTPSERVER_DEBUG_
printf("> HTTPSocket[%d] : HTTP Response body - file name [ %s ]\r\n", s, HTTPSock_Status[get_seqnum].file_name);
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////
#ifdef _HTTPSERVER_DEBUG_
printf("> HTTPSocket[%d] : HTTP Response body - file len [ %ld ]byte\r\n", s, file_len);
#endif
}
else
{
// Send process end
send_len = file_len;
#ifdef _HTTPSERVER_DEBUG_
printf("> HTTPSocket[%d] : HTTP Response end - file len [ %ld ]byte\r\n", s, send_len);
#endif
}
#ifdef _USE_FLASH_
if(HTTPSock_Status[get_seqnum]->storage_type == DATAFLASH) addr = start_addr;
#endif
}
else // remained parts
{
#ifdef _USE_FLASH_
if(HTTPSock_Status[get_seqnum]->storage_type == DATAFLASH)
{
addr = HTTPSock_Status[get_seqnum].file_start + HTTPSock_Status[get_seqnum].file_offset;
}
#endif
send_len = HTTPSock_Status[get_seqnum].file_len - HTTPSock_Status[get_seqnum].file_offset;
if(send_len > DATA_BUF_SIZE - 1)
{
send_len = DATA_BUF_SIZE - 1;
//HTTPSock_Status[get_seqnum]->file_offset += send_len;
}
else
{
#ifdef _HTTPSERVER_DEBUG_
printf("> HTTPSocket[%d] : HTTP Response end - file len [ %ld ]byte\r\n", s, HTTPSock_Status[get_seqnum].file_len);
#endif
// Send process end
flag_datasend_end = 1;
}
#ifdef _HTTPSERVER_DEBUG_
printf("> HTTPSocket[%d] : HTTP Response body - send len [ %ld ]byte\r\n", s, send_len);
#endif
}
/*****************************************************/
//HTTPSock_Status[get_seqnum]->storage_type == NONE
//HTTPSock_Status[get_seqnum]->storage_type == CODEFLASH
//HTTPSock_Status[get_seqnum]->storage_type == SDCARD
//HTTPSock_Status[get_seqnum]->storage_type == DATAFLASH
/*****************************************************/
if(HTTPSock_Status[get_seqnum].storage_type == CODEFLASH)
{
if(HTTPSock_Status[get_seqnum].file_len) start_addr = HTTPSock_Status[get_seqnum].file_start;
read_userReg_webContent(start_addr, &buf[0], HTTPSock_Status[get_seqnum].file_offset, send_len);
}
#ifdef _USE_SDCARD_
else if(HTTPSock_Status[get_seqnum].storage_type == SDCARD)
{
// Data read from SD Card
fr = f_read(&fs, &buf[0], send_len, (void *)&blocklen);
if(fr != FR_OK)
{
send_len = 0;
#ifdef _HTTPSERVER_DEBUG_
printf("> HTTPSocket[%d] : [FatFs] Error code return: %d (File Read) / HTTP Send Failed - %s\r\n", s, fr, HTTPSock_Status[get_seqnum].file_name);
#endif
}
else
{
*(buf+send_len+1) = 0; // Insert '/0' for indicates the 'End of String' (null terminated)
}
}
#endif
#ifdef _USE_FLASH_
else if(HTTPSock_Status[get_seqnum]->storage_type == DATAFLASH)
{
// Data read from external data flash memory
read_from_flashbuf(addr, &buf[0], send_len);
*(buf+send_len+1) = 0; // Insert '/0' for indicates the 'End of String' (null terminated)
}
#endif
else
{
send_len = 0;
}
// Requested content send to HTTP client
#ifdef _HTTPSERVER_DEBUG_
printf("> HTTPSocket[%d] : [Send] HTTP Response body [ %ld ]byte\r\n", s, send_len);
#endif
if(send_len) send(s, buf, send_len);
else flag_datasend_end = 1;
if(flag_datasend_end)
{
HTTPSock_Status[get_seqnum].file_start = 0;
HTTPSock_Status[get_seqnum].file_len = 0;
HTTPSock_Status[get_seqnum].file_offset = 0;
flag_datasend_end = 0;
}
else
{
HTTPSock_Status[get_seqnum].file_offset += send_len;
#ifdef _HTTPSERVER_DEBUG_
printf("> HTTPSocket[%d] : HTTP Response body - offset [ %ld ]\r\n", s, HTTPSock_Status[get_seqnum].file_offset);
#endif
}
// ## 20141219 Eric added, for 'File object structure' (fs) allocation reduced (8 -> 1)
#ifdef _USE_SDCARD_
f_close(&fs);
#endif
// ## 20141219 added end
}
static void send_http_response_cgi(uint8_t s, uint8_t * buf, uint8_t * http_body, uint16_t file_len)
{
uint16_t send_len = 0;
#ifdef _HTTPSERVER_DEBUG_
printf("> HTTPSocket[%d] : HTTP Response Header + Body - CGI\r\n", s);
#endif
send_len = sprintf((char *)buf, "%s%d\r\n\r\n%s", RES_CGIHEAD_OK, file_len, http_body);
#ifdef _HTTPSERVER_DEBUG_
printf("> HTTPSocket[%d] : HTTP Response Header + Body - send len [ %d ]byte\r\n", s, send_len);
#endif
send(s, buf, send_len);
}
static int8_t http_disconnect(uint8_t sn)
{
setSn_CR(sn,Sn_CR_DISCON);
/* wait to process the command... */
while(getSn_CR(sn));
return SOCK_OK;
}
static void http_process_handler(uint8_t s, st_http_request * p_http_request)
{
uint8_t * uri_name;
uint32_t content_addr = 0;
uint16_t content_num = 0;
uint32_t file_len = 0;
uint8_t uri_buf[MAX_URI_SIZE]={0x00, };
uint16_t http_status;
int8_t get_seqnum;
uint8_t content_found;
if((get_seqnum = getHTTPSequenceNum(s)) == -1) return; // exception handling; invalid number
http_status = 0;
http_response = pHTTP_RX;
file_len = 0;
//method Analyze
switch (p_http_request->METHOD)
{
case METHOD_ERR :
http_status = STATUS_BAD_REQ;
send_http_response_header(s, 0, 0, http_status);
break;
case METHOD_HEAD :
case METHOD_GET :
get_http_uri_name(p_http_request->URI, uri_buf);
uri_name = uri_buf;
if (!strcmp((char *)uri_name, "/")) strcpy((char *)uri_name, INITIAL_WEBPAGE); // If URI is "/", respond by index.html
if (!strcmp((char *)uri_name, "m")) strcpy((char *)uri_name, M_INITIAL_WEBPAGE);
if (!strcmp((char *)uri_name, "mobile")) strcpy((char *)uri_name, MOBILE_INITIAL_WEBPAGE);
find_http_uri_type(&p_http_request->TYPE, uri_name); // Checking requested file types (HTML, TEXT, GIF, JPEG and Etc. are included)
#ifdef _HTTPSERVER_DEBUG_
printf("\r\n> HTTPSocket[%d] : HTTP Method GET\r\n", s);
printf("> HTTPSocket[%d] : Request Type = %d\r\n", s, p_http_request->TYPE);
printf("> HTTPSocket[%d] : Request URI = %s\r\n", s, uri_name);
#endif
if(p_http_request->TYPE == PTYPE_CGI)
{
content_found = http_get_cgi_handler(uri_name, pHTTP_TX, &file_len);
if(content_found && (file_len <= (DATA_BUF_SIZE-(strlen(RES_CGIHEAD_OK)+8))))
{
send_http_response_cgi(s, http_response, pHTTP_TX, (uint16_t)file_len);
}
else
{
send_http_response_header(s, PTYPE_CGI, 0, STATUS_NOT_FOUND);
}
}
else
{
// Find the User registered index for web content
if(find_userReg_webContent(uri_buf, &content_num, &file_len))
{
content_found = 1; // Web content found in code flash memory
content_addr = (uint32_t)content_num;
HTTPSock_Status[get_seqnum].storage_type = CODEFLASH;
}
// Not CGI request, Web content in 'SD card' or 'Data flash' requested
#ifdef _USE_SDCARD_
#ifdef _HTTPSERVER_DEBUG_
printf("\r\n> HTTPSocket[%d] : Searching the requested content\r\n", s);
#endif
if((fr = f_open(&fs, (const char *)uri_name, FA_READ)) == 0)
{
content_found = 1; // file open succeed
file_len = fs.fsize;
content_addr = fs.sclust;
HTTPSock_Status[get_seqnum].storage_type = SDCARD;
}
#elif _USE_FLASH_
else if(/* Read content from Dataflash */)
{
content_found = 1;
HTTPSock_Status[get_seqnum]->storage_type = DATAFLASH;
; // To do
}
#endif
else
{
content_found = 0; // fail to find content
}
if(!content_found)
{
#ifdef _HTTPSERVER_DEBUG_
printf("> HTTPSocket[%d] : Unknown Page Request\r\n", s);
#endif
http_status = STATUS_NOT_FOUND;
}
else
{
#ifdef _HTTPSERVER_DEBUG_
printf("> HTTPSocket[%d] : Find Content [%s] ok - Start [%ld] len [ %ld ]byte\r\n", s, uri_name, content_addr, file_len);
#endif
http_status = STATUS_OK;
}
// Send HTTP header
if(http_status)
{
#ifdef _HTTPSERVER_DEBUG_
printf("> HTTPSocket[%d] : Requested content len = [ %ld ]byte\r\n", s, file_len);
#endif
send_http_response_header(s, p_http_request->TYPE, file_len, http_status);
}
// Send HTTP body (content)
if(http_status == STATUS_OK)
{
send_http_response_body(s, uri_name, http_response, content_addr, file_len);
}
}
break;
case METHOD_POST :
mid((char *)p_http_request->URI, "/", " HTTP", (char *)uri_buf);
uri_name = uri_buf;
find_http_uri_type(&p_http_request->TYPE, uri_name); // Check file type (HTML, TEXT, GIF, JPEG are included)
#ifdef _HTTPSERVER_DEBUG_
printf("\r\n> HTTPSocket[%d] : HTTP Method POST\r\n", s);
printf("> HTTPSocket[%d] : Request URI = %s ", s, uri_name);
printf("Type = %d\r\n", p_http_request->TYPE);
#endif
if(p_http_request->TYPE == PTYPE_CGI) // HTTP POST Method; CGI Process
{
content_found = http_post_cgi_handler(uri_name, p_http_request, http_response, &file_len);
#ifdef _HTTPSERVER_DEBUG_
printf("> HTTPSocket[%d] : [CGI: %s] / Response len [ %ld ]byte\r\n", s, content_found?"Content found":"Content not found", file_len);
#endif
if(content_found && (file_len <= (DATA_BUF_SIZE-(strlen(RES_CGIHEAD_OK)+8))))
{
send_http_response_cgi(s, pHTTP_TX, http_response, (uint16_t)file_len);
// Reset the H/W for apply to the change configuration information
if(content_found == HTTP_RESET) HTTPServer_ReStart();
}
else
{
send_http_response_header(s, PTYPE_CGI, 0, STATUS_NOT_FOUND);
}
}
else // HTTP POST Method; Content not found
{
send_http_response_header(s, 0, 0, STATUS_NOT_FOUND);
}
break;
default :
http_status = STATUS_BAD_REQ;
send_http_response_header(s, 0, 0, http_status);
break;
}
}
void httpServer_time_handler(void)
{
httpServer_tick_1s++;
}
uint32_t get_httpServer_timecount(void)
{
return httpServer_tick_1s;
}
void reg_httpServer_webContent(uint8_t * content_name, uint8_t * content)
{
uint16_t name_len;
uint32_t content_len;
if(content_name == NULL || content == NULL)
{
return;
}
else if(total_content_cnt >= MAX_CONTENT_CALLBACK)
{
return;
}
name_len = strlen((char *)content_name);
content_len = strlen((char *)content);
web_content[total_content_cnt].content_name = malloc(name_len+1);
strcpy((char *)web_content[total_content_cnt].content_name, (const char *)content_name);
web_content[total_content_cnt].content_len = content_len;
web_content[total_content_cnt].content = content;
total_content_cnt++;
}
uint8_t display_reg_webContent_list(void)
{
uint16_t i;
uint8_t ret;
if(total_content_cnt == 0)
{
printf(">> Web content file not found\r\n");
ret = 0;
}
else
{
printf("\r\n=== List of Web content in code flash ===\r\n");
for(i = 0; i < total_content_cnt; i++)
{
printf(" [%d] ", i+1);
printf("%s, ", web_content[i].content_name);
printf("%ld byte, ", web_content[i].content_len);
if(web_content[i].content_len < 30) printf("[%s]\r\n", web_content[i].content);
else printf("[ ... ]\r\n");
}
printf("=========================================\r\n\r\n");
ret = 1;
}
return ret;
}
uint8_t find_userReg_webContent(uint8_t * content_name, uint16_t * content_num, uint32_t * file_len)
{
uint16_t i;
uint8_t ret = 0; // '0' means 'File Not Found'
for(i = 0; i < total_content_cnt; i++)
{
if(!strcmp((char *)content_name, (char *)web_content[i].content_name))
{
*file_len = web_content[i].content_len;
*content_num = i;
ret = 1; // If the requested content found, ret set to '1' (Found)
break;
}
}
return ret;
}
uint16_t read_userReg_webContent(uint16_t content_num, uint8_t * buf, uint32_t offset, uint16_t size)
{
uint16_t ret = 0;
uint8_t * ptr;
if(content_num > total_content_cnt) return 0;
ptr = web_content[content_num].content;
if(offset) ptr += offset;
strncpy((char *)buf, (char *)ptr, size);
*(buf+size) = 0; // Insert '/0' for indicates the 'End of String' (null terminated)
ret = strlen((void *)buf);
return ret;
}