HTTP and HTTPS library for Mbed OS 5

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

For the example program, see: sandbox/http-example.

This library is used to make HTTP and HTTPS calls from Mbed OS 5 applications.

HTTP Request API

NetworkInterface* network = /* obtain a NetworkInterface object */

const char body[] = "{\"hello\":\"world\"}";

HttpRequest* request = new HttpRequest(network, HTTP_POST, "http://httpbin.org/post");
request->set_header("Content-Type", "application/json");
HttpResponse* response = request->send(body, strlen(body));
// if response is NULL, check response->get_error()

printf("status is %d - %s\n", response->get_status_code(), response->get_status_message());
printf("body is:\n%s\n", response->get_body_as_string().c_str());

delete request; // also clears out the response

HTTPS Request API

// pass in the root certificates that you trust, there is no central CA registry in Mbed OS
const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n"
    /* rest of the CA root certificates */;

NetworkInterface* network = /* obtain a NetworkInterface object */

const char body[] = "{\"hello\":\"world\"}";

HttpsRequest* request = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET "https://httpbin.org/status/418");
HttpResponse* response = request->send();
// if response is NULL, check response->get_error()

printf("status is %d - %s\n", response->get_status_code(), response->get_status_message());
printf("body is:\n%s\n", response->get_body().c_str());

delete request;

Note: You can get the root CA for a domain easily from Firefox. Click on the green padlock, click More information > Security > View certificate > Details. Select the top entry in the 'Certificate Hierarchy' and click Export.... This gives you a PEM file. Add the content of the PEM file to your root CA list (here's an image).

Mbed TLS Entropy configuration

If your target does not have a built-in TRNG, or other entropy sources, add the following macros to your mbed_app.json file to disable entropy:

{
    "macros": [
        "MBEDTLS_TEST_NULL_ENTROPY",
        "MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES"
    ]
}

Note that this is not secure, and you should not deploy this device into production with this configuration.

Memory usage

Small requests where the body of the response is cached by the library (like the one found in main-http.cpp), require 4K of RAM. When the request is finished they require 1.5K of RAM, depending on the size of the response. This applies both to HTTP and HTTPS. If you need to handle requests that return a large response body, see 'Dealing with large body'.

HTTPS requires additional memory: on FRDM-K64F about 50K of heap space (at its peak). This means that you cannot use HTTPS on devices with less than 128K of memory, asyou also need to reserve memory for the stack and network interface.

Dealing with large response body

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 ofthe underlying network connection.

void body_callback(const char* data, uint32_t data_len) {
    // do something with the data
}

HttpRequest* req = new HttpRequest(network, HTTP_GET, "http://pathtolargefile.com", &body_callback);
req->send(NULL, 0);

Dealing with a large request body

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.

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

    // if you don't have any more data, set *out_size to 0
}

HttpRequest* req = new HttpRequest(network, HTTP_POST, "http://my_api.com/upload");
req->send(callback(&get_chunk));

Socket re-use

By default the library opens a new socket per request. This is wasteful, especially when dealing with TLS requests. You can re-use sockets like this:

HTTP

TCPSocket* socket = new TCPSocket();

nsapi_error_t open_result = socket->open(network);
// check open_result

nsapi_error_t connect_result = socket->connect("httpbin.org", 80);
// check connect_result

// Pass in `socket`, instead of `network` as first argument
HttpRequest* req = new HttpRequest(socket, HTTP_GET, "http://httpbin.org/status/418");

HTTPS

TLSSocket* socket = new TLSSocket();

nsapi_error_t r;
// make sure to check the return values for the calls below (should return NSAPI_ERROR_OK)
r = socket->open(network);
r = socket->set_root_ca_cert(SSL_CA_PEM);
r = socket->connect("httpbin.org", 443);

// Pass in `socket`, instead of `network` as first argument, and omit the `SSL_CA_PEM` argument
HttpsRequest* get_req = new HttpsRequest(socket, HTTP_GET, "https://httpbin.org/status/418");

Request logging

To make debugging easier you can log the raw request body that goes over the line. This also works with chunked encoding.

uint8_t *request_buffer = (uint8_t*)calloc(2048, 1);
req->set_request_log_buffer(request_buffer, 2048);

// after the request is done:
printf("\n----- Request buffer -----\n");
for (size_t ix = 0; ix < req->get_request_log_buffer_length(); ix++) {
    printf("%02x ", request_buffer[ix]);
}
printf("\n");

Integration tests

Integration tests are located in the TESTS folder and are ran through Greentea. Instructions on how to run the tests are in http-example.

Mbed OS 5.10 or lower

If you want to use this library on Mbed OS 5.10 or lower, you need to add the TLSSocket library to your project. This library is included in Mbed OS 5.11 and up.

Tested on

  • K64F with Ethernet.
  • NUCLEO_F411RE with ESP8266.
  • ODIN-W2 with WiFi.
  • K64F with Atmel 6LoWPAN shield.
  • DISCO-L475VG-IOT01A with WiFi.
  • Mbed Simulator.

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;