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
|
#include "llviewerprecompiledheaders.h"
#include "hippoRestRequest.h"
#ifndef CURL_STATICLIB
#define CURL_STATICLIB 1
#endif
#include <curl/curl.h>
#include <stdtypes.h>
#include <llerror.h>
static size_t curlWrite(void *ptr, size_t size, size_t nmemb, void *userData)
{
std::string *result = (std::string*)userData;
size_t bytes = (size * nmemb);
result->append((char*)ptr, bytes);
return nmemb;
}
//static
int HippoRestRequest::getBlocking(const std::string &url, std::string *result)
{
llinfos << "Requesting: " << url << llendl;
char curlErrorBuffer[CURL_ERROR_SIZE];
CURL* curlp = curl_easy_init();
curl_easy_setopt(curlp, CURLOPT_NOSIGNAL, 1); // don't use SIGALRM for timeouts
curl_easy_setopt(curlp, CURLOPT_TIMEOUT, 5); // seconds
curl_easy_setopt(curlp, CURLOPT_WRITEFUNCTION, curlWrite);
curl_easy_setopt(curlp, CURLOPT_WRITEDATA, result);
curl_easy_setopt(curlp, CURLOPT_URL, url.c_str());
curl_easy_setopt(curlp, CURLOPT_ERRORBUFFER, curlErrorBuffer);
curl_easy_setopt(curlp, CURLOPT_FAILONERROR, 1);
*result = "";
S32 curlSuccess = curl_easy_perform(curlp);
S32 httpStatus = 499;
curl_easy_getinfo(curlp, CURLINFO_RESPONSE_CODE, &httpStatus);
if (curlSuccess != 0) {
llwarns << "CURL ERROR (HTTP Status " << httpStatus << "): " << curlErrorBuffer << llendl;
} else if (httpStatus != 200) {
llwarns << "HTTP Error " << httpStatus << ", but no Curl error." << llendl;
}
curl_easy_cleanup(curlp);
return httpStatus;
}
|