@@ -38,7 +38,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
38
38
#include " noise.h"
39
39
40
40
static std::mutex g_httpfetch_mutex;
41
- static std::unordered_map<unsigned long , std::queue<HTTPFetchResult>>
41
+ static std::unordered_map<u64 , std::queue<HTTPFetchResult>>
42
42
g_httpfetch_results;
43
43
static PcgRandom g_callerid_randomness;
44
44
@@ -52,22 +52,21 @@ HTTPFetchRequest::HTTPFetchRequest() :
52
52
53
53
static void httpfetch_deliver_result (const HTTPFetchResult &fetch_result)
54
54
{
55
- unsigned long caller = fetch_result.caller ;
55
+ u64 caller = fetch_result.caller ;
56
56
if (caller != HTTPFETCH_DISCARD) {
57
57
MutexAutoLock lock (g_httpfetch_mutex);
58
58
g_httpfetch_results[caller].emplace (fetch_result);
59
59
}
60
60
}
61
61
62
- static void httpfetch_request_clear (unsigned long caller);
62
+ static void httpfetch_request_clear (u64 caller);
63
63
64
- unsigned long httpfetch_caller_alloc ()
64
+ u64 httpfetch_caller_alloc ()
65
65
{
66
66
MutexAutoLock lock (g_httpfetch_mutex);
67
67
68
- // Check each caller ID except HTTPFETCH_DISCARD
69
- const unsigned long discard = HTTPFETCH_DISCARD;
70
- for (unsigned long caller = discard + 1 ; caller != discard; ++caller) {
68
+ // Check each caller ID except reserved ones
69
+ for (u64 caller = HTTPFETCH_CID_START; caller != 0 ; ++caller) {
71
70
auto it = g_httpfetch_results.find (caller);
72
71
if (it == g_httpfetch_results.end ()) {
73
72
verbosestream << " httpfetch_caller_alloc: allocating "
@@ -79,18 +78,17 @@ unsigned long httpfetch_caller_alloc()
79
78
}
80
79
81
80
FATAL_ERROR (" httpfetch_caller_alloc: ran out of caller IDs" );
82
- return discard;
83
81
}
84
82
85
- unsigned long httpfetch_caller_alloc_secure ()
83
+ u64 httpfetch_caller_alloc_secure ()
86
84
{
87
85
MutexAutoLock lock (g_httpfetch_mutex);
88
86
89
87
// Generate random caller IDs and make sure they're not
90
- // already used or equal to HTTPFETCH_DISCARD
88
+ // already used or reserved.
91
89
// Give up after 100 tries to prevent infinite loop
92
- u8 tries = 100 ;
93
- unsigned long caller;
90
+ size_t tries = 100 ;
91
+ u64 caller;
94
92
95
93
do {
96
94
caller = (((u64) g_callerid_randomness.next ()) << 32 ) |
@@ -100,7 +98,8 @@ unsigned long httpfetch_caller_alloc_secure()
100
98
FATAL_ERROR (" httpfetch_caller_alloc_secure: ran out of caller IDs" );
101
99
return HTTPFETCH_DISCARD;
102
100
}
103
- } while (g_httpfetch_results.find (caller) != g_httpfetch_results.end ());
101
+ } while (caller >= HTTPFETCH_CID_START &&
102
+ g_httpfetch_results.find (caller) != g_httpfetch_results.end ());
104
103
105
104
verbosestream << " httpfetch_caller_alloc_secure: allocating "
106
105
<< caller << std::endl;
@@ -110,7 +109,7 @@ unsigned long httpfetch_caller_alloc_secure()
110
109
return caller;
111
110
}
112
111
113
- void httpfetch_caller_free (unsigned long caller)
112
+ void httpfetch_caller_free (u64 caller)
114
113
{
115
114
verbosestream<<" httpfetch_caller_free: freeing "
116
115
<<caller<<std::endl;
@@ -122,7 +121,7 @@ void httpfetch_caller_free(unsigned long caller)
122
121
}
123
122
}
124
123
125
- bool httpfetch_async_get (unsigned long caller, HTTPFetchResult &fetch_result)
124
+ bool httpfetch_async_get (u64 caller, HTTPFetchResult &fetch_result)
126
125
{
127
126
MutexAutoLock lock (g_httpfetch_mutex);
128
127
@@ -242,7 +241,6 @@ HTTPFetchOngoing::HTTPFetchOngoing(const HTTPFetchRequest &request_,
242
241
243
242
// Set static cURL options
244
243
curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1 );
245
- curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1 );
246
244
curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1 );
247
245
curl_easy_setopt (curl, CURLOPT_MAXREDIRS, 3 );
248
246
curl_easy_setopt (curl, CURLOPT_ENCODING, " gzip" );
@@ -393,10 +391,17 @@ const HTTPFetchResult * HTTPFetchOngoing::complete(CURLcode res)
393
391
}
394
392
395
393
if (res != CURLE_OK) {
396
- errorstream << request.url << " not found ("
397
- << curl_easy_strerror (res) << " )"
398
- << " (response code " << result.response_code << " )"
394
+ errorstream << " HTTPFetch for " << request.url << " failed ("
395
+ << curl_easy_strerror (res) << " )" << std::endl;
396
+ } else if (result.response_code >= 400 ) {
397
+ errorstream << " HTTPFetch for " << request.url
398
+ << " returned response code " << result.response_code
399
399
<< std::endl;
400
+ if (result.caller == HTTPFETCH_PRINT_ERR && !result.data .empty ()) {
401
+ errorstream << " Response body:" << std::endl;
402
+ safe_print_string (errorstream, result.data );
403
+ errorstream << std::endl;
404
+ }
400
405
}
401
406
402
407
return &result;
@@ -474,7 +479,7 @@ class CurlFetchThread : public Thread
474
479
m_requests.push_back (req);
475
480
}
476
481
477
- void requestClear (unsigned long caller, Event *event)
482
+ void requestClear (u64 caller, Event *event)
478
483
{
479
484
Request req;
480
485
req.type = RT_CLEAR;
@@ -505,7 +510,7 @@ class CurlFetchThread : public Thread
505
510
506
511
}
507
512
else if (req.type == RT_CLEAR) {
508
- unsigned long caller = req.fetch_request .caller ;
513
+ u64 caller = req.fetch_request .caller ;
509
514
510
515
// Abort all ongoing fetches for the caller
511
516
for (std::vector<HTTPFetchOngoing*>::iterator
@@ -778,7 +783,7 @@ void httpfetch_async(const HTTPFetchRequest &fetch_request)
778
783
g_httpfetch_thread->start ();
779
784
}
780
785
781
- static void httpfetch_request_clear (unsigned long caller)
786
+ static void httpfetch_request_clear (u64 caller)
782
787
{
783
788
if (g_httpfetch_thread->isRunning ()) {
784
789
Event event;
@@ -827,7 +832,7 @@ void httpfetch_async(const HTTPFetchRequest &fetch_request)
827
832
httpfetch_deliver_result (fetch_result);
828
833
}
829
834
830
- static void httpfetch_request_clear (unsigned long caller)
835
+ static void httpfetch_request_clear (u64 caller)
831
836
{
832
837
}
833
838
0 commit comments