1 #include "ACEXML/common/URL_Addr.h"
3 #if !defined (__ACEXML_INLINE__)
4 #include "ACEXML/common/URL_Addr.inl"
5 #endif /* __ACEXML_INLINE__ */
7 #include "ace/Log_Msg.h"
8 #include "ace/OS_Memory.h"
9 #include "ace/OS_NS_stdio.h"
10 #include "ace/OS_NS_stdlib.h"
11 #include "ace/OS_NS_string.h"
14 ACEXML_URL_Addr::ACEXML_URL_Addr ()
22 ACEXML_URL_Addr::addr_to_string (ACEXML_Char
*s
,
24 int ipaddr_format
) const
26 size_t total_len
= this->calculate_length (ipaddr_format
);
31 ACE_OS::sprintf (s
, ACE_TEXT ("%s:%d/%s"),
32 ACE_TEXT_CHAR_TO_TCHAR (ipaddr_format
== 0
33 ? this->get_host_name ()
34 : this->get_host_addr ()),
35 this->get_port_number (),
36 this->get_path_name ());
42 ACEXML_URL_Addr::addr_to_string (int ipaddr_format
)
44 size_t size
= this->calculate_length (ipaddr_format
);
45 if (size
> this->addr_string_len_
)
47 ACE_ALLOCATOR_RETURN (this->addr_string_
,
48 (ACEXML_Char
*) ACE_OS::realloc(this->addr_string_
,
50 this->addr_string_len_
= size
;
52 ACE_OS::sprintf (this->addr_string_
,
53 ACE_TEXT ("%s:%d/%s"),
54 ACE_TEXT_CHAR_TO_TCHAR (ipaddr_format
== 0
55 ? this->get_host_name ()
56 : this->get_host_addr ()),
57 this->get_port_number (),
58 this->get_path_name ());
59 return this->addr_string_
;
62 #if defined (ACE_USES_WCHAR)
64 ACEXML_URL_Addr::string_to_addr (const char* s
,
67 return this->string_to_addr (ACE_TEXT_CHAR_TO_TCHAR (s
), address_family
);
69 #endif /* ACE_USES_WCHAR */
72 ACEXML_URL_Addr::string_to_addr (const ACEXML_Char
* s
,
73 int /*address_family */)
78 const ACEXML_Char
* http
= ACE_TEXT ("http://");
79 size_t http_len
= ACE_OS::strlen (http
);
81 // Check validity of URL
82 if (ACE_OS::strncmp (http
, s
, http_len
) != 0)
83 ACE_ERROR_RETURN ((LM_ERROR
, ACE_TEXT ("Invalid URL %s\n"), s
), -1);
85 const ACEXML_Char
* url
= 0;
87 for (url
= s
+ http_len
; *url
!= '\0' && *url
!= ':' && *url
!= '/'; ++url
)
90 size_t host_len
= url
- s
;
93 ACEXML_Char
* host_name
= 0;
94 ACE_NEW_RETURN (host_name
, ACEXML_Char
[host_len
+ 1], -1);
95 ACE_OS::strncpy (host_name
, s
+ http_len
, host_len
);
96 host_name
[host_len
] = '\0';
97 std::unique_ptr
<ACEXML_Char
[]> cleanup_host_name (host_name
);
99 // Get the port number (if any)
100 unsigned short port
= ACE_DEFAULT_HTTP_PORT
;
103 port
= (unsigned short) ACE_OS::strtol (++url
, 0, 10); // Skip over ':'
104 while ( *url
!= '\0' && *url
!= '/' )
109 int result
= this->ACE_INET_Addr::set (port
, host_name
);
115 const ACEXML_Char
* path_name
= 0;
117 path_name
= ACE_TEXT ("/");
121 ACE_ALLOCATOR_RETURN (this->path_name_
, ACE_OS::strdup (path_name
), -1);
125 ACEXML_URL_Addr::ACEXML_URL_Addr (const ACEXML_URL_Addr
&addr
)
131 if (this->set (addr
) == -1)
132 ACE_ERROR ((LM_ERROR
,
134 ACE_TEXT ("ACEXML_URL_Addr::ACEXML_URL_Addr")));
138 ACEXML_URL_Addr::set (const ACEXML_URL_Addr
&addr
)
140 ACE_OS::free (this->path_name_
);
141 ACE_OS::free (this->addr_string_
);
142 if (this->ACE_INET_Addr::set (addr
) == -1)
147 ACE_ALLOCATOR_RETURN (this->path_name_
,
148 ACE_OS::strdup (addr
.path_name_
),
150 if (addr
.addr_string_
)
151 ACE_ALLOCATOR_RETURN (this->addr_string_
,
152 ACE_OS::strdup (addr
.addr_string_
),
154 this->addr_string_len_
= addr
.addr_string_len_
;
159 ACEXML_URL_Addr::ACEXML_URL_Addr (const ACEXML_Char
*host_name
,
160 const ACEXML_Char
*path_name
,
162 : ACE_INET_Addr (port
, host_name
),
163 path_name_ (ACE_OS::strdup (path_name
)),
169 ACEXML_URL_Addr::~ACEXML_URL_Addr ()
171 ACE_OS::free (this->path_name_
);
172 ACE_OS::free (this->addr_string_
);
173 this->path_name_
= 0;