コード例 #1
0
ファイル: httpServer.c プロジェクト: alenyashka/W7500
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;
	}
コード例 #2
0
ファイル: httpServer.c プロジェクト: alenyashka/W7500
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
}
コード例 #3
0
ファイル: httpServer.c プロジェクト: xd785/WIZ550web
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;
    }
}
コード例 #4
0
ファイル: httpServer.c プロジェクト: xd785/WIZ550web
void httpServer_run(uint8_t seqnum)
{
    uint8_t s;	// socket number
    int16_t len;
    //uint32_t gettime = 0;	// 20150828 ## Eric removed

    uint8_t ret = 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;

                // ## 20150828, Eric / Bongjun Hur added
                if ((len = recv(s, (uint8_t *)http_request, len)) < 0) break;	// Exception handler

                ////////////////////////////////////////////////////////////////////////////////
                // Todo; User defined custom command handler (userHandler.c)
                ret = custom_command_handler((uint8_t *)http_request);
                ////////////////////////////////////////////////////////////////////////////////

                if(ret > 0) // Custom command handler
                {
                    // Todo: Users can change this parts for custom function added
                    //if(ret == COMMAND_SUCCESS)		send(s, (uint8_t *)"CMDOK", 5);
                    //else if(ret == COMMAND_ERROR)	send(s, (uint8_t *)"CMDERROR", 8);

                    HTTPSock_Status[seqnum].sock_status = STATE_HTTP_RES_DONE;
                }
                else // HTTP process 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_
                    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);

                    /*
                    							// ## 20150828 Eric removed
                    							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);
        http_disconnect(s);
        break;

    case SOCK_INIT:
        listen(s);
        break;

    case SOCK_LISTEN:
        break;

    case SOCK_SYNSENT:
    //case SOCK_SYNSENT_M:
    case SOCK_SYNRECV:
        //case SOCK_SYNRECV_M:
        break;

    case SOCK_CLOSED:
    default :
#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;
    } // end of switch

#ifdef _USE_WATCHDOG_
    HTTPServer_WDT_Reset();
#endif
}
/* 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
}
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);
	}
}