summaryrefslogtreecommitdiff
path: root/indra/newview/llxmlrpctransaction.cpp
blob: 5b24d46bb0b174141831bbe61303dd5f2ef034ae (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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/**
 * @file llxmlrpctransaction.cpp
 * @brief LLXMLRPCTransaction and related class implementations
 *
 * $LicenseInfo:firstyear=2006&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "llviewerprecompiledheaders.h"
// include this to get winsock2 because openssl attempts to include winsock1
#include "llwin32headers.h"
#include <openssl/x509_vfy.h>
#include <openssl/ssl.h>
#include "llsecapi.h"

#include "llxmlrpctransaction.h"
#include "llxmlrpclistener.h"

#include "httpcommon.h"
#include "llhttpconstants.h"
#include "httprequest.h"
#include "httpoptions.h"
#include "httpheaders.h"
#include "bufferarray.h"
#include "llversioninfo.h"
#include "llviewercontrol.h"
#include "llxmlnode.h"
#include "stringize.h"

// Have to include these last to avoid queue redefinition!

#include "llappviewer.h"
#include "lltrans.h"

#include "boost/move/unique_ptr.hpp"

namespace boost
{
    using ::boost::movelib::unique_ptr; // move unique_ptr into the boost namespace.
}

// Static instance of LLXMLRPCListener declared here so that every time we
// bring in this code, we instantiate a listener. If we put the static
// instance of LLXMLRPCListener into llxmlrpclistener.cpp, the linker would
// simply omit llxmlrpclistener.o, and shouting on the LLEventPump would do
// nothing.
static LLXMLRPCListener listener("LLXMLRPCTransaction");

class LLXMLRPCTransaction::Handler : public LLCore::HttpHandler
{
public:
    Handler(LLCore::HttpRequest::ptr_t &request, LLXMLRPCTransaction::Impl *impl);

    void onCompleted(LLCore::HttpHandle handle,
                     LLCore::HttpResponse* response) override;

    typedef std::shared_ptr<LLXMLRPCTransaction::Handler> ptr_t;

private:
    LLXMLRPCTransaction::Impl *mImpl;
    LLCore::HttpRequest::ptr_t mRequest;
};

class LLXMLRPCTransaction::Impl
{
public:
    typedef LLXMLRPCTransaction::EStatus    EStatus;

    LLCore::HttpRequest::ptr_t  mHttpRequest;


    EStatus             mStatus;
    CURLcode            mCurlCode;
    std::string         mStatusMessage;
    std::string         mStatusURI;
    LLCore::HttpResponse::TransferStats::ptr_t  mTransferStats;
    Handler::ptr_t      mHandler;
    LLCore::HttpHandle  mPostH;

    std::string         mURI;
    std::string         mProxyAddress;

    std::string         mResponseText;
    LLSD                mResponseData;
    bool                mHasResponse;
    bool                mResponseParsed;

    std::string         mCertStore;
    LLSD                mErrorCertData;

    Impl
    (
        const std::string& uri,
        const std::string& method,
        const LLSD& params,
        const LLSD& httpParams
    );

    bool process();

    void setStatus(EStatus code, const std::string& message = "", const std::string& uri = "");
    void setHttpStatus(const LLCore::HttpStatus &status);

private:
    bool parseResponse(LLXMLNodePtr root);
    bool parseValue(LLSD& target, LLXMLNodePtr source);
};

LLXMLRPCTransaction::Handler::Handler(LLCore::HttpRequest::ptr_t &request,
        LLXMLRPCTransaction::Impl *impl) :
    mImpl(impl),
    mRequest(request)
{
}

void LLXMLRPCTransaction::Handler::onCompleted(LLCore::HttpHandle handle,
    LLCore::HttpResponse * response)
{
    LLCore::HttpStatus status = response->getStatus();

    if (!status)
    {
        mImpl->setHttpStatus(status);
        LLSD errordata = status.getErrorData();
        mImpl->mErrorCertData = errordata;

        if ((status.toULong() != CURLE_SSL_PEER_CERTIFICATE) &&
            (status.toULong() != CURLE_SSL_CACERT))
        {
            // if we have a curl error that's not already been handled
            // (a non cert error), then generate the warning message as
            // appropriate
            LL_WARNS() << "LLXMLRPCTransaction error "
                << status.toHex() << ": " << status.toString() << LL_ENDL;
            LL_WARNS() << "LLXMLRPCTransaction request URI: "
                << mImpl->mURI << LL_ENDL;
        }

        return;
    }

    mImpl->mTransferStats = response->getTransferStats();

    // The contents of a buffer array are potentially noncontiguous, so we
    // will need to copy them into an contiguous block of memory for XMLRPC.
    LLCore::BufferArray *body = response->getBody();
    mImpl->mResponseText.resize(body->size());

    body->read(0, mImpl->mResponseText.data(), body->size());

    // We do not do the parsing in the HTTP coroutine, since it could exhaust
    // the coroutine stack in extreme cases. Instead, we flag the data buffer
    // as ready, and let mImpl decode it in its process() method, on the main
    // coroutine. HB
    mImpl->mHasResponse = true;
    mImpl->setStatus(LLXMLRPCTransaction::StatusComplete);
}

//=========================================================================

LLXMLRPCTransaction::Impl::Impl
(
    const std::string& uri,
    const std::string& method,
    const LLSD& params,
    const LLSD& http_params
)
    : mHttpRequest()
    , mStatus(LLXMLRPCTransaction::StatusNotStarted)
    , mURI(uri)
    , mHasResponse(false)
    , mResponseParsed(false)
{
    LLCore::HttpOptions::ptr_t httpOpts;
    LLCore::HttpHeaders::ptr_t httpHeaders;

    if (!mHttpRequest)
    {
        mHttpRequest = LLCore::HttpRequest::ptr_t(new LLCore::HttpRequest);
    }

    // LLRefCounted starts with a 1 ref, so don't add a ref in the smart pointer
    httpOpts = LLCore::HttpOptions::ptr_t(new LLCore::HttpOptions());

    // Delay between repeats will start from 5 sec and grow to 20 sec with each repeat
    httpOpts->setMinBackoff((LLCore::HttpTime)5E6L);
    httpOpts->setMaxBackoff((LLCore::HttpTime)20E6L);

    httpOpts->setTimeout(http_params.has("timeout") ? http_params["timeout"].asInteger() : 40L);
    if (http_params.has("retries"))
    {
        httpOpts->setRetries(http_params["retries"].asInteger());
    }
    if (http_params.has("DNSCacheTimeout"))
    {
        httpOpts->setDNSCacheTimeout(http_params["DNSCacheTimeout"].asInteger());
    }

    bool vefifySSLCert = !gSavedSettings.getBOOL("NoVerifySSLCert");
    mCertStore = gSavedSettings.getString("CertStore");

    httpOpts->setSSLVerifyPeer(vefifySSLCert);
    httpOpts->setSSLVerifyHost(vefifySSLCert);

    // LLRefCounted starts with a 1 ref, so don't add a ref in the smart pointer
    httpHeaders = LLCore::HttpHeaders::ptr_t(new LLCore::HttpHeaders());

    httpHeaders->append(HTTP_OUT_HEADER_CONTENT_TYPE, HTTP_CONTENT_TEXT_XML);

    const LLVersionInfo& vi(LLVersionInfo::instance());
    std::string user_agent = vi.getChannel() + llformat(" %d.%d.%d (%llu)",
        vi.getMajor(), vi.getMinor(), vi.getPatch(), vi.getBuild());

    httpHeaders->append(HTTP_OUT_HEADER_USER_AGENT, user_agent);

    ///* Setting the DNS cache timeout to -1 disables it completely.
    //This might help with bug #503 */
    //httpOpts->setDNSCacheTimeout(-1);

    std::string request =
        "<?xml version=\"1.0\"?><methodCall><methodName>" + method +
        "</methodName><params><param>" + params.asXMLRPCValue() +
        "</param></params></methodCall>";

    LLCore::BufferArray::ptr_t body = LLCore::BufferArray::ptr_t(new LLCore::BufferArray());

    body->append(request.c_str(), request.size());

    mHandler = LLXMLRPCTransaction::Handler::ptr_t(new Handler(mHttpRequest, this));

    mPostH = mHttpRequest->requestPost(LLCore::HttpRequest::DEFAULT_POLICY_ID,
        mURI, body.get(), httpOpts, httpHeaders, mHandler);
}

