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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 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
   } catch(std::exception& e) { throw HTTP_Error(fmt("HTTP connection to {} failed: {}", hostname, e.what())); }
×
40

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

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

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

54
      if(std::chrono::system_clock::now() - start_time > timeout)
2✔
55
         throw HTTP_Error("Timeout while reading message body");
×
56

57
      oss.write(cast_uint8_ptr_to_char(buf.data()), static_cast<std::streamsize>(got));
1✔
58
   }
59

60
   return oss.str();
1✔
61
}
1✔
62

63
bool needs_url_encoding(char c) {
×
64
   if(c >= 'A' && c <= 'Z')
×
65
      return false;
66
   if(c >= 'a' && c <= 'z')
×
67
      return false;
68
   if(c >= '0' && c <= '9')
×
69
      return false;
70
   if(c == '-' || c == '_' || c == '.' || c == '~')
×
71
      return false;
×
72
   return true;
73
}
74

75
}
76

77
std::string url_encode(std::string_view in) {
×
78
   std::ostringstream out;
×
79

80
   for(auto c : in) {
×
81
      if(needs_url_encoding(c))
×
82
         out << '%' << hex_encode(cast_char_ptr_to_uint8(&c), 1);
×
83
      else
84
         out << c;
×
85
   }
86

87
   return out.str();
×
88
}
×
89

90
std::ostream& operator<<(std::ostream& o, const Response& resp) {
×
91
   o << "HTTP " << resp.status_code() << " " << resp.status_message() << "\n";
×
92
   for(const auto& h : resp.headers())
×
93
      o << "Header '" << h.first << "' = '" << h.second << "'\n";
×
94
   o << "Body " << std::to_string(resp.body().size()) << " bytes:\n";
×
95
   o.write(cast_uint8_ptr_to_char(resp.body().data()), resp.body().size());
×
96
   return o;
×
97
}
98

