aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/newview/hippoRestRequest.cpp
blob: ed159074dddf07c9de9ab3fff703dcae2d256c94 (plain)
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


#include "llviewerprecompiledheaders.h"

#include "hippoRestRequest.h"

#ifndef CURL_STATICLIB
#define CURL_STATICLIB 1
#endif
#include <curl/curl.h>

#include <stdtypes.h>
#include <llbufferstream.h>
#include <llerror.h>
#include <llhttpclient.h>
#include <llurlrequest.h>
#include <llxmltree.h>


// ********************************************************************
 
 
class HippoRestComplete : public LLURLRequestComplete
{
	public:
		HippoRestComplete(HippoRestHandler *handler) :
			mHandler(handler),
			mStatus(499),
			mReason("Request completed w/o status")
		{
		}
 
		~HippoRestComplete()
		{
			delete mHandler;
		}
 
		// Called once for each header received, prior to httpStatus
		void header(const std::string& header, const std::string& value)
		{
			mHandler->addHeader(header, value);
		}

		// Always called on request completion, prior to complete
		void httpStatus(U32 status, const std::string& reason)
		{
			LLURLRequestComplete::httpStatus(status, reason);
			mStatus = status;
			mReason = reason;
		}

		void complete(const LLChannelDescriptors &channels, const buffer_ptr_t &buffer)
		{
			mHandler->handle(mStatus, mReason, channels, buffer);
		}

	private:
		HippoRestHandler *mHandler;
		int mStatus;
		std::string mReason;
};


// ********************************************************************


static std::string gEmptyString;

void HippoRestHandler::addHeader(const std::string &header, const std::string &content)
{
	mHeaders[header] = content;
}

bool HippoRestHandler::hasHeader(const std::string &header) const
{
	return (mHeaders.find(header) != mHeaders.end());
}

const std::string &HippoRestHandler::getHeader(const std::string &header) const
{
	std::map<std::string, std::string>::const_iterator it;
	it = mHeaders.find(header);
	if (it != mHeaders.end()) {
		return it->second;
	} else {
		return gEmptyString;
	}
}


// ********************************************************************


void HippoRestHandlerRaw::handle(int status, const std::string &reason,
								 const LLChannelDescriptors &channels,
								 const boost::shared_ptr<LLBufferArray> &body)
{
	if (status == 200) {
		std::string data;
		LLBufferArray *buffer = body.get();
		LLBufferArray::segment_iterator_t it, end = buffer->endSegment();
		for (it=buffer->beginSegment(); it!=end; ++it)
			if (it->isOnChannel(channels.in()))
				data.append((char*)it->data(), it->size());
		result(data);
	} else {
		llwarns << "Rest request error " << status << ": " << reason << llendl;
	}
}

void HippoRestHandlerXml::handle(int status, const std::string &reason,
								 const LLChannelDescriptors &channels,
								 const boost::shared_ptr<LLBufferArray> &body)
{
	if (status == 200) {
		LLXmlTree *tree = new LLXmlTree();
		bool success = tree->parseBufferStart();
		LLBufferArray *buffer = body.get();
		LLBufferArray::segment_iterator_t it, end = buffer->endSegment();
		for (it=buffer->beginSegment(); success && (it!=end); ++it)
			if (it->isOnChannel(channels.in()))
				success = success && tree->parseBuffer((char*)it->data(), it->size());
		success = success && tree->parseBufferFinalize();
		if (success) result(tree);
		delete tree;
	} else {
		llwarns << "Rest request error " << status << ": " << reason << llendl;
	}
}


// ********************************************************************


class BodyData : public LLIOPipe
{
	public:
		virtual ~BodyData() { }
		virtual const char *getContentMimeType() const = 0;
};

class BodyDataRaw : public BodyData
{
	public:
		explicit BodyDataRaw(const std::string &data) :
			mData(data)
		{
		}
		virtual ~BodyDataRaw() { }
 
		const char *getContentMimeType() const { return "application/octet-stream"; }
 
		EStatus process_impl(const LLChannelDescriptors &channels,
							 buffer_ptr_t &buffer, bool &eos,
							 LLSD &context, LLPumpIO *pump)
		{
			LLBufferStream ostream(channels, buffer.get());
			ostream.write(mData.data(), mData.size());
			eos = true;
			return STATUS_DONE;
		}

