4 * HTTP ancestor class and common classes.
6 * Portable Windows Library
8 * Copyright (c) 1993-2002 Equivalence Pty. Ltd.
10 * The contents of this file are subject to the Mozilla Public License
11 * Version 1.0 (the "License"); you may not use this file except in
12 * compliance with the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/
15 * Software distributed under the License is distributed on an "AS IS"
16 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17 * the License for the specific language governing rights and limitations
20 * The Original Code is Portable Windows Library.
22 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
24 * Contributor(s): ______________________________________.
27 * Revision 1.104 2004/06/03 13:30:58 csoutheren
28 * Renamed INSTANTIATE_FACTORY to avoid potential namespace collisions
29 * Added documentaton on new PINSTANTIATE_FACTORY macro
30 * Added generic form of PINSTANTIATE_FACTORY
32 * Revision 1.103 2004/06/03 12:47:58 csoutheren
33 * Decomposed PFactory declarations to hopefully avoid problems with Windows DLLs
35 * Revision 1.102 2004/06/01 07:28:45 csoutheren
36 * Changed URL parsing to use abstract factory code
38 * Revision 1.101 2004/04/04 00:21:47 csoutheren
39 * FIxed problem with some URL parsing
41 * Revision 1.100 2004/04/03 08:22:20 csoutheren
42 * Remove pseudo-RTTI and replaced with real RTTI
44 * Revision 1.99 2004/03/23 05:08:21 csoutheren
45 * Fixed problem with use of ShellExecuteEx function
47 * Revision 1.98 2004/03/13 06:30:52 rjongbloed
48 * Virtualised parse function.
50 * Revision 1.97 2004/02/24 11:14:44 rjongbloed
51 * Fixed correct initialisation of some internal variables in URL if parse fails.
53 * Revision 1.96 2004/01/17 17:44:17 csoutheren
54 * Changed to use PString::MakeEmpty
56 * Revision 1.95 2003/11/18 09:22:17 csoutheren
57 * Fixed problems with PURL::OpenBrowser, thanks to David Parr
59 * Revision 1.94 2003/08/27 03:37:45 dereksmithies
60 * Fix initialization of pathStr so it really is empty. BIG thanks to Diego Tartara.
62 * Revision 1.93 2003/07/22 03:26:10 csoutheren
63 * Fixed problem with parsing default H323 addresses
65 * Revision 1.92 2003/06/23 15:31:40 ykiryanov
66 * Slightly changed call to ShellExecuteEx to make compatible with Win32
68 * Revision 1.91 2003/06/23 14:31:33 ykiryanov
69 * Modified for WinCE - used ShellExecuteEx instead of ShellExecute
71 * Revision 1.90 2003/06/05 00:15:54 rjongbloed
72 * Fixed callto bug created by previous patch.
74 * Revision 1.89 2003/06/04 01:42:05 rjongbloed
75 * Fixed h323 scheme, does not have a "password" field.
77 * Revision 1.88 2003/06/02 02:46:45 rjongbloed
78 * Fixed issue with callto URL parsing incorrect username field.
79 * Added automatic removal of illegal (though common) "//" in callto URL.
81 * Revision 1.87 2003/05/05 07:30:17 craigs
82 * Fixed problem with URLs that do not specify schemes
84 * Revision 1.86 2003/05/02 13:50:23 craigs
85 * Fixed a problem with callto:localhost
87 * Revision 1.85 2003/05/02 13:20:33 craigs
88 * Fixed callto problems
90 * Revision 1.84 2003/04/28 04:41:22 robertj
91 * Changed URL parsing so if a default scheme is present then explicit scheme
92 * must be "known" to avoid ambiguity with host:port parsing.
94 * Revision 1.83 2003/04/10 00:13:56 robertj
95 * Fixed correct decoding of user/password/host/port field, for non h323 schemes.
97 * Revision 1.82 2003/04/08 06:28:14 craigs
98 * Fixed introduced problem with HTTP server mistaking relative URLs for proxy requests
100 * Revision 1.81 2003/04/04 08:03:55 robertj
101 * Fixed special case of h323 URL default port changing depending on
102 * if it the host is an endpoint or gatekeeper.
104 * Revision 1.80 2003/04/04 05:18:08 robertj
105 * Added "callto", "tel" and fixed "h323" URL types.
107 * Revision 1.79 2002/12/02 00:17:03 robertj
108 * Fixed URL parsing/display problems with non-path URL type eg mailto
110 * Revision 1.78 2002/11/22 06:16:49 robertj
111 * Fixed usage of URI (relative http/https URL).
113 * Revision 1.77 2002/11/20 02:10:56 robertj
114 * Fixed some more realtive/absolute path issues.
116 * Revision 1.76 2002/11/20 01:01:49 robertj
117 * Fixed GNU compatibility
119 * Revision 1.75 2002/11/20 00:49:37 robertj
120 * Fixed correct interpretation of url re double slashes as per latest RFC,
121 * including file: mapping and relative paths. Probably still more to do.
123 * Revision 1.74 2002/11/19 22:45:03 robertj
124 * Fixed support for file: scheme under unix
126 * Revision 1.73 2002/11/19 10:36:50 robertj
127 * Added functions to set anf get "file:" URL. as PFilePath and do the right
128 * things with platform dependent directory components.
130 * Revision 1.72 2002/11/06 22:47:25 robertj
131 * Fixed header comment (copyright etc)
133 * Revision 1.71 2002/09/23 07:17:24 robertj
134 * Changes to allow winsock2 to be included.
136 * Revision 1.70 2002/08/28 08:06:11 craigs
137 * Fixed problem (again) with file:// URLs
139 * Revision 1.69 2002/08/28 05:11:23 craigs
140 * Fixed problem with file:// URLs
142 * Revision 1.68 2002/05/02 05:11:29 craigs
143 * Fixed problem with not translating + chars in URL query parameters
145 * Revision 1.67 2002/03/19 23:39:57 robertj
146 * Fixed string output to include PathOnly variant, lost in previous mod.
148 * Revision 1.66 2002/03/19 23:24:08 robertj
149 * Fixed problems with backward compatibility on parameters processing.
151 * Revision 1.65 2002/03/18 05:02:27 robertj
152 * Added functions to set component parts of URL.
153 * Fixed output of parameters when more than one ';' involved.
155 * Revision 1.64 2001/11/09 05:46:14 robertj
156 * Removed double slash on sip URL.
157 * Fixed extra : if have username but no password.
160 * Revision 1.63 2001/11/08 00:32:49 robertj
161 * Added parsing of ';' based parameter fields into string dictionary if there are multiple parameters, with '=' values.
163 * Revision 1.62 2001/10/31 01:33:07 robertj
164 * Added extra const for constant HTTP tag name strings.
166 * Revision 1.61 2001/10/03 00:26:34 robertj
167 * Upgraded client to HTTP/1.1 and for chunked mode entity bodies.
169 * Revision 1.60 2001/09/28 00:45:42 robertj
170 * Broke out internal static function for unstranslating URL strings.
172 * Revision 1.59 2001/07/16 00:43:06 craigs
173 * Added ability to parse other transport URLs
175 * Revision 1.58 2000/05/02 08:29:07 craigs
176 * Removed "memory leaks" caused by brain-dead GNU linker
178 * Revision 1.57 1999/05/11 12:24:18 robertj
179 * Fixed URL parser so leading blanks are ignored.
181 * Revision 1.56 1999/05/04 15:26:01 robertj
182 * Improved HTTP/1.1 compatibility (pass through user commands).
183 * Fixed problems with quicktime installer.
185 * Revision 1.55 1999/04/21 01:56:13 robertj
186 * Fixed problem with escape codes greater that %80
188 * Revision 1.54 1999/01/16 12:45:54 robertj
189 * Added RTSP schemes to URL's
191 * Revision 1.53 1998/11/30 05:38:15 robertj
192 * Moved PURL::Open() code to .cxx file to avoid linking unused code.
194 * Revision 1.52 1998/11/30 04:51:53 robertj
195 * New directory structure
197 * Revision 1.51 1998/09/23 06:22:07 robertj
198 * Added open source copyright license.
200 * Revision 1.50 1998/02/03 10:02:34 robertj
201 * Added ability to get scheme, host and port from URL as a string.
203 * Revision 1.49 1998/02/03 06:27:26 robertj
204 * Fixed URL encoding to be closer to RFC
206 * Revision 1.48 1998/01/26 02:49:16 robertj
209 * Revision 1.47 1997/11/10 12:40:20 robertj
210 * Fixed illegal character set for URL's.
212 * Revision 1.46 1997/07/14 11:47:10 robertj
213 * Added "const" to numerous variables.
215 * Revision 1.45 1997/07/12 09:45:01 robertj
216 * Fixed bug when URL has + sign in somthing other than parameters.
218 * Revision 1.44 1997/06/06 08:54:47 robertj
219 * Allowed username/password on http scheme URL.
221 * Revision 1.43 1997/04/06 07:46:09 robertj
222 * Fixed bug where URL has more than special character ('?', '#' etc).
224 * Revision 1.42 1997/03/28 04:40:24 robertj
225 * Added tags for cookies.
227 * Revision 1.41 1997/03/18 22:03:44 robertj
228 * Fixed bug that incorrectly parses URL with double slashes.
230 * Revision 1.40 1997/02/14 13:55:44 robertj
231 * Fixed bug in URL for reproducing fields with special characters, must be escaped and weren't.
233 * Revision 1.39 1997/01/12 04:15:21 robertj
234 * Globalised MIME tag strings.
236 * Revision 1.38 1996/09/14 13:09:28 robertj
238 * rearranged sockets to help support IPX.
239 * added indirect channel class and moved all protocols to descend from it,
240 * separating the protocol from the low level byte transport.
242 * Revision 1.37 1996/08/25 09:37:41 robertj
243 * Added function to detect "local" host name.
244 * Fixed printing of trailing '/' in empty URL, is distinction between with and without.
246 * Revision 1.36 1996/08/22 13:22:26 robertj
247 * Fixed bug in print of URLs, extra @ signs.
249 * Revision 1.35 1996/08/19 13:42:40 robertj
250 * Fixed errors in URL parsing and display.
251 * Fixed "Forbidden" problem out of HTTP authorisation system.
252 * Fixed authorisation so if have no user/password on basic authentication, does not require it.
254 * Revision 1.34 1996/07/27 04:13:47 robertj
255 * Fixed use of HTTP proxy on non-persistent connections.
257 * Revision 1.33 1996/07/15 10:37:20 robertj
258 * Improved proxy "self" detection (especially localhost).
260 * Revision 1.32 1996/06/28 13:20:24 robertj
261 * Modified HTTPAuthority so gets PHTTPReqest (mainly for URL) passed in.
262 * Moved HTTP form resource to another compilation module.
263 * Fixed memory leak in POST command.
265 * Revision 1.31 1996/06/10 10:00:00 robertj
266 * Added global function for query parameters parsing.
268 * Revision 1.30 1996/06/07 13:52:23 robertj
269 * Added PUT to HTTP proxy FTP. Necessitating redisign of entity body processing.
271 * Revision 1.29 1996/06/05 12:33:04 robertj
272 * Fixed bug in parsing URL with no path, is NOT absolute!
274 * Revision 1.28 1996/05/30 10:07:26 robertj
275 * Fixed bug in version number checking of return code compatibility.
277 * Revision 1.27 1996/05/26 03:46:42 robertj
278 * Compatibility to GNU 2.7.x
280 * Revision 1.26 1996/05/23 10:02:13 robertj
281 * Added common function for GET and HEAD commands.
282 * Fixed status codes to be the actual status code instead of sequential enum.
283 * This fixed some problems with proxy pass through of status codes.
284 * Fixed bug in URL parsing of username and passwords.
286 * Revision 1.19.1.1 1996/04/17 11:08:22 craigs
287 * New version by craig pending confirmation by robert
289 * Revision 1.19 1996/04/05 01:46:30 robertj
290 * Assured PSocket::Write always writes the number of bytes specified, no longer need write loops.
291 * Added workaraound for NT Netscape Navigator bug with persistent connections.
293 * Revision 1.18 1996/03/31 09:05:07 robertj
296 * Revision 1.17 1996/03/17 05:48:07 robertj
297 * Fixed host name print out of URLs.
298 * Added hit count to PHTTPResource.
300 * Revision 1.16 1996/03/16 05:00:26 robertj
301 * Added ParseReponse() for splitting reponse line into code and info.
302 * Added client side support for HTTP socket.
303 * Added hooks for proxy support in HTTP socket.
304 * Added translation type to TranslateString() to accommodate query variables.
305 * Defaulted scheme field in URL to "http".
306 * Inhibited output of port field on string conversion of URL according to scheme.
308 * Revision 1.15 1996/03/11 10:29:50 robertj
309 * Fixed bug in help image HTML.
311 * Revision 1.14 1996/03/10 13:15:24 robertj
312 * Redesign to make resources thread safe.
314 * Revision 1.13 1996/03/02 03:27:37 robertj
315 * Added function to translate a string to a form suitable for inclusion in a URL.
316 * Added radio button and selection boxes to HTTP form resource.
317 * Fixed bug in URL parsing, losing first / if hostname specified.
319 * Revision 1.12 1996/02/25 11:14:24 robertj
320 * Radio button support for forms.
322 * Revision 1.11 1996/02/25 03:10:34 robertj
323 * Removed pass through HTTP resource.
324 * Fixed PHTTPConfig resource to use correct name for config key.
326 * Revision 1.10 1996/02/19 13:48:28 robertj
327 * Put multiple uses of literal strings into const variables.
328 * Fixed URL parsing so that the unmangling of strings occurs correctly.
329 * Moved nested classes from PHTTPForm.
330 * Added overwrite option to AddResource().
331 * Added get/set string to PHTTPString resource.
333 * Revision 1.9 1996/02/13 13:09:17 robertj
334 * Added extra parameters to callback function in PHTTPResources, required
335 * by descendants to make informed decisions on data being loaded.
337 * Revision 1.8 1996/02/08 12:26:29 robertj
338 * Redesign of resource callback mechanism.
339 * Added new resource types for HTML data entry forms.
341 * Revision 1.7 1996/02/03 11:33:19 robertj
342 * Changed RadCmd() so can distinguish between I/O error and unknown command.
344 * Revision 1.6 1996/02/03 11:11:49 robertj
345 * Numerous bug fixes.
346 * Added expiry date and ismodifiedsince support.
348 * Revision 1.5 1996/01/30 23:32:40 robertj
351 * Revision 1.4 1996/01/28 14:19:09 robertj
352 * Split HTML into separate source file.
353 * Beginning of pass through resource type.
354 * Changed PCharArray in OnLoadData to PString for convenience in mangling data.
355 * Made PHTTPSpace return standard page on selection of partial path.
357 * Revision 1.3 1996/01/28 02:49:16 robertj
358 * Further implementation.
360 * Revision 1.2 1996/01/26 02:24:30 robertj
361 * Further implemetation.
363 * Revision 1.1 1996/01/23 13:04:32 robertj
369 #pragma implementation "http.h"
370 #pragma implementation "url.h"
374 #include <ptlib/sockets.h>
375 #include <ptclib/http.h>
376 #include <ptclib/url.h>
381 #include <shellapi.h>
386 // http://host:port/path...
387 // https://host:port/path....
388 // gopher://host:port
391 // prospero://host:port
392 // ftp://user:password@host:port/path...
393 // telnet://user:password@host:port
394 // file://hostname/path...
396 // mailto:user@hostname
399 #define DEFAULT_FTP_PORT 21
400 #define DEFAULT_TELNET_PORT 23
401 #define DEFAULT_GOPHER_PORT 70
402 #define DEFAULT_HTTP_PORT 80
403 #define DEFAULT_NNTP_PORT 119
404 #define DEFAULT_WAIS_PORT 210
405 #define DEFAULT_HTTPS_PORT 443
406 #define DEFAULT_RTSP_PORT 554
407 #define DEFAULT_RTSPU_PORT 554
408 #define DEFAULT_PROSPERO_PORT 1525
409 #define DEFAULT_H323_PORT 1720
410 #define DEFAULT_H323RAS_PORT 1719
411 #define DEFAULT_SIP_PORT 5060
414 struct schemeStruct {
419 BOOL defaultToUserIfNoAt;
420 BOOL defaultHostToLocal;
425 BOOL relativeImpliesScheme;
429 #define DEFAULT_SCHEME 0
430 #define FILE_SCHEME 1
432 static schemeStruct const SchemeTable[] = {
433 // scheme user pass host @def defhost query params frags path rel port
434 { "http", TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, DEFAULT_HTTP_PORT }, // Must be first
435 { "file", FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, 0 }, // Must be second
436 { "https", FALSE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, DEFAULT_HTTPS_PORT },
437 { "gopher", FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, DEFAULT_GOPHER_PORT },
438 { "wais", FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, DEFAULT_WAIS_PORT },
439 { "nntp", FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, DEFAULT_NNTP_PORT },
440 { "prospero", FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, DEFAULT_PROSPERO_PORT },
441 { "rtsp", FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, DEFAULT_RTSP_PORT },
442 { "rtspu", FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, DEFAULT_RTSPU_PORT },
443 { "ftp", TRUE, TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, DEFAULT_FTP_PORT },
444 { "telnet", TRUE, TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, DEFAULT_TELNET_PORT },
445 { "mailto", FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, 0 },
446 { "news", FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, 0 },
447 { "h323", TRUE, FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, DEFAULT_H323_PORT },
448 { "sip", TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, DEFAULT_SIP_PORT },
449 { "tel", FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, 0 },
450 { "fax", FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, 0 },
451 { "callto", FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, 0 },
452 { NULL, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 0 }
455 static const schemeStruct * GetSchemeInfo(const PCaselessString & scheme)
458 for (i = 0; SchemeTable[i].name != NULL; i++) {
459 if (scheme == SchemeTable[i].name)
460 return &SchemeTable[i];
467 PINSTANTIATE_FACTORY(PURLScheme
)
469 class PURLLegacyScheme
: public PURLScheme
472 PURLLegacyScheme(const char * _scheme
)
473 : scheme(_scheme
) { }
475 BOOL
Parse(const PString
& url
, PURL
& purl
) const
476 { return purl
.LegacyParse(url
, this); }
478 PString
AsString(PURL::UrlFormat fmt
, const PURL
& purl
) const
479 { return purl
.LegacyAsString(fmt
, this); }
481 PString
GetName() const
488 BOOL defaultToUserIfNoAt
;
489 BOOL defaultHostToLocal
;
494 BOOL relativeImpliesScheme
;
499 #define DEFINE_LEGACY_URL_SCHEME(schemeName, user, pass, host, def, defhost, query, params, frags, path, rel, port) \
500 class PURLLegacyScheme_##schemeName : public PURLLegacyScheme \
503 PURLLegacyScheme_##schemeName() \
504 : PURLLegacyScheme(#schemeName ) \
506 hasUsername = user; \
507 hasPassword = pass; \
508 hasHostPort = host; \
509 defaultToUserIfNoAt = def; \
510 defaultHostToLocal = defhost; \
512 hasParameters = params; \
513 hasFragments = frags; \
515 relativeImpliesScheme = rel; \
516 defaultPort = port; \
519 static PAbstractSingletonFactory<PURLScheme, PURLLegacyScheme_##schemeName> schemeName##Factory(#schemeName ); \
521 DEFINE_LEGACY_URL_SCHEME(http, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, DEFAULT_HTTP_PORT )
522 DEFINE_LEGACY_URL_SCHEME(file
, FALSE
, FALSE
, TRUE
, FALSE
, TRUE
, FALSE
, FALSE
, FALSE
, TRUE
, FALSE
, 0)
523 DEFINE_LEGACY_URL_SCHEME(https
, FALSE
, FALSE
, TRUE
, FALSE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, DEFAULT_HTTPS_PORT
)
524 DEFINE_LEGACY_URL_SCHEME(gopher
, FALSE
, FALSE
, TRUE
, FALSE
, TRUE
, FALSE
, FALSE
, FALSE
, TRUE
, FALSE
, DEFAULT_GOPHER_PORT
)
525 DEFINE_LEGACY_URL_SCHEME(wais
, FALSE
, FALSE
, TRUE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, TRUE
, FALSE
, DEFAULT_WAIS_PORT
)
526 DEFINE_LEGACY_URL_SCHEME(nntp
, FALSE
, FALSE
, TRUE
, FALSE
, TRUE
, FALSE
, FALSE
, FALSE
, TRUE
, FALSE
, DEFAULT_NNTP_PORT
)
527 DEFINE_LEGACY_URL_SCHEME(prospero
, FALSE
, FALSE
, TRUE
, FALSE
, TRUE
, FALSE
, FALSE
, FALSE
, TRUE
, FALSE
, DEFAULT_PROSPERO_PORT
)
528 DEFINE_LEGACY_URL_SCHEME(rtsp
, FALSE
, FALSE
, TRUE
, FALSE
, TRUE
, FALSE
, FALSE
, FALSE
, TRUE
, FALSE
, DEFAULT_RTSP_PORT
)
529 DEFINE_LEGACY_URL_SCHEME(rtspu
, FALSE
, FALSE
, TRUE
, FALSE
, TRUE
, FALSE
, FALSE
, FALSE
, TRUE
, FALSE
, DEFAULT_RTSPU_PORT
)
530 DEFINE_LEGACY_URL_SCHEME(ftp
, TRUE
, TRUE
, TRUE
, FALSE
, TRUE
, FALSE
, FALSE
, FALSE
, TRUE
, FALSE
, DEFAULT_FTP_PORT
)
531 DEFINE_LEGACY_URL_SCHEME(telnet
, TRUE
, TRUE
, TRUE
, FALSE
, TRUE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, DEFAULT_TELNET_PORT
)
532 DEFINE_LEGACY_URL_SCHEME(mailto
, FALSE
, FALSE
, FALSE
, FALSE
, TRUE
, TRUE
, FALSE
, FALSE
, FALSE
, FALSE
, 0)
533 DEFINE_LEGACY_URL_SCHEME(news
, FALSE
, FALSE
, FALSE
, FALSE
, TRUE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, 0)
534 DEFINE_LEGACY_URL_SCHEME(h323
, TRUE
, FALSE
, TRUE
, TRUE
, FALSE
, FALSE
, TRUE
, FALSE
, FALSE
, FALSE
, DEFAULT_H323_PORT
)
535 DEFINE_LEGACY_URL_SCHEME(sip
, TRUE
, TRUE
, TRUE
, FALSE
, FALSE
, FALSE
, TRUE
, FALSE
, FALSE
, FALSE
, DEFAULT_SIP_PORT
)
536 DEFINE_LEGACY_URL_SCHEME(tel
, FALSE
, FALSE
, FALSE
, TRUE
, FALSE
, FALSE
, TRUE
, FALSE
, FALSE
, FALSE
, 0)
537 DEFINE_LEGACY_URL_SCHEME(fax
, FALSE
, FALSE
, FALSE
, TRUE
, FALSE
, FALSE
, TRUE
, FALSE
, FALSE
, FALSE
, 0)
538 DEFINE_LEGACY_URL_SCHEME(callto
, FALSE
, FALSE
, FALSE
, TRUE
, FALSE
, FALSE
, TRUE
, FALSE
, FALSE
, FALSE
, 0)
540 #define DEFAULT_SCHEME "http"
541 #define FILE_SCHEME "file"
543 //////////////////////////////////////////////////////////////////////////////
547 : //scheme(SchemeTable[DEFAULT_SCHEME].name),
548 scheme(DEFAULT_SCHEME
),
555 PURL::PURL(const char * str
, const char * defaultScheme
)
557 Parse(str
, defaultScheme
);
561 PURL::PURL(const PString
& str
, const char * defaultScheme
)
563 Parse(str
, defaultScheme
);
567 PURL::PURL(const PFilePath
& filePath
)
568 : //scheme(SchemeTable[FILE_SCHEME].name),
573 PStringArray pathArray
= filePath
.GetDirectory().GetPath();
574 hostname
= pathArray
[0];
577 for (i
= 1; i
< pathArray
.GetSize(); i
++)
578 pathArray
[i
-1] = pathArray
[i
];
579 pathArray
[i
-1] = filePath
.GetFileName();
585 PObject::Comparison
PURL::Compare(const PObject
& obj
) const
587 PAssert(PIsDescendant(&obj
, PURL
), PInvalidCast
);
588 return urlString
.Compare(((const PURL
&)obj
).urlString
);
592 PINDEX
PURL::HashFunction() const
594 return urlString
.HashFunction();
598 void PURL::PrintOn(ostream
& stream
) const
604 void PURL::ReadFrom(istream
& stream
)
612 PString
PURL::TranslateString(const PString
& str
, TranslationType type
)
616 PString safeChars
= "abcdefghijklmnopqrstuvwxyz"
617 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
618 "0123456789$-_.!*'(),";
620 case LoginTranslation
:
621 safeChars
+= "+;?&=";
624 case PathTranslation
:
625 safeChars
+= "+:@&=";
628 case QueryTranslation
:
631 PINDEX pos
= (PINDEX
)-1;
632 while ((pos
+= 1+strspn(&xlat
[pos
+1], safeChars
)) < xlat
.GetLength())
633 xlat
.Splice(psprintf("%%%02X", (BYTE
)xlat
[pos
]), pos
, 1);
635 if (type
== QueryTranslation
) {
636 PINDEX space
= (PINDEX
)-1;
637 while ((space
= xlat
.Find(' ', space
+1)) != P_MAX_INDEX
)
645 PString
PURL::UntranslateString(const PString
& str
, TranslationType type
)
651 if (type
== PURL::QueryTranslation
) {
653 while ((pos
= xlat
.Find('+', pos
+1)) != P_MAX_INDEX
)
658 while ((pos
= xlat
.Find('%', pos
+1)) != P_MAX_INDEX
) {
659 int digit1
= xlat
[pos
+1];
660 int digit2
= xlat
[pos
+2];
661 if (isxdigit(digit1
) && isxdigit(digit2
)) {
663 (isdigit(digit2
) ? (digit2
-'0') : (toupper(digit2
)-'A'+10)) +
664 ((isdigit(digit1
) ? (digit1
-'0') : (toupper(digit1
)-'A'+10)) << 4));
665 xlat
.Delete(pos
+1, 2);
673 static void SplitVars(const PString
& str
, PStringToString
& vars
, char sep1
, char sep2
)
677 PINDEX sep1next
= str
.Find(sep1
, sep1prev
);
678 if (sep1next
== P_MAX_INDEX
)
679 sep1next
--; // Implicit assumption string is not a couple of gigabytes long ...
681 PINDEX sep2pos
= str
.Find(sep2
, sep1prev
);
682 if (sep2pos
> sep1next
)
685 PCaselessString key
= PURL::UntranslateString(str(sep1prev
, sep2pos
-1), PURL::QueryTranslation
);
687 PString data
= PURL::UntranslateString(str(sep2pos
+1, sep1next
-1), PURL::QueryTranslation
);
689 if (vars
.Contains(key
))
690 vars
.SetAt(key
, vars
[key
] + ',' + data
);
692 vars
.SetAt(key
, data
);
695 sep1prev
= sep1next
+1;
696 } while (sep1prev
!= P_MAX_INDEX
);
700 void PURL::SplitQueryVars(const PString
& queryStr
, PStringToString
& queryVars
)
702 SplitVars(queryStr
, queryVars
, '&', '=');
706 BOOL
PURL::InternalParse(const char * cstr
, const char * defaultScheme
)
711 username
.MakeEmpty();
712 password
.MakeEmpty();
713 hostname
.MakeEmpty();
715 relativePath
= FALSE
;
718 paramVars
.RemoveAll();
719 fragment
.MakeEmpty();
720 queryVars
.RemoveAll();
722 // copy the string so we can take bits off it
723 while (isspace(*cstr
))
727 // Character set as per RFC2396
729 while (isalnum(url
[pos
]) || url
[pos
] == '+' || url
[pos
] == '-' || url
[pos
] == '.')
734 // get information which tells us how to parse URL for this
736 PURLScheme
* schemeInfo
= NULL
;
738 // Determine if the URL has an explicit scheme
739 if (url
[pos
] == ':') {
741 // get the scheme information, or get the default scheme
742 schemeInfo
= PGenericFactory
<PURLScheme
>::CreateInstance(url
.Left(pos
));
743 if (schemeInfo
== NULL
&& defaultScheme
== NULL
) {
744 PGenericFactory
<PURLScheme
>::KeyList_T keyList
= PGenericFactory
<PURLScheme
>::GetKeyList();
745 if (keyList
.size() != 0)
746 schemeInfo
= PGenericFactory
<PURLScheme
>::CreateInstance(keyList
[0]);
748 if (schemeInfo
!= NULL
)
749 url
.Delete(0, pos
+1);
752 // if we could not match a scheme, then use the specified default scheme
753 if (schemeInfo
== NULL
&& defaultScheme
!= NULL
)
754 schemeInfo
= PGenericFactory
<PURLScheme
>::CreateInstance(defaultScheme
);
756 // if that still fails, then use the global default scheme
757 if (schemeInfo
== NULL
)
758 schemeInfo
= PGenericFactory
<PURLScheme
>::CreateInstance(DEFAULT_SCHEME
);
760 //scheme = schemeInfo->name;
761 scheme
= schemeInfo
->GetName();
763 if (!schemeInfo
->Parse(url
, *this))
769 BOOL
PURL::LegacyParse(const PString
& _url
, const PURLLegacyScheme
* schemeInfo
)
774 // Super special case!
775 if (scheme
*= "callto") {
777 // Actually not part of MS spec, but a lot of people put in the // into
778 // the URL, so we take it out of it is there.
779 if (url
.GetLength() > 2 && url
[0] == '/' && url
[1] == '/')
782 // For some bizarre reason callto uses + instead of ; for paramters
783 // We do a loop so that phone numbers of the form +61243654666 still work
786 } while (pos
!= P_MAX_INDEX
&& isdigit(url
[pos
+1]));
788 if (pos
!= P_MAX_INDEX
) {
789 SplitVars(url(pos
+1, P_MAX_INDEX
), paramVars
, '+', '=');
790 url
.Delete(pos
, P_MAX_INDEX
);
793 hostname
= paramVars("gateway");
795 username
= UntranslateString(url
, LoginTranslation
);
797 PCaselessString type
= paramVars("type");
798 if (type
== "directory") {
800 if (pos
== P_MAX_INDEX
)
801 username
= UntranslateString(url
, LoginTranslation
);
803 hostname
= UntranslateString(url
.Left(pos
), LoginTranslation
);
804 username
= UntranslateString(url
.Mid(pos
+1), LoginTranslation
);
808 // Now look for an @ and split user and host
810 if (pos
!= P_MAX_INDEX
) {
811 username
= UntranslateString(url
.Left(pos
), LoginTranslation
);
812 hostname
= UntranslateString(url
.Mid(pos
+1), LoginTranslation
);
815 if (type
== "ip" || type
== "host")
816 hostname
= UntranslateString(url
, LoginTranslation
);
818 username
= UntranslateString(url
, LoginTranslation
);
823 // Allow for [ipv6] form
824 pos
= hostname
.Find(']');
825 if (pos
== P_MAX_INDEX
)
827 pos
= hostname
.Find(':', pos
);
828 if (pos
!= P_MAX_INDEX
) {
829 port
= (WORD
)hostname
.Mid(pos
+1).AsUnsigned();
830 hostname
.Delete(pos
, P_MAX_INDEX
);
833 password
= paramVars("password");
837 // if the URL should have leading slash, then remove it if it has one
838 if (schemeInfo
!= NULL
&& schemeInfo
->hasHostPort
&& schemeInfo
->hasPath
) {
839 if (url
.GetLength() > 2 && url
[0] == '/' && url
[1] == '/')
845 // parse user/password/host/port
846 if (!relativePath
&& schemeInfo
->hasHostPort
) {
847 PString endHostChars
;
848 if (schemeInfo
->hasPath
)
850 if (schemeInfo
->hasQuery
)
852 if (schemeInfo
->hasParameters
)
854 if (schemeInfo
->hasFragments
)
856 if (endHostChars
.IsEmpty())
859 pos
= url
.FindOneOf(endHostChars
);
861 PString uphp
= url
.Left(pos
);
862 if (pos
!= P_MAX_INDEX
)
867 // if the URL is of type UserPasswordHostPort, then parse it
868 if (schemeInfo
->hasUsername
) {
869 // extract username and password
870 PINDEX pos2
= uphp
.Find('@');
871 PINDEX pos3
= P_MAX_INDEX
;
872 if (schemeInfo
->hasPassword
)
873 pos3
= uphp
.Find(':');
880 if (schemeInfo
->defaultToUserIfNoAt
) {
881 if (pos3
== P_MAX_INDEX
)
882 username
= UntranslateString(uphp
, LoginTranslation
);
884 username
= UntranslateString(uphp
.Left(pos3
), LoginTranslation
);
885 password
= UntranslateString(uphp
.Mid(pos3
+1), LoginTranslation
);
893 username
= UntranslateString(uphp
.Left(pos2
), LoginTranslation
);
895 username
= UntranslateString(uphp
.Left(pos3
), LoginTranslation
);
896 password
= UntranslateString(uphp(pos3
+1, pos2
-1), LoginTranslation
);
898 uphp
.Delete(0, pos2
+1);
902 // if the URL does not have a port, then this is the hostname
903 if (schemeInfo
->defaultPort
== 0)
904 hostname
= UntranslateString(uphp
, LoginTranslation
);
906 // determine if the URL has a port number
907 // Allow for [ipv6] form
908 pos
= uphp
.Find(']');
909 if (pos
== P_MAX_INDEX
)
911 pos
= uphp
.Find(':', pos
);
912 if (pos
== P_MAX_INDEX
)
913 hostname
= UntranslateString(uphp
, LoginTranslation
);
915 hostname
= UntranslateString(uphp
.Left(pos
), LoginTranslation
);
916 port
= (WORD
)uphp
.Mid(pos
+1).AsUnsigned();
919 if (hostname
.IsEmpty() && schemeInfo
->defaultHostToLocal
)
920 hostname
= PIPSocket::GetHostName();
924 if (schemeInfo
->hasQuery
) {
925 // chop off any trailing query
927 if (pos
!= P_MAX_INDEX
) {
928 SplitQueryVars(url(pos
+1, P_MAX_INDEX
), queryVars
);
929 url
.Delete(pos
, P_MAX_INDEX
);
933 if (schemeInfo
->hasParameters
) {
934 // chop off any trailing parameters
936 if (pos
!= P_MAX_INDEX
) {
937 SplitVars(url(pos
+1, P_MAX_INDEX
), paramVars
, ';', '=');
938 url
.Delete(pos
, P_MAX_INDEX
);
942 if (schemeInfo
->hasFragments
) {
943 // chop off any trailing fragment
945 if (pos
!= P_MAX_INDEX
) {
946 fragment
= UntranslateString(url(pos
+1, P_MAX_INDEX
), PathTranslation
);
947 url
.Delete(pos
, P_MAX_INDEX
);
951 if (schemeInfo
->hasPath
)
952 SetPathStr(url
); // the hierarchy is what is left
954 // if the rest of the URL isn't a path, then we are finished!
955 pathStr
= UntranslateString(url
, PathTranslation
);
959 if (port
== 0 && schemeInfo
->defaultPort
!= 0 && !relativePath
) {
960 // Yes another horrible, horrible special case!
961 if (scheme
== "h323" && paramVars("type") == "gk")
962 port
= DEFAULT_H323RAS_PORT
;
964 port
= schemeInfo
->defaultPort
;
972 PFilePath
PURL::AsFilePath() const
974 //if (scheme != SchemeTable[FILE_SCHEME].name)
975 // return PString::Empty();
976 if (scheme
!= FILE_SCHEME
)
977 return PString::Empty();
982 for (PINDEX i
= 0; i
< path
.GetSize(); i
++) {
984 str
<< PDIR_SEPARATOR
;
989 if (hostname
!= "localhost")
991 for (PINDEX i
= 0; i
< path
.GetSize(); i
++)
992 str
<< PDIR_SEPARATOR
<< path
[i
];
999 PString
PURL::AsString(UrlFormat fmt
) const
1004 if (scheme
.IsEmpty())
1005 return PString::Empty();
1007 //const schemeStruct * schemeInfo = GetSchemeInfo(scheme);
1008 //if (schemeInfo == NULL)
1009 // schemeInfo = &SchemeTable[PARRAYSIZE(SchemeTable)-1];
1010 const PURLScheme
* schemeInfo
= PGenericFactory
<PURLScheme
>::CreateInstance(scheme
);
1011 if (schemeInfo
== NULL
)
1012 schemeInfo
= PGenericFactory
<PURLScheme
>::CreateInstance(DEFAULT_SCHEME
);
1014 return schemeInfo
->AsString(fmt
, *this);
1017 PString
PURL::LegacyAsString(PURL::UrlFormat fmt
, const PURLLegacyScheme
* schemeInfo
) const
1022 if (fmt
== HostPortOnly
) {
1023 str
<< scheme
<< ':';
1026 if (schemeInfo
->relativeImpliesScheme
)
1027 return PString::Empty();
1031 if (schemeInfo
->hasPath
&& schemeInfo
->hasHostPort
)
1034 if (schemeInfo
->hasUsername
) {
1036 str
<< TranslateString(username
, LoginTranslation
);
1037 if (schemeInfo
->hasPassword
&& !password
)
1038 str
<< ':' << TranslateString(password
, LoginTranslation
);
1043 if (schemeInfo
->hasHostPort
)
1046 if (schemeInfo
->defaultPort
!= 0) {
1047 if (port
!= schemeInfo
->defaultPort
)
1054 // URIOnly and PathOnly
1055 if (schemeInfo
->hasPath
) {
1056 for (i
= 0; i
< path
.GetSize(); i
++) {
1057 if (i
> 0 || !relativePath
)
1059 str
<< TranslateString(path
[i
], PathTranslation
);
1063 str
<< TranslateString(pathStr
, PathTranslation
);
1065 if (fmt
== URIOnly
) {
1067 str
<< "#" << TranslateString(fragment
, PathTranslation
);
1069 for (i
= 0; i
< paramVars
.GetSize(); i
++) {
1070 str
<< ';' << TranslateString(paramVars
.GetKeyAt(i
), QueryTranslation
);
1071 PString data
= paramVars
.GetDataAt(i
);
1073 str
<< '=' << TranslateString(data
, QueryTranslation
);
1076 if (!queryVars
.IsEmpty())
1077 str
<< '?' << GetQuery();
1084 void PURL::SetScheme(const PString
& s
)
1091 void PURL::SetUserName(const PString
& u
)
1098 void PURL::SetPassword(const PString
& p
)
1105 void PURL::SetHostName(const PString
& h
)
1112 void PURL::SetPort(WORD newPort
)
1119 void PURL::SetPathStr(const PString
& p
)
1123 path
= pathStr
.Tokenise("/", TRUE
);
1125 if (path
.GetSize() > 0 && path
[0].IsEmpty())
1128 for (PINDEX i
= 0; i
< path
.GetSize(); i
++) {
1129 path
[i
] = UntranslateString(path
[i
], PathTranslation
);
1130 if (i
> 0 && path
[i
] == ".." && path
[i
-1] != "..") {
1140 void PURL::SetPath(const PStringArray
& p
)
1144 pathStr
.MakeEmpty();
1145 for (PINDEX i
= 0; i
< path
.GetSize(); i
++)
1146 pathStr
+= '/' + path
[i
];
1152 PString
PURL::GetParameters() const
1156 for (PINDEX i
= 0; i
< paramVars
.GetSize(); i
++) {
1159 str
<< paramVars
.GetKeyAt(i
);
1160 PString data
= paramVars
.GetDataAt(i
);
1169 void PURL::SetParameters(const PString
& parameters
)
1171 SplitVars(parameters
, paramVars
, ';', '=');
1176 void PURL::SetParamVars(const PStringToString
& p
)
1183 void PURL::SetParamVar(const PString
& key
, const PString
& data
)
1186 paramVars
.RemoveAt(key
);
1188 paramVars
.SetAt(key
, data
);
1193 PString
PURL::GetQuery() const
1197 for (PINDEX i
= 0; i
< queryVars
.GetSize(); i
++) {
1200 str
<< TranslateString(queryVars
.GetKeyAt(i
), QueryTranslation
)
1202 << TranslateString(queryVars
.GetDataAt(i
), QueryTranslation
);
1209 void PURL::SetQuery(const PString
& queryStr
)
1211 SplitQueryVars(queryStr
, queryVars
);
1216 void PURL::SetQueryVars(const PStringToString
& q
)
1223 void PURL::SetQueryVar(const PString
& key
, const PString
& data
)
1226 queryVars
.RemoveAt(key
);
1228 queryVars
.SetAt(key
, data
);
1233 BOOL
PURL::OpenBrowser(const PString
& url
)
1236 SHELLEXECUTEINFO sei
;
1237 ZeroMemory(&sei
, sizeof(SHELLEXECUTEINFO
));
1238 sei
.cbSize
= sizeof(SHELLEXECUTEINFO
);
1239 sei
.lpVerb
= TEXT("open");
1242 if (ShellExecuteEx(&sei
) != 0)
1246 MessageBox(NULL
, "Unable to open page"&url
, PProcess::Current().GetName(), MB_TASKMODAL
);
1249 MessageBox(NULL
, _T("Unable to open page"), A2T(PProcess::Current().GetName()), MB_APPLMODAL
);
1250 #endif // _WIN32_WCE
1257 void PURL::Recalculate()
1259 //if (scheme.IsEmpty())
1260 // scheme = SchemeTable[DEFAULT_SCHEME].name;
1261 if (scheme
.IsEmpty())
1262 scheme
= DEFAULT_SCHEME
;
1264 urlString
= AsString(HostPortOnly
) + AsString(URIOnly
);
1268 //////////////////////////////////////////////////////////////////////////////
1271 static char const * const HTTPCommands
[PHTTP::NumCommands
] = {
1272 // HTTP 1.0 commands
1273 "GET", "HEAD", "POST",
1275 // HTTP 1.1 commands
1276 "PUT", "DELETE", "TRACE", "OPTIONS",
1283 const char * const PHTTP::AllowTag
= "Allow";
1284 const char * const PHTTP::AuthorizationTag
= "Authorization";
1285 const char * const PHTTP::ContentEncodingTag
= "Content-Encoding";
1286 const char * const PHTTP::ContentLengthTag
= "Content-Length";
1287 const char * const PHTTP::ContentTypeTag
= "Content-Type";
1288 const char * const PHTTP::DateTag
= "Date";
1289 const char * const PHTTP::ExpiresTag
= "Expires";
1290 const char * const PHTTP::FromTag
= "From";
1291 const char * const PHTTP::IfModifiedSinceTag
= "If-Modified-Since";
1292 const char * const PHTTP::LastModifiedTag
= "Last-Modified";
1293 const char * const PHTTP::LocationTag
= "Location";
1294 const char * const PHTTP::PragmaTag
= "Pragma";
1295 const char * const PHTTP::PragmaNoCacheTag
= "no-cache";
1296 const char * const PHTTP::RefererTag
= "Referer";
1297 const char * const PHTTP::ServerTag
= "Server";
1298 const char * const PHTTP::UserAgentTag
= "User-Agent";
1299 const char * const PHTTP::WWWAuthenticateTag
= "WWW-Authenticate";
1300 const char * const PHTTP::MIMEVersionTag
= "MIME-Version";
1301 const char * const PHTTP::ConnectionTag
= "Connection";
1302 const char * const PHTTP::KeepAliveTag
= "Keep-Alive";
1303 const char * const PHTTP::TransferEncodingTag
= "Transfer-Encoding";
1304 const char * const PHTTP::ChunkedTag
= "chunked";
1305 const char * const PHTTP::ProxyConnectionTag
= "Proxy-Connection";
1306 const char * const PHTTP::ProxyAuthorizationTag
= "Proxy-Authorization";
1307 const char * const PHTTP::ProxyAuthenticateTag
= "Proxy-Authenticate";
1308 const char * const PHTTP::ForwardedTag
= "Forwarded";
1309 const char * const PHTTP::SetCookieTag
= "Set-Cookie";
1310 const char * const PHTTP::CookieTag
= "Cookie";
1315 : PInternetProtocol("www 80", NumCommands
, HTTPCommands
)
1320 PINDEX
PHTTP::ParseResponse(const PString
& line
)
1322 PINDEX endVer
= line
.Find(' ');
1323 if (endVer
== P_MAX_INDEX
) {
1324 lastResponseInfo
= "Bad response";
1325 lastResponseCode
= PHTTP::InternalServerError
;
1329 lastResponseInfo
= line
.Left(endVer
);
1330 PINDEX endCode
= line
.Find(' ', endVer
+1);
1331 lastResponseCode
= line(endVer
+1,endCode
-1).AsInteger();
1332 if (lastResponseCode
== 0)
1333 lastResponseCode
= PHTTP::InternalServerError
;
1334 lastResponseInfo
&= line
.Mid(endCode
);
1339 // End Of File ///////////////////////////////////////////////////////////////