mbed-http

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         // extra newline
00089         size += 2;
00090 
00091         // Now let's print it
00092         char* req = (char*)calloc(size + 1, 1);
00093         char* originalReq = req;
00094 
00095         if (strlen(parsed_url->query())) {
00096             sprintf(req, "%s %s?%s HTTP/1.1\r\n", method_str, parsed_url->path(), parsed_url->query());
00097         } else {
00098             sprintf(req, "%s %s%s HTTP/1.1\r\n", method_str, parsed_url->path(), parsed_url->query());
00099         }
00100         req += strlen(method_str) + 1 + strlen(parsed_url->path()) + (strlen(parsed_url->query()) ? strlen(parsed_url->query()) + 1 : 0) + 1 + 8 + 2;
00101 
00102         typedef map<string, string>::iterator it_type;
00103         for(it_type it = headers.begin(); it != headers.end(); it++) {
00104             // line is KEY: VALUE\r\n
00105             sprintf(req, "%s: %s\r\n", it->first.c_str(), it->second.c_str());
00106             req += it->first.length() + 1 + 1 + it->second.length() + 2;
00107         }
00108 
00109         sprintf(req, "\r\n");
00110         req += 2;
00111 
00112         if (body_size > 0) {
00113             memcpy(req, body, body_size);
00114         }
00115         req += body_size;
00116 
00117         sprintf(req, "\r\n");
00118         req += 2;
00119 
00120         // Uncomment to debug...
00121         // printf("----- BEGIN REQUEST -----\n");
00122         // printf("%s", originalReq);
00123         // printf("----- END REQUEST -----\n");
00124 
00125         return originalReq;
00126     }
00127 
00128 private:
00129     http_method method;
00130     ParsedUrl* parsed_url;
00131     map<string, string> headers;
00132 };
00133 
00134 #endif // _MBED_HTTP_REQUEST_BUILDER_H_