root/trunk/whisperlib/net/http/http_proxy.h

Revision 7, 3.6 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
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: Catalin Popescu
31 //
32 // You can register  a path in an http server as a proxy to another server :)
33 // (helps with rpc and othe stuff..)
34 //
35 // NOTE: This is for demonstration only, the performance is pretty poor !!
36 //
37
38 #ifndef __NET_HTTP_HTTP_PROXY_H__
39 #define __NET_HTTP_HTTP_PROXY_H__
40
41 #include <set>
42 #include <map>
43 #include <string>
44
45 #include <whisperlib/common/base/types.h>
46 #include <whisperlib/net/http/http_server_protocol.h>
47 #include <whisperlib/net/http/http_client_protocol.h>
48
49 namespace http {
50
51 class Proxy {
52  public:
53   Proxy(net::Selector* selector,
54         net::NetFactory* net_factory,
55         net::PROTOCOL net_protocol,
56         http::Server* server,
57         bool dlog_level);
58
59   // Well .. you should take care of the pending requests blah ..
60   ~Proxy();
61
62   bool RegisterPath(const string& path,
63                     net::HostPort remote_server,
64                     const ClientParams* params);
65   bool UnregisterPath(const string& path);
66
67  private:
68   struct ProxyReqStruct {
69     ClientRequest* creq_;
70     ServerRequest* sreq_;
71     ClientStreamReceiverProtocol* proto_;
72     Closure* callback_;
73     Closure* done_callback_;
74     bool http_header_sent_;
75     bool paused_;
76     ProxyReqStruct()
77         : creq_(NULL), sreq_(NULL), proto_(NULL),
78           callback_(NULL), done_callback_(NULL),
79           http_header_sent_(false), paused_(false) {
80     }
81   };
82
83   void ProcessRequest(net::HostPort remote_server,
84                       const ClientParams* params,
85                       ServerRequest* request);
86   void StreamingCallback(ProxyReqStruct* preq);
87   void FinalizeRequest(ProxyReqStruct* preq);
88   void ClearRequest(ProxyReqStruct* preq);
89
90   net::Selector* const selector_;
91   net::NetFactory* const net_factory_;
92   const net::PROTOCOL net_protocol_;
93   http::Server* const server_;
94   const bool dlog_level_;
95   typedef map<string, http::Server::ServerCallback*> ProcMap;
96   ProcMap registered_processors_;
97
98   typedef set<ProxyReqStruct*> ReqSet;
99   ReqSet pending_requests_;
100  private:
101   DISALLOW_EVIL_CONSTRUCTORS(Proxy);
102 };
103
104 ////////////////////////////////////////////////////////////////////////////////
105 }
106
107 #endif  // __NET_HTTP_HTTP_PROXY_H__
Note: See TracBrowser for help on using the browser.