| 1 |
// Copyright (c) 2009, Whispersoft s.r.l. |
|---|
| 2 |
// All rights reserved. |
|---|
| 3 |
// |
|---|
| 4 |
// Redistribution and use in source and binary forms, with or without |
|---|
| 5 |
// modification, are permitted provided that the following conditions are |
|---|
| 6 |
// met: |
|---|
| 7 |
// |
|---|
| 8 |
// * Redistributions of source code must retain the above copyright |
|---|
| 9 |
// notice, this list of conditions and the following disclaimer. |
|---|
| 10 |
// * Redistributions in binary form must reproduce the above |
|---|
| 11 |
// copyright notice, this list of conditions and the following disclaimer |
|---|
| 12 |
// in the documentation and/or other materials provided with the |
|---|
| 13 |
// distribution. |
|---|
| 14 |
// * Neither the name of Whispersoft s.r.l. nor the names of its |
|---|
| 15 |
// contributors may be used to endorse or promote products derived from |
|---|
| 16 |
// this software without specific prior written permission. |
|---|
| 17 |
// |
|---|
| 18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 19 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 20 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 21 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 22 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 25 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 26 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 27 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 28 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 29 |
// |
|---|
| 30 |
// Author: Cosmin Tudorache |
|---|
| 31 |
|
|---|
| 32 |
#ifndef __NET_RPC_LIB_SERVER_RPC_HTTP_PROCESSOR_H__ |
|---|
| 33 |
#define __NET_RPC_LIB_SERVER_RPC_HTTP_PROCESSOR_H__ |
|---|
| 34 |
|
|---|
| 35 |
#include <string> |
|---|
| 36 |
#include <map> |
|---|
| 37 |
#include <whisperlib/net/http/http_server_protocol.h> |
|---|
| 38 |
#include <whisperlib/net/rpc/lib/server/rpc_services_manager.h> |
|---|
| 39 |
#include <whisperlib/net/rpc/lib/server/irpc_result_handler.h> |
|---|
| 40 |
#include <whisperlib/net/rpc/lib/server/irpc_async_query_executor.h> |
|---|
| 41 |
#include <whisperlib/net/rpc/lib/codec/binary/rpc_binary_codec.h> |
|---|
| 42 |
#include <whisperlib/net/rpc/lib/codec/json/rpc_json_codec.h> |
|---|
| 43 |
|
|---|
| 44 |
namespace rpc { |
|---|
| 45 |
|
|---|
| 46 |
//////////////////////////////////////// |
|---|
| 47 |
// |
|---|
| 48 |
// HTTP tunneling |
|---|
| 49 |
// |
|---|
| 50 |
// A class to processes rpc queries enclosed in http requests. |
|---|
| 51 |
// |
|---|
| 52 |
// The general workflow: |
|---|
| 53 |
// 1. the HTTP server, upon receiving a request, calls us to |
|---|
| 54 |
// process the request |
|---|
| 55 |
// 2. we decode rpc query from http request |
|---|
| 56 |
// 3. we asynchronous execute the query |
|---|
| 57 |
// 4. we encode response in http request, and send it back |
|---|
| 58 |
// |
|---|
| 59 |
// This is somehow similar to rpc_server_connection, with the |
|---|
| 60 |
// following exceptions: |
|---|
| 61 |
// |
|---|
| 62 |
// On HTTP tunneling: - There is no handshake. |
|---|
| 63 |
// - The codec is specified in every request. |
|---|
| 64 |
// |
|---|
| 65 |
class HttpProcessor : protected IResultHandler { |
|---|
| 66 |
public: |
|---|
| 67 |
HttpProcessor(rpc::ServicesManager& manager, |
|---|
| 68 |
IAsyncQueryExecutor& executor, |
|---|
| 69 |
bool enable_auto_forms); |
|---|
| 70 |
virtual ~HttpProcessor(); |
|---|
| 71 |
|
|---|
| 72 |
// Attach this processor object to the given server, on |
|---|
| 73 |
// the specified path. |
|---|
| 74 |
// e.g. if path is "/abc/d" then all http request on URL |
|---|
| 75 |
// http://<server-ip>/abc/d and also all |
|---|
| 76 |
// http://<server-ip>/abc/d/* are dispatched to this processor. |
|---|
| 77 |
// returns: |
|---|
| 78 |
// success value. |
|---|
| 79 |
bool AttachToServer(http::Server* server, const string& process_path); |
|---|
| 80 |
|
|---|
| 81 |
////////////////////////////////////////////////////////////// |
|---|
| 82 |
// |
|---|
| 83 |
// Methods available only from the selector thread. |
|---|
| 84 |
// |
|---|
| 85 |
protected: |
|---|
| 86 |
// Called by the http server(on selector thread) to handle a http request. |
|---|
| 87 |
// This method is registered to a http server, to receive requests. |
|---|
| 88 |
void CallbackProcessHTTPRequest(http::ServerRequest* req); |
|---|
| 89 |
|
|---|
| 90 |
// Reply to the given request. The request should have been prepared already, |
|---|
| 91 |
// containing all the data that needs to be sent back to client. |
|---|
| 92 |
void CallbackReplyToHTTPRequest(http::ServerRequest* req, |
|---|
| 93 |
http::HttpReturnCode rCode); |
|---|
| 94 |
|
|---|
| 95 |
////////////////////////////////////////////////////////////////////// |
|---|
| 96 |
// |
|---|
| 97 |
// Methods available to any external thread (worker threads). |
|---|
| 98 |
// |
|---|
| 99 |
// create a reply rpc-packet, and queue a closure to send it. |
|---|
| 100 |
void WriteReply(uint32 xid, |
|---|
| 101 |
rpc::REPLY_STATUS status, |
|---|
| 102 |
const io::MemoryStream& result); |
|---|
| 103 |
|
|---|
| 104 |
////////////////////////////////////////////////////////////////////// |
|---|
| 105 |
// |
|---|
| 106 |
// IResultHandler interface methods |
|---|
| 107 |
// |
|---|
| 108 |
// Called by the execution system (possibly a worker) to announce |
|---|
| 109 |
// the result of a query. |
|---|
| 110 |
void HandleRPCResult(const rpc::Query& q); |
|---|
| 111 |
|
|---|
| 112 |
protected: |
|---|
| 113 |
// The http server using this processor. |
|---|
| 114 |
http::Server* httpServer_; |
|---|
| 115 |
|
|---|
| 116 |
// The http url path where we listen for requests. |
|---|
| 117 |
string http_path_; |
|---|
| 118 |
|
|---|
| 119 |
// The rpc services manager. |
|---|
| 120 |
rpc::ServicesManager& servicesManager_; |
|---|
| 121 |
|
|---|
| 122 |
// the query executor. Estabilished in constructor. |
|---|
| 123 |
IAsyncQueryExecutor& asyncQueryExecutor_; |
|---|
| 124 |
|
|---|
| 125 |
// true if we're registered in the executor. |
|---|
| 126 |
bool registeredToQueryExecutor_; |
|---|
| 127 |
|
|---|
| 128 |
struct ExecutingRequest { |
|---|
| 129 |
http::ServerRequest* req_; |
|---|
| 130 |
rpc::Codec& codec_; |
|---|
| 131 |
uint32 xid_; |
|---|
| 132 |
ExecutingRequest(http::ServerRequest* req, |
|---|
| 133 |
rpc::Codec& codec, |
|---|
| 134 |
uint32 xid); |
|---|
| 135 |
~ExecutingRequest(); |
|---|
| 136 |
}; |
|---|
| 137 |
|
|---|
| 138 |
// map of requests being executed: [http request * -> ExecutingRequest] |
|---|
| 139 |
typedef map<uint32, ExecutingRequest*> MapOfRequests; |
|---|
| 140 |
MapOfRequests requestsInExecution_; |
|---|
| 141 |
|
|---|
| 142 |
// synchronize access to map requestsInExecution_ |
|---|
| 143 |
synch::Mutex accessRequestsInExecution_; |
|---|
| 144 |
|
|---|
| 145 |
// Used for encoding / decoding of stuff |
|---|
| 146 |
rpc::BinaryCodec binary_codec_; |
|---|
| 147 |
rpc::JsonCodec json_codec_; |
|---|
| 148 |
|
|---|
| 149 |
// Javascript that needs included in http auto forms |
|---|
| 150 |
string auto_forms_js_; |
|---|
| 151 |
private: |
|---|
| 152 |
DISALLOW_EVIL_CONSTRUCTORS(HttpProcessor); |
|---|
| 153 |
}; |
|---|
| 154 |
} |
|---|
| 155 |
#endif // __NET_RPC_LIB_SERVER_RPC_HTTP_PROCESSOR_H__ |
|---|