Experimental HTTP and HTTPS library for mbed OS 5
Embed:
(wiki syntax)
Show/hide line numbers
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, uint32_t body_size, uint32_t &size, bool skip_content_length = false) { 00062 const char* method_str = http_method_str(method); 00063 00064 bool is_chunked = has_header("Transfer-Encoding", "chunked"); 00065 00066 if (!is_chunked && (method == HTTP_POST || method == HTTP_PUT || method == HTTP_DELETE || body_size > 0)) { 00067 char buffer[10]; 00068 snprintf(buffer, 10, "%lu", body_size); 00069 set_header("Content-Length", string(buffer)); 00070 } 00071 00072 size = 0; 00073 00074 // first line is METHOD PATH+QUERY HTTP/1.1\r\n 00075 size += strlen(method_str) + 1 + strlen(parsed_url->path()) + (strlen(parsed_url->query()) ? strlen(parsed_url->query()) + 1 : 0) + 1 + 8 + 2; 00076 00077 // after that we'll do the headers 00078 typedef map<string, string>::iterator it_type; 00079 for(it_type it = headers.begin(); it != headers.end(); it++) { 00080 // line is KEY: VALUE\r\n 00081 size += it->first.length() + 1 + 1 + it->second.length() + 2; 00082 } 00083 00084 // then the body, first an extra newline 00085 size += 2; 00086 00087 if (!is_chunked) { 00088 // body 00089 size += body_size; 00090 } 00091 00092 // Now let's print it 00093 char* req = (char*)calloc(size + 1, 1); 00094 char* originalReq = req; 00095 00096 if (strlen(parsed_url->query())) { 00097 sprintf(req, "%s %s?%s HTTP/1.1\r\n", method_str, parsed_url->path(), parsed_url->query()); 00098 } else { 00099 sprintf(req, "%s %s%s HTTP/1.1\r\n", method_str, parsed_url->path(), parsed_url->query()); 00100 } 00101 req += strlen(method_str) + 1 + strlen(parsed_url->path()) + (strlen(parsed_url->query()) ? strlen(parsed_url->query()) + 1 : 0) + 1 + 8 + 2; 00102 00103 typedef map<string, string>::iterator it_type; 00104 for(it_type it = headers.begin(); it != headers.end(); it++) { 00105 // line is KEY: VALUE\r\n 00106 sprintf(req, "%s: %s\r\n", it->first.c_str(), it->second.c_str()); 00107 req += it->first.length() + 1 + 1 + it->second.length() + 2; 00108 } 00109 00110 sprintf(req, "\r\n"); 00111 req += 2; 00112 00113 if (body_size > 0) { 00114 memcpy(req, body, body_size); 00115 } 00116 req += body_size; 00117 00118 // Uncomment to debug... 00119 // printf("----- BEGIN REQUEST -----\n"); 00120 // printf("%s", originalReq); 00121 // printf("----- END REQUEST -----\n"); 00122 00123 return originalReq; 00124 } 00125 00126 private: 00127 bool has_header(const char* key, const char* value = NULL) { 00128 typedef map<string, string>::iterator it_type; 00129 for(it_type it = headers.begin(); it != headers.end(); it++) { 00130 if (strcmp(it->first.c_str(), key) == 0) { // key matches 00131 if (value == NULL || (strcmp(it->second.c_str(), value) == 0)) { // value is NULL or matches 00132 return true; 00133 } 00134 } 00135 } 00136 00137 return false; 00138 } 00139 00140 http_method method; 00141 ParsedUrl* parsed_url; 00142 map<string, string> headers; 00143 }; 00144 00145 #endif // _MBED_HTTP_REQUEST_BUILDER_H_
Generated on Sat Jul 16 2022 20:32:54 by
1.7.2