	private:
		std::string mData;
};

class BodyDataXml : public BodyData
{
	public:
		explicit BodyDataXml(const LLXmlTree *tree) :
			mTree(tree)
		{
		}

		virtual ~BodyDataXml()
		{
			if (mTree) delete mTree;
		}

		const char *getContentMimeType() const { return "application/xml"; }

		EStatus process_impl(const LLChannelDescriptors &channels,
							 buffer_ptr_t &buffer, bool &eos,
							 LLSD &context, LLPumpIO *pump)
		{
			std::string data;
			mTree->write(data);
			LLBufferStream ostream(channels, buffer.get());
			ostream.write(data.data(), data.size());
			eos = true;
			return STATUS_DONE;
		}

	private:
		const LLXmlTree *mTree;
};


// ********************************************************************


static void request(const std::string &url,
					LLURLRequest::ERequestAction method,
					BodyData *body,
					HippoRestHandler *handler, float timeout);


// static
void HippoRestRequest::get(const std::string &url,
						   HippoRestHandler *handler, float timeout)
{
	request(url, LLURLRequest::HTTP_GET, 0, handler, timeout);
}

// static
void HippoRestRequest::put(const std::string &url, const std::string &body,
						   HippoRestHandler *handler, float timeout)
{
	request(url, LLURLRequest::HTTP_PUT, new BodyDataRaw(body), handler, timeout);
}

// static
void HippoRestRequest::put(const std::string &url, const LLXmlTree *body,
						   HippoRestHandler *handler, float timeout)
{
	request(url, LLURLRequest::HTTP_PUT, new BodyDataXml(body), handler, timeout);
}

// static
void HippoRestRequest::post(const std::string &url, const std::string &body,
							HippoRestHandler *handler, float timeout)
{
	request(url, LLURLRequest::HTTP_POST, new BodyDataRaw(body), handler, timeout);
}

// static
void HippoRestRequest::post(const std::string &url, const LLXmlTree *body,
							HippoRestHandler *handler, float timeout)
{
	request(url, LLURLRequest::HTTP_POST, new BodyDataXml(body), handler, timeout);
}


// ********************************************************************


static void request(const std::string &url,
					LLURLRequest::ERequestAction method,
					BodyData *body,
					HippoRestHandler *handler, float timeout)
{
	if (!LLHTTPClient::hasPump())
	{
		// !!! responder->completed(U32_MAX, "No pump", LLSD());
		return;
	}
	LLPumpIO::chain_t chain;

	LLURLRequest *req = new LLURLRequest(method, url);
	req->checkRootCertificate(true);

	/*
	// Insert custom headers if the caller sent any
	if (headers.isMap())
	{
		LLSD::map_const_iterator iter = headers.beginMap();
		LLSD::map_const_iterator end  = headers.endMap();
 
		for (; iter != end; ++iter)
		{
			std::ostringstream header;
			//if the header is "Pragma" with no value
			//the caller intends to force libcurl to drop
			//the Pragma header it so gratuitously inserts
			//Before inserting the header, force libcurl
			//to not use the proxy (read: llurlrequest.cpp)
			static const std::string PRAGMA("Pragma");
			if ((iter->first == PRAGMA) && (iter->second.asString().empty()))
			{
				req->useProxy(false);
			}
			header << iter->first << ": " << iter->second.asString() ;
			lldebugs << "header = " << header.str() << llendl;
			req->addHeader(header.str().c_str());
		}
	}
	*/

	if ((method != LLURLRequest::HTTP_PUT) && (method != LLURLRequest::HTTP_POST)) {
		std::string accept = "Accept: ";
		accept += handler->getAcceptMimeType();
		req->addHeader(accept.c_str());
	}

	req->setCallback(new HippoRestComplete(handler));

	if ((method == LLURLRequest::HTTP_PUT) || (method == LLURLRequest::HTTP_POST)) {
		std::string content = "Content-Type: ";
		content += body->getContentMimeType();
		req->addHeader(content.c_str());
		chain.push_back(LLIOPipe::ptr_t(body));
	}

	chain.push_back(LLIOPipe::ptr_t(req));
	LLHTTPClient::getPump().addChain(chain, timeout);
}


// ********************************************************************


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