Example #1
0
UINT32   http_fwup(HTTPParameters ClientParams)
{
    INT32                   nRetCode;
    UINT32                  nSize,nTotal = 0;
    CHAR*                   Buffer;
    HTTP_SESSION_HANDLE     pHTTP;
    CHAR token[32];
    UINT32 content_length=0, size=32;
    struct pbuf *p;

    do
    {
        Buffer = (CHAR*)tls_mem_alloc(HTTP_CLIENT_BUFFER_SIZE);
        if(Buffer == NULL)
            return HTTP_CLIENT_ERROR_NO_MEMORY;
        memset(Buffer, 0, HTTP_CLIENT_BUFFER_SIZE);
        TLS_DBGPRT_INFO("\nHTTP Client v1.0\n\n");
        // Open the HTTP request handle
        pHTTP = HTTPClientOpenRequest(0);
        if(!pHTTP)
        {
            nRetCode =  HTTP_CLIENT_ERROR_INVALID_HANDLE;
            break;
        }
        // Set the Verb
        nRetCode = HTTPClientSetVerb(pHTTP,VerbGet);
        if(nRetCode != HTTP_CLIENT_SUCCESS)
        {
            break;
        }
#if TLS_CONFIG_HTTP_CLIENT_AUTH
        // Set authentication
        if(ClientParams.AuthType != AuthSchemaNone)
        {
            if((nRetCode = HTTPClientSetAuth(pHTTP,ClientParams.AuthType,NULL)) != HTTP_CLIENT_SUCCESS)
            {
                break;
            }

            // Set authentication
            if((nRetCode = HTTPClientSetCredentials(pHTTP,ClientParams.UserName,ClientParams.Password)) != HTTP_CLIENT_SUCCESS)
            {
                break;
            }
        }
#endif //TLS_CONFIG_HTTP_CLIENT_AUTH
#if TLS_CONFIG_HTTP_CLIENT_PROXY
        // Use Proxy server
        if(ClientParams.UseProxy == TRUE)
        {
            if((nRetCode = HTTPClientSetProxy(pHTTP,ClientParams.ProxyHost,ClientParams.ProxyPort,NULL,NULL)) != HTTP_CLIENT_SUCCESS)
            {

                break;
            }
        }
#endif //TLS_CONFIG_HTTP_CLIENT_PROXY
        if((nRetCode = HTTPClientSendRequest(pHTTP,ClientParams.Uri,NULL,0,FALSE,0,0)) != HTTP_CLIENT_SUCCESS)
        {
            break;
        }
        // Retrieve the the headers and analyze them
        if((nRetCode = HTTPClientRecvResponse(pHTTP,3)) != HTTP_CLIENT_SUCCESS)
        {
            break;
        }
        memset(token, 0, 32);
        if((nRetCode = HTTPClientFindFirstHeader(pHTTP, "content-length", token, &size)) != HTTP_CLIENT_SUCCESS)
        {
            HTTPClientFindCloseHeader(pHTTP);
            break;
        }
        HTTPClientFindCloseHeader(pHTTP);
        content_length = atol(strstr(token,":")+1);
        nRetCode = socket_fwup_accept(0, ERR_OK);
        if(nRetCode != ERR_OK)
            break;
        // Get the data until we get an error or end of stream code
        while(nRetCode == HTTP_CLIENT_SUCCESS || nRetCode != HTTP_CLIENT_EOS)
        {
            // Set the size of our buffer
            nSize = HTTP_CLIENT_BUFFER_SIZE - 4;
            // Get the data
            nRetCode = HTTPClientReadData(pHTTP,Buffer+3,nSize,0,&nSize);
            if(nRetCode != HTTP_CLIENT_SUCCESS && nRetCode != HTTP_CLIENT_EOS)
                break;
            while (1) {
                p = pbuf_alloc(PBUF_TRANSPORT, nSize + 3, PBUF_REF);
                if (p != NULL) {
                    break;
                } else {
                    /* delay 1 ticks */
                    tls_os_time_delay(1);
                }
            }
            if(nTotal == 0)
                *Buffer = SOCKET_FWUP_START;
            else if(nRetCode == HTTP_CLIENT_EOS)
                *Buffer = SOCKET_FWUP_END;
            else
                *Buffer = SOCKET_FWUP_DATA;
            *(Buffer+1) = (nSize>>8) & 0xFF;
            *(Buffer+2) = nSize & 0xFF;
            p->payload =  Buffer;
            p->len = p->tot_len = nSize + 3;
            nTotal += nSize;
            if(content_length)
                printf("Download %%%d\n", nTotal*100/content_length);
            nRetCode = socket_fwup_recv(0, p, ERR_OK);
            if(nRetCode != ERR_OK)
                break;
        }
    } while(0); // Run only once
    tls_mem_free(Buffer);
    if(pHTTP)
        HTTPClientCloseRequest(&pHTTP);
    if(ClientParams.Verbose == TRUE)
    {
        printf("\n\nHTTP Client terminated %d (got %d kb)\n\n",nRetCode,(nTotal/ 1024));
    }
    if(nRetCode)
        socket_fwup_err(0, nRetCode);
    return nRetCode;
}
Example #2
0
UINT32   http_snd_req(HTTPParameters ClientParams, HTTP_VERB verb, CHAR* pSndData, u8 parseXmlJson)
{
		INT32                   nRetCode;
    UINT32                  nSize,nTotal = 0;
    CHAR*                   Buffer = NULL;
    HTTP_SESSION_HANDLE     pHTTP;
    UINT32                  nSndDataLen ;
#if DEMO_HTTP_XML_PARSE
    XML_Parser parser;
#elif DEMO_HTTP_SXML_PARSE
    CHAR * buf_cache = NULL;
    UINT32 cur_pos = 0;
#endif
#if DEMO_HTTP_JSON_PARSE
    json_parser jsonParser;
    json_printer printer;
#endif
    do
    {
#if !DEMO_HTTP_XML_PARSE && DEMO_HTTP_SXML_PARSE
        buf_cache = (CHAR*)tls_mem_alloc(HTTP_CLIENT_BUFFER_SIZE);
        if(buf_cache == NULL)
            return HTTP_CLIENT_ERROR_NO_MEMORY;
        memset(buf_cache , 0, HTTP_CLIENT_BUFFER_SIZE);
#endif
        Buffer = (CHAR*)tls_mem_alloc(HTTP_CLIENT_BUFFER_SIZE);
        if(Buffer == NULL)
        {
#if !DEMO_HTTP_XML_PARSE && DEMO_HTTP_SXML_PARSE
            tls_mem_free(buf_cache);
#endif
            return HTTP_CLIENT_ERROR_NO_MEMORY;
        }
        memset(Buffer, 0, HTTP_CLIENT_BUFFER_SIZE);
        printf("\nHTTP Client v1.0\n\n");
        nSndDataLen = (pSndData==NULL ? 0 : strlen(pSndData));
        // Open the HTTP request handle
        pHTTP = HTTPClientOpenRequest(0);
        if(!pHTTP)
        {
            nRetCode =  HTTP_CLIENT_ERROR_INVALID_HANDLE;
            break;
        }
        // Set the Verb
        nRetCode = HTTPClientSetVerb(pHTTP,verb);
        if(nRetCode != HTTP_CLIENT_SUCCESS)
        {
            break;
        }
#if TLS_CONFIG_HTTP_CLIENT_AUTH
        // Set authentication
        if(ClientParams.AuthType != AuthSchemaNone)
        {
            if((nRetCode = HTTPClientSetAuth(pHTTP,ClientParams.AuthType,NULL)) != HTTP_CLIENT_SUCCESS)
            {
                break;
            }

            // Set authentication
            if((nRetCode = HTTPClientSetCredentials(pHTTP,ClientParams.UserName,ClientParams.Password)) != HTTP_CLIENT_SUCCESS)
            {
                break;
            }
        }
#endif //TLS_CONFIG_HTTP_CLIENT_AUTH
#if TLS_CONFIG_HTTP_CLIENT_PROXY
        // Use Proxy server
        if(ClientParams.UseProxy == TRUE)
        {
            if((nRetCode = HTTPClientSetProxy(pHTTP,ClientParams.ProxyHost,ClientParams.ProxyPort,NULL,NULL)) != HTTP_CLIENT_SUCCESS)
            {

                break;
            }
        }
#endif //TLS_CONFIG_HTTP_CLIENT_PROXY
	 if((nRetCode = HTTPClientSendRequest(pHTTP,ClientParams.Uri,pSndData,nSndDataLen,verb==VerbPost || verb==VerbPut,0,0)) != HTTP_CLIENT_SUCCESS)
        {
            break;
        }
        // Retrieve the the headers and analyze them
        if((nRetCode = HTTPClientRecvResponse(pHTTP,30)) != HTTP_CLIENT_SUCCESS)
        {
            break;
        }
	 printf("Start to receive data from remote server...\n");
#if DEMO_HTTP_XML_PARSE
        if(parseXmlJson == 1)
            xmlParseInit(&parser);
#endif
#if DEMO_HTTP_JSON_PARSE
        if(parseXmlJson == 2)
            jsonParseInit(&jsonParser, &printer);
#endif
        // Get the data until we get an error or end of stream code
        while(nRetCode == HTTP_CLIENT_SUCCESS || nRetCode != HTTP_CLIENT_EOS)
        {
            // Set the size of our buffer
            nSize = HTTP_CLIENT_BUFFER_SIZE;   
            // Get the data
            nRetCode = HTTPClientReadData(pHTTP,Buffer,nSize,300,&nSize);
            if(nRetCode != HTTP_CLIENT_SUCCESS && nRetCode != HTTP_CLIENT_EOS)
                break;
            printf("%s", Buffer);
#if DEMO_HTTP_XML_PARSE
            if(parseXmlJson == 1)
                xmlParse(parser, Buffer, nSize, nRetCode == HTTP_CLIENT_EOS);
#elif DEMO_HTTP_SXML_PARSE
            if(parseXmlJson == 1)
            {
                if(cur_pos + nSize < HTTP_CLIENT_BUFFER_SIZE-1)
                {
                    memcpy(buf_cache+cur_pos, Buffer, nSize);
                    cur_pos += nSize;
                    if(nRetCode == HTTP_CLIENT_EOS)
                        sxml_parse_all(buf_cache);
                }
            }
#endif
#if DEMO_HTTP_JSON_PARSE
            if(parseXmlJson == 2)
                jsonParse(&jsonParser, &printer, Buffer, nSize, nRetCode == HTTP_CLIENT_EOS);
#endif
            nTotal += nSize;
        }
    } while(0); // Run only once
    tls_mem_free(Buffer);
#if !DEMO_HTTP_XML_PARSE && DEMO_HTTP_SXML_PARSE
    tls_mem_free(buf_cache);
#endif
    if(pHTTP)
        HTTPClientCloseRequest(&pHTTP);
    if(ClientParams.Verbose == TRUE)
    {
        printf("\n\nHTTP Client terminated %d (got %d kb)\n\n",nRetCode,(nTotal/ 1024));
    }
    return nRetCode;
}
Example #3
0
/**
* This method send a http_request (GET-Method) and the returned
* output will send to the internal result-variable.
*
* @todo add error handling
*
*/
void HTTP_Client::send_http_request()
{
	//Clean buffer first
	strcpy(this->c_http_buffer, "");

	do
	{
		// Open the HTTP request handle
		this->o_http_session = HTTPClientOpenRequest(0);

		// Use Proxy server
		if (this->b_use_proxy)
		{
			if ((this->i_return_code = HTTPClientSetProxy(this->o_http_session, this->o_request_parameters.ProxyHost,
				this->o_request_parameters.ProxyPort, NULL, NULL)) != HTTP_CLIENT_SUCCESS)
			{
				break;
			}
		}

		// Send a request for the home page 
		if ((this->i_return_code = HTTPClientSendRequest(this->o_http_session, this->o_request_parameters.Uri, NULL, 0, FALSE, 0, 0)) != HTTP_CLIENT_SUCCESS)
		{
			break;
		}

		// Retrieve the the headers and analyze them
		if ((this->i_return_code = HTTPClientRecvResponse(this->o_http_session, 3)) != HTTP_CLIENT_SUCCESS)
		{
			break;
		}

		// Get the data until we get an error or end of stream code
		// printf("Each dot represents %d bytes:\n",HTTP_BUFFER_SIZE );
		while (this->i_return_code == HTTP_CLIENT_SUCCESS || this->i_return_code != HTTP_CLIENT_EOS)
		{
			// Set the size of our buffer
			this->i_size = HTTP_CLIENT_BUFFER_SIZE;

			// Get the data
			this->i_return_code = HTTPClientReadData(this->o_http_session, this->c_http_buffer, this->i_size, 0, &this->i_size);
			this->i_total_size += this->i_size;

		}
		HTTPClientCloseRequest(&this->o_http_session);
	} while (0); // Run only once

	//copy buffer to the internal output-string and delete the last newline-character 
	if (strcmp(this->c_http_buffer, "") != 0)
	{
		this->s_http_result = this->c_http_buffer;
		this->s_http_result.erase(remove(this->s_http_result.begin(), this->s_http_result.end(), '\n'), this->s_http_result.end());
	}
	else
		this->s_http_result = "";



	//clear buffer
	strcpy(this->c_http_buffer, "");

}