bool LLXMLRPCTransaction::Impl::parseResponse(LLXMLNodePtr root)
{
    // We have already checked in LLXMLNode::parseBuffer() that root contains
    // exactly one child.
    if (!root->hasName("methodResponse"))
    {
        LL_WARNS() << "Invalid root element in XML response; request URI: "
                   << mURI << LL_ENDL;
        return false;
    }

    LLXMLNodePtr first = root->getFirstChild();
    LLXMLNodePtr second = first->getFirstChild();
    if (first && !first->getNextSibling() && second &&
        !second->getNextSibling())
    {
        if (first->hasName("fault"))
        {
            LLSD fault;
            if (parseValue(fault, second) && fault.isMap() &&
                fault.has("faultCode") && fault.has("faultString"))
            {
                LL_WARNS() << "Request failed. faultCode: '"
                        << fault.get("faultCode").asString()
                        << "', faultString: '"
                        << fault.get("faultString").asString()
                        << "', request URI: " << mURI << LL_ENDL;
                return false;
            }
        }
        else if (first->hasName("params") &&
                 second->hasName("param") && !second->getNextSibling())
        {
            LLXMLNodePtr third = second->getFirstChild();
            if (third && !third->getNextSibling() &&
                parseValue(mResponseData, third))
            {
                return true;
            }
        }
    }

    LL_WARNS() << "Invalid response format; request URI: " << mURI << LL_ENDL;
    return false;
}

