This repository was archived by the owner on Jul 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_request_builder.h
More file actions
145 lines (118 loc) · 4.72 KB
/
Copy pathhttp_request_builder.h
File metadata and controls
145 lines (118 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* PackageLicenseDeclared: Apache-2.0
* Copyright (c) 2017 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _MBED_HTTP_REQUEST_BUILDER_H_
#define _MBED_HTTP_REQUEST_BUILDER_H_
#include <string>
#include <map>
#include "http_parser.h"
#include "http_parsed_url.h"
class HttpRequestBuilder {
public:
HttpRequestBuilder(http_method a_method, ParsedUrl* a_parsed_url)
: method(a_method), parsed_url(a_parsed_url)
{
string host(parsed_url->host());
char port_str[10];
sprintf(port_str, ":%d", parsed_url->port());
if (strcmp(parsed_url->schema(), "http") == 0 && parsed_url->port() != 80) {
host += string(port_str);
}
else if (strcmp(parsed_url->schema(), "https") == 0 && parsed_url->port() != 443) {
host += string(port_str);
}
set_header("Host", host);
}
/**
* Set a header for the request
* If the key already exists, it will be overwritten...
*/
void set_header(string key, string value) {
map<string, string>::iterator it = headers.find(key);
if (it != headers.end()) {
it->second = value;
}
else {
headers.insert(headers.end(), pair<string, string>(key, value));
}
}
char* build(const void* body, uint32_t body_size, uint32_t &size, bool skip_content_length = false) {
const char* method_str = http_method_str(method);
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, "%lu", body_size);
set_header("Content-Length", string(buffer));
}
size = 0;
// first line is METHOD PATH+QUERY HTTP/1.1\r\n
size += strlen(method_str) + 1 + strlen(parsed_url->path()) + (strlen(parsed_url->query()) ? strlen(parsed_url->query()) + 1 : 0) + 1 + 8 + 2;
// after that we'll do the headers
typedef map<string, string>::iterator it_type;
for(it_type it = headers.begin(); it != headers.end(); it++) {
// line is KEY: VALUE\r\n
size += it->first.length() + 1 + 1 + it->second.length() + 2;
}
// then the body, first an extra newline
size += 2;
if (!is_chunked) {
// body
size += body_size;
}
// Now let's print it
char* req = (char*)calloc(size + 1, 1);
char* originalReq = req;
if (strlen(parsed_url->query())) {
sprintf(req, "%s %s?%s HTTP/1.1\r\n", method_str, parsed_url->path(), parsed_url->query());
} else {
sprintf(req, "%s %s%s HTTP/1.1\r\n", method_str, parsed_url->path(), parsed_url->query());
}
req += strlen(method_str) + 1 + strlen(parsed_url->path()) + (strlen(parsed_url->query()) ? strlen(parsed_url->query()) + 1 : 0) + 1 + 8 + 2;
typedef map<string, string>::iterator it_type;
for(it_type it = headers.begin(); it != headers.end(); it++) {
// line is KEY: VALUE\r\n
sprintf(req, "%s: %s\r\n", it->first.c_str(), it->second.c_str());
req += it->first.length() + 1 + 1 + it->second.length() + 2;
}
sprintf(req, "\r\n");
req += 2;
if (body_size > 0) {
memcpy(req, body, body_size);
}
req += body_size;
// Uncomment to debug...
// printf("----- BEGIN REQUEST -----\n");
// printf("%s", originalReq);
// printf("----- END REQUEST -----\n");
return originalReq;
}
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;
};
#endif // _MBED_HTTP_REQUEST_BUILDER_H_