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.
Fork of mbed-http by
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 set_header("Host", string(parsed_url->host())); 00032 } 00033 00034 /** 00035 * Set a header for the request 00036 * If the key already exists, it will be overwritten... 00037 */ 00038 void set_header(string key, string value) { 00039 map<string, string>::iterator it = headers.find(key); 00040 00041 if (it != headers.end()) { 00042 it->second = value; 00043 } 00044 else { 00045 headers.insert(headers.end(), pair<string, string>(key, value)); 00046 } 00047 } 00048 00049 char* build(const void* body, size_t body_size, size_t &size) { 00050 const char* method_str = http_method_str(method); 00051 00052 if (method == HTTP_POST || method == HTTP_PUT) { 00053 char buffer[10]; 00054 snprintf(buffer, 10, "%d", body_size); 00055 set_header("Content-Length", string(buffer)); 00056 } 00057 00058 // first line is METHOD PATH+QUERY HTTP/1.1\r\n 00059 size += strlen(method_str) + 1 + strlen(parsed_url->path()) + (strlen(parsed_url->query()) ? strlen(parsed_url->query()) + 1 : 0) + 1 + 8 + 2; 00060 00061 // after that we'll do the headers 00062 typedef map<string, string>::iterator it_type; 00063 for(it_type it = headers.begin(); it != headers.end(); it++) { 00064 // line is KEY: VALUE\r\n 00065 size += it->first.length() + 1 + 1 + it->second.length() + 2; 00066 } 00067 00068 // then the body, first an extra newline 00069 size += 2; 00070 00071 // body 00072 size += body_size; 00073 00074 // extra newline 00075 size += 2; 00076 00077 // Now let's print it 00078 char* req = (char*)calloc(size + 1, 1); 00079 char* originalReq = req; 00080 00081 if (strlen(parsed_url->query())) { 00082 sprintf(req, "%s %s?%s HTTP/1.1\r\n", method_str, parsed_url->path(), parsed_url->query()); 00083 } else { 00084 sprintf(req, "%s %s%s HTTP/1.1\r\n", method_str, parsed_url->path(), parsed_url->query()); 00085 } 00086 req += strlen(method_str) + 1 + strlen(parsed_url->path()) + (strlen(parsed_url->query()) ? strlen(parsed_url->query()) + 1 : 0) + 1 + 8 + 2; 00087 00088 typedef map<string, string>::iterator it_type; 00089 for(it_type it = headers.begin(); it != headers.end(); it++) { 00090 // line is KEY: VALUE\r\n 00091 sprintf(req, "%s: %s\r\n", it->first.c_str(), it->second.c_str()); 00092 req += it->first.length() + 1 + 1 + it->second.length() + 2; 00093 } 00094 00095 sprintf(req, "\r\n"); 00096 req += 2; 00097 00098 if (body_size > 0) { 00099 memcpy(req, body, body_size); 00100 } 00101 req += body_size; 00102 00103 sprintf(req, "\r\n"); 00104 req += 2; 00105 00106 // Uncomment to debug... 00107 // printf("----- BEGIN REQUEST -----\n"); 00108 // printf("%s", originalReq); 00109 // printf("----- END REQUEST -----\n"); 00110 00111 return originalReq; 00112 } 00113 00114 private: 00115 http_method method; 00116 ParsedUrl* parsed_url; 00117 map<string, string> headers; 00118 }; 00119 00120 #endif // _MBED_HTTP_REQUEST_BUILDER_H_
Generated on Fri Jul 22 2022 08:07:18 by
1.7.2