bool LLXMLRPCTransaction::Impl::parseValue(LLSD& target, LLXMLNodePtr src)
{
    return src->fromXMLRPCValue(target);
}

bool LLXMLRPCTransaction::Impl::process()
{
    if (!mPostH || !mHttpRequest)
    {
        LL_WARNS() << "transaction failed." << LL_ENDL;
        return true; //failed, quit.
    }

    // Parse the response when we have one and it has not yet been parsed. HB
    if (mHasResponse && !mResponseParsed)
    {
        LLXMLNodePtr root;
        bool strip_escaped_strings = LLXMLNode::sStripEscapedStrings;
        LLXMLNode::sStripEscapedStrings = false;
        if (!LLXMLNode::parseBuffer(mResponseText.data(), mResponseText.size(),
                                    root, nullptr))
        {
            LL_WARNS() << "Failed parsing XML in response; request URI: "
                       << mURI << LL_ENDL;
        }
        else if (parseResponse(root))
        {
            LL_INFOS() << "XMLRPC response parsed successfully; request URI: "
                       << mURI << LL_ENDL;
        }
        else
        {
            LL_WARNS() << "XMLRPC response parsing failed; request URI: "
                       << mURI << LL_ENDL;
        }
        LLXMLNode::sStripEscapedStrings = strip_escaped_strings;
        mResponseParsed = true;
    }

    switch (mStatus)
    {
        case LLXMLRPCTransaction::StatusComplete:
        case LLXMLRPCTransaction::StatusCURLError:
        case LLXMLRPCTransaction::StatusXMLRPCError:
        case LLXMLRPCTransaction::StatusOtherError:
        {
            return true;
        }

        case LLXMLRPCTransaction::StatusNotStarted:
        {
            setStatus(LLXMLRPCTransaction::StatusStarted);
            break;
        }

        default:
            break;
    }

    LLCore::HttpStatus status = mHttpRequest->update(0);

    status = mHttpRequest->getStatus();
    if (!status)
    {
        return false;
    }

    return false;
}

void LLXMLRPCTransaction::Impl::setStatus(EStatus status,
    const std::string& message, const std::string& uri)
{
    mStatus = status;
    mStatusMessage = message;
    mStatusURI = uri;

    if (mStatusMessage.empty())
    {
        switch (mStatus)
        {
            case StatusNotStarted:
                mStatusMessage = "(not started)";
                break;

            case StatusStarted:
                mStatusMessage = "(waiting for server response)";
                break;

            case StatusDownloading:
                mStatusMessage = "(reading server response)";
                break;

            case StatusComplete:
                mStatusMessage = "(done)";
                break;
            default:
                // Usually this means that there's a problem with the login server,
                // not with the client.  Direct user to status page.
                mStatusMessage = LLTrans::getString("server_is_down");
                mStatusURI = "http://status.secondlifegrid.net/";
        }
    }
}

void LLXMLRPCTransaction::Impl::setHttpStatus(const LLCore::HttpStatus &status)
{
    CURLcode code = static_cast<CURLcode>(status.toULong());
    std::string message;
    std::string uri = "http://support.secondlife.com";
    LLURI failuri(mURI);
    LLStringUtil::format_map_t args;

    switch (code)
    {
    case CURLE_COULDNT_RESOLVE_HOST:
        args["[HOSTNAME]"] = failuri.hostName();
        message = LLTrans::getString("couldnt_resolve_host", args);
        break;

#if CURLE_SSL_PEER_CERTIFICATE != CURLE_SSL_CACERT
    case CURLE_SSL_PEER_CERTIFICATE:
        message = LLTrans::getString("ssl_peer_certificate");
        break;
#endif
    case CURLE_SSL_CACERT:
    case CURLE_SSL_CONNECT_ERROR:
        message = LLTrans::getString("ssl_connect_error");
        break;

    default:
        break;
    }

    mCurlCode = code;
    setStatus(StatusCURLError, message, uri);

}

LLXMLRPCTransaction::LLXMLRPCTransaction
(
    const std::string& uri,
    const std::string& method,
    const LLSD& params,
    const LLSD& http_params
)
: impl(*new Impl(uri, method, params, http_params))
{
}

LLXMLRPCTransaction::~LLXMLRPCTransaction()
{
    delete &impl;
}

bool LLXMLRPCTransaction::process()
{
    return impl.process();
}

LLXMLRPCTransaction::EStatus LLXMLRPCTransaction::status(int* curlCode)
{
    if (curlCode)
    {
        *curlCode =
            (impl.mStatus == StatusCURLError)
                ? impl.mCurlCode
                : CURLE_OK;
    }

    return impl.mStatus;
}

std::string LLXMLRPCTransaction::statusMessage()
{
    return impl.mStatusMessage;
}

LLSD LLXMLRPCTransaction::getErrorCertData()
{
    return impl.mErrorCertData;
}

std::string LLXMLRPCTransaction::statusURI()
{
    return impl.mStatusURI;
}

const LLSD& LLXMLRPCTransaction::response()
{
    return impl.mResponseData;
}


F64 LLXMLRPCTransaction::transferRate()
{
    if (impl.mStatus != StatusComplete)
    {
        return 0.0L;
    }

    double rate_bits_per_sec = impl.mTransferStats->mSpeedDownload * 8.0;

    LL_INFOS("AppInit") << "Buffer size:   " << impl.mResponseText.size() << " B" << LL_ENDL;
    LL_DEBUGS("AppInit") << "Transfer size: " << impl.mTransferStats->mSizeDownload << " B" << LL_ENDL;
    LL_DEBUGS("AppInit") << "Transfer time: " << impl.mTransferStats->mTotalTime << " s" << LL_ENDL;
    LL_INFOS("AppInit") << "Transfer rate: " << rate_bits_per_sec / 1000.0 << " Kb/s" << LL_ENDL;

    return rate_bits_per_sec;
}