-
Notifications
You must be signed in to change notification settings - Fork 766
Expand file tree
/
Copy pathsc_http.cpp
More file actions
408 lines (320 loc) · 10.7 KB
/
Copy pathsc_http.cpp
File metadata and controls
408 lines (320 loc) · 10.7 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// ==========================================================================
// Dedmonwakeen's Raid DPS/TPS Simulator.
// Send questions to natehieter@gmail.com
// ==========================================================================
#include "sc_http.hpp"
#include "util/concurrency.hpp"
#include "util/io.hpp"
#include "util/util.hpp"
#include <iostream>
#include <cstring>
// Cross-Platform Support for HTTP-Download =================================
// ==========================================================================
// PLATFORM INDEPENDENT SECTION
// ==========================================================================
cache::cache_control_t cache::cache_control_t::singleton;
#ifdef SC_NO_NETWORKING
namespace http
{
void set_proxy( util::string_view, util::string_view, const unsigned) {}
const proxy_t& get_proxy() { static proxy_t p {}; return p; }
http_connection_pool_t* pool() { return nullptr; }
void cache_load( const std::string&) {}
void cache_save( const std::string&) {}
void clear_cache() {}
int get( std::string& /*result */,
const std::string& /* url */,
cache::behavior_e /* caching */,
const std::string& /* confirmation */,
const std::vector<std::string>& /* headers */ )
{
return 503;
}
std::tuple<std::string, std::string> normalize_header( util::string_view )
{
return {};
}
} // Namespace http ends
#else
#ifdef SC_USE_CURL
#include "sc_http_curl.hpp"
http::curl_connection_pool_t connection_pool;
#endif /* SC_USE_CURL */
#ifdef SC_USE_WININET
#include "sc_http_wininet.hpp"
http::wininet_connection_pool_t connection_pool;
#endif /* SC_USE_WININET */
namespace { // UNNAMED NAMESPACE ==========================================
http::proxy_t proxy;
const bool HTTP_CACHE_DEBUG = false;
mutex_t cache_mutex;
struct url_cache_entry_t
{
// Not necessarily UTF-8; may contain zero bytes. Should really be vector<uint8_t>.
std::string result;
std::string last_modified_header;
cache::cache_era modified, validated;
url_cache_entry_t() :
modified( cache::cache_era::INVALID ), validated( cache::cache_era::INVALID )
{}
};
using url_db_t = std::unordered_map<std::string, url_cache_entry_t>;
url_db_t url_db;
// cache_clear ==============================================================
void cache_clear()
{
// writer lock
auto_lock_t lock( cache_mutex );
url_db.clear();
}
// download =================================================================
int download( url_cache_entry_t& entry,
std::string& result,
const std::string& url,
const std::vector<std::string>& headers = {} )
{
auto handle = connection_pool.handle( url );
// Curl was not able to initialize, get out instantly
if ( !handle->initialized() )
{
std::cerr << "Unable to initialize networking functionality" << std::endl;
return 500;
}
handle->add_request_headers( headers );
// Add If-Modified-Since
if ( !entry.last_modified_header.empty() )
{
handle->add_request_header( "If-Modified-Since: " + entry.last_modified_header );
}
auto res = handle->get( url );
int response_code = handle->response_code();
if ( !res )
{
std::cerr << "Unable to fetch result (" << handle->error() << ")" << std::endl;
return handle->response_code();
}
// Unconditionally return response body
result.assign( handle->result() );
// Cache results on successful fetch
if ( response_code == 200 )
{
entry.last_modified_header.clear();
entry.result.assign( handle->result() );
auto response_headers = handle->headers();
if ( response_headers.find( "last-modified" ) != response_headers.end() )
{
entry.last_modified_header = response_headers[ "last-modified" ];
}
entry.validated = cache::era();
entry.modified = cache::era();
}
// Re-use cached copy, validate the cache
else if ( response_code == 304 )
{
entry.validated = cache::era();
result.assign( entry.result );
// Everything is ok so return 200 instead of 304
response_code = 200;
}
return response_code;
}
} // UNNAMED NAMESPACE ====================================================
// http::set_proxy ==========================================================
void http::set_proxy( util::string_view proxy_type,
util::string_view proxy_host,
const unsigned proxy_port )
{
proxy.type = std::string( proxy_type );
proxy.host = std::string( proxy_host );
proxy.port = proxy_port;
}
const http::proxy_t& http::get_proxy()
{
return proxy;
}
http::http_handle_t::http_handle_t() = default;
http::http_handle_t::~http_handle_t() = default;
http::http_connection_pool_t::http_connection_pool_t() = default;
http::http_connection_pool_t::~http_connection_pool_t() = default;
// http::clear_cache ========================================================
void http::clear_cache()
{
cache_clear();
}
// http::cache_load =========================================================
namespace {
std::string cache_get( std::istream& is )
{
std::string result;
while ( is )
{
unsigned char c = is.get();
if ( ! c )
break;
result += c;
}
return result;
}
void cache_put( std::ostream& os, const std::string& s )
{ os.write( s.c_str(), s.size() + 1 ); }
void cache_put( std::ostream& os, const char* s )
{ os.write( s, std::strlen( s ) + 1 ); }
} // unnamed namespace
void http::cache_load( const std::string& file_name )
{
auto_lock_t lock( cache_mutex );
try
{
io::ifstream file;
file.open( file_name, std::ios::binary );
if ( !file ) return;
file.exceptions( std::ios::eofbit | std::ios::failbit | std::ios::badbit );
file.unsetf( std::ios::skipws );
if ( cache_get( file ) != SC_VERSION )
{
// invalid version, GTFO
return;
}
std::string content;
content.reserve( 16 * 1024 );
while ( ! file.eof() )
{
std::string url = cache_get( file );
std::string last_modified = cache_get( file );
uint32_t size;
file.read( reinterpret_cast<char*>( &size ), sizeof( size ) );
content.resize( size );
file.read( &content[ 0 ], size );
url_cache_entry_t& c = url_db[ url ];
c.result = content;
c.last_modified_header = last_modified;
c.modified = c.validated = cache::cache_era::IN_THE_BEGINNING;
}
}
catch ( ... )
{}
}
// http::cache_save =========================================================
void http::cache_save( const std::string& file_name )
{
auto_lock_t lock( cache_mutex );
try
{
io::ofstream file;
file.open( file_name, std::ios::binary );
if ( ! file ) return;
file.exceptions( std::ios::eofbit | std::ios::failbit | std::ios::badbit );
cache_put( file, SC_VERSION );
for ( const auto& entry : url_db )
{
if ( entry.second.validated == cache::cache_era::INVALID )
continue;
cache_put( file, entry.first );
cache_put( file, entry.second.last_modified_header );
uint32_t size = as<uint32_t>( entry.second.result.size() );
file.write( reinterpret_cast<const char*>( &size ), sizeof( size ) );
file.write( entry.second.result.data(), size );
}
}
catch ( ... )
{}
}
// http::pool ===============================================================
http::http_connection_pool_t* http::pool()
{
return &connection_pool;
}
// http::get ================================================================
int http::get( std::string& result,
const std::string& url,
cache::behavior_e caching,
const std::string& confirmation,
const std::vector<std::string>& headers )
{
result.clear();
std::string encoded_url = url;
util::urlencode( encoded_url );
int response_code = 200;
auto_lock_t lock( cache_mutex );
url_cache_entry_t& entry = url_db[ encoded_url ];
if ( HTTP_CACHE_DEBUG )
{
io::ofstream http_log;
http_log.open( "simc_http_log.txt", std::ios::app );
std::ostream::sentry s( http_log );
if ( s )
{
fmt::print( http_log, "{}: get(\"{}\") [", cache::era(), url );
if ( entry.validated != cache::cache_era::INVALID )
{
if ( entry.validated >= cache::era() )
http_log << "hot";
else if ( caching != cache::CURRENT )
http_log << "warm";
else
http_log << "cold";
fmt::print( http_log, ": ({},{})", entry.modified, entry.validated );
}
else
http_log << "miss";
if ( caching != cache::ONLY &&
( entry.validated == cache::cache_era::INVALID ||
( caching == cache::CURRENT && entry.validated < cache::era() ) ) )
http_log << " download";
http_log << "]\n";
}
}
if ( entry.validated < cache::era() && ( caching == cache::CURRENT || entry.validated == cache::cache_era::INVALID ) )
{
if ( caching == cache::ONLY )
return 404;
fmt::print( "@" ); std::fflush( stdout );
response_code = download( entry, result, encoded_url, headers );
if ( HTTP_CACHE_DEBUG && entry.modified < entry.validated )
{
io::ofstream http_log;
http_log.open( "simc_http_log.txt", std::ios::app );
fmt::print( http_log, "{}: Unmodified ({},{})\n", cache::era(), entry.modified, entry.validated );
}
if ( !confirmation.empty() && ( entry.result.find( confirmation ) == std::string::npos ) )
{
//fmt::print( "\nsimulationcraft: HTTP failed on '{}'\n", url );
//fmt::print( "{}\n", ( result.empty() ? "empty" : result.c_str() ) );
//fflush( stdout );
return 409; // "Conflict"
}
}
// No result from the download process, grab it from the cache, only if the download process was
// returned OK status.
if ( result.empty() && response_code == 200 )
{
result = entry.result;
}
return response_code;
}
std::tuple<std::string, std::string> http::normalize_header( util::string_view header_str )
{
// Find first ':'
auto pos = header_str.find( ':' );
// Don't support foldable headers since they are deprecated anyhow
if ( pos == std::string::npos || header_str[ 0 ] == ' ' )
{
return {};
}
auto key = std::string( header_str.substr( 0, pos ) );
auto value = std::string( header_str.substr( pos + 1 ) );
// Transform all header keys to lowercase to sanitize the input
std::transform( key.begin(), key.end(), key.begin(), tolower );
// Prune whitespaces from values
while ( !value.empty() &&
( value.back() == ' ' || value.back() == '\n' || value.back() == '\t' || value.back() == '\r' ) )
{
value.pop_back();
}
while ( !value.empty() && ( value.front() == ' ' || value.front() == '\t' ) )
{
value.erase( value.begin() );
}
return std::make_tuple( key, value );
}
#endif /* NO_SC_NETWORKING */