Go to the documentation of this file.00001
00002
00003 #include "ace/INet/URLBase.h"
00004 #include "ace/INet/IOS_util.h"
00005
00006 #if !defined (__ACE_INLINE__)
00007 #include "ace/INet/URLBase.inl"
00008 #endif
00009
00010 #include "ace/INet/String_IOStream.h"
00011
00012 #include "ace/INet/ClientRequestHandler.h"
00013 #include <istream>
00014
00015 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00016
00017 namespace ACE
00018 {
00019 namespace INet
00020 {
00021
00022 URLStream::URLStream (const URLStream& url_stream)
00023 : request_handler_ref_ (url_stream.request_handler_ref_),
00024 request_handler_ (url_stream.request_handler_)
00025 {
00026 }
00027
00028 URLStream::~URLStream ()
00029 {
00030 }
00031
00032 bool URLStream::operator ! ()
00033 {
00034 return this->request_handler_ == 0 || !this->request_handler_->is_response_ok ();
00035 }
00036
00037 URLStream::operator bool ()
00038 {
00039 return this->request_handler_ != 0 && this->request_handler_->is_response_ok ();
00040 }
00041
00042 std::istream& URLStream::operator * ()
00043 {
00044 return this->request_handler_ ?
00045 this->request_handler_->response_stream () :
00046 ACE::IOS::Null::in_stream_;
00047 }
00048
00049 std::istream* URLStream::operator -> ()
00050 {
00051 return this->request_handler_ ?
00052 &this->request_handler_->response_stream () :
00053 &ACE::IOS::Null::in_stream_;
00054 }
00055
00056 URLStream::URLStream (ClientRequestHandler& rh)
00057 : request_handler_ (&rh)
00058 {
00059 }
00060
00061 URLStream::URLStream (ClientRequestHandler* rh)
00062 : request_handler_ref_ (rh),
00063 request_handler_ (rh)
00064 {
00065 }
00066
00067 const ACE_CString URL_Base::empty_;
00068
00069 URL_Base::URL_Base ()
00070 {
00071 }
00072
00073 URL_Base::~URL_Base () {}
00074
00075 bool URL_Base::parse (const ACE_CString& url_string)
00076 {
00077 static const int eof =
00078 std::char_traits<ACE::IOS::CString_OStream::char_type>::eof ();
00079
00080 ACE_CString uri = url_string;
00081 if (this->strip_scheme (uri))
00082 {
00083 ACE::IOS::CString_OStream sos;
00084 ACE::IOS::CString_IStream sis (uri);
00085
00086 int ch;
00087
00088
00089 if ((ch = this->parse_authority (sis)) == '/' ||
00090 !this->has_authority ())
00091 {
00092
00093 sos.put (ch);
00094 for (ch = sis.get (); ch != '?' && ch != '#' && ch != eof ;ch = sis.get ())
00095 sos.put (ch);
00096 this->set_path (sos.str ());
00097 sos.clear ();
00098 }
00099 else
00100 {
00101
00102 this->set_path (empty_);
00103 }
00104
00105 if (ch == '?')
00106 {
00107
00108 for (ch = sis.get (); ch != '#' && ch != eof ;ch = sis.get ())
00109 sos.put (ch);
00110 this->set_query (sos.str ());
00111 sos.clear ();
00112 }
00113
00114 if (ch == '#')
00115 {
00116
00117 sos << sis.rdbuf ();
00118 this->set_fragment (sos.str ());
00119 }
00120 else if (ch != eof)
00121 {
00122
00123 return false;
00124 }
00125
00126
00127 return this->validate ();
00128 }
00129 return false;
00130 }
00131
00132 int URL_Base::parse_authority(std::istream& is)
00133 {
00134 return is.get ();
00135 }
00136
00137 bool URL_Base::has_authority ()
00138 {
00139 return false;
00140 }
00141
00142 bool URL_Base::validate ()
00143 {
00144 return true;
00145 }
00146
00147 URLStream URL_Base::open () const
00148 {
00149 ClientRequestHandler* rh = this->create_default_request_handler ();
00150 if (rh)
00151 {
00152 rh->handle_open_request (*this);
00153 return URLStream (rh);
00154 }
00155 else
00156 return URLStream (0);
00157 }
00158
00159 URLStream URL_Base::open (ClientRequestHandler& rh) const
00160 {
00161 rh.handle_open_request (*this);
00162 return URLStream (rh);
00163 }
00164
00165 URL_Base* URL_Base::create_from_string (const ACE_CString& url_string)
00166 {
00167 ACE_CString::size_type pos = url_string.find (':');
00168 if (pos >0 )
00169 {
00170 Factory* url_factory = 0;
00171 if (factories_->find (url_string.substr (0, pos), url_factory) == 0)
00172 {
00173 return url_factory->create_from_string (url_string);
00174 }
00175 }
00176
00177 return 0;
00178 }
00179
00180 #if defined (ACE_HAS_WCHAR)
00181 bool URL_Base::parse (const ACE_WString& url_string)
00182 {
00183 return this->parse (ACE_Wide_To_Ascii (url_string.c_str ()).char_rep ());
00184 }
00185
00186 ACE_WString URL_Base::to_wstring () const
00187 {
00188 return ACE_Ascii_To_Wide (this->to_string().c_str ()).wchar_rep ();
00189 }
00190
00191 URL_Base* URL_Base::create_from_wstring (const ACE_WString& url_string)
00192 {
00193 return create_from_string (ACE_Wide_To_Ascii (url_string.c_str ()).char_rep ());
00194 }
00195 #endif
00196
00197 bool URL_Base::strip_scheme (ACE_CString& url_string)
00198 {
00199
00200
00201
00202
00203
00204 ACE_CString::size_type pos = url_string.find (':');
00205 if (pos > 0 && url_string[pos+1] == '/' && url_string[pos+1] == '/')
00206 {
00207
00208 if (this->get_protocol () != url_string.substr (0, pos))
00209 {
00210 return false;
00211 }
00212 url_string = url_string.substr (pos+3);
00213 }
00214 return true;
00215 }
00216
00217 void URL_Base::register_factory (Factory* url_factory)
00218 {
00219 if (factories_ == 0)
00220 {
00221 factories_ = URL_Base::TURLFactorySingleton::instance ();
00222 }
00223 if (url_factory)
00224 factories_->bind (url_factory->protocol (), url_factory);
00225 }
00226
00227 void URL_Base::deregister_factory (Factory* url_factory)
00228 {
00229 if (factories_ && url_factory)
00230 {
00231 factories_->unbind (url_factory->protocol ());
00232 };
00233 }
00234
00235 URL_Base::TURLFactoryMap* URL_Base::factories_ = 0;
00236
00237 URL_Base::Factory::Factory ()
00238 {}
00239
00240 URL_Base::Factory::~Factory ()
00241 {}
00242
00243 URL_INetBase::URL_INetBase (u_short port)
00244 : URL_Base (), port_ (port)
00245 {
00246 }
00247
00248 URL_INetBase::~URL_INetBase () {}
00249
00250 int URL_INetBase::parse_authority (std::istream& is)
00251 {
00252 ACE::IOS::CString_OStream sos;
00253 return this->parse_authority_i (is, sos, 0);
00254 }
00255
00256 int URL_INetBase::parse_authority_i (std::istream& is,
00257 std::ostream& os,
00258 int lastch)
00259 {
00260 static const int eof =
00261 std::char_traits<ACE::IOS::CString_OStream::char_type>::eof ();
00262
00263 ACE::IOS::CString_OStream& sos =
00264 dynamic_cast<ACE::IOS::CString_OStream&> (os);
00265
00266 int ch = lastch;
00267 if (ch == 0)
00268 {
00269
00270 for (ch = is.get ();
00271 #if defined (ACE_HAS_IPV6)
00272 ch != '[' && ch != '/' && ch != ':' && ch != '@' && ch != '?' && ch != '#' && ch != eof ;
00273 #else
00274 ch != '/' && ch != ':' && ch != '@' && ch != '?' && ch != '#' && ch != eof ;
00275 #endif
00276 ch = is.get ())
00277 sos.put (ch);
00278 }
00279
00280 #if defined (ACE_HAS_IPV6)
00281 if (ch == '[')
00282 {
00283 sos.clear ();
00284 for (ch = is.get (); ch != ']' && ch != eof ;ch = is.get ())
00285 sos.put (ch);
00286 if (ch != eof)
00287 ch = is.get ();
00288 if (ch != '/' && ch != ':' && ch != '?' && ch != '#' && ch != eof)
00289 {
00290 this->set_host (empty_);
00291 ch = eof;
00292 }
00293 else
00294 {
00295 this->set_host (sos.str ());
00296 }
00297 }
00298 else
00299 {
00300 #endif
00301 this->set_host (sos.str ());
00302 #if defined (ACE_HAS_IPV6)
00303 }
00304 #endif
00305 sos.clear ();
00306
00307 if (ch == ':')
00308 {
00309 u_short port = 0;
00310 is >> port;
00311 ch = is.get ();
00312 if (ch == '/' || ch == '?' || ch == '#' || ch == eof)
00313 this->set_port (port);
00314 else
00315 this->set_port (0);
00316 }
00317 else
00318 {
00319 this->set_port (this->default_port ());
00320 }
00321
00322 return ch;
00323 }
00324
00325 bool URL_INetBase::has_authority ()
00326 {
00327 return true;
00328 }
00329
00330 bool URL_INetBase::validate ()
00331 {
00332 return !this->host_.empty () && this->port_>0;
00333 }
00334
00335 ACE_CString URL_INetBase::get_authority () const
00336 {
00337 ACE::IOS::CString_OStream sos;
00338 sos << this->get_host().c_str ();
00339 if (this->get_port () != this->default_port ())
00340 sos << ':' << this->get_port ();
00341 return sos.str ();
00342 }
00343
00344 URL_INetAuthBase::authenticator_map URL_INetAuthBase::authenticators_;
00345
00346 URL_INetAuthBase::URL_INetAuthBase (u_short port)
00347 : URL_INetBase (port)
00348 {
00349 }
00350
00351 URL_INetAuthBase::~URL_INetAuthBase () {}
00352
00353 ACE_CString URL_INetAuthBase::get_authority () const
00354 {
00355 ACE::IOS::CString_OStream sos;
00356 if (!this->get_user_info ().empty ())
00357 sos << this->get_user_info ().c_str () << "@";
00358 sos << this->get_host().c_str ();
00359 if (this->get_port () != this->default_port ())
00360 sos << ':' << this->get_port ();
00361 return sos.str ();
00362 }
00363
00364 int URL_INetAuthBase::parse_authority (std::istream& is)
00365 {
00366 static const int eof =
00367 std::char_traits<ACE::IOS::CString_OStream::char_type>::eof ();
00368
00369 ACE::IOS::CString_OStream sos;
00370
00371 int ch;
00372
00373 for (ch = is.get ();
00374 #if defined (ACE_HAS_IPV6)
00375 ch != '[' && ch != '/' && ch != ':' && ch != '@' && ch != '?' && ch != '#' && ch != eof ;
00376 #else
00377 ch != '/' && ch != ':' && ch != '@' && ch != '?' && ch != '#' && ch != eof ;
00378 #endif
00379 ch = is.get ())
00380 sos.put (ch);
00381
00382 if (ch == '@')
00383 {
00384 this->set_user_info (sos.str ());
00385 sos.clear ();
00386 ch = URL_INetBase::parse_authority_i (is, sos, 0);
00387 }
00388 else
00389 {
00390 ch = URL_INetBase::parse_authority_i (is, sos, ch);
00391 }
00392
00393 return ch;
00394 }
00395
00396 bool URL_INetAuthBase::add_authenticator (const ACE_CString& auth_id,
00397 AuthenticatorBase* authenticator)
00398 {
00399 if (URL_INetAuthBase::authenticators_.find (auth_id) == -1)
00400 {
00401 return URL_INetAuthBase::authenticators_.bind (auth_id,
00402 authenticator_ptr (authenticator)) == 0;
00403 }
00404 return false;
00405 }
00406
00407 bool URL_INetAuthBase::has_authenticator (const ACE_CString& auth_id)
00408 {
00409 return (URL_INetAuthBase::authenticators_.find (auth_id) == 0);
00410 }
00411
00412 AuthenticatorBase* URL_INetAuthBase::remove_authenticator (const ACE_CString& auth_id)
00413 {
00414 authenticator_ptr auth;
00415 if (URL_INetAuthBase::authenticators_.unbind (auth_id, auth) == 0)
00416 {
00417 auth.release ();
00418 }
00419 return 0;
00420 }
00421
00422 bool URL_INetAuthBase::authenticate (AuthenticationBase& authentication)
00423 {
00424 ACE_GUARD_RETURN (ACE_SYNCH::RECURSIVE_MUTEX,
00425 _guard,
00426 URL_INetAuthBase::authenticators_.mutex (),
00427 false);
00428
00429 authenticator_map::iterator it = URL_INetAuthBase::authenticators_.begin ();
00430 for (; it != URL_INetAuthBase::authenticators_.end ();
00431 ++it)
00432 {
00433 authenticator_ptr auth_ptr = (*it).int_id_;
00434
00435
00436 if (URL_INetAuthBase::authenticators_.mutex ().release () != 0)
00437 return false;
00438
00439 if (auth_ptr->authenticate (authentication))
00440 return true;
00441
00442
00443 if (URL_INetAuthBase::authenticators_.mutex ().acquire () != 0)
00444 return false;
00445 }
00446
00447 return false;
00448 }
00449 }
00450 }
00451
00452 ACE_END_VERSIONED_NAMESPACE_DECL