99
Response http_sync(const http_exch_fn& http_transact,
1✔
100
                   std::string_view verb,
101
                   std::string_view url,
102
                   std::string_view content_type,
103
                   const std::vector<uint8_t>& body,
104
                   size_t allowable_redirects) {
105
   if(url.empty())
1✔
106
      throw HTTP_Error("URL empty");
×
107

108
   const auto protocol_host_sep = url.find("://");
1✔
109
   if(protocol_host_sep == std::string::npos)
1✔
110
      throw HTTP_Error(fmt("Invalid URL '{}'", url));
×
111

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

114
   std::string hostname, loc, service;
1✔
115

116
   if(host_loc_sep == std::string::npos) {
1✔
117
      hostname = url.substr(protocol_host_sep + 3, std::string::npos);
1✔
118
      loc = "/";
1✔
119
   } else {
120
      hostname = url.substr(protocol_host_sep + 3, host_loc_sep - protocol_host_sep - 3);
×
121
      loc = url.substr(host_loc_sep, std::string::npos);
×
122
   }
123

124
   const auto port_sep = hostname.find(':');
1✔
125
   if(port_sep == std::string::npos) {
1✔
126
      service = "http";
1✔
127
      // hostname not modified
128
   } else {
129
      service = hostname.substr(port_sep + 1, std::string::npos);
×
130
      hostname = hostname.substr(0, port_sep);
×
131
   }
132

133
   std::ostringstream outbuf;
1✔
134

135
   outbuf << verb << " " << loc << " HTTP/1.0\r\n";
1✔
136
   outbuf << "Host: " << hostname << "\r\n";
1✔
137

138
   if(verb == "GET") {
1✔
139
      outbuf << "Accept: */*\r\n";
×
140
      outbuf << "Cache-Control: no-cache\r\n";
×
141
   } else if(verb == "POST")
1✔
142
      outbuf << "Content-Length: " << body.size() << "\r\n";
1✔
143

144
   if(!content_type.empty())
1✔
145
      outbuf << "Content-Type: " << content_type << "\r\n";
1✔
146
   outbuf << "Connection: close\r\n\r\n";
1✔
147
   outbuf.write(cast_uint8_ptr_to_char(body.data()), body.size());
1✔
148

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

151
   std::string line1;
1✔
152
   std::getline(io, line1);
1✔
153
   if(!io || line1.empty())
1✔
154
      throw HTTP_Error("No response");
×
155

156
   std::stringstream response_stream(line1);
1✔
157
   std::string http_version;
1✔
158
   unsigned int status_code;
1✔
159
   std::string status_message;
1✔
160

161
   response_stream >> http_version >> status_code;
1✔
162

163
   std::getline(response_stream, status_message);
1✔
164

165
   if(!response_stream || http_version.substr(0, 5) != "HTTP/")
2✔
166
      throw HTTP_Error("Not an HTTP response");
×
167

168
   std::map<std::string, std::string> headers;
1✔
169
   std::string header_line;
1✔
170
   while(std::getline(io, header_line) && header_line != "\r") {
11✔
171
      auto sep = header_line.find(": ");
10✔
172
      if(sep == std::string::npos || sep > header_line.size() - 2)
10✔
173
         throw HTTP_Error(fmt("Invalid HTTP header '{}'", header_line));
×
174
      const std::string key = header_line.substr(0, sep);
10✔
175

176
      if(sep + 2 < header_line.size() - 1) {
10✔
177
         const std::string val = header_line.substr(sep + 2, (header_line.size() - 1) - (sep + 2));
10✔
178
         headers[key] = val;
20✔
179
      }
10✔
180
   }
10✔
181

182
   if(status_code == 301 && headers.contains("Location")) {
1✔
183
      if(allowable_redirects == 0)
×
184
         throw HTTP_Error("HTTP redirection count exceeded");
×
185
      return GET_sync(headers["Location"], allowable_redirects - 1);
×
186
   }
187

188
   std::vector<uint8_t> resp_body;
1✔
189
   std::vector<uint8_t> buf(4096);
1✔
190
   while(io.good()) {
2✔
191
      io.read(cast_uint8_ptr_to_char(buf.data()), buf.size());
1✔
192
      const size_t got = static_cast<size_t>(io.gcount());
1✔
193
      resp_body.insert(resp_body.end(), buf.data(), &buf[got]);
1✔
194
   }
195

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

198
   if(!header_size.empty()) {
1✔
199
      if(resp_body.size() != to_u32bit(header_size))
1✔
200
         throw HTTP_Error(fmt("Content-Length disagreement, header says {} got {}", header_size, resp_body.size()));
×
201
   }
202

203
   return Response(status_code, status_message, resp_body, headers);
1✔
204
}
6✔
205

206
Response http_sync(std::string_view verb,
1✔
207
                   std::string_view url,
208
                   std::string_view content_type,
209
                   const std::vector<uint8_t>& body,
210
                   size_t allowable_redirects,
211
                   std::chrono::milliseconds timeout) {
212
   auto transact_with_timeout = [timeout](
2✔
213
                                   std::string_view hostname, std::string_view service, std::string_view message) {
214
      return http_transact(hostname, service, message, timeout);
1✔
215
   };
1✔
216

217
   return http_sync(transact_with_timeout, verb, url, content_type, body, allowable_redirects);
2✔
218
}
219

220
Response GET_sync(std::string_view url, size_t allowable_redirects, std::chrono::milliseconds timeout) {
×
221
   return http_sync("GET", url, "", std::vector<uint8_t>(), allowable_redirects, timeout);
×
222
}
223

224
Response POST_sync(std::string_view url,
1✔
225
                   std::string_view content_type,
226
                   const std::vector<uint8_t>& body,
227
                   size_t allowable_redirects,
228
                   std::chrono::milliseconds timeout) {
229
   return http_sync("POST", url, content_type, body, allowable_redirects, timeout);
1✔
230
}
231

232
}
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