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.
Dependents: MQTTGateway2 MQTTGatewayK64 http-example-wnc GuardRoom ... more
Diff: source/http_request_builder.h
- Revision:
- 23:15fa2726f793
- Parent:
- 21:fcd2bfd31a39
- Child:
- 26:fe4e03a404fb
diff -r 71fc1b1894f8 -r 15fa2726f793 source/http_request_builder.h
--- a/source/http_request_builder.h Wed Jan 03 11:23:22 2018 +0000
+++ b/source/http_request_builder.h Thu Jan 11 13:49:06 2018 +0100
@@ -58,10 +58,12 @@
}
}
- char* build(const void* body, size_t body_size, size_t &size) {
+ char* build(const void* body, size_t body_size, size_t &size, bool skip_content_length = false) {
const char* method_str = http_method_str(method);
- if (method == HTTP_POST || method == HTTP_PUT || method == HTTP_DELETE || body_size > 0) {
+ bool is_chunked = has_header("Transfer-Encoding", "chunked");
+
+ if (!is_chunked && (method == HTTP_POST || method == HTTP_PUT || method == HTTP_DELETE || body_size > 0)) {
char buffer[10];
snprintf(buffer, 10, "%d", body_size);
set_header("Content-Length", string(buffer));
@@ -82,11 +84,13 @@
// then the body, first an extra newline
size += 2;
- // body
- size += body_size;
+ if (!is_chunked) {
+ // body
+ size += body_size;
- // extra newline
- size += 2;
+ // extra newline
+ size += 2;
+ }
// Now let's print it
char* req = (char*)calloc(size + 1, 1);
@@ -114,8 +118,10 @@
}
req += body_size;
- sprintf(req, "\r\n");
- req += 2;
+ if (!is_chunked) {
+ sprintf(req, "\r\n");
+ req += 2;
+ }
// Uncomment to debug...
// printf("----- BEGIN REQUEST -----\n");
@@ -126,6 +132,19 @@
}
private:
+ bool has_header(const char* key, const char* value = NULL) {
+ typedef map<string, string>::iterator it_type;
+ for(it_type it = headers.begin(); it != headers.end(); it++) {
+ if (strcmp(it->first.c_str(), key) == 0) { // key matches
+ if (value == NULL || (strcmp(it->second.c_str(), value) == 0)) { // value is NULL or matches
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
http_method method;
ParsedUrl* parsed_url;
map<string, string> headers;