fix superfluous \r\n after body

Fork of mbed-http by sandbox

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers http_request_builder.h Source File

http_request_builder.h

00001 /*
00002  * PackageLicenseDeclared: Apache-2.0
00003  * Copyright (c) 2017 ARM Limited
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #ifndef _MBED_HTTP_REQUEST_BUILDER_H_
00019 #define _MBED_HTTP_REQUEST_BUILDER_H_
00020 
00021 #include <string>
00022 #include <map>
00023 #include "http_parser.h"
00024 #include "http_parsed_url.h"
00025 
00026 class HttpRequestBuilder {
00027 public:
00028     HttpRequestBuilder(http_method a_method, ParsedUrl* a_parsed_url)
00029         : method(a_method), parsed_url(a_parsed_url)
00030     {
00031         string host(parsed_url->host());
00032 
00033         char port_str[10];
00034         sprintf(port_str, ":%d", parsed_url->port());
00035 
00036         if (strcmp(parsed_url->schema(), "http") == 0 && parsed_url->port() != 80) {
00037             host += string(port_str);
00038         }
00039         else if (strcmp(parsed_url->schema(), "https") == 0 && parsed_url->port() != 443) {
00040             host += string(port_str);
00041         }
00042 
00043         set_header("Host", host);
00044     }
00045 
00046     /**
00047      * Set a header for the request
00048      * If the key already exists, it will be overwritten...
00049      */
00050     void set_header(string key, string value) {
00051         map<string, string>::iterator it = headers.find(key);
00052 
00053         if (it != headers.end()) {
00054             it->second = value;
00055         }
00056         else {
00057             headers.insert(headers.end(), pair<string, string>(key, value));
00058         }
00059     }
00060 
00061     char* build(const void* body, size_t body_size, size_t &size) {
00062         const char* method_str = http_method_str(method);
00063 
00064         if (method == HTTP_POST || method == HTTP_PUT || method == HTTP_DELETE || body_size > 0) {
00065             char buffer[10];
00066             snprintf(buffer, 10, "%d", body_size);
00067             set_header("Content-Length", string(buffer));
00068         }
00069 
00070         size = 0;
00071 
00072         // first line is METHOD PATH+QUERY HTTP/1.1\r\n
00073         size += strlen(method_str) + 1 + strlen(parsed_url->path()) + (strlen(parsed_url->query()) ? strlen(parsed_url->query()) + 1 : 0) + 1 + 8 + 2;
00074 
00075         // after that we'll do the headers
00076         typedef map<string, string>::iterator it_type;
00077         for(it_type it = headers.begin(); it != headers.end(); it++) {
00078             // line is KEY: VALUE\r\n
00079             size += it->first.length() + 1 + 1 + it->second.length() + 2;
00080         }
00081 
00082         // then the body, first an extra newline
00083         size += 2;
00084 
00085         // body
00086         size += body_size;
00087 
00088         // Now let's print it
00089         char* req = (char*)calloc(size + 1, 1);
00090         char* originalReq = req;
00091 
00092         if (strlen(parsed_url->query())) {
00093             sprintf(req, "%s %s?%s HTTP/1.1\r\n", method_str, parsed_url->path(), parsed_url->query());
00094         } else {
00095             sprintf(req, "%s %s%s HTTP/1.1\r\n", method_str, parsed_url->path(), parsed_url->query());
00096         }
00097         req += strlen(method_str) + 1 + strlen(parsed_url->path()) + (strlen(parsed_url->query()) ? strlen(parsed_url->query()) + 1 : 0) + 1 + 8 + 2;
00098 
00099         typedef map<string, string>::iterator it_type;
00100         for(it_type it = headers.begin(); it != headers.end(); it++) {
00101             // line is KEY: VALUE\r\n
00102             sprintf(req, "%s: %s\r\n", it->first.c_str(), it->second.c_str());
00103             req += it->first.length() + 1 + 1 + it->second.length() + 2;
00104         }
00105 
00106         sprintf(req, "\r\n");
00107         req += 2;
00108 
00109         if (body_size > 0) {
00110             memcpy(req, body, body_size);
00111         }
00112         req += body_size;
00113 
00114         // Uncomment to debug...
00115         // printf("----- BEGIN REQUEST -----\n");
00116         // printf("%s", originalReq);
00117         // printf("----- END REQUEST -----\n");
00118 
00119         return originalReq;
00120     }
00121 
00122 private:
00123     http_method method;
00124     ParsedUrl* parsed_url;
00125     map<string, string> headers;
00126 };
00127 
00128 #endif // _MBED_HTTP_REQUEST_BUILDER_H_