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

randombit / botan / 5133556677

31 May 2023 02:11PM UTC coverage: 91.735% (-0.3%) from 92.012%
5133556677

Pull #3568

github

web-flow
Merge de48a2eb6 into 1cbeffafb
Pull Request #3568: Change clang-format AllowShortBlocksOnASingleLine from true to Empty

76059 of 82912 relevant lines covered (91.73%)

12004312.75 hits per line

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

64.52
/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) {
×
41
      throw HTTP_Error(fmt("HTTP connection to {} failed: {}", hostname, e.what()));
×
42
   }
×
43

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

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

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

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

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

66
   return oss.str();
1✔
67
}
1✔
68

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

85
}  // namespace
86

87
std::string url_encode(std::string_view in) {
×
88
   std::ostringstream out;
×
89

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

98
   return out.str();
×
99
}
×
100

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

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

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

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

128
   std::string hostname, loc, service;
1✔
129

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

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

147
   std::ostringstream outbuf;
1✔
148

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

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

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

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

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

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

178
   response_stream >> http_version >> status_code;
1✔
179

180
   std::getline(response_stream, status_message);
1✔
181

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

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

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

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

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

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

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

224
   return Response(status_code, status_message, resp_body, headers);
1✔
225
}
6✔
226

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

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

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

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

253
}  // 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