• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

65.57
/src/lib/utils/http_util/http_util.cpp
1
/*
2
* Sketchy HTTP client
3
* (C) 2013,2016 Jack Lloyd
4
*     2017 René Korthaus, Rohde & Schwarz Cybersecurity
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8

9
#include <botan/internal/http_util.h>
10

11
#include <botan/hex.h>
12
#include <botan/internal/fmt.h>
13
#include <botan/internal/os_utils.h>
14
#include <botan/internal/parsing.h>
15
#include <botan/internal/socket.h>
16
#include <botan/internal/stl_util.h>
17
#include <sstream>
18

19
namespace Botan::HTTP {
20

21
namespace {
22

23
/*
24
* Connect to a host, write some bytes, then read until the server
25
* closes the socket.
26
*/
27
std::string http_transact(std::string_view hostname,
1✔
28
                          std::string_view service,
29
                          std::string_view message,
30
                          std::chrono::milliseconds timeout) {
31
   std::unique_ptr<OS::Socket> socket;
1✔
32

33
   const std::chrono::system_clock::time_point start_time = std::chrono::system_clock::now();
1✔
34

35
   try {
1✔
36
      socket = OS::open_socket(hostname, service, timeout);
1✔
37
      if(!socket) {
1✔
38
         throw Not_Implemented("No socket support enabled in build");
×
39
      }
40
   } catch(std::exception& e) { throw HTTP_Error(fmt("HTTP connection to {} failed: {}", hostname, e.what())); }
×
41

42
   // Blocks until entire message has been written
43
   socket->write(cast_char_ptr_to_uint8(message.data()), message.size());
1✔
44

45
   if(std::chrono::system_clock::now() - start_time > timeout) {
2✔
46
      throw HTTP_Error("Timeout during writing message body");
×
47
   }
48

49
   std::ostringstream oss;
1✔
50
   std::vector<uint8_t> buf(BOTAN_DEFAULT_BUFFER_SIZE);
1✔
51
   while(true) {
2✔
52
      const size_t got = socket->read(buf.data(), buf.size());
2✔
53
      if(got == 0) {  // EOF
2✔
54
         break;
55
      }
56

57
      if(std::chrono::system_clock::now() - start_time > timeout) {
2✔
58
         throw HTTP_Error("Timeout while reading message body");
×
59
      }
60

61
      oss.write(cast_uint8_ptr_to_char(buf.data()), static_cast<std::streamsize>(got));
1✔
62
   }
63

64
   return oss.str();
1✔
65
}
1✔
66

67
bool needs_url_encoding(char c) {
×
68
   if(c >= 'A' && c <= 'Z') {
×
69
      return false;
70
   }
71
   if(c >= 'a' && c <= 'z') {
×
72
      return false;
73
   }
74
   if(c >= '0' && c <= '9') {
×
75
      return false;
76
   }
77
   if(c == '-' || c == '_' || c == '.' || c == '~') {
×
78
      return false;
×
79
   }
80
   return true;
81
}
82

83
}  // namespace
84

85
std::string url_encode(std::string_view in) {
×
86
   std::ostringstream out;
×
87

88
   for(auto c : in) {
×
89
      if(needs_url_encoding(c)) {
×
90
         out << '%' << hex_encode(cast_char_ptr_to_uint8(&c), 1);
×
91
      } else {
92
         out << c;
×
93
      }
94
   }
95

96
   return out.str();
×
97
}
×
98

99
std::ostream& operator<<(std::ostream& o, const Response& resp) {
×
100
   o << "HTTP " << resp.status_code() << " " << resp.status_message() << "\n";
×
101
   for(const auto& h : resp.headers()) {
×
102
      o << "Header '" << h.first << "' = '" << h.second << "'\n";
×
103
   }
104
   o << "Body " << std::to_string(resp.body().size()) << " bytes:\n";
×
105
   o.write(cast_uint8_ptr_to_char(resp.body().data()), resp.body().size());
×
106
   return o;
×
107
}
108

