summaryrefslogtreecommitdiff
path: root/ACE/protocols/ace/INet/HTTP_ClientRequestHandler.cpp
blob: ade87052b3736d309bf3ed39f956887461994a2d (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
// $Id$

#include "ace/INet/HTTP_ClientRequestHandler.h"

#if !defined (__ACE_INLINE__)
#include "ace/INet/HTTP_ClientRequestHandler.inl"
#endif

#include "ace/INet/INet_Log.h"
#include "ace/Auto_Ptr.h"
#include "ace/Functor_String.h"

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

namespace ACE
{
  namespace HTTP
  {

    SessionFactoryRegistry::SessionFactoryRegistry ()
      {
      }

    SessionFactoryRegistry::~SessionFactoryRegistry ()
      {
      }

    void SessionFactoryRegistry::register_session_factory (
        const ACE_CString& scheme,
        SessionFactory* factory)
      {
        if (factory == 0)
          this->factory_map_.unbind (scheme);
        else
          this->factory_map_.rebind (scheme, factory);
      }

    SessionFactory*
    SessionFactoryRegistry::find_session_factory (const ACE_CString& scheme)
      {
        SessionFactory* factory = 0;
        this->factory_map_.find (scheme, factory);
        return factory;
      }

    SessionFactoryRegistry& SessionFactoryRegistry::instance ()
      {
        return *ACE_Singleton<SessionFactoryRegistry, ACE_SYNCH::MUTEX>::instance ();
      }

    SessionHolder::SessionHolder ()
      {
      }

    SessionHolder::~SessionHolder()
      {
      }

    SessionFactory_Impl::SessionHolder_Impl::SessionHolder_Impl ()
      : session_ (true)
      {
      }

    SessionFactory_Impl::SessionHolder_Impl::~SessionHolder_Impl()
      {
      }

    SessionBase& SessionFactory_Impl::SessionHolder_Impl::session ()
      {
        return this->session_;
      }

    SessionFactory_Impl& SessionFactory_Impl::factory_ =
        *ACE_Singleton<SessionFactory_Impl,ACE_SYNCH::NULL_MUTEX>::instance ();

    SessionFactory_Impl::SessionFactory_Impl ()
      {
        INET_DEBUG (6, (LM_INFO, DLINFO
                                 ACE_TEXT ("HTTP_SessionFactory_Impl::ctor - ")
                                 ACE_TEXT ("registering session factory for scheme [%C]\n"),
                                 URL::protocol ().c_str ()));
        SessionFactoryRegistry::instance ().register_session_factory (URL::protocol (), this);
      }

    SessionFactory_Impl::~SessionFactory_Impl ()
      {
      }

    ACE::INet::ConnectionHolder*
    SessionFactory_Impl::create_connection (
        const ACE::INet::ConnectionKey& key) const
      {
        INET_TRACE ("HTTP_SessionFactory_Impl::create_connection");

        const ClientRequestHandler::HttpConnectionKey& ikey =
            dynamic_cast<const ClientRequestHandler::HttpConnectionKey&> (key);

        SessionHolder_Impl* session_holder = 0;
        ACE_NEW_RETURN (session_holder,
                        SessionHolder_Impl (),
                        0);
        ACE_Auto_Ptr<SessionHolder_Impl> session_safe_ref (session_holder);

        (*session_holder)->set_host (ikey.host (), ikey.port ());
        if (ikey.is_proxy_connection ())
          {
            (*session_holder)->set_proxy_target (ikey.proxy_target_host (),
                                                 ikey.proxy_target_port ());
          }

        if ((*session_holder)->connect (true))
          {
            return session_safe_ref.release ();
          }

        return 0;
      }

    ClientRequestHandler::HttpConnectionKey::HttpConnectionKey (
        const ACE_CString& host,
        u_short port)
      : INetConnectionKey (host, port),
        proxy_connection_ (false),
        proxy_target_port_ (0)
      {
      }

    ClientRequestHandler::HttpConnectionKey::HttpConnectionKey (
        const ACE_CString& proxy_host,
        u_short proxy_port,
        const ACE_CString& host,
        u_short port)
      : INetConnectionKey (proxy_host, proxy_port),
        proxy_connection_ (true),
        proxy_target_host_ (host),
        proxy_target_port_ (port)
      {
      }

    ClientRequestHandler::HttpConnectionKey::~HttpConnectionKey()
      {
      }

    u_long ClientRequestHandler::HttpConnectionKey::hash () const
      {
        if (this->proxy_connection_)
          return ACE_Hash<ACE_CString>()(this->proxy_target_host_) +
                this->proxy_target_port_ +
                (this->proxy_connection_ ? 1 : 0);
        else
          return INetConnectionKey::hash () +
                (this->proxy_connection_ ? 1 : 0);
      }

    ACE::INet::ConnectionKey* ClientRequestHandler::HttpConnectionKey::duplicate () const
      {
        ConnectionKey* k = 0;
        if (this->proxy_connection_)
          {
            ACE_NEW_RETURN (k,
                            HttpConnectionKey (this->host (), this->port (),
                                               this->proxy_target_host_,
                                               this->proxy_target_port_),
                            0);
          }
        else
          {
            ACE_NEW_RETURN (k,
                            HttpConnectionKey (this->host (), this->port ()),
                            0);
          }
        return k;
      }

    bool ClientRequestHandler::HttpConnectionKey::equal (const ACE::INet::ConnectionKey& key) const
      {
        try {
          const HttpConnectionKey& http_key = dynamic_cast<const HttpConnectionKey&> (key);
          return (INetConnectionKey::equal (key) &&
                    this->proxy_connection_ == http_key.is_proxy_connection () &&
                    (!this->proxy_connection_ ||
                     (this->proxy_target_host_ == http_key.proxy_target_host () &&
                      this->proxy_target_port_ == http_key.proxy_target_port ())));
        }
        catch (...) {
          return false;
        }
      }

    ClientRequestHandler::ClientRequestHandler ()
      : request_ (Request::HTTP_1_0),
        session_ (0)
      {
      }

    ClientRequestHandler::~ClientRequestHandler ()
      {
        this->release_connection ();
      }

    bool ClientRequestHandler::is_response_ok () const
      {
        return this->response_.get_status ().is_ok () &&
                !const_cast<ClientRequestHandler*> (this)->response_stream ().bad ();
      }

    std::istream& ClientRequestHandler::handle_open_request (
        const ACE::INet::URL_Base& url)
      {
        const URL& http_url = dynamic_cast<const URL&> (url);
        return this->handle_get_request (http_url);
      }

    std::istream& ClientRequestHandler::handle_get_request (
        const URL& http_url)
      {
        bool connected = false;
        if (http_url.has_proxy ())
          connected = this->initialize_connection (http_url.get_scheme (),
                                                   http_url.get_host(),
                                                   http_url.get_port (),
                                                   true,
                                                   http_url.get_proxy_host(),
                                                   http_url.get_proxy_port ());
        else
          connected = this->initialize_connection (http_url.get_scheme (),
                                                   http_url.get_host(),
                                                   http_url.get_port ());

        if (connected)
          {
            this->request_.reset (Request::HTTP_GET,
                                  http_url.get_request_uri (),
                                  this->request_.get_version ());

            this->response_.reset ();

            this->initialize_request (http_url, this->request_);

            if (!this->session ()->send_request (this->request_) ||
                !this->session ()->receive_response (this->response_))
              {
                this->close_connection ();

                this->handle_request_error(http_url);
              }
          }
        else
          {
            this->handle_connection_error (http_url);
          }

        return this->response_stream ();
      }

    void ClientRequestHandler::on_eof ()
      {
        this->release_connection ();
      }

    bool ClientRequestHandler::initialize_connection (const ACE_CString& scheme,
                                                      const ACE_CString& host,
                                                      u_short port,
                                                      bool proxy_conn,
                                                      const ACE_CString& proxy_host,
                                                      u_short proxy_port)
      {
        SessionFactory* session_factory =
          SessionFactoryRegistry::instance ().find_session_factory (scheme);

        if (session_factory == 0)
          {
            INET_ERROR (1, (LM_ERROR, DLINFO
                            ACE_TEXT ("ClientRequestHandler::initialize_connection - ")
                            ACE_TEXT ("unable to find session factory for scheme [%C]\n"),
                            scheme.c_str ()));
            return false;
          }

        ACE::INet::ConnectionHolder* pch = 0;
        if (proxy_conn)
          {
            if (!this->connection_cache ().claim_connection (HttpConnectionKey (proxy_host,
                                                                                proxy_port,
                                                                                host,
                                                                                port),
                                                            pch,
                                                            *session_factory))
              return false;
          }
        else
          {
            if (!this->connection_cache ().claim_connection (HttpConnectionKey (host,
                                                                                port),
                                                            pch,
                                                            *session_factory))
              return false;
          }

        this->session (dynamic_cast<SessionHolder*> (pch));
        return true;
      }

    void ClientRequestHandler::initialize_request (const URL& /*url*/, Request& /*request*/)
      {
      }

    void ClientRequestHandler::handle_request_error (const URL& /*url*/)
      {
      }

    void ClientRequestHandler::handle_connection_error (const URL& /*url*/)
      {
      }

    void ClientRequestHandler::release_connection ()
      {
        if (this->session_)
          {
            if (this->session ()->is_proxy_connection ())
              {
                this->connection_cache ().release_connection (
                    HttpConnectionKey (this->session ()->get_host (),
                                       this->session ()->get_port (),
                                       this->session ()->get_proxy_target_host (),
                                       this->session ()->get_proxy_target_port ()),
                    this->session_);
              }
            else
              {
                this->connection_cache ().release_connection (
                    HttpConnectionKey (this->session ()->get_host (),
                                       this->session ()->get_port ()),
                    this->session_);
              }
            this->session_ = 0;
          }
      }

    void ClientRequestHandler::close_connection ()
      {
        if (this->session_)
          {
            if (this->session ()->is_proxy_connection ())
              {
                this->connection_cache ().release_connection (
                    HttpConnectionKey (this->session ()->get_host (),
                                       this->session ()->get_port (),
                                       this->session ()->get_proxy_target_host (),
                                       this->session ()->get_proxy_target_port ()),
                    this->session_);
              }
            else
              {
                this->connection_cache ().release_connection (
                    HttpConnectionKey (this->session ()->get_host (),
                                       this->session ()->get_port ()),
                    this->session_);
              }
            this->session_ = 0;
          }
      }

  }
}

ACE_END_VERSIONED_NAMESPACE_DECL