sandbox / mbed-http

Dependents:   MQTTGateway2 MQTTGatewayK64 http-example-wnc GuardRoom ... more

Files at this revision

API Documentation at this revision

Comitter:
Jan Jongboom
Date:
Fri Aug 10 11:30:37 2018 +0100
Parent:
28:9a04ed79d67e
Child:
30:3ad153a3fdfd
Commit message:
Force usage of uint32_t instead of size_t - required for compilation on 64-bit systems

Changed in this revision

README.md Show annotated file Show diff for this revision Revisions of this file
http_parser/http_parser.c Show annotated file Show diff for this revision Revisions of this file
http_parser/http_parser.h Show annotated file Show diff for this revision Revisions of this file
source/http_request.h Show annotated file Show diff for this revision Revisions of this file
source/http_request_builder.h Show annotated file Show diff for this revision Revisions of this file
source/http_request_parser.h Show annotated file Show diff for this revision Revisions of this file
source/http_response.h Show annotated file Show diff for this revision Revisions of this file
source/https_request.h Show annotated file Show diff for this revision Revisions of this file
--- a/README.md	Tue Mar 27 11:07:02 2018 +0200
+++ b/README.md	Fri Aug 10 11:30:37 2018 +0100
@@ -59,7 +59,7 @@
 By default the library will store the full request body on the heap. This works well for small responses, but you'll run out of memory when receiving a large response body. To mitigate this you can pass in a callback as the last argument to the request constructor. This callback will be called whenever a chunk of the body is received. You can set the request chunk size in the `HTTP_RECEIVE_BUFFER_SIZE` macro (see `mbed_lib.json` for the definition) although it also depends on the buffer size of the underlying network connection.
 
 ```cpp
-void body_callback(const char* data, size_t data_len) {
+void body_callback(const char* data, uint32_t data_len) {
     // do something with the data
 }
 
@@ -72,7 +72,7 @@
 If you cannot load the full request into memory, you can pass a callback into the `send` function. Through this callback you can feed in chunks of the request body. This is very useful if you want to send files from a file system.
 
 ```cpp
-const void * get_chunk(size_t* out_size) {
+const void * get_chunk(uint32_t* out_size) {
     // set the value of out_size (via *out_size = 10) to the size of the buffer
     // return the buffer
 
--- a/http_parser/http_parser.c	Tue Mar 27 11:07:02 2018 +0200
+++ b/http_parser/http_parser.c	Fri Aug 10 11:30:37 2018 +0100
@@ -631,10 +631,10 @@
   return s_dead;
 }
 
-size_t http_parser_execute (http_parser *parser,
+uint32_t http_parser_execute (http_parser *parser,
                             const http_parser_settings *settings,
                             const char *data,
-                            size_t len)
+                            uint32_t len)
 {
   char c, ch;
   int8_t unhex_val;
@@ -1536,7 +1536,7 @@
             {
               const char* p_cr;
               const char* p_lf;
-              size_t limit = data + len - p;
+              uint32_t limit = data + len - p;
 
               limit = MIN(limit, HTTP_MAX_HEADER_SIZE);
 
@@ -2160,13 +2160,13 @@
 
 const char *
 http_errno_name(enum http_errno err) {
-  assert(((size_t) err) < ARRAY_SIZE(http_strerror_tab));
+  assert(((uint32_t) err) < ARRAY_SIZE(http_strerror_tab));
   return http_strerror_tab[err].name;
 }
 
 const char *
 http_errno_description(enum http_errno err) {
-  assert(((size_t) err) < ARRAY_SIZE(http_strerror_tab));
+  assert(((uint32_t) err) < ARRAY_SIZE(http_strerror_tab));
   return http_strerror_tab[err].description;
 }
 
@@ -2257,7 +2257,7 @@
   enum http_host_state s;
 
   const char *p;
-  size_t buflen = u->field_data[UF_HOST].off + u->field_data[UF_HOST].len;
+  uint32_t buflen = u->field_data[UF_HOST].off + u->field_data[UF_HOST].len;
 
   assert(u->field_set & (1 << UF_HOST));
 
@@ -2340,7 +2340,7 @@
 }
 
 int
-http_parser_parse_url(const char *buf, size_t buflen, int is_connect,
+http_parser_parse_url(const char *buf, uint32_t buflen, int is_connect,
                       struct http_parser_url *u)
 {
   enum state s;
--- a/http_parser/http_parser.h	Tue Mar 27 11:07:02 2018 +0200
+++ b/http_parser/http_parser.h	Fri Aug 10 11:30:37 2018 +0100
@@ -29,7 +29,7 @@
 #define HTTP_PARSER_VERSION_MINOR 7
 #define HTTP_PARSER_VERSION_PATCH 1
 
-typedef unsigned int size_t;
+#include "stdlib.h"
 
 #if defined(_WIN32) && !defined(__MINGW32__) && \
   (!defined(_MSC_VER) || _MSC_VER<1600) && !defined(__WINE__)
@@ -87,7 +87,7 @@
  * many times for each string. E.G. you might get 10 callbacks for "on_url"
  * each providing just a few characters more data.
  */
-typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);
+typedef int (*http_data_cb) (http_parser*, const char *at, uint32_t length);
 typedef int (*http_cb) (http_parser*);
 
 
@@ -390,10 +390,10 @@
 
 /* Executes the parser. Returns number of parsed bytes. Sets
  * `parser->http_errno` on error. */
-size_t http_parser_execute(http_parser *parser,
+uint32_t http_parser_execute(http_parser *parser,
                            const http_parser_settings *settings,
                            const char *data,
-                           size_t len);
+                           uint32_t len);
 
 
 /* If http_should_keep_alive() in the on_headers_complete or
@@ -417,7 +417,7 @@
 void http_parser_url_init(struct http_parser_url *u);
 
 /* Parse a URL; return nonzero on failure */
-int http_parser_parse_url(const char *buf, size_t buflen,
+int http_parser_parse_url(const char *buf, uint32_t buflen,
                           int is_connect,
                           struct http_parser_url *u);
 
--- a/source/http_request.h	Tue Mar 27 11:07:02 2018 +0200
+++ b/source/http_request.h	Fri Aug 10 11:30:37 2018 +0100
@@ -51,7 +51,7 @@
                                 If not set, the complete body will be allocated on the HttpResponse object,
                                 which might use lots of memory.
     */
-    HttpRequest(NetworkInterface* aNetwork, http_method aMethod, const char* url, Callback<void(const char *at, size_t length)> aBodyCallback = 0)
+    HttpRequest(NetworkInterface* aNetwork, http_method aMethod, const char* url, Callback<void(const char *at, uint32_t length)> aBodyCallback = 0)
         : network(aNetwork), method(aMethod), body_callback(aBodyCallback)
     {
         error = 0;
@@ -74,7 +74,7 @@
                                 If not set, the complete body will be allocated on the HttpResponse object,
                                 which might use lots of memory.
     */
-    HttpRequest(TCPSocket* aSocket, http_method aMethod, const char* url, Callback<void(const char *at, size_t length)> aBodyCallback = 0)
+    HttpRequest(TCPSocket* aSocket, http_method aMethod, const char* url, Callback<void(const char *at, uint32_t length)> aBodyCallback = 0)
         : socket(aSocket), method(aMethod), body_callback(aBodyCallback)
     {
         error = 0;
@@ -126,7 +126,7 @@
             return NULL;
         }
 
-        size_t request_size = 0;
+        uint32_t request_size = 0;
         char* request = request_builder->build(body, body_size, request_size);
 
         ret = send_buffer(request, request_size);
@@ -148,7 +148,7 @@
      * @return An HttpResponse pointer on success, or NULL on failure.
      *         See get_error() for the error code.
      */
-    HttpResponse* send(Callback<const void*(size_t*)> body_cb) {
+    HttpResponse* send(Callback<const void*(uint32_t*)> body_cb) {
 
         nsapi_error_t ret;
 
@@ -159,7 +159,7 @@
 
         set_header("Transfer-Encoding", "chunked");
 
-        size_t request_size = 0;
+        uint32_t request_size = 0;
         char* request = request_builder->build(NULL, 0, request_size);
 
         // first... send this request headers without the body
@@ -173,14 +173,14 @@
 
         // ok... now it's time to start sending chunks...
         while (1) {
-            size_t size;
+            uint32_t size;
             const void *buffer = body_cb(&size);
 
             if (size == 0) break;
 
             // so... size in HEX, \r\n, data, \r\n again
             char size_buff[10]; // if sending length of more than 8 digits, you have another problem on a microcontroller...
-            size_t size_buff_size = sprintf(size_buff, "%X\r\n", size);
+            uint32_t size_buff_size = sprintf(size_buff, "%X\r\n", size);
             if ((total_send_count = send_buffer(size_buff, size_buff_size)) < 0) {
                 free(request);
                 error = total_send_count;
@@ -262,7 +262,7 @@
         return NSAPI_ERROR_OK;
     }
 
-    nsapi_size_or_error_t send_buffer(char* buffer, size_t buffer_size) {
+    nsapi_size_or_error_t send_buffer(char* buffer, uint32_t buffer_size) {
         nsapi_size_or_error_t total_send_count = 0;
         while (total_send_count < buffer_size) {
             nsapi_size_or_error_t send_result = socket->send(buffer + total_send_count, buffer_size - total_send_count);
@@ -296,7 +296,7 @@
         while ((recv_ret = socket->recv(recv_buffer, HTTP_RECEIVE_BUFFER_SIZE)) > 0) {
 
             // Pass the chunk into the http_parser
-            size_t nparsed = parser.execute((const char*)recv_buffer, recv_ret);
+            uint32_t nparsed = parser.execute((const char*)recv_buffer, recv_ret);
             if (nparsed != recv_ret) {
                 // printf("Parsing failed... parsed %d bytes, received %d bytes\n", nparsed, recv_ret);
                 error = -2101;
@@ -333,7 +333,7 @@
     NetworkInterface* network;
     TCPSocket* socket;
     http_method method;
-    Callback<void(const char *at, size_t length)> body_callback;
+    Callback<void(const char *at, uint32_t length)> body_callback;
 
     ParsedUrl* parsed_url;
 
--- a/source/http_request_builder.h	Tue Mar 27 11:07:02 2018 +0200
+++ b/source/http_request_builder.h	Fri Aug 10 11:30:37 2018 +0100
@@ -58,7 +58,7 @@
         }
     }
 
-    char* build(const void* body, size_t body_size, size_t &size, bool skip_content_length = false) {
+    char* build(const void* body, unsigned int body_size, uint32_t &size, bool skip_content_length = false) {
         const char* method_str = http_method_str(method);
 
         bool is_chunked = has_header("Transfer-Encoding", "chunked");
--- a/source/http_request_parser.h	Tue Mar 27 11:07:02 2018 +0200
+++ b/source/http_request_parser.h	Fri Aug 10 11:30:37 2018 +0100
@@ -24,7 +24,7 @@
 class HttpParser {
 public:
 
-    HttpParser(HttpResponse* a_response, http_parser_type parser_type, Callback<void(const char *at, size_t length)> a_body_callback = 0)
+    HttpParser(HttpResponse* a_response, http_parser_type parser_type, Callback<void(const char *at, uint32_t length)> a_body_callback = 0)
         : response(a_response), body_callback(a_body_callback)
     {
         settings = new http_parser_settings();
@@ -55,7 +55,7 @@
         }
     }
 
-    size_t execute(const char* buffer, size_t buffer_size) {
+    uint32_t execute(const char* buffer, uint32_t buffer_size) {
         return http_parser_execute(parser, settings, buffer, buffer_size);
     }
 
@@ -69,25 +69,25 @@
         return 0;
     }
 
-    int on_url(http_parser* parser, const char *at, size_t length) {
+    int on_url(http_parser* parser, const char *at, uint32_t length) {
         string s(at, length);
         response->set_url(s);
         return 0;
     }
 
-    int on_status(http_parser* parser, const char *at, size_t length) {
+    int on_status(http_parser* parser, const char *at, uint32_t length) {
         string s(at, length);
         response->set_status(parser->status_code, s);
         return 0;
     }
 
-    int on_header_field(http_parser* parser, const char *at, size_t length) {
+    int on_header_field(http_parser* parser, const char *at, uint32_t length) {
         string s(at, length);
         response->set_header_field(s);
         return 0;
     }
 
-    int on_header_value(http_parser* parser, const char *at, size_t length) {
+    int on_header_value(http_parser* parser, const char *at, uint32_t length) {
         string s(at, length);
         response->set_header_value(s);
         return 0;
@@ -99,7 +99,7 @@
         return 0;
     }
 
-    int on_body(http_parser* parser, const char *at, size_t length) {
+    int on_body(http_parser* parser, const char *at, uint32_t length) {
         response->increase_body_length(length);
 
         if (body_callback) {
@@ -132,19 +132,19 @@
         return ((HttpParser*)parser->data)->on_message_begin(parser);
     }
 
-    static int on_url_callback(http_parser* parser, const char *at, size_t length) {
+    static int on_url_callback(http_parser* parser, const char *at, uint32_t length) {
         return ((HttpParser*)parser->data)->on_url(parser, at, length);
     }
 
-    static int on_status_callback(http_parser* parser, const char *at, size_t length) {
+    static int on_status_callback(http_parser* parser, const char *at, uint32_t length) {
         return ((HttpParser*)parser->data)->on_status(parser, at, length);
     }
 
-    static int on_header_field_callback(http_parser* parser, const char *at, size_t length) {
+    static int on_header_field_callback(http_parser* parser, const char *at, uint32_t length) {
         return ((HttpParser*)parser->data)->on_header_field(parser, at, length);
     }
 
-    static int on_header_value_callback(http_parser* parser, const char *at, size_t length) {
+    static int on_header_value_callback(http_parser* parser, const char *at, uint32_t length) {
         return ((HttpParser*)parser->data)->on_header_value(parser, at, length);
     }
 
@@ -152,7 +152,7 @@
         return ((HttpParser*)parser->data)->on_headers_complete(parser);
     }
 
-    static int on_body_callback(http_parser* parser, const char *at, size_t length) {
+    static int on_body_callback(http_parser* parser, const char *at, uint32_t length) {
         return ((HttpParser*)parser->data)->on_body(parser, at, length);
     }
 
@@ -169,7 +169,7 @@
     }
 
     HttpResponse* response;
-    Callback<void(const char *at, size_t length)> body_callback;
+    Callback<void(const char *at, uint32_t length)> body_callback;
     http_parser* parser;
     http_parser_settings* settings;
 };
--- a/source/http_response.h	Tue Mar 27 11:07:02 2018 +0200
+++ b/source/http_response.h	Fri Aug 10 11:30:37 2018 +0100
@@ -42,7 +42,7 @@
             free(body);
         }
 
-        for (size_t ix = 0; ix < header_fields.size(); ix++) {
+        for (uint32_t ix = 0; ix < header_fields.size(); ix++) {
             delete header_fields[ix];
             delete header_values[ix];
         }
@@ -106,15 +106,15 @@
     }
 
     void set_headers_complete() {
-        for (size_t ix = 0; ix < header_fields.size(); ix++) {
+        for (uint32_t ix = 0; ix < header_fields.size(); ix++) {
             if (strcicmp(header_fields[ix]->c_str(), "content-length") == 0) {
-                expected_content_length = (size_t)atoi(header_values[ix]->c_str());
+                expected_content_length = (uint32_t)atoi(header_values[ix]->c_str());
                 break;
             }
         }
     }
 
-    size_t get_headers_length() {
+    uint32_t get_headers_length() {
         return header_fields.size();
     }
 
@@ -126,7 +126,7 @@
         return header_values;
     }
 
-    void set_body(const char *at, size_t length) {
+    void set_body(const char *at, uint32_t length) {
         // Connection: close, could not specify Content-Length, nor chunked... So do it like this:
         if (expected_content_length == 0 && length > 0) {
             is_chunked = true;
@@ -145,7 +145,6 @@
                 char* original_body = body;
                 body = (char*)realloc(body, body_offset + length);
                 if (body == NULL) {
-                    printf("[HttpResponse] realloc for %d bytes failed\n", body_offset + length);
                     free(original_body);
                     return;
                 }
@@ -166,11 +165,11 @@
         return s;
     }
 
-    void increase_body_length(size_t length) {
+    void increase_body_length(uint32_t length) {
         body_length += length;
     }
 
-    size_t get_body_length() {
+    uint32_t get_body_length() {
         return body_offset;
     }
 
@@ -216,15 +215,15 @@
     bool concat_header_field;
     bool concat_header_value;
 
-    size_t expected_content_length;
+    uint32_t expected_content_length;
 
     bool is_chunked;
 
     bool is_message_completed;
 
     char * body;
-    size_t body_length;
-    size_t body_offset;
+    uint32_t body_length;
+    uint32_t body_offset;
 };
 
 #endif
--- a/source/https_request.h	Tue Mar 27 11:07:02 2018 +0200
+++ b/source/https_request.h	Fri Aug 10 11:30:37 2018 +0100
@@ -53,7 +53,7 @@
                  const char* ssl_ca_pem,
                  http_method method,
                  const char* url,
-                 Callback<void(const char *at, size_t length)> body_callback = 0)
+                 Callback<void(const char *at, uint32_t length)> body_callback = 0)
     {
         _parsed_url = new ParsedUrl(url);
         _body_callback = body_callback;
@@ -79,7 +79,7 @@
     HttpsRequest(TLSSocket* socket,
                  http_method method,
                  const char* url,
-                 Callback<void(const char *at, size_t length)> body_callback = 0)
+                 Callback<void(const char *at, uint32_t length)> body_callback = 0)
     {
         _parsed_url = new ParsedUrl(url);
         _body_callback = body_callback;
@@ -128,7 +128,7 @@
             return NULL;
         }
 
-        size_t request_size = 0;
+        uint32_t request_size = 0;
         char* request = _request_builder->build(body, body_size, request_size);
 
         ret = send_buffer((const unsigned char*)request, request_size);
@@ -150,7 +150,7 @@
      * @return An HttpResponse pointer on success, or NULL on failure.
      *         See get_error() for the error code.
      */
-    HttpResponse* send(Callback<const void*(size_t*)> body_cb) {
+    HttpResponse* send(Callback<const void*(uint32_t*)> body_cb) {
 
         nsapi_error_t ret;
 
@@ -161,7 +161,7 @@
 
         set_header("Transfer-Encoding", "chunked");
 
-        size_t request_size = 0;
+        uint32_t request_size = 0;
         char* request = _request_builder->build(NULL, 0, request_size);
 
         // first... send this request headers without the body
@@ -175,14 +175,14 @@
 
         // ok... now it's time to start sending chunks...
         while (1) {
-            size_t size;
+            uint32_t size;
             const void *buffer = body_cb(&size);
 
             if (size == 0) break;
 
             // so... size in HEX, \r\n, data, \r\n again
             char size_buff[10]; // if sending length of more than 8 digits, you have another problem on a microcontroller...
-            size_t size_buff_size = sprintf(size_buff, "%X\r\n", size);
+            uint32_t size_buff_size = sprintf(size_buff, "%X\r\n", size);
             if ((total_send_count = send_buffer((const unsigned char*)size_buff, size_buff_size)) < 0) {
                 free(request);
                 _error = total_send_count;
@@ -299,7 +299,7 @@
         return NSAPI_ERROR_OK;
     }
 
-    nsapi_size_or_error_t send_buffer(const unsigned char *buffer, size_t buffer_size) {
+    nsapi_size_or_error_t send_buffer(const unsigned char *buffer, uint32_t buffer_size) {
         nsapi_size_or_error_t ret = mbedtls_ssl_write(_tlssocket->get_ssl_context(), (const unsigned char *) buffer, buffer_size);
 
         if (ret < 0) {
@@ -330,10 +330,10 @@
         /* Read data out of the socket */
         while ((ret = mbedtls_ssl_read(_tlssocket->get_ssl_context(), (unsigned char *) recv_buffer, HTTP_RECEIVE_BUFFER_SIZE)) > 0) {
             // Don't know if this is actually needed, but OK
-            size_t _bpos = static_cast<size_t>(ret);
+            uint32_t _bpos = static_cast<uint32_t>(ret);
             recv_buffer[_bpos] = 0;
 
-            size_t nparsed = parser.execute((const char*)recv_buffer, _bpos);
+            uint32_t nparsed = parser.execute((const char*)recv_buffer, _bpos);
             if (nparsed != _bpos) {
                 print_mbedtls_error("parser_error", nparsed);
                 // parser error...
@@ -373,7 +373,7 @@
     TLSSocket* _tlssocket;
     bool _we_created_the_socket;
 
-    Callback<void(const char *at, size_t length)> _body_callback;
+    Callback<void(const char *at, uint32_t length)> _body_callback;
     ParsedUrl* _parsed_url;
     HttpRequestBuilder* _request_builder;
     HttpResponse* _response;