//--------------------------------------------------------------------------- int HTTP_Client::Open (Ztring Url) { if (Handle) Close(); //init Handle=HTTPClientOpenRequest(0); //Mehtod if (HTTPClientSetVerb(Handle, VerbGet)!=0) { Close(); return 0; } //Send request if (HTTPClientSendRequest(Handle, (char*)(Url.To_Local().c_str()), NULL, 0, FALSE, 0, 0)!=0) { Close(); return 0; } //Receive response if (HTTPClientRecvResponse(Handle, 3)!=0) { Close(); return 0; } return 1; }
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; }
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; }