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; } }