diff options
author | McCabe Maxsted | 2010-07-07 11:40:20 -0700 |
---|---|---|
committer | McCabe Maxsted | 2010-07-07 11:40:20 -0700 |
commit | 35eaded540f31378845b7a88e212a86a68825040 (patch) | |
tree | 05a91e33fe0de6afa555afa2c350b889e770bbcb /linden/indra/newview | |
parent | Cleaned up the grid manager layout some (diff) | |
download | meta-impy-35eaded540f31378845b7a88e212a86a68825040.zip meta-impy-35eaded540f31378845b7a88e212a86a68825040.tar.gz meta-impy-35eaded540f31378845b7a88e212a86a68825040.tar.bz2 meta-impy-35eaded540f31378845b7a88e212a86a68825040.tar.xz |
Updated hipporestrequest code to hippo 0.6.3
Diffstat (limited to 'linden/indra/newview')
-rw-r--r-- | linden/indra/newview/hippoGridManager.cpp | 5 | ||||
-rw-r--r-- | linden/indra/newview/hippoRestRequest.cpp | 298 | ||||
-rw-r--r-- | linden/indra/newview/hippoRestRequest.h | 98 |
3 files changed, 394 insertions, 7 deletions
diff --git a/linden/indra/newview/hippoGridManager.cpp b/linden/indra/newview/hippoGridManager.cpp index 0160233..e89e3ff 100644 --- a/linden/indra/newview/hippoGridManager.cpp +++ b/linden/indra/newview/hippoGridManager.cpp | |||
@@ -160,7 +160,10 @@ const std::string& HippoGridInfo::getRealCurrencySymbol() const | |||
160 | void HippoGridInfo::setPlatform(Platform platform) | 160 | void HippoGridInfo::setPlatform(Platform platform) |
161 | { | 161 | { |
162 | mPlatform = platform; | 162 | mPlatform = platform; |
163 | mCurrencySymbol = (mPlatform == PLATFORM_SECONDLIFE)? "L$": "OS$"; | 163 | if (mPlatform == PLATFORM_SECONDLIFE) |
164 | { | ||
165 | mCurrencySymbol = "L$"; | ||
166 | } | ||
164 | } | 167 | } |
165 | 168 | ||
166 | 169 | ||
diff --git a/linden/indra/newview/hippoRestRequest.cpp b/linden/indra/newview/hippoRestRequest.cpp index 20e6187..ed15907 100644 --- a/linden/indra/newview/hippoRestRequest.cpp +++ b/linden/indra/newview/hippoRestRequest.cpp | |||
@@ -10,7 +10,303 @@ | |||
10 | #include <curl/curl.h> | 10 | #include <curl/curl.h> |
11 | 11 | ||
12 | #include <stdtypes.h> | 12 | #include <stdtypes.h> |
13 | #include <llbufferstream.h> | ||
13 | #include <llerror.h> | 14 | #include <llerror.h> |
15 | #include <llhttpclient.h> | ||
16 | #include <llurlrequest.h> | ||
17 | #include <llxmltree.h> | ||
18 | |||
19 | |||
20 | // ******************************************************************** | ||
21 | |||
22 | |||
23 | class HippoRestComplete : public LLURLRequestComplete | ||
24 | { | ||
25 | public: | ||
26 | HippoRestComplete(HippoRestHandler *handler) : | ||
27 | mHandler(handler), | ||
28 | mStatus(499), | ||
29 | mReason("Request completed w/o status") | ||
30 | { | ||
31 | } | ||
32 | |||
33 | ~HippoRestComplete() | ||
34 | { | ||
35 | delete mHandler; | ||
36 | } | ||
37 | |||
38 | // Called once for each header received, prior to httpStatus | ||
39 | void header(const std::string& header, const std::string& value) | ||
40 | { | ||
41 | mHandler->addHeader(header, value); | ||
42 | } | ||
43 | |||
44 | // Always called on request completion, prior to complete | ||
45 | void httpStatus(U32 status, const std::string& reason) | ||
46 | { | ||
47 | LLURLRequestComplete::httpStatus(status, reason); | ||
48 | mStatus = status; | ||
49 | mReason = reason; | ||
50 | } | ||
51 | |||
52 | void complete(const LLChannelDescriptors &channels, const buffer_ptr_t &buffer) | ||
53 | { | ||
54 | mHandler->handle(mStatus, mReason, channels, buffer); | ||
55 | } | ||
56 | |||
57 | private: | ||
58 | HippoRestHandler *mHandler; | ||
59 | int mStatus; | ||
60 | std::string mReason; | ||
61 | }; | ||
62 | |||
63 | |||
64 | // ******************************************************************** | ||
65 | |||
66 | |||
67 | static std::string gEmptyString; | ||
68 | |||
69 | void HippoRestHandler::addHeader(const std::string &header, const std::string &content) | ||
70 | { | ||
71 | mHeaders[header] = content; | ||
72 | } | ||
73 | |||
74 | bool HippoRestHandler::hasHeader(const std::string &header) const | ||
75 | { | ||
76 | return (mHeaders.find(header) != mHeaders.end()); | ||
77 | } | ||
78 | |||
79 | const std::string &HippoRestHandler::getHeader(const std::string &header) const | ||
80 | { | ||
81 | std::map<std::string, std::string>::const_iterator it; | ||
82 | it = mHeaders.find(header); | ||
83 | if (it != mHeaders.end()) { | ||
84 | return it->second; | ||
85 | } else { | ||
86 | return gEmptyString; | ||
87 | } | ||
88 | } | ||
89 | |||
90 | |||
91 | // ******************************************************************** | ||
92 | |||
93 | |||
94 | void HippoRestHandlerRaw::handle(int status, const std::string &reason, | ||
95 | const LLChannelDescriptors &channels, | ||
96 | const boost::shared_ptr<LLBufferArray> &body) | ||
97 | { | ||
98 | if (status == 200) { | ||
99 | std::string data; | ||
100 | LLBufferArray *buffer = body.get(); | ||
101 | LLBufferArray::segment_iterator_t it, end = buffer->endSegment(); | ||
102 | for (it=buffer->beginSegment(); it!=end; ++it) | ||
103 | if (it->isOnChannel(channels.in())) | ||
104 | data.append((char*)it->data(), it->size()); | ||
105 | result(data); | ||
106 | } else { | ||
107 | llwarns << "Rest request error " << status << ": " << reason << llendl; | ||
108 | } | ||
109 | } | ||
110 | |||
111 | void HippoRestHandlerXml::handle(int status, const std::string &reason, | ||
112 | const LLChannelDescriptors &channels, | ||
113 | const boost::shared_ptr<LLBufferArray> &body) | ||
114 | { | ||
115 | if (status == 200) { | ||
116 | LLXmlTree *tree = new LLXmlTree(); | ||
117 | bool success = tree->parseBufferStart(); | ||
118 | LLBufferArray *buffer = body.get(); | ||
119 | LLBufferArray::segment_iterator_t it, end = buffer->endSegment(); | ||
120 | for (it=buffer->beginSegment(); success && (it!=end); ++it) | ||
121 | if (it->isOnChannel(channels.in())) | ||
122 | success = success && tree->parseBuffer((char*)it->data(), it->size()); | ||
123 | success = success && tree->parseBufferFinalize(); | ||
124 | if (success) result(tree); | ||
125 | delete tree; | ||
126 | } else { | ||
127 | llwarns << "Rest request error " << status << ": " << reason << llendl; | ||
128 | } | ||
129 | } | ||
130 | |||
131 | |||
132 | // ******************************************************************** | ||
133 | |||
134 | |||
135 | class BodyData : public LLIOPipe | ||
136 | { | ||
137 | public: | ||
138 | virtual ~BodyData() { } | ||
139 | virtual const char *getContentMimeType() const = 0; | ||
140 | }; | ||
141 | |||
142 | class BodyDataRaw : public BodyData | ||
143 | { | ||
144 | public: | ||
145 | explicit BodyDataRaw(const std::string &data) : | ||
146 | mData(data) | ||
147 | { | ||
148 | } | ||
149 | virtual ~BodyDataRaw() { } | ||
150 | |||
151 | const char *getContentMimeType() const { return "application/octet-stream"; } | ||
152 | |||
153 | EStatus process_impl(const LLChannelDescriptors &channels, | ||
154 | buffer_ptr_t &buffer, bool &eos, | ||
155 | LLSD &context, LLPumpIO *pump) | ||
156 | { | ||
157 | LLBufferStream ostream(channels, buffer.get()); | ||
158 | ostream.write(mData.data(), mData.size()); | ||
159 | eos = true; | ||
160 | return STATUS_DONE; | ||
161 | } | ||
162 | |||
163 | private: | ||
164 | std::string mData; | ||
165 | }; | ||
166 | |||
167 | class BodyDataXml : public BodyData | ||
168 | { | ||
169 | public: | ||
170 | explicit BodyDataXml(const LLXmlTree *tree) : | ||
171 | mTree(tree) | ||
172 | { | ||
173 | } | ||
174 | |||
175 | virtual ~BodyDataXml() | ||
176 | { | ||
177 | if (mTree) delete mTree; | ||
178 | } | ||
179 | |||
180 | const char *getContentMimeType() const { return "application/xml"; } | ||
181 | |||
182 | EStatus process_impl(const LLChannelDescriptors &channels, | ||
183 | buffer_ptr_t &buffer, bool &eos, | ||
184 | LLSD &context, LLPumpIO *pump) | ||
185 | { | ||
186 | std::string data; | ||
187 | mTree->write(data); | ||
188 | LLBufferStream ostream(channels, buffer.get()); | ||
189 | ostream.write(data.data(), data.size()); | ||
190 | eos = true; | ||
191 | return STATUS_DONE; | ||
192 | } | ||
193 | |||
194 | private: | ||
195 | const LLXmlTree *mTree; | ||
196 | }; | ||
197 | |||
198 | |||
199 | // ******************************************************************** | ||
200 | |||
201 | |||
202 | static void request(const std::string &url, | ||
203 | LLURLRequest::ERequestAction method, | ||
204 | BodyData *body, | ||
205 | HippoRestHandler *handler, float timeout); | ||
206 | |||
207 | |||
208 | // static | ||
209 | void HippoRestRequest::get(const std::string &url, | ||
210 | HippoRestHandler *handler, float timeout) | ||
211 | { | ||
212 | request(url, LLURLRequest::HTTP_GET, 0, handler, timeout); | ||
213 | } | ||
214 | |||
215 | // static | ||
216 | void HippoRestRequest::put(const std::string &url, const std::string &body, | ||
217 | HippoRestHandler *handler, float timeout) | ||
218 | { | ||
219 | request(url, LLURLRequest::HTTP_PUT, new BodyDataRaw(body), handler, timeout); | ||
220 | } | ||
221 | |||
222 | // static | ||
223 | void HippoRestRequest::put(const std::string &url, const LLXmlTree *body, | ||
224 | HippoRestHandler *handler, float timeout) | ||
225 | { | ||
226 | request(url, LLURLRequest::HTTP_PUT, new BodyDataXml(body), handler, timeout); | ||
227 | } | ||
228 | |||
229 | // static | ||
230 | void HippoRestRequest::post(const std::string &url, const std::string &body, | ||
231 | HippoRestHandler *handler, float timeout) | ||
232 | { | ||
233 | request(url, LLURLRequest::HTTP_POST, new BodyDataRaw(body), handler, timeout); | ||
234 | } | ||
235 | |||
236 | // static | ||
237 | void HippoRestRequest::post(const std::string &url, const LLXmlTree *body, | ||
238 | HippoRestHandler *handler, float timeout) | ||
239 | { | ||
240 | request(url, LLURLRequest::HTTP_POST, new BodyDataXml(body), handler, timeout); | ||
241 | } | ||
242 | |||
243 | |||
244 | // ******************************************************************** | ||
245 | |||
246 | |||
247 | static void request(const std::string &url, | ||
248 | LLURLRequest::ERequestAction method, | ||
249 | BodyData *body, | ||
250 | HippoRestHandler *handler, float timeout) | ||
251 | { | ||
252 | if (!LLHTTPClient::hasPump()) | ||
253 | { | ||
254 | // !!! responder->completed(U32_MAX, "No pump", LLSD()); | ||
255 | return; | ||
256 | } | ||
257 | LLPumpIO::chain_t chain; | ||
258 | |||
259 | LLURLRequest *req = new LLURLRequest(method, url); | ||
260 | req->checkRootCertificate(true); | ||
261 | |||
262 | /* | ||
263 | // Insert custom headers if the caller sent any | ||
264 | if (headers.isMap()) | ||
265 | { | ||
266 | LLSD::map_const_iterator iter = headers.beginMap(); | ||
267 | LLSD::map_const_iterator end = headers.endMap(); | ||
268 | |||
269 | for (; iter != end; ++iter) | ||
270 | { | ||
271 | std::ostringstream header; | ||
272 | //if the header is "Pragma" with no value | ||
273 | //the caller intends to force libcurl to drop | ||
274 | //the Pragma header it so gratuitously inserts | ||
275 | //Before inserting the header, force libcurl | ||
276 | //to not use the proxy (read: llurlrequest.cpp) | ||
277 | static const std::string PRAGMA("Pragma"); | ||
278 | if ((iter->first == PRAGMA) && (iter->second.asString().empty())) | ||
279 | { | ||
280 | req->useProxy(false); | ||
281 | } | ||
282 | header << iter->first << ": " << iter->second.asString() ; | ||
283 | lldebugs << "header = " << header.str() << llendl; | ||
284 | req->addHeader(header.str().c_str()); | ||
285 | } | ||
286 | } | ||
287 | */ | ||
288 | |||
289 | if ((method != LLURLRequest::HTTP_PUT) && (method != LLURLRequest::HTTP_POST)) { | ||
290 | std::string accept = "Accept: "; | ||
291 | accept += handler->getAcceptMimeType(); | ||
292 | req->addHeader(accept.c_str()); | ||
293 | } | ||
294 | |||
295 | req->setCallback(new HippoRestComplete(handler)); | ||
296 | |||
297 | if ((method == LLURLRequest::HTTP_PUT) || (method == LLURLRequest::HTTP_POST)) { | ||
298 | std::string content = "Content-Type: "; | ||
299 | content += body->getContentMimeType(); | ||
300 | req->addHeader(content.c_str()); | ||
301 | chain.push_back(LLIOPipe::ptr_t(body)); | ||
302 | } | ||
303 | |||
304 | chain.push_back(LLIOPipe::ptr_t(req)); | ||
305 | LLHTTPClient::getPump().addChain(chain, timeout); | ||
306 | } | ||
307 | |||
308 | |||
309 | // ******************************************************************** | ||
14 | 310 | ||
15 | 311 | ||
16 | static size_t curlWrite(void *ptr, size_t size, size_t nmemb, void *userData) | 312 | static size_t curlWrite(void *ptr, size_t size, size_t nmemb, void *userData) |
@@ -22,7 +318,7 @@ static size_t curlWrite(void *ptr, size_t size, size_t nmemb, void *userData) | |||
22 | } | 318 | } |
23 | 319 | ||
24 | 320 | ||
25 | //static | 321 | // static |
26 | int HippoRestRequest::getBlocking(const std::string &url, std::string *result) | 322 | int HippoRestRequest::getBlocking(const std::string &url, std::string *result) |
27 | { | 323 | { |
28 | llinfos << "Requesting: " << url << llendl; | 324 | llinfos << "Requesting: " << url << llendl; |
diff --git a/linden/indra/newview/hippoRestRequest.h b/linden/indra/newview/hippoRestRequest.h index 1dcb4a0..727dbf7 100644 --- a/linden/indra/newview/hippoRestRequest.h +++ b/linden/indra/newview/hippoRestRequest.h | |||
@@ -1,15 +1,103 @@ | |||
1 | #ifndef __HIPPO_REST_REQTUEST_H__ | 1 | #ifndef __HIPPO_REST_REQUEST_H__ |
2 | #define __HIPPO_REST_REQTUEST_H__ | 2 | #define __HIPPO_REST_REQUEST_H__ |
3 | 3 | ||
4 | 4 | ||
5 | #include <map> | ||
5 | #include <string> | 6 | #include <string> |
6 | 7 | ||
8 | #include <boost/shared_ptr.hpp> | ||
9 | |||
10 | class LLBufferArray; | ||
11 | class LLChannelDescriptors; | ||
12 | class LLXmlTree; | ||
13 | |||
14 | |||
15 | #define HIPPO_REST_TIMEOUT 60.f | ||
16 | |||
17 | |||
18 | // ******************************************************************** | ||
19 | |||
20 | |||
21 | class HippoRestHandler | ||
22 | { | ||
23 | public: | ||
24 | virtual ~HippoRestHandler() { } | ||
25 | |||
26 | virtual const char *getAcceptMimeType() const = 0; | ||
27 | |||
28 | bool hasHeader(const std::string &header) const; | ||
29 | const std::string &getHeader(const std::string &header) const; | ||
30 | |||
31 | private: | ||
32 | // These functions are called by the request engine | ||
33 | void addHeader(const std::string &header, const std::string &content); | ||
34 | virtual void handle(int status, const std::string &reason, | ||
35 | const LLChannelDescriptors &channels, | ||
36 | const boost::shared_ptr<LLBufferArray> &body) = 0; | ||
37 | |||
38 | std::map<std::string, std::string> mHeaders; | ||
39 | |||
40 | friend class HippoRestComplete; | ||
41 | }; | ||
42 | |||
43 | |||
44 | class HippoRestHandlerRaw : public HippoRestHandler | ||
45 | { | ||
46 | public: | ||
47 | virtual ~HippoRestHandlerRaw() { } | ||
48 | |||
49 | const char *getAcceptMimeType() const { return "application/octet-stream"; } | ||
50 | |||
51 | private: | ||
52 | // This function must be implemented to receive the content | ||
53 | // it is executed on (status == 200) only | ||
54 | virtual void result(const std::string &content) = 0; | ||
55 | |||
56 | // This function is called by the request engine | ||
57 | void handle(int status, const std::string &reason, | ||
58 | const LLChannelDescriptors &channels, | ||
59 | const boost::shared_ptr<LLBufferArray> &body); | ||
60 | }; | ||
61 | |||
62 | |||
63 | class HippoRestHandlerXml : public HippoRestHandler | ||
64 | { | ||
65 | public: | ||
66 | virtual ~HippoRestHandlerXml() { } | ||
67 | |||
68 | const char *getAcceptMimeType() const { return "application/xml"; } | ||
69 | |||
70 | private: | ||
71 | // This function must be implemented to receive the content | ||
72 | virtual void result(LLXmlTree *content) = 0; | ||
73 | |||
74 | // This function is called by the request engine | ||
75 | void handle(int status, const std::string &reason, | ||
76 | const LLChannelDescriptors &channels, | ||
77 | const boost::shared_ptr<LLBufferArray> &body); | ||
78 | }; | ||
79 | |||
80 | |||
81 | // ******************************************************************** | ||
82 | |||
7 | 83 | ||
8 | class HippoRestRequest | 84 | class HippoRestRequest |
9 | { | 85 | { |
10 | public: | 86 | public: |
11 | static int getBlocking(const std::string &url, std::string *result); | 87 | // asynchronous interface |
12 | 88 | static void get(const std::string &url, | |
89 | HippoRestHandler *handler, float timeout=HIPPO_REST_TIMEOUT); | ||
90 | static void put(const std::string &url, const std::string &body, | ||
91 | HippoRestHandler *handler, float timeout=HIPPO_REST_TIMEOUT); | ||
92 | static void put(const std::string &url, const LLXmlTree *body, | ||
93 | HippoRestHandler *handler, float timeout=HIPPO_REST_TIMEOUT); | ||
94 | static void post(const std::string &url, const std::string &body, | ||
95 | HippoRestHandler *handler, float timeout=HIPPO_REST_TIMEOUT); | ||
96 | static void post(const std::string &url, const LLXmlTree *body, | ||
97 | HippoRestHandler *handler, float timeout=HIPPO_REST_TIMEOUT); | ||
98 | |||
99 | // synchronous interface | ||
100 | static int getBlocking(const std::string &url, std::string *result); | ||
13 | }; | 101 | }; |
14 | 102 | ||
15 | 103 | ||