void HTTPProcessor(uint8 s, char * buf) { uint8* http_response; st_http_request *http_request; http_response = (uint8*)rx_buf; http_request = (st_http_request*)tx_buf; memset(tx_buf,0x00,MAX_URI_SIZE); parse_http_request(http_request, buf); // After analyze request, convert into http_request memset(rx_buf,0x00,MAX_URI_SIZE); //method Analyze switch (http_request->METHOD) { case METHOD_ERR : memcpy(http_response, ERROR_REQUEST_PAGE, sizeof(ERROR_REQUEST_PAGE)); TCPSend(s, (int8 *)http_response, strlen((char*)http_response)); break; case METHOD_HEAD: case METHOD_GET: case METHOD_POST: if (!strcmp((char*)http_request->URI, "/")) strcpy(http_request->URI, (char const*)homepage_default); // If URI is "/", respond by index.htm RESTProcessor(http_request); //get http type from type find_http_uri_type(&http_request->TYPE, http_request->URI); //Check file type (HTML, TEXT, GIF, JPEG are included) if(http_request->TYPE == PTYPE_PL || http_request->TYPE == PTYPE_CGI) { CGIProcessor(http_request, (char*)http_response); } FILESend(s, http_request, (char*)http_response); break; default: break; } }
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 http_process_handler(uint8_t sock, st_http_request * p_http_request) { uint8_t * uri_name; uint8_t uri_buf[MAX_URI_SIZE]={0x00, }; int32_t content_len = 0; uint16_t status_code = 0; uint16_t content_type; int8_t table_num; //int8_t seq_num; //if((seq_num = getHTTPSequenceNum(sock)) == -1) return; // exception handling; invalid number http_response = httpserver.recvbuf; http_response_body = httpserver.sendbuf; // method Analyze switch (p_http_request->METHOD) { case HTTP_REQ_METHOD_HEAD : case HTTP_REQ_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_RESOURCE); // If URI is "/", respond by index find_http_uri_type(&p_http_request->TYPE, uri_name); // Checking requested file types (HTML, TEXT, GIF, JPEG and Etc. are included) break; case HTTP_REQ_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 break; case HTTP_REQ_METHOD_PUT : 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 break; case HTTP_REQ_METHOD_DELETE : 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 break; case HTTP_REQ_METHOD_ERR : default : status_code = HTTP_RES_CODE_NOT_IMPLE; break; } #ifdef _HTTPSERVER_DEBUG_ printf("\r\n> HTTPSocket[%d] : HTTP Method = %.4x\r\n", sock, p_http_request->METHOD); printf("> HTTPSocket[%d] : Request Type = %d\r\n", sock, p_http_request->TYPE); printf("> HTTPSocket[%d] : Request URI = %s\r\n", sock, uri_name); #endif if(p_http_request->TYPE == 0) // REST API request or Requested file type not found { table_num = search_http_resources(p_http_request->METHOD, uri_name); // get the resource table number if(table_num < 0) // HTTP resource search failed { //content_type = HTTP_RES_TYPE_TEXT; content_type = HTTP_RES_TYPE_JSON; if(table_num == RESTAPI_ERROR_RESOURCE_NOT_FOUND) status_code = HTTP_RES_CODE_NOT_FOUND; // uri unmatched if(table_num == RESTAPI_ERROR_METHOD_NOT_ALLOWED) status_code = HTTP_RES_CODE_NOT_ALLOWED; // uri matched but not supported method else status_code = HTTP_RES_CODE_NOT_FOUND; } else // HTTP resource search success { // REST API function handler // If necessary, generating JSON object of HTTP response body and copy the object to send buffer(http_response_body) content_len = http_resources_handler(p_http_request, http_response_body, table_num, status_code); // content_len variable: content body length or API handling results (e.g., http error) if(content_len == 1) { content_type = HTTP_RES_TYPE_JSON; status_code = HTTP_RES_CODE_CREATED; content_len = 0; } else if(content_len > 0) { content_type = HTTP_RES_TYPE_JSON; status_code = HTTP_RES_CODE_OK; } else if(content_len == 0) { content_type = HTTP_RES_TYPE_JSON; status_code = HTTP_RES_CODE_NO_CONTENT; } else if(content_len == RESTAPI_ERROR_CONFLICT) { content_type = HTTP_RES_TYPE_JSON; status_code = HTTP_RES_CODE_CONFLICT; } else { content_type = HTTP_RES_TYPE_JSON; status_code = HTTP_RES_CODE_NOT_FOUND; } } } else { // GET method: find requested file in storage (not implemented), //e.g., if(search_http_storage(...))... // If the size of the requested file is larger than the buffer size, use the file_len / file_offset field in HTTPSock statuc // Other methods: return 'content not found' content_type = HTTP_RES_TYPE_JSON; status_code = HTTP_RES_CODE_NOT_FOUND; } // HTTP response error codes; 4xx or 5xx // Generate the JSON body {"message": "xxxxxx", "code": xxx} if(((status_code & HTTP_RES_CODE_BAD_REQUEST) == HTTP_RES_CODE_BAD_REQUEST) || ((status_code & HTTP_RES_CODE_INT_SERVER) == HTTP_RES_CODE_INT_SERVER)) { // Generating JSON object of HTTP Error messages // e.g., {"errors":{ "error" : { "message":"Method not allowed", "code":404 } } content_len = make_http_response_error_message(http_response_body, status_code); } // Generate and Send the HTTP response 'header' send_http_response_header(sock, http_response, content_type, content_len, status_code); // If necessary, Send the HTTP response 'body' if(p_http_request->METHOD != HTTP_REQ_METHOD_HEAD) { if(content_len > 0) send_http_response_body(sock, http_response_body, content_len); } }