109
Response http_sync(const http_exch_fn& http_transact,
1✔
110
                   std::string_view verb,
111
                   std::string_view url,
112
                   std::string_view content_type,
113
                   const std::vector<uint8_t>& body,
114
                   size_t allowable_redirects) {
115
   if(url.empty()) {
1✔
116
      throw HTTP_Error("URL empty");
×
117
   }
118

119
   const auto protocol_host_sep = url.find("://");
1✔
120
   if(protocol_host_sep == std::string::npos) {
1✔
121
      throw HTTP_Error(fmt("Invalid URL '{}'", url));
×
122
   }
123

124
   const auto host_loc_sep = url.find('/', protocol_host_sep + 3);
1✔
125

126
   std::string hostname, loc, service;
1✔
127

128
   if(host_loc_sep == std::string::npos) {
1✔
129
      hostname = url.substr(protocol_host_sep + 3, std::string::npos);
1✔
130
      loc = "/";
1✔
131
   } else {
132
      hostname = url.substr(protocol_host_sep + 3, host_loc_sep - protocol_host_sep - 3);
×
133
      loc = url.substr(host_loc_sep, std::string::npos);
×
134
   }
135

136
   const auto port_sep = hostname.find(':');
1✔
137
   if(port_sep == std::string::npos) {
1✔
138
      service = "http";
1✔
139
      // hostname not modified
140
   } else {
141
      service = hostname.substr(port_sep + 1, std::string::npos);
×
142
      hostname = hostname.substr(0, port_sep);
×
143
   }
144

145
   std::ostringstream outbuf;
1✔
146

147
   outbuf << verb << " " << loc << " HTTP/1.0\r\n";
1✔
148
   outbuf << "Host: " << hostname << "\r\n";
1✔
149

150
   if(verb == "GET") {
1✔
151
      outbuf << "Accept: */*\r\n";
×
152
      outbuf << "Cache-Control: no-cache\r\n";
×
153
   } else if(verb == "POST") {
1✔
154
      outbuf << "Content-Length: " << body.size() << "\r\n";
1✔
155
   }
156

157
   if(!content_type.empty()) {
1✔
158
      outbuf << "Content-Type: " << content_type << "\r\n";
1✔
159
   }
160
   outbuf << "Connection: close\r\n\r\n";
1✔
161
   outbuf.write(cast_uint8_ptr_to_char(body.data()), body.size());
1✔
162

163
   std::istringstream io(http_transact(hostname, service, outbuf.str()));
3✔
164

165
   std::string line1;
1✔
166
   std::getline(io, line1);
1✔
167
   if(!io || line1.empty()) {
1✔
168
      throw HTTP_Error("No response");
×
169
   }
170

171
   std::stringstream response_stream(line1);
1✔
172
   std::string http_version;
1✔
173
   unsigned int status_code;
1✔
174
   std::string status_message;
1✔
175

176
   response_stream >> http_version >> status_code;
1✔
177

178
   std::getline(response_stream, status_message);
1✔
179

180
   if(!response_stream || http_version.substr(0, 5) != "HTTP/") {
2✔
181
      throw HTTP_Error("Not an HTTP response");
×
182
   }
183

184
   std::map<std::string, std::string> headers;
1✔
185
   std::string header_line;
1✔
186
   while(std::getline(io, header_line) && header_line != "\r") {
7✔
187
      auto sep = header_line.find(": ");
6✔
188
      if(sep == std::string::npos || sep > header_line.size() - 2) {
6✔
189
         throw HTTP_Error(fmt("Invalid HTTP header '{}'", header_line));
×
190
      }
191
      const std::string key = header_line.substr(0, sep);
6✔
192

193
      if(sep + 2 < header_line.size() - 1) {
6✔
194
         const std::string val = header_line.substr(sep + 2, (header_line.size() - 1) - (sep + 2));
6✔
195
         headers[key] = val;
12✔
196
      }
6✔
197
   }
6✔
198

199
   if(status_code == 301 && headers.contains("Location")) {
1✔
200
      if(allowable_redirects == 0) {
×
201
         throw HTTP_Error("HTTP redirection count exceeded");
×
202
      }
203
      return GET_sync(headers["Location"], allowable_redirects - 1);
×
204
   }
205

206
   std::vector<uint8_t> resp_body;
1✔
207
   std::vector<uint8_t> buf(4096);
1✔
208
   while(io.good()) {
2✔
209
      io.read(cast_uint8_ptr_to_char(buf.data()), buf.size());
1✔
210
      const size_t got = static_cast<size_t>(io.gcount());
1✔
211
      resp_body.insert(resp_body.end(), buf.data(), &buf[got]);
1✔
212
   }
213

214
   const std::string header_size = search_map(headers, std::string("Content-Length"));
2✔
215

216
   if(!header_size.empty()) {
1✔
217
      if(resp_body.size() != to_u32bit(header_size)) {
1✔
218
         throw HTTP_Error(fmt("Content-Length disagreement, header says {} got {}", header_size, resp_body.size()));
×
219
      }
220
   }
221

222
   return Response(status_code, status_message, resp_body, headers);
1✔
223
}
6✔
224

225
Response http_sync(std::string_view verb,
1✔
226
                   std::string_view url,
227
                   std::string_view content_type,
228
                   const std::vector<uint8_t>& body,
229
                   size_t allowable_redirects,
230
                   std::chrono::milliseconds timeout) {
231
   auto transact_with_timeout = [timeout](
2✔
232
                                   std::string_view hostname, std::string_view service, std::string_view message) {
233
      return http_transact(hostname, service, message, timeout);
1✔
234
   };
1✔
235

236
   return http_sync(transact_with_timeout, verb, url, content_type, body, allowable_redirects);
2✔
237
}
238

239
Response GET_sync(std::string_view url, size_t allowable_redirects, std::chrono::milliseconds timeout) {
×
240
   return http_sync("GET", url, "", std::vector<uint8_t>(), allowable_redirects, timeout);
×
241
}
242

243
Response POST_sync(std::string_view url,
1✔
244
                   std::string_view content_type,
245
                   const std::vector<uint8_t>& body,
246
                   size_t allowable_redirects,
247
                   std::chrono::milliseconds timeout) {
248
   return http_sync("POST", url, content_type, body, allowable_redirects, timeout);
1✔
249
}
250

251
}  // namespace Botan::HTTP
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc