Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / apps / JAWS2 / HTTPU / parse_url.cpp
blob602ded44f821db76ff9a7724706a44077092a1e4
1 #include "ace/OS_NS_string.h"
2 #include "ace/OS_NS_stdlib.h"
3 #include "ace/Log_Msg.h"
4 #include "HTTPU/parse_url.h"
6 HTTP_Parse_URL::HTTP_Parse_URL (const char *url)
7 : url_ (0),
8 scheme_ (0),
9 user_ (0),
10 passwd_ (0),
11 host_ (0),
12 port_ (-1),
13 url_path_ (0),
14 error_ (URL_ERROR_NONE),
15 is_cgi_ (0)
17 this->init (url);
20 HTTP_Parse_URL::~HTTP_Parse_URL ()
22 if (this->url_)
23 ACE_OS::free (this->url_);
24 this->url_ = 0;
25 this->scheme_ = 0;
26 this->user_ = 0;
27 this->passwd_ = 0;
28 this->host_ = 0;
29 this->port_ = -1;
30 this->url_path_ = 0;
34 void
35 HTTP_Parse_URL::init( const char *url )
37 // Should really reset completely and cleanly here before
38 // doing anything else!
39 if ( url == 0 )
40 return;
42 if ( url_ )
43 ACE_OS::free( url_ );
45 url_ = ACE_OS::strdup( url );
46 if ( url_ == 0 )
48 error_ = URL_ERROR_STRDUP;
49 return;
52 if (ACE_OS::strlen (this->url_) > 3 && ACE_OS::strstr ("://", this->url_))
54 // Parse it out completely. Figure out what it is later.
55 parse_url();
57 else
59 this->url_path_ = this->url_;
60 this->is_cgi (this->url_path_);
65 void
66 HTTP_Parse_URL::parse_url ()
68 char *p = this->url_;
70 char *q;
71 if ((q = ACE_OS::strchr (this->url_, '\r'))
72 || (q = ACE_OS::strchr (this->url_, '\n')))
73 *q = '\0';
75 this->parse_scheme (p);
76 if (*p == '\0')
78 this->error_ = URL_ERROR_SCHEME;
79 return;
82 // Parse past "//"
83 if (*p != '/' || *(p+1) != '/')
85 this->error_ = URL_ERROR_SLASHSLASH;
86 return;
88 p += 2;
90 this->parse_host (p);
92 while (*p == '/')
93 p++;
95 if (*p == '\0')
96 return;
98 this->url_path_ = p;
99 this->is_cgi (this->url_path_);
102 void
103 HTTP_Parse_URL::parse_scheme (char *&p)
105 // Parse the scheme. The scheme is delimited by a ':'.
106 if (*p != '\0')
108 this->scheme_ = p++;
109 for (;;)
111 switch (*p)
113 case '\0':
114 break;
115 case ':':
116 *p++ = '\0';
117 break;
118 default:
119 p++;
120 continue;
122 break;
127 void
128 HTTP_Parse_URL::parse_host (char *&p)
130 // Parse user, password, host, port
131 if (*p == '/' || *p == '\0')
133 this->set_port_from_scheme ();
134 return;
137 char *at = 0;
138 char *colon1 = 0;
139 char *colon2 = 0;
140 char *q = p;
141 while (*q != '\0')
143 if (*q == '/')
145 *q = '\0';
146 q++;
147 break;
149 if (*q == ':')
151 if (colon1 == 0)
153 if (at != 0 && colon2 == 0)
154 colon2 = q;
155 else
156 colon1 = q;
158 else
160 if (at != 0 && colon2 == 0)
161 colon2 = q;
164 if (*q == '@')
166 if (at == 0)
167 at = q;
169 q++;
172 // no user, no port
173 if (at == 0 && colon1 == 0)
175 if (*p != '\0' && *p != '/')
176 this->host_ = p;
179 // no user, port
180 else if (at == 0 && colon1 != 0)
182 if (p != colon1)
183 this->host_ = p;
184 *colon1++ = '\0';
185 this->port_ = ACE_OS::atoi (colon1);
188 // user, no passwd, no port
189 else if (at != 0 && colon1 == 0 && colon2 == 0)
191 this->user_ = p;
192 *at++ = '\0';
193 if (*at != '\0' && *at != '/')
194 this->host_ = at;
197 // user, no passwd, port
198 else if (at != 0 && colon1 == 0 && colon2 != 0)
200 this->user_ = p;
201 *at++ = '\0';
202 if (at != colon2)
203 this->host_ = at;
204 *colon2++ = '\0';
205 this->port_ = ACE_OS::atoi (colon2);
208 // user, passwd, no port
209 else if (at != 0 && colon1 != 0 && colon2 == 0)
211 this->user_ = p;
212 *colon1++ = '\0';
213 this->passwd_ = colon1;
214 *at++ = '\0';
215 if (*at != '\0')
216 this->host_ = at;
219 // user, passwd, and port
220 else if (at != 0 && colon1 != 0 && colon2 != 0)
222 this->user_ = p;
223 *colon1++ = '\0';
224 this->passwd_ = colon1;
225 *at++ = '\0';
226 if (at != colon2)
227 this->host_ = at;
228 *colon2++ = '\0';
229 this->port_ = ACE_OS::atoi (colon2);
232 // impossible!
233 else
235 ACE_ERROR ((LM_ERROR, "uh oh!\n"));
236 p = q;
237 return;
240 this->set_port_from_scheme ();
241 p = q;
244 void
245 HTTP_Parse_URL::set_port_from_scheme ()
247 if (ACE_OS::strcmp (this->scheme_, "ftp") == 0)
249 if (this->port_ == -1)
250 this->port_ = 21;
251 if (this->user_ == 0)
253 this->user_ = "anonymous";
255 // *** need something better here
256 this->passwd_ = "a@b.c";
259 else if (ACE_OS::strcmp (this->scheme_, "http") == 0)
261 if (this->port_ == -1)
262 this->port_ = 80;
266 const char *
267 HTTP_Parse_URL::scheme () const
269 return this->scheme_;
272 const char *
273 HTTP_Parse_URL::user () const
275 return this->user_;
278 const char *
279 HTTP_Parse_URL::passwd () const
281 return this->passwd_;
284 const char *
285 HTTP_Parse_URL::host () const
287 return this->host_;
291 HTTP_Parse_URL::port () const
293 return this->port_;
296 const char *
297 HTTP_Parse_URL::url_path () const
299 return this->url_path_ ? this->url_path_ : "";
302 void
303 HTTP_Parse_URL::is_cgi (const char *path)
305 int yes;
307 yes = (ACE_OS::strchr (path, '?') != 0);
308 if (!yes && (ACE_OS::strlen (path) >= 3))
309 yes = (ACE_OS::strstr (path, "cgi") != 0);
310 if (!yes)
311 yes = (ACE_OS::strstr (path, "asp") != 0);
313 this->is_cgi_ = yes;
317 HTTP_Parse_URL::is_cgi () const
319 return this->is_cgi_;