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 printf("file len : %d\r\n", file_len); } #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; }
static void http_process_handler(uint8_t s, st_http_request * p_http_request) { uint8_t * uri_name; uint32_t content_addr = 0; uint32_t file_len = 0; uint8_t post_name[32]= {0x00,}; // POST method request file name #ifdef _WILL_BE_IM_ uint32_t content_len = 0; uint16_t post_len = 0; // POST uint8_t sub[10]; // POST #endif 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; // HTTP 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 : uri_name = get_http_uri_name(p_http_request->URI); 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 <= (2048-(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 { // If No CGI request, Try to find The requested web content in storage (e.g., 'SD card' or 'Data flash') #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; } else { #ifdef _HTTPSERVER_DEBUG_ printf("> HTTPSocket[%d] : [FatFs] Error code return: %d\r\n", s, fr); #endif content_found = 0; // file open failed } #endif 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 *)post_name); uri_name = post_name; 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, post_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(post_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 <= (2048-(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; } }
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 }
/* HTTP Server Run */ void httpServer_run(uint16_t server_port) { int8_t sock; // HW socket number uint8_t sock_status; // HW socket status int8_t seqnum; // Sequence number int16_t len; #ifdef _HTTPSERVER_DEBUG_ uint8_t destip[4] = {0, }; // Destination IP address uint16_t destport = 0; // Destination Port number #endif sock = getAvailableHTTPSocketNum(); // Get the H/W socket number if(sock < 0) return; // HW socket allocation failed seqnum = getHTTPSequenceNum(sock); http_request = (st_http_request *)httpserver.recvbuf; // HTTP Request Structure parsed_http_request = (st_http_request *)http_req; //parsed_http_request = (st_http_request *)httpserver.sendbuf; // old /* Web Service Start */ sock_status = getSn_SR(sock); switch(sock_status) { case SOCK_ESTABLISHED: // Interrupt clear if(getSn_IR(sock) & Sn_IR_CON) { setSn_IR(sock, Sn_IR_CON); } // HTTP Process states switch(HTTPSock[seqnum].status) { case STATE_HTTP_IDLE : if ((len = getSn_RX_RSR(sock)) > 0) { if (len > DATA_BUF_SIZE) len = DATA_BUF_SIZE; if ((len = recv(sock, (uint8_t *)http_request, len)) < 0) break; // Exception handler *(((uint8_t *)http_request) + len) = '\0'; // End of string (EOS) marker parse_http_request(parsed_http_request, (uint8_t *)http_request); #ifdef _HTTPSERVER_DEBUG_ printf("> HTTP Request START ==========\r\n"); printf("%s", (uint8_t *)http_request); printf("> HTTP Request END ============\r\n"); #endif #ifdef _HTTPSERVER_DEBUG_ getSn_DIPR(sock, destip); destport = getSn_DPORT(sock); printf("\r\n"); printf("> HTTPSocket[%d] : HTTP Request received ", sock); 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", sock); #endif // HTTP 'response' handler; includes send_http_response_header / body function http_process_handler(sock, parsed_http_request); if(HTTPSock[seqnum].file_len > 0) HTTPSock[seqnum].status = STATE_HTTP_RES_INPROC; else HTTPSock[seqnum].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", sock); #endif // Repeatedly send remaining data to client send_http_response_body(sock, http_response, 0); if(HTTPSock[seqnum].file_len == 0) HTTPSock[seqnum].status = STATE_HTTP_RES_DONE; break; case STATE_HTTP_RES_DONE : #ifdef _HTTPSERVER_DEBUG_ printf("> HTTPSocket[%d] : [State] STATE_HTTP_RES_DONE\r\n", sock); #endif // Socket file info structure re-initialize HTTPSock[seqnum].file_len = 0; HTTPSock[seqnum].file_offset = 0; HTTPSock[seqnum].file_start = 0; HTTPSock[seqnum].status = STATE_HTTP_IDLE; #ifdef _USE_WATCHDOG_ HTTPServer_WDT_Reset(); #endif http_disconnect(sock); break; default : break; } break; case SOCK_CLOSE_WAIT: #ifdef _HTTPSERVER_DEBUG_ printf("> HTTPSocket[%d] : ClOSE_WAIT\r\n", sock); // if a peer requests to close the current connection #endif // Socket file info structure re-initialize: HTTP connection 'close' HTTPSock[seqnum].file_len = 0; HTTPSock[seqnum].file_offset = 0; HTTPSock[seqnum].file_start = 0; HTTPSock[seqnum].status = STATE_HTTP_IDLE; http_disconnect(sock); break; case SOCK_INIT: listen(sock); break; case SOCK_LISTEN: break; case SOCK_SYNSENT: //case SOCK_SYNSENT_M: case SOCK_SYNRECV: //case SOCK_SYNRECV_M: break; case SOCK_CLOSED: #ifdef _HTTPSERVER_DEBUG_ //printf("> HTTPSocket[%d] : CLOSED\r\n", sock); #endif if(server_port == 0) server_port = HTTP_SERVER_PORT; if(socket(sock, Sn_MR_TCP, server_port, 0x00) == sock) // Init / Reinitialize the socket { #ifdef _HTTPSERVER_DEBUG_ printf("> HTTPSocket[%d] : SERVER OPEN, Port: %d\r\n", sock, server_port); #endif ; } break; default : break; } // end of switch #ifdef _USE_WATCHDOG_ HTTPServer_WDT_Reset(); #endif }