File size: 2,830 Bytes
fd49381
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include <string>

using namespace std;

#include "xmlrpc-c/AbyssServer.hpp"
using xmlrpc_c::AbyssServer;
#include "xmlrpc-c/registry.hpp"

#include "xmlrpc-c/abyss_reqhandler_xmlrpc.hpp"


namespace xmlrpc_c {

abyssReqhandlerXmlrpc::abyssReqhandlerXmlrpc(
    xmlrpc_c::registryPtr const& registryP) :
    registryP(registryP)
{}



static void
handleXmlRpc(AbyssServer::Session * const sessionP,
             xmlrpc_c::registry *   const registryP,
             bool *                 const responseStartedP) {

    string const callXml(sessionP->body());
    string responseXml;

    registryP->processCall(callXml, &responseXml);

    sessionP->setRespStatus(200);

    sessionP->setRespContentType("text/xml charset=utf-8");

    sessionP->setRespContentLength(responseXml.size());

    *responseStartedP = true;

    sessionP->writeResponse(responseXml);
}



void
abyssReqhandlerXmlrpc::abortRequest(
    AbyssServer::Session * const  sessionP,
    bool                   const  responseStarted,
    AbyssServer::Exception const& e) {

    if (responseStarted) {
        // We can't send an error response because we failed
        // partway through sending a non-error response.  All we can
        // do is perhaps log the error and then close the connection.
        this->handleUnreportableFailure(e);
    } else 
        sessionP->sendErrorResponse(e);
}



void
abyssReqhandlerXmlrpc::handleRequest(
    AbyssServer::Session * const sessionP,
    bool *                 const handledP) {

    bool responseStarted;
        // We have at least started to send an HTTP response.  (This is
        // important in error handling, because it means it is too late to
        // send an error response).

    responseStarted = false;

    try {
        switch (sessionP->method()) {
        case AbyssServer::Session::METHOD_POST: {
            if (sessionP->uriPathName() == "/RPC2") {

                handleXmlRpc(sessionP, this->registryP.get(),
                             &responseStarted);
                *handledP = true;
            } else
                *handledP = false;
        } break;
        default:
            *handledP = false;
        }
    } catch (AbyssServer::Exception const& e) {
        this->abortRequest(sessionP, responseStarted, e);
        *handledP = true;
    } catch (exception const& e) {
        this->abortRequest(sessionP, responseStarted,
                           AbyssServer::Exception(500, e.what()));
        *handledP = true;
    }
}



void
abyssReqhandlerXmlrpc::handleUnreportableFailure(
    AbyssServer::Exception const& ) {
/*-----------------------------------------------------------------------------
   This is the default implementation of the virtual method.
-----------------------------------------------------------------------------*/
}



}  // namespace