コード例 #1
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
}
コード例 #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
}
/* 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
}