1 #include "ace/INet/URLBase.h"
2 #include "ace/INet/IOS_util.h"
4 #if !defined (__ACE_INLINE__)
5 #include "ace/INet/URLBase.inl"
8 #include "ace/INet/String_IOStream.h"
10 #include "ace/INet/ClientRequestHandler.h"
13 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
19 URLStream::URLStream (const URLStream
& url_stream
)
20 : request_handler_ref_ (url_stream
.request_handler_ref_
),
21 request_handler_ (url_stream
.request_handler_
)
25 URLStream::~URLStream ()
29 bool URLStream::operator ! ()
31 return this->request_handler_
== 0 || !this->request_handler_
->is_response_ok ();
34 URLStream::operator bool ()
36 return this->request_handler_
!= 0 && this->request_handler_
->is_response_ok ();
39 std::istream
& URLStream::operator * ()
41 return this->request_handler_
?
42 this->request_handler_
->response_stream () :
43 ACE::IOS::Null::in_stream_
;
46 std::istream
* URLStream::operator -> ()
48 return this->request_handler_
?
49 &this->request_handler_
->response_stream () :
50 &ACE::IOS::Null::in_stream_
;
53 URLStream::URLStream (ClientRequestHandler
& rh
)
54 : request_handler_ (&rh
)
58 URLStream::URLStream (ClientRequestHandler
* rh
)
59 : request_handler_ref_ (rh
),
64 const ACE_CString
URL_Base::empty_
;
70 URL_Base::~URL_Base () {}
72 bool URL_Base::parse (const ACE_CString
& url_string
)
74 static const int eof
=
75 std::char_traits
<ACE::IOS::CString_OStream::char_type
>::eof ();
77 ACE_CString uri
= url_string
;
78 if (this->strip_scheme (uri
))
80 ACE::IOS::CString_OStream sos
;
81 ACE::IOS::CString_IStream
sis (uri
);
85 // parse authority part (if any)
86 if ((ch
= this->parse_authority (sis
)) == '/' ||
87 !this->has_authority ()) // relative paths allowed if no authority
91 for (ch
= sis
.get (); ch
!= '?' && ch
!= '#' && ch
!= eof
;ch
= sis
.get ())
93 this->set_path (sos
.str ());
99 this->set_path (empty_
);
105 for (ch
= sis
.get (); ch
!= '#' && ch
!= eof
;ch
= sis
.get ())
107 this->set_query (sos
.str ());
115 this->set_fragment (sos
.str ());
123 // check for (minimum) correctness
124 return this->validate ();
129 int URL_Base::parse_authority(std::istream
& is
)
134 bool URL_Base::has_authority ()
139 bool URL_Base::validate ()
144 URLStream
URL_Base::open () const
146 ClientRequestHandler
* rh
= this->create_default_request_handler ();
149 rh
->handle_open_request (*this);
150 return URLStream (rh
);
153 return URLStream (0);
156 URLStream
URL_Base::open (ClientRequestHandler
& rh
) const
158 rh
.handle_open_request (*this);
159 return URLStream (rh
);
162 URL_Base
* URL_Base::create_from_string (const ACE_CString
& url_string
)
164 ACE_CString::size_type pos
= url_string
.find (':');
167 Factory
* url_factory
= 0;
168 if (factories_
->find (url_string
.substr (0, pos
), url_factory
) == 0)
170 return url_factory
->create_from_string (url_string
);
177 #if defined (ACE_HAS_WCHAR)
178 bool URL_Base::parse (const ACE_WString
& url_string
)
180 return this->parse (ACE_Wide_To_Ascii (url_string
.c_str ()).char_rep ());
183 ACE_WString
URL_Base::to_wstring () const
185 ACE_Ascii_To_Wide
ws(this->to_string().c_str ());
186 return ws
.wchar_rep ();
189 URL_Base
* URL_Base::create_from_wstring (const ACE_WString
& url_string
)
191 return create_from_string (ACE_Wide_To_Ascii (url_string
.c_str ()).char_rep ());
195 bool URL_Base::strip_scheme (ACE_CString
& url_string
)
197 // since this will be called at a point where the
198 // actual URL class is already known (and with that
199 // the protocol prefix) we allow for the fact we
200 // may get a url passed without the actual prefix
202 ACE_CString::size_type pos
= url_string
.find (':');
203 if (pos
> 0 && url_string
[pos
+1] == '/' && url_string
[pos
+2] == '/')
205 // in case we find a scheme check for the right protocol
206 if (this->get_protocol () != url_string
.substr (0, pos
))
210 url_string
= url_string
.substr (pos
+3); // skip '<protocol>://'
215 void URL_Base::register_factory (Factory
* url_factory
)
219 factories_
= URL_Base::TURLFactorySingleton::instance ();
222 factories_
->bind (url_factory
->protocol (), url_factory
);
225 void URL_Base::deregister_factory (Factory
* url_factory
)
227 if (factories_
&& url_factory
)
229 factories_
->unbind (url_factory
->protocol ());
233 URL_Base::TURLFactoryMap
* URL_Base::factories_
= 0;
235 URL_Base::Factory::Factory ()
238 URL_Base::Factory::~Factory ()
241 URL_INetBase::URL_INetBase (u_short port
)
242 : URL_Base (), port_ (port
)
246 URL_INetBase::~URL_INetBase () {}
248 int URL_INetBase::parse_authority (std::istream
& is
)
250 ACE::IOS::CString_OStream sos
;
251 return this->parse_authority_i (is
, sos
, 0);
254 int URL_INetBase::parse_authority_i (std::istream
& is
,
258 static const int eof
=
259 std::char_traits
<ACE::IOS::CString_OStream::char_type
>::eof ();
261 ACE::IOS::CString_OStream
& sos
=
262 dynamic_cast<ACE::IOS::CString_OStream
&> (os
);
269 #if defined (ACE_HAS_IPV6)
270 ch
!= '[' && ch
!= '/' && ch
!= ':' && ch
!= '@' && ch
!= '?' && ch
!= '#' && ch
!= eof
;
272 ch
!= '/' && ch
!= ':' && ch
!= '@' && ch
!= '?' && ch
!= '#' && ch
!= eof
;
278 #if defined (ACE_HAS_IPV6)
282 for (ch
= is
.get (); ch
!= ']' && ch
!= eof
;ch
= is
.get ())
285 ch
= is
.get (); // skip ']'
286 if (ch
!= '/' && ch
!= ':' && ch
!= '?' && ch
!= '#' && ch
!= eof
)
288 this->set_host (empty_
); // invalid URL, clear host field
289 ch
= eof
; // stop parsing
293 this->set_host (sos
.str ());
299 this->set_host (sos
.str ());
300 #if defined (ACE_HAS_IPV6)
308 is
>> port
; // should stop at '/' or '?' or '#' or eof
310 if (ch
== '/' || ch
== '?' || ch
== '#' || ch
== eof
)
311 this->set_port (port
);
317 this->set_port (this->default_port ());
323 bool URL_INetBase::has_authority ()
328 bool URL_INetBase::validate ()
330 return !this->host_
.empty () && this->port_
>0;
333 ACE_CString
URL_INetBase::get_authority () const
335 ACE::IOS::CString_OStream sos
;
336 sos
<< this->get_host().c_str ();
337 if (this->get_port () != this->default_port ())
338 sos
<< ':' << this->get_port ();
342 URL_INetAuthBase::authenticator_map
URL_INetAuthBase::authenticators_
;
344 URL_INetAuthBase::URL_INetAuthBase (u_short port
)
345 : URL_INetBase (port
)
349 URL_INetAuthBase::~URL_INetAuthBase () {}
351 ACE_CString
URL_INetAuthBase::get_authority () const
353 ACE::IOS::CString_OStream sos
;
354 if (!this->get_user_info ().empty ())
355 sos
<< this->get_user_info ().c_str () << "@";
356 sos
<< this->get_host().c_str ();
357 if (this->get_port () != this->default_port ())
358 sos
<< ':' << this->get_port ();
362 int URL_INetAuthBase::parse_authority (std::istream
& is
)
364 static const int eof
=
365 std::char_traits
<ACE::IOS::CString_OStream::char_type
>::eof ();
367 ACE::IOS::CString_OStream sos
;
370 // parse userinfo (if any)
372 #if defined (ACE_HAS_IPV6)
373 ch
!= '[' && ch
!= '/' && ch
!= ':' && ch
!= '@' && ch
!= '?' && ch
!= '#' && ch
!= eof
;
375 ch
!= '/' && ch
!= ':' && ch
!= '@' && ch
!= '?' && ch
!= '#' && ch
!= eof
;
382 this->set_user_info (sos
.str ());
384 ch
= URL_INetBase::parse_authority_i (is
, sos
, 0);
388 ch
= URL_INetBase::parse_authority_i (is
, sos
, ch
);
394 bool URL_INetAuthBase::add_authenticator (const ACE_CString
& auth_id
,
395 AuthenticatorBase
* authenticator
)
397 if (URL_INetAuthBase::authenticators_
.find (auth_id
) == -1)
399 return URL_INetAuthBase::authenticators_
.bind (auth_id
,
400 authenticator_ptr (authenticator
)) == 0;
405 bool URL_INetAuthBase::has_authenticator (const ACE_CString
& auth_id
)
407 return (URL_INetAuthBase::authenticators_
.find (auth_id
) == 0);
410 AuthenticatorBase
* URL_INetAuthBase::remove_authenticator (const ACE_CString
& auth_id
)
412 authenticator_ptr auth
;
413 if (URL_INetAuthBase::authenticators_
.unbind (auth_id
, auth
) == 0)
420 bool URL_INetAuthBase::authenticate (AuthenticationBase
& authentication
)
422 ACE_GUARD_RETURN (ACE_SYNCH::RECURSIVE_MUTEX
,
424 URL_INetAuthBase::authenticators_
.mutex (),
427 authenticator_map::iterator it
= URL_INetAuthBase::authenticators_
.begin ();
428 for (; it
!= URL_INetAuthBase::authenticators_
.end ();
431 authenticator_ptr auth_ptr
= (*it
).int_id_
;
433 // release lock before calling user code
434 if (URL_INetAuthBase::authenticators_
.mutex ().release () != 0)
437 if (auth_ptr
->authenticate (authentication
))
441 if (URL_INetAuthBase::authenticators_
.mutex ().acquire () != 0)
450 ACE_END_VERSIONED_NAMESPACE_DECL