Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BSP_B-L475E-IOT01 mbed es_wifi jsmn
HTTPHelper.h
00001 #pragma once 00002 00003 #include <mbed.h> 00004 #include "es_wifi.h" 00005 00006 #define HTTP_200OK "200 OK\r\n" 00007 #define HTTP_CL "Content-Length: " 00008 00009 class HTTPHelper 00010 { 00011 private: 00012 char m_io_buffer[ES_WIFI_PAYLOAD_SIZE]; 00013 00014 public: 00015 00016 HTTPHelper() {} 00017 ~HTTPHelper(){} 00018 00019 00020 bool HttpParseResponse(TCPConnector *net,uint8_t socket, char *content_buffer, uint32_t content_buffer_size, bool *status_ok, uint32_t *content_size) 00021 { 00022 uint16_t recv_bytes = 0; 00023 uint16_t http_content_len = 0; 00024 char *http_content_pivot = NULL; 00025 uint16_t content_chunk_size; 00026 bool res = false; 00027 00028 *status_ok = false; 00029 *content_size = 0; 00030 00031 // receive RESPONSE 00032 m_io_buffer[0] = 0; 00033 if(net->tcp_read(socket,m_io_buffer,ES_WIFI_PAYLOAD_SIZE,&recv_bytes,false)) 00034 { 00035 if (recv_bytes > 0) 00036 { 00037 m_io_buffer[recv_bytes]=0; 00038 printf("%s\r\n",m_io_buffer); 00039 http_parse_response(m_io_buffer, recv_bytes, status_ok, &http_content_len); 00040 printf("> HTTP OK = %d Content len = %d\r\n", *status_ok, http_content_len); 00041 *content_size = http_content_len; 00042 00043 /* fetch content */ 00044 http_content_pivot = strstr(m_io_buffer, "\r\n\r\n"); 00045 if (http_content_pivot != NULL) 00046 { 00047 /* copy first content chunk */ 00048 http_content_pivot += 4; 00049 content_chunk_size = strlen(http_content_pivot); 00050 if (content_chunk_size < content_buffer_size) strcpy(content_buffer, http_content_pivot); 00051 http_content_len -= content_chunk_size; 00052 printf("http_content_len %d content_chunk_size %d\r\n",http_content_len,content_chunk_size); 00053 00054 /* continue to fetch content chunck */ 00055 while (http_content_len > 0) 00056 { 00057 printf("http_content_len %d\r\n",http_content_len); 00058 if( net->tcp_read(socket,m_io_buffer,ES_WIFI_PAYLOAD_SIZE,&recv_bytes,true) == false ) break; 00059 m_io_buffer[recv_bytes] = 0; 00060 http_content_len -= recv_bytes; 00061 if (recv_bytes == 0) 00062 { 00063 printf("> ERROR : http content len overflow\r\n"); 00064 break; 00065 } 00066 strcat(content_buffer,m_io_buffer); 00067 } 00068 res = (http_content_len == 0); 00069 } 00070 00071 } 00072 } 00073 00074 return res; 00075 00076 } 00077 00078 private: 00079 00080 void http_parse_response(char *http_chunk, uint16_t http_chunk_len, bool *status_code_ok, uint16_t *content_len) 00081 { 00082 char *line_pivot, *key_pivot; 00083 00084 *status_code_ok = false; 00085 *content_len = 0; 00086 00087 line_pivot = strstr(http_chunk, "\r\n"); 00088 if (line_pivot == NULL) 00089 return ; 00090 00091 key_pivot = strstr(http_chunk, HTTP_200OK); 00092 00093 if (key_pivot != NULL) 00094 *status_code_ok = true; 00095 else 00096 *status_code_ok = false; 00097 00098 *content_len = 0; 00099 while (1) 00100 { 00101 line_pivot = strstr(line_pivot + 2, "\r\n"); 00102 if (line_pivot == NULL) 00103 break; 00104 key_pivot = strstr(http_chunk, HTTP_CL); 00105 if (key_pivot == NULL) 00106 continue; 00107 if (key_pivot < line_pivot) 00108 { 00109 key_pivot += strlen(HTTP_CL); 00110 *content_len = atoi((char const *)key_pivot); 00111 break; 00112 } 00113 } 00114 } 00115 00116 00117 };
Generated on Fri Jul 15 2022 03:03:13 by
1.7.2