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

randombit / botan / 19204652950

09 Nov 2025 06:35AM UTC coverage: 90.617% (-0.005%) from 90.622%
19204652950

Pull #4995

github

web-flow
Merge effd01cea into e27d33208
Pull Request #4995: Reduce/Fix cppcheck warnings in utils directory

100607 of 111025 relevant lines covered (90.62%)

12696390.86 hits per line

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

67.97
/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/mem_ops.h>
12
#include <botan/internal/fmt.h>
13
#include <botan/internal/mem_utils.h>
14
#include <botan/internal/parsing.h>
15
#include <botan/internal/socket.h>
16
#include <iomanip>
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(as_span_of_bytes(message));
1✔
46

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

51
   std::ostringstream oss;
1✔
52
   std::vector<uint8_t> buf(DefaultBufferSize);
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) {
1✔
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 << '%' << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(c);
×
93
         out << std::dec << std::nouppercase;  // reset flags
×
94
      } else {
95
         out << c;
×
96
      }
97
   }
98

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

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

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

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

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

129
   const auto [hostname, location, service] = [&]() {
1✔
130
      std::string host;
1✔
131
      std::string loc;
1✔
132
      std::string svc;
1✔
133

134
      if(host_loc_sep == std::string::npos) {
1✔
135
         host = url.substr(protocol_host_sep + 3);
1✔
136
         loc = "/";
1✔
137
      } else {
138
         host = url.substr(protocol_host_sep + 3, host_loc_sep - protocol_host_sep - 3);
×
139
         loc = url.substr(host_loc_sep);
×
140
      }
141

142
      const auto port_sep = host.find(':');
1✔
143
      if(port_sep == std::string::npos) {
1✔
144
         svc = "http";
1✔
145
         // hostname not modified
146
      } else {
147
         svc = host.substr(port_sep + 1, std::string::npos);
×
148
         host.resize(port_sep);  // Keep only hostname part, remove port number
×
149
      }
150

151
      return std::tuple(host, loc, svc);
1✔
152
   }();
2✔
153

154
   std::ostringstream outbuf;
1✔
155
   outbuf << verb << " " << location << " HTTP/1.0\r\n";
1✔
156
   outbuf << "Host: " << hostname << "\r\n";
1✔
157

158
   if(verb == "GET") {
1✔
159
      outbuf << "Accept: */*\r\n";
×
160
      outbuf << "Cache-Control: no-cache\r\n";
×
161
   } else if(verb == "POST") {
1✔
162
      outbuf << "Content-Length: " << body.size() << "\r\n";
1✔
163
   }
164

165
   if(!content_type.empty()) {
1✔
166
      outbuf << "Content-Type: " << content_type << "\r\n";
1✔
167
   }
168
   outbuf << "Connection: close\r\n\r\n";
1✔
169
   outbuf.write(cast_uint8_ptr_to_char(body.data()), body.size());
1✔
170

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

173
   std::string line1;
1✔
174
   std::getline(io, line1);
1✔
175
   if(!io || line1.empty()) {
1✔
176
      throw HTTP_Error("No response");
×
177
   }
178

179
   std::stringstream response_stream(line1);
1✔
180
   std::string http_version;
1✔
181
   unsigned int status_code = 0;
1✔
182
   std::string status_message;
1✔
183

184
   response_stream >> http_version >> status_code;
1✔
185

186
   std::getline(response_stream, status_message);
1✔
187

188
   if(!response_stream || !http_version.starts_with("HTTP/")) {
2✔
189
      throw HTTP_Error("Not an HTTP response");
×
190
   }
191

192
   std::map<std::string, std::string> headers;
1✔
193
   std::string header_line;
1✔
194
   while(std::getline(io, header_line) && header_line != "\r") {
9✔
195
      auto sep = header_line.find(": ");
8✔
196
      if(sep == std::string::npos || sep > header_line.size() - 2) {
8✔
197
         throw HTTP_Error(fmt("Invalid HTTP header '{}'", header_line));
×
198
      }
199
      const std::string key = header_line.substr(0, sep);
8✔
200

201
      if(sep + 2 < header_line.size() - 1) {
8✔
202
         const std::string val = header_line.substr(sep + 2, (header_line.size() - 1) - (sep + 2));
8✔
203
         headers[key] = val;
16✔
204
      }
8✔
205
   }
8✔
206

207
   if(status_code == 301 && headers.contains("Location")) {
1✔
208
      if(allowable_redirects == 0) {
×
209
         throw HTTP_Error("HTTP redirection count exceeded");
×
210
      }
211
      return GET_sync(headers["Location"], allowable_redirects - 1);
×
212
   }
213

214
   std::vector<uint8_t> resp_body;
1✔
215
   std::vector<uint8_t> buf(4096);
1✔
216
   while(io.good()) {
1✔
217
      io.read(cast_uint8_ptr_to_char(buf.data()), buf.size());
1✔
218
      const size_t got = static_cast<size_t>(io.gcount());
1✔
219
      resp_body.insert(resp_body.end(), buf.data(), &buf[got]);
2✔
220
   }
221

222
   auto cl_hdr = headers.find("Content-Length");
1✔
223
   if(cl_hdr != headers.end()) {
1✔
224
      const std::string header_size = cl_hdr->second;
1✔
225
      if(resp_body.size() != to_u32bit(header_size)) {
1✔
226
         throw HTTP_Error(fmt("Content-Length disagreement, header says {} got {}", header_size, resp_body.size()));
×
227
      }
228
   }
1✔
229

230
   return Response(status_code, status_message, resp_body, headers);
1✔
231
}
2✔
232

233
Response http_sync(std::string_view verb,
1✔
234
                   std::string_view url,
235
                   std::string_view content_type,
236
                   const std::vector<uint8_t>& body,
237
                   size_t allowable_redirects,
238
                   std::chrono::milliseconds timeout) {
239
   auto transact_with_timeout = [timeout](
2✔
240
                                   std::string_view hostname, std::string_view service, std::string_view message) {
241
      return http_transact(hostname, service, message, timeout);
1✔
242
   };
1✔
243

244
   return http_sync(transact_with_timeout, verb, url, content_type, body, allowable_redirects);
2✔
245
}
246

247
Response GET_sync(std::string_view url, size_t allowable_redirects, std::chrono::milliseconds timeout) {
×
248
   return http_sync("GET", url, "", std::vector<uint8_t>(), allowable_redirects, timeout);
×
249
}
250

251
Response POST_sync(std::string_view url,
1✔
252
                   std::string_view content_type,
253
                   const std::vector<uint8_t>& body,
254
                   size_t allowable_redirects,
255
                   std::chrono::milliseconds timeout) {
256
   return http_sync("POST", url, content_type, body, allowable_redirects, timeout);
1✔
257
}
258

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

© 2026 Coveralls, Inc