]> git.saurik.com Git - wxWidgets.git/blame - src/common/uri.cpp
fix ANSI mode build
[wxWidgets.git] / src / common / uri.cpp
CommitLineData
dd65d8c8
RN
1/////////////////////////////////////////////////////////////////////////////
2// Name: uri.cpp
3// Purpose: Implementation of a uri parser
4// Author: Ryan Norton
5// Created: 10/26/04
6// RCS-ID: $Id$
7// Copyright: (c) 2004 Ryan Norton
8// Licence: wxWindows
9/////////////////////////////////////////////////////////////////////////////
10
11// ===========================================================================
12// declarations
13// ===========================================================================
14
15// ---------------------------------------------------------------------------
16// headers
17// ---------------------------------------------------------------------------
18
dd65d8c8
RN
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
0bf751e7
VS
26#ifndef WX_PRECOMP
27 #include "wx/crt.h"
28#endif
29
dd65d8c8
RN
30#include "wx/uri.h"
31
32// ---------------------------------------------------------------------------
33// definitions
34// ---------------------------------------------------------------------------
35
4115960d 36IMPLEMENT_CLASS(wxURI, wxObject)
dd65d8c8
RN
37
38// ===========================================================================
39// implementation
40// ===========================================================================
41
42// ---------------------------------------------------------------------------
43// utilities
44// ---------------------------------------------------------------------------
45
46// ---------------------------------------------------------------------------
47//
48// wxURI
49//
50// ---------------------------------------------------------------------------
51
52// ---------------------------------------------------------------------------
53// Constructors
54// ---------------------------------------------------------------------------
55
56wxURI::wxURI() : m_hostType(wxURI_REGNAME), m_fields(0)
57{
58}
846978d7 59
dd65d8c8
RN
60wxURI::wxURI(const wxString& uri) : m_hostType(wxURI_REGNAME), m_fields(0)
61{
62 Create(uri);
63}
64
60431236 65wxURI::wxURI(const wxURI& uri) : wxObject(), m_hostType(wxURI_REGNAME), m_fields(0)
dd65d8c8 66{
b60b2ec8 67 Assign(uri);
dd65d8c8
RN
68}
69
70// ---------------------------------------------------------------------------
71// Destructor and cleanup
72// ---------------------------------------------------------------------------
73
74wxURI::~wxURI()
75{
76 Clear();
77}
78
79void wxURI::Clear()
80{
4860d40d 81 m_scheme = m_userinfo = m_server = m_port = m_path =
525d8583 82 m_query = m_fragment = wxEmptyString;
dd65d8c8
RN
83
84 m_hostType = wxURI_REGNAME;
85
86 m_fields = 0;
87}
88
89// ---------------------------------------------------------------------------
90// Create
91//
846978d7 92// This creates the URI - all we do here is call the main parsing method
dd65d8c8
RN
93// ---------------------------------------------------------------------------
94
86470d43 95const wxChar* wxURI::Create(const wxString& uri)
846978d7 96{
dd65d8c8
RN
97 if (m_fields)
98 Clear();
99
86501081
VS
100 // FIXME-UTF8: rewrite ParseXXX() methods using iterators
101 // NB: using wxWxCharBuffer instead of just c_str() avoids keeping
102 // converted string in memory for longer than needed
103 return Parse(wxWxCharBuffer(uri.c_str()));
846978d7 104}
dd65d8c8
RN
105
106// ---------------------------------------------------------------------------
ce321570 107// Escape Methods
dd65d8c8 108//
846978d7 109// TranslateEscape unencodes a 3 character URL escape sequence
ce321570 110//
dd65d8c8 111// Escape encodes an invalid URI character into a 3 character sequence
ce321570 112//
dd65d8c8
RN
113// IsEscape determines if the input string contains an escape sequence,
114// if it does, then it moves the input string past the escape sequence
ce321570
RN
115//
116// Unescape unencodes all 3 character URL escape sequences in a wxString
dd65d8c8
RN
117// ---------------------------------------------------------------------------
118
c9f78968 119wxUniChar wxURI::TranslateEscape(const wxString::const_iterator& s)
dd65d8c8 120{
c9f78968
VS
121 wxChar c1(*s);
122 wxChar c2(*(s + 1));
8404931e 123
c9f78968
VS
124 wxASSERT_MSG( IsHex(c1) && IsHex(c2), wxT("Invalid escape sequence!"));
125
126 return wx_truncate_cast(wxChar, (CharToHex(c1) << 4 ) | CharToHex(c2));
dd65d8c8
RN
127}
128
86470d43
RN
129wxString wxURI::Unescape(const wxString& uri)
130{
131 wxString new_uri;
132
c9f78968 133 for (wxString::const_iterator i = uri.begin(); i != uri.end(); ++i)
86470d43 134 {
c9f78968 135 if ( *i == wxT('%') )
86470d43 136 {
c9f78968 137 new_uri += wxURI::TranslateEscape(i + 1);
86470d43
RN
138 i += 2;
139 }
d8d7193d 140 else
c9f78968 141 new_uri += *i;
86470d43
RN
142 }
143
144 return new_uri;
145}
146
dd65d8c8
RN
147void wxURI::Escape(wxString& s, const wxChar& c)
148{
149 const wxChar* hdig = wxT("0123456789abcdef");
ce321570 150 s += wxT('%');
dd65d8c8 151 s += hdig[(c >> 4) & 15];
846978d7 152 s += hdig[c & 15];
dd65d8c8
RN
153}
154
155bool wxURI::IsEscape(const wxChar*& uri)
156{
ce321570
RN
157 // pct-encoded = "%" HEXDIG HEXDIG
158 if(*uri == wxT('%') && IsHex(*(uri+1)) && IsHex(*(uri+2)))
dd65d8c8 159 return true;
dd65d8c8
RN
160 else
161 return false;
162}
163
4860d40d
RN
164// ---------------------------------------------------------------------------
165// GetUser
166// GetPassword
167//
168// Gets the username and password via the old URL method.
169// ---------------------------------------------------------------------------
170wxString wxURI::GetUser() const
171{
172 size_t dwPasswordPos = m_userinfo.find(':');
173
174 if (dwPasswordPos == wxString::npos)
175 dwPasswordPos = 0;
176
177 return m_userinfo(0, dwPasswordPos);
178}
179
180wxString wxURI::GetPassword() const
181{
182 size_t dwPasswordPos = m_userinfo.find(':');
183
184 if (dwPasswordPos == wxString::npos)
185 return wxT("");
186 else
187 return m_userinfo(dwPasswordPos+1, m_userinfo.length() + 1);
188}
189
dd65d8c8 190// ---------------------------------------------------------------------------
86470d43 191// BuildURI
dd65d8c8 192//
846978d7 193// BuildURI() builds the entire URI into a useable
dd65d8c8 194// representation, including proper identification characters such as slashes
ce321570
RN
195//
196// BuildUnescapedURI() does the same thing as BuildURI(), only it unescapes
197// the components that accept escape sequences
dd65d8c8
RN
198// ---------------------------------------------------------------------------
199
86470d43 200wxString wxURI::BuildURI() const
846978d7 201{
dd65d8c8
RN
202 wxString ret;
203
204 if (HasScheme())
205 ret = ret + m_scheme + wxT(":");
206
207 if (HasServer())
208 {
209 ret += wxT("//");
210
4860d40d
RN
211 if (HasUserInfo())
212 ret = ret + m_userinfo + wxT("@");
dd65d8c8
RN
213
214 ret += m_server;
215
216 if (HasPort())
217 ret = ret + wxT(":") + m_port;
218 }
219
220 ret += m_path;
221
222 if (HasQuery())
223 ret = ret + wxT("?") + m_query;
224
225 if (HasFragment())
226 ret = ret + wxT("#") + m_fragment;
227
228 return ret;
229}
230
86470d43
RN
231wxString wxURI::BuildUnescapedURI() const
232{
233 wxString ret;
234
235 if (HasScheme())
236 ret = ret + m_scheme + wxT(":");
237
238 if (HasServer())
239 {
240 ret += wxT("//");
241
4860d40d
RN
242 if (HasUserInfo())
243 ret = ret + wxURI::Unescape(m_userinfo) + wxT("@");
86470d43
RN
244
245 if (m_hostType == wxURI_REGNAME)
246 ret += wxURI::Unescape(m_server);
247 else
248 ret += m_server;
249
250 if (HasPort())
251 ret = ret + wxT(":") + m_port;
252 }
253
254 ret += wxURI::Unescape(m_path);
255
256 if (HasQuery())
257 ret = ret + wxT("?") + wxURI::Unescape(m_query);
258
259 if (HasFragment())
260 ret = ret + wxT("#") + wxURI::Unescape(m_fragment);
261
262 return ret;
263}
264
dd65d8c8 265// ---------------------------------------------------------------------------
ce321570 266// Assignment
dd65d8c8
RN
267// ---------------------------------------------------------------------------
268
b60b2ec8
RN
269wxURI& wxURI::Assign(const wxURI& uri)
270{
271 //assign fields
272 m_fields = uri.m_fields;
273
274 //ref over components
275 m_scheme = uri.m_scheme;
4860d40d 276 m_userinfo = uri.m_userinfo;
b60b2ec8
RN
277 m_server = uri.m_server;
278 m_hostType = uri.m_hostType;
279 m_port = uri.m_port;
280 m_path = uri.m_path;
281 m_query = uri.m_query;
282 m_fragment = uri.m_fragment;
dd65d8c8
RN
283
284 return *this;
285}
286
ce321570
RN
287wxURI& wxURI::operator = (const wxURI& uri)
288{
289 return Assign(uri);
290}
291
b60b2ec8 292wxURI& wxURI::operator = (const wxString& string)
846978d7 293{
dd65d8c8
RN
294 Create(string);
295 return *this;
296}
297
ce321570
RN
298// ---------------------------------------------------------------------------
299// Comparison
300// ---------------------------------------------------------------------------
301
dd65d8c8 302bool wxURI::operator == (const wxURI& uri) const
846978d7 303{
dd65d8c8
RN
304 if (HasScheme())
305 {
306 if(m_scheme != uri.m_scheme)
307 return false;
308 }
309 else if (uri.HasScheme())
310 return false;
311
312
313 if (HasServer())
314 {
4860d40d 315 if (HasUserInfo())
dd65d8c8 316 {
4860d40d 317 if (m_userinfo != uri.m_userinfo)
dd65d8c8
RN
318 return false;
319 }
4860d40d 320 else if (uri.HasUserInfo())
dd65d8c8
RN
321 return false;
322
323 if (m_server != uri.m_server ||
324 m_hostType != uri.m_hostType)
325 return false;
326
327 if (HasPort())
328 {
329 if(m_port != uri.m_port)
330 return false;
331 }
332 else if (uri.HasPort())
333 return false;
334 }
335 else if (uri.HasServer())
336 return false;
337
338
339 if (HasPath())
340 {
341 if(m_path != uri.m_path)
342 return false;
343 }
344 else if (uri.HasPath())
345 return false;
346
347 if (HasQuery())
348 {
349 if (m_query != uri.m_query)
350 return false;
351 }
352 else if (uri.HasQuery())
353 return false;
354
355 if (HasFragment())
356 {
357 if (m_fragment != uri.m_fragment)
358 return false;
359 }
360 else if (uri.HasFragment())
361 return false;
362
363 return true;
364}
365
366// ---------------------------------------------------------------------------
367// IsReference
368//
369// if there is no authority or scheme, it is a reference
370// ---------------------------------------------------------------------------
371
372bool wxURI::IsReference() const
373{ return !HasScheme() || !HasServer(); }
374
375// ---------------------------------------------------------------------------
376// Parse
377//
378// Master URI parsing method. Just calls the individual parsing methods
379//
380// URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
4cc52142 381// URI-reference = URI / relative
dd65d8c8
RN
382// ---------------------------------------------------------------------------
383
86501081 384const wxChar* wxURI::Parse(const wxChar *uri)
dd65d8c8
RN
385{
386 uri = ParseScheme(uri);
387 uri = ParseAuthority(uri);
388 uri = ParsePath(uri);
389 uri = ParseQuery(uri);
390 return ParseFragment(uri);
391}
392
393// ---------------------------------------------------------------------------
394// ParseXXX
395//
396// Individual parsers for each URI component
397// ---------------------------------------------------------------------------
398
86501081 399const wxChar* wxURI::ParseScheme(const wxChar *uri)
dd65d8c8
RN
400{
401 wxASSERT(uri != NULL);
402
403 //copy of the uri - used for figuring out
404 //length of each component
405 const wxChar* uricopy = uri;
406
407 //Does the uri have a scheme (first character alpha)?
408 if (IsAlpha(*uri))
409 {
410 m_scheme += *uri++;
411
412 //scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
846978d7 413 while (IsAlpha(*uri) || IsDigit(*uri) ||
ce321570
RN
414 *uri == wxT('+') ||
415 *uri == wxT('-') ||
846978d7
WS
416 *uri == wxT('.'))
417 {
418 m_scheme += *uri++;
dd65d8c8
RN
419 }
420
421 //valid scheme?
ce321570 422 if (*uri == wxT(':'))
846978d7 423 {
dd65d8c8
RN
424 //mark the scheme as valid
425 m_fields |= wxURI_SCHEME;
426
427 //move reference point up to input buffer
428 uricopy = ++uri;
429 }
846978d7 430 else
dd65d8c8 431 //relative uri with relative path reference
525d8583 432 m_scheme = wxEmptyString;
dd65d8c8 433 }
846978d7 434// else
dd65d8c8
RN
435 //relative uri with _possible_ relative path reference
436
437 return uricopy;
438}
439
440const wxChar* wxURI::ParseAuthority(const wxChar* uri)
441{
442 // authority = [ userinfo "@" ] host [ ":" port ]
846978d7 443 if (*uri == wxT('/') && *(uri+1) == wxT('/'))
dd65d8c8 444 {
97ad053b 445 //skip past the two slashes
dd65d8c8
RN
446 uri += 2;
447
97ad053b
VZ
448 // ############# DEVIATION FROM RFC #########################
449 // Don't parse the server component for file URIs
450 if(m_scheme != wxT("file"))
451 {
452 //normal way
4860d40d 453 uri = ParseUserInfo(uri);
dd65d8c8
RN
454 uri = ParseServer(uri);
455 return ParsePort(uri);
97ad053b 456 }
dd65d8c8
RN
457 }
458
459 return uri;
460}
461
4860d40d 462const wxChar* wxURI::ParseUserInfo(const wxChar* uri)
dd65d8c8
RN
463{
464 wxASSERT(uri != NULL);
465
466 //copy of the uri - used for figuring out
467 //length of each component
468 const wxChar* uricopy = uri;
469
470 // userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
846978d7 471 while(*uri && *uri != wxT('@') && *uri != wxT('/') && *uri != wxT('#') && *uri != wxT('?'))
dd65d8c8 472 {
24ca04e7 473 if(IsUnreserved(*uri) ||
ce321570 474 IsSubDelim(*uri) || *uri == wxT(':'))
4860d40d 475 m_userinfo += *uri++;
24ca04e7
VZ
476 else if (IsEscape(uri))
477 {
478 m_userinfo += *uri++;
479 m_userinfo += *uri++;
480 m_userinfo += *uri++;
481 }
dd65d8c8 482 else
4860d40d 483 Escape(m_userinfo, *uri++);
dd65d8c8
RN
484 }
485
ce321570 486 if(*uri == wxT('@'))
dd65d8c8
RN
487 {
488 //valid userinfo
4860d40d 489 m_fields |= wxURI_USERINFO;
dd65d8c8
RN
490
491 uricopy = ++uri;
492 }
493 else
4860d40d 494 m_userinfo = wxEmptyString;
dd65d8c8
RN
495
496 return uricopy;
497}
498
499const wxChar* wxURI::ParseServer(const wxChar* uri)
500{
501 wxASSERT(uri != NULL);
502
503 //copy of the uri - used for figuring out
504 //length of each component
505 const wxChar* uricopy = uri;
506
507 // host = IP-literal / IPv4address / reg-name
508 // IP-literal = "[" ( IPv6address / IPvFuture ) "]"
ce321570 509 if (*uri == wxT('['))
dd65d8c8 510 {
e854db32
RN
511 ++uri; //some compilers don't support *&ing a ++*
512 if (ParseIPv6address(uri) && *uri == wxT(']'))
dd65d8c8
RN
513 {
514 ++uri;
515 m_hostType = wxURI_IPV6ADDRESS;
846978d7 516
dd65d8c8 517 wxStringBufferLength theBuffer(m_server, uri - uricopy);
2c09fb3b 518 wxTmemcpy(theBuffer, uricopy, uri-uricopy);
dd65d8c8
RN
519 theBuffer.SetLength(uri-uricopy);
520 }
521 else
522 {
523 uri = uricopy;
524
e854db32
RN
525 ++uri; //some compilers don't support *&ing a ++*
526 if (ParseIPvFuture(uri) && *uri == wxT(']'))
dd65d8c8
RN
527 {
528 ++uri;
846978d7
WS
529 m_hostType = wxURI_IPVFUTURE;
530
dd65d8c8 531 wxStringBufferLength theBuffer(m_server, uri - uricopy);
2c09fb3b 532 wxTmemcpy(theBuffer, uricopy, uri-uricopy);
dd65d8c8
RN
533 theBuffer.SetLength(uri-uricopy);
534 }
846978d7 535 else
dd65d8c8
RN
536 uri = uricopy;
537 }
538 }
846978d7 539 else
dd65d8c8
RN
540 {
541 if (ParseIPv4address(uri))
542 {
543 m_hostType = wxURI_IPV4ADDRESS;
544
545 wxStringBufferLength theBuffer(m_server, uri - uricopy);
2c09fb3b 546 wxTmemcpy(theBuffer, uricopy, uri-uricopy);
dd65d8c8
RN
547 theBuffer.SetLength(uri-uricopy);
548 }
846978d7 549 else
dd65d8c8
RN
550 uri = uricopy;
551 }
552
553 if(m_hostType == wxURI_REGNAME)
554 {
555 uri = uricopy;
556 // reg-name = *( unreserved / pct-encoded / sub-delims )
846978d7 557 while(*uri && *uri != wxT('/') && *uri != wxT(':') && *uri != wxT('#') && *uri != wxT('?'))
dd65d8c8 558 {
24ca04e7
VZ
559 if(IsUnreserved(*uri) || IsSubDelim(*uri))
560 m_server += *uri++;
561 else if (IsEscape(uri))
562 {
dd65d8c8 563 m_server += *uri++;
24ca04e7
VZ
564 m_server += *uri++;
565 m_server += *uri++;
566 }
dd65d8c8
RN
567 else
568 Escape(m_server, *uri++);
846978d7 569 }
dd65d8c8
RN
570 }
571
572 //mark the server as valid
573 m_fields |= wxURI_SERVER;
574
575 return uri;
576}
577
846978d7 578
dd65d8c8
RN
579const wxChar* wxURI::ParsePort(const wxChar* uri)
580{
581 wxASSERT(uri != NULL);
582
583 // port = *DIGIT
ce321570 584 if(*uri == wxT(':'))
dd65d8c8
RN
585 {
586 ++uri;
846978d7 587 while(IsDigit(*uri))
dd65d8c8
RN
588 {
589 m_port += *uri++;
846978d7 590 }
dd65d8c8
RN
591
592 //mark the port as valid
593 m_fields |= wxURI_PORT;
594 }
595
596 return uri;
597}
598
8404931e 599const wxChar* wxURI::ParsePath(const wxChar* uri, bool bReference, bool bNormalize)
dd65d8c8
RN
600{
601 wxASSERT(uri != NULL);
602
603 //copy of the uri - used for figuring out
604 //length of each component
605 const wxChar* uricopy = uri;
606
607 /// hier-part = "//" authority path-abempty
608 /// / path-absolute
609 /// / path-rootless
610 /// / path-empty
611 ///
612 /// relative-part = "//" authority path-abempty
613 /// / path-absolute
614 /// / path-noscheme
615 /// / path-empty
616 ///
617 /// path-abempty = *( "/" segment )
618 /// path-absolute = "/" [ segment-nz *( "/" segment ) ]
619 /// path-noscheme = segment-nz-nc *( "/" segment )
620 /// path-rootless = segment-nz *( "/" segment )
621 /// path-empty = 0<pchar>
622 ///
623 /// segment = *pchar
624 /// segment-nz = 1*pchar
625 /// segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
626 /// ; non-zero-length segment without any colon ":"
627 ///
628 /// pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
ce321570 629 if (*uri == wxT('/'))
dd65d8c8
RN
630 {
631 m_path += *uri++;
632
846978d7
WS
633 while(*uri && *uri != wxT('#') && *uri != wxT('?'))
634 {
24ca04e7 635 if( IsUnreserved(*uri) || IsSubDelim(*uri) ||
ce321570 636 *uri == wxT(':') || *uri == wxT('@') || *uri == wxT('/'))
846978d7 637 m_path += *uri++;
24ca04e7
VZ
638 else if (IsEscape(uri))
639 {
640 m_path += *uri++;
641 m_path += *uri++;
642 m_path += *uri++;
643 }
846978d7
WS
644 else
645 Escape(m_path, *uri++);
dd65d8c8
RN
646 }
647
648 if (bNormalize)
649 {
650 wxStringBufferLength theBuffer(m_path, m_path.length() + 1);
651 Normalize(theBuffer, true);
652 theBuffer.SetLength(wxStrlen(theBuffer));
653 }
654 //mark the path as valid
655 m_fields |= wxURI_PATH;
656 }
657 else if(*uri) //Relative path
658 {
659 if (bReference)
660 {
661 //no colon allowed
846978d7 662 while(*uri && *uri != wxT('#') && *uri != wxT('?'))
dd65d8c8 663 {
24ca04e7 664 if(IsUnreserved(*uri) || IsSubDelim(*uri) ||
ce321570 665 *uri == wxT('@') || *uri == wxT('/'))
846978d7 666 m_path += *uri++;
24ca04e7
VZ
667 else if (IsEscape(uri))
668 {
669 m_path += *uri++;
670 m_path += *uri++;
671 m_path += *uri++;
672 }
846978d7
WS
673 else
674 Escape(m_path, *uri++);
dd65d8c8 675 }
846978d7 676 }
dd65d8c8
RN
677 else
678 {
846978d7 679 while(*uri && *uri != wxT('#') && *uri != wxT('?'))
dd65d8c8 680 {
24ca04e7 681 if(IsUnreserved(*uri) || IsSubDelim(*uri) ||
ce321570 682 *uri == wxT(':') || *uri == wxT('@') || *uri == wxT('/'))
846978d7 683 m_path += *uri++;
24ca04e7
VZ
684 else if (IsEscape(uri))
685 {
686 m_path += *uri++;
687 m_path += *uri++;
688 m_path += *uri++;
689 }
846978d7
WS
690 else
691 Escape(m_path, *uri++);
dd65d8c8
RN
692 }
693 }
694
695 if (uri != uricopy)
846978d7 696 {
dd65d8c8
RN
697 if (bNormalize)
698 {
699 wxStringBufferLength theBuffer(m_path, m_path.length() + 1);
700 Normalize(theBuffer);
701 theBuffer.SetLength(wxStrlen(theBuffer));
702 }
703
704 //mark the path as valid
705 m_fields |= wxURI_PATH;
706 }
707 }
708
709 return uri;
710}
711
712
713const wxChar* wxURI::ParseQuery(const wxChar* uri)
714{
715 wxASSERT(uri != NULL);
716
717 // query = *( pchar / "/" / "?" )
ce321570 718 if (*uri == wxT('?'))
dd65d8c8
RN
719 {
720 ++uri;
ce321570 721 while(*uri && *uri != wxT('#'))
dd65d8c8 722 {
24ca04e7 723 if (IsUnreserved(*uri) || IsSubDelim(*uri) ||
ce321570 724 *uri == wxT(':') || *uri == wxT('@') || *uri == wxT('/') || *uri == wxT('?'))
846978d7 725 m_query += *uri++;
24ca04e7
VZ
726 else if (IsEscape(uri))
727 {
728 m_query += *uri++;
729 m_query += *uri++;
730 m_query += *uri++;
731 }
dd65d8c8 732 else
846978d7 733 Escape(m_query, *uri++);
dd65d8c8
RN
734 }
735
736 //mark the server as valid
737 m_fields |= wxURI_QUERY;
738 }
739
740 return uri;
741}
742
743
744const wxChar* wxURI::ParseFragment(const wxChar* uri)
745{
746 wxASSERT(uri != NULL);
747
748 // fragment = *( pchar / "/" / "?" )
ce321570 749 if (*uri == wxT('#'))
dd65d8c8
RN
750 {
751 ++uri;
752 while(*uri)
753 {
24ca04e7 754 if (IsUnreserved(*uri) || IsSubDelim(*uri) ||
ce321570 755 *uri == wxT(':') || *uri == wxT('@') || *uri == wxT('/') || *uri == wxT('?'))
846978d7 756 m_fragment += *uri++;
24ca04e7
VZ
757 else if (IsEscape(uri))
758 {
759 m_fragment += *uri++;
760 m_fragment += *uri++;
761 m_fragment += *uri++;
762 }
dd65d8c8 763 else
846978d7 764 Escape(m_fragment, *uri++);
dd65d8c8
RN
765 }
766
767 //mark the server as valid
768 m_fields |= wxURI_FRAGMENT;
769 }
770
771 return uri;
772}
773
774// ---------------------------------------------------------------------------
ce321570 775// Resolve
dd65d8c8 776//
ce321570 777// Builds missing components of this uri from a base uri
dd65d8c8 778//
ce321570
RN
779// A version of the algorithm outlined in the RFC is used here
780// (it is shown in comments)
781//
846978d7 782// Note that an empty URI inherits all components
dd65d8c8
RN
783// ---------------------------------------------------------------------------
784
8404931e 785void wxURI::Resolve(const wxURI& base, int flags)
dd65d8c8 786{
846978d7 787 wxASSERT_MSG(!base.IsReference(),
dd65d8c8
RN
788 wxT("wxURI to inherit from must not be a reference!"));
789
ce321570 790 // If we arn't being strict, enable the older (pre-RFC2396)
dd65d8c8
RN
791 // loophole that allows this uri to inherit other
792 // properties from the base uri - even if the scheme
793 // is defined
8404931e
VZ
794 if ( !(flags & wxURI_STRICT) &&
795 HasScheme() && base.HasScheme() &&
796 m_scheme == base.m_scheme )
846978d7 797 {
dd65d8c8
RN
798 m_fields -= wxURI_SCHEME;
799 }
800
801
802 // Do nothing if this is an absolute wxURI
803 // if defined(R.scheme) then
804 // T.scheme = R.scheme;
805 // T.authority = R.authority;
806 // T.path = remove_dot_segments(R.path);
807 // T.query = R.query;
808 if (HasScheme())
809 {
810 return;
811 }
812
ea4daac4 813 //No scheme - inherit
dd65d8c8
RN
814 m_scheme = base.m_scheme;
815 m_fields |= wxURI_SCHEME;
816
817 // All we need to do for relative URIs with an
818 // authority component is just inherit the scheme
819 // if defined(R.authority) then
820 // T.authority = R.authority;
821 // T.path = remove_dot_segments(R.path);
822 // T.query = R.query;
823 if (HasServer())
824 {
825 return;
826 }
827
828 //No authority - inherit
4860d40d 829 if (base.HasUserInfo())
dd65d8c8 830 {
4860d40d
RN
831 m_userinfo = base.m_userinfo;
832 m_fields |= wxURI_USERINFO;
dd65d8c8 833 }
846978d7 834
dd65d8c8
RN
835 m_server = base.m_server;
836 m_hostType = base.m_hostType;
837 m_fields |= wxURI_SERVER;
846978d7 838
dd65d8c8
RN
839 if (base.HasPort())
840 {
841 m_port = base.m_port;
842 m_fields |= wxURI_PORT;
843 }
846978d7 844
dd65d8c8
RN
845
846 // Simple path inheritance from base
847 if (!HasPath())
848 {
849 // T.path = Base.path;
850 m_path = base.m_path;
851 m_fields |= wxURI_PATH;
846978d7 852
dd65d8c8
RN
853
854 // if defined(R.query) then
855 // T.query = R.query;
856 // else
857 // T.query = Base.query;
858 // endif;
859 if (!HasQuery())
860 {
861 m_query = base.m_query;
862 m_fields |= wxURI_QUERY;
863 }
864 }
865 else
866 {
867 // if (R.path starts-with "/") then
868 // T.path = remove_dot_segments(R.path);
869 // else
870 // T.path = merge(Base.path, R.path);
871 // T.path = remove_dot_segments(T.path);
872 // endif;
873 // T.query = R.query;
ce321570 874 if (m_path[0u] != wxT('/'))
dd65d8c8 875 {
ea4daac4 876 //Merge paths
c9f78968
VS
877 wxString::const_iterator op = m_path.begin();
878 wxString::const_iterator bp = base.m_path.begin() + base.m_path.length();
dd65d8c8
RN
879
880 //not a ending directory? move up
ce321570 881 if (base.m_path[0] && *(bp-1) != wxT('/'))
c9f78968 882 UpTree(base.m_path.begin(), bp);
dd65d8c8
RN
883
884 //normalize directories
846978d7 885 while(*op == wxT('.') && *(op+1) == wxT('.') &&
ce321570 886 (*(op+2) == '\0' || *(op+2) == wxT('/')) )
dd65d8c8 887 {
c9f78968 888 UpTree(base.m_path.begin(), bp);
dd65d8c8
RN
889
890 if (*(op+2) == '\0')
891 op += 2;
892 else
893 op += 3;
894 }
895
c9f78968
VS
896 m_path = base.m_path.substr(0, bp - base.m_path.begin()) +
897 m_path.substr((op - m_path.begin()), m_path.length());
dd65d8c8
RN
898 }
899 }
ce321570 900
846978d7 901 //T.fragment = R.fragment;
dd65d8c8
RN
902}
903
904// ---------------------------------------------------------------------------
846978d7 905// UpTree
dd65d8c8 906//
ce321570 907// Moves a URI path up a directory
dd65d8c8
RN
908// ---------------------------------------------------------------------------
909
ce321570 910//static
c9f78968
VS
911void wxURI::UpTree(wxString::const_iterator uristart,
912 wxString::const_iterator& uri)
913{
914 if (uri != uristart && *(uri-1) == wxT('/'))
915 {
916 uri -= 2;
917 }
918
919 for(;uri != uristart; --uri)
920 {
921 if (*uri == wxT('/'))
922 {
923 ++uri;
924 break;
925 }
926 }
927
928 //!!!TODO:HACK!!!//
929 if (uri == uristart && *uri == wxT('/'))
930 ++uri;
931 //!!!//
932}
933
934// FIXME-UTF8: fix Normalize() to use iterators instead of having this method!
935/*static*/ void wxURI::UpTree(const wxChar* uristart, const wxChar*& uri)
dd65d8c8 936{
ce321570 937 if (uri != uristart && *(uri-1) == wxT('/'))
dd65d8c8
RN
938 {
939 uri -= 2;
940 }
846978d7 941
dd65d8c8
RN
942 for(;uri != uristart; --uri)
943 {
ce321570 944 if (*uri == wxT('/'))
dd65d8c8
RN
945 {
946 ++uri;
947 break;
948 }
949 }
950
951 //!!!TODO:HACK!!!//
ce321570 952 if (uri == uristart && *uri == wxT('/'))
dd65d8c8
RN
953 ++uri;
954 //!!!//
955}
c9f78968 956// end of FIXME-UTF8
dd65d8c8 957
ce321570
RN
958// ---------------------------------------------------------------------------
959// Normalize
960//
961// Normalizes directories in-place
962//
963// I.E. ./ and . are ignored
964//
965// ../ and .. are removed if a directory is before it, along
966// with that directory (leading .. and ../ are kept)
967// ---------------------------------------------------------------------------
968
969//static
8404931e 970void wxURI::Normalize(wxChar* s, bool bIgnoreLeads)
dd65d8c8
RN
971{
972 wxChar* cp = s;
973 wxChar* bp = s;
974
ce321570 975 if(s[0] == wxT('/'))
dd65d8c8
RN
976 ++bp;
977
978 while(*cp)
979 {
ce321570
RN
980 if (*cp == wxT('.') && (*(cp+1) == wxT('/') || *(cp+1) == '\0')
981 && (bp == cp || *(cp-1) == wxT('/')))
dd65d8c8
RN
982 {
983 //. _or_ ./ - ignore
984 if (*(cp+1) == '\0')
985 cp += 1;
986 else
987 cp += 2;
988 }
846978d7 989 else if (*cp == wxT('.') && *(cp+1) == wxT('.') &&
ce321570
RN
990 (*(cp+2) == wxT('/') || *(cp+2) == '\0')
991 && (bp == cp || *(cp-1) == wxT('/')))
dd65d8c8
RN
992 {
993 //.. _or_ ../ - go up the tree
994 if (s != bp)
995 {
996 UpTree((const wxChar*)bp, (const wxChar*&)s);
997
998 if (*(cp+2) == '\0')
999 cp += 2;
1000 else
1001 cp += 3;
1002 }
1003 else if (!bIgnoreLeads)
1004
1005 {
1006 *bp++ = *cp++;
1007 *bp++ = *cp++;
1008 if (*cp)
1009 *bp++ = *cp++;
1010
1011 s = bp;
1012 }
1013 else
1014 {
1015 if (*(cp+2) == '\0')
1016 cp += 2;
1017 else
1018 cp += 3;
1019 }
1020 }
1021 else
846978d7 1022 *s++ = *cp++;
dd65d8c8
RN
1023 }
1024
1025 *s = '\0';
1026}
1027
1028// ---------------------------------------------------------------------------
ce321570
RN
1029// ParseH16
1030//
1031// Parses 1 to 4 hex values. Returns true if the first character of the input
846978d7 1032// string is a valid hex character. It is the caller's responsability to move
ce321570
RN
1033// the input string back to its original position on failure.
1034// ---------------------------------------------------------------------------
1035
1036bool wxURI::ParseH16(const wxChar*& uri)
1037{
1038 // h16 = 1*4HEXDIG
1039 if(!IsHex(*++uri))
1040 return false;
1041
1042 if(IsHex(*++uri) && IsHex(*++uri) && IsHex(*++uri))
1043 ++uri;
1044
1045 return true;
1046}
1047
1048// ---------------------------------------------------------------------------
1049// ParseIPXXX
1050//
846978d7
WS
1051// Parses a certain version of an IP address and moves the input string past
1052// it. Returns true if the input string contains the proper version of an ip
1053// address. It is the caller's responsability to move the input string back
ce321570 1054// to its original position on failure.
dd65d8c8
RN
1055// ---------------------------------------------------------------------------
1056
1057bool wxURI::ParseIPv4address(const wxChar*& uri)
1058{
1059 //IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
1060 //
1061 //dec-octet = DIGIT ; 0-9
1062 // / %x31-39 DIGIT ; 10-99
1063 // / "1" 2DIGIT ; 100-199
1064 // / "2" %x30-34 DIGIT ; 200-249
1065 // / "25" %x30-35 ; 250-255
1066 size_t iIPv4 = 0;
1067 if (IsDigit(*uri))
1068 {
1069 ++iIPv4;
1070
846978d7 1071
dd65d8c8
RN
1072 //each ip part must be between 0-255 (dupe of version in for loop)
1073 if( IsDigit(*++uri) && IsDigit(*++uri) &&
1074 //100 or less (note !)
846978d7
WS
1075 !( (*(uri-2) < wxT('2')) ||
1076 //240 or less
1077 (*(uri-2) == wxT('2') &&
ce321570 1078 (*(uri-1) < wxT('5') || (*(uri-1) == wxT('5') && *uri <= wxT('5')))
dd65d8c8
RN
1079 )
1080 )
1081 )
1082 {
1083 return false;
1084 }
1085
1086 if(IsDigit(*uri))++uri;
1087
1088 //compilers should unroll this loop
1089 for(; iIPv4 < 4; ++iIPv4)
1090 {
ce321570 1091 if (*uri != wxT('.') || !IsDigit(*++uri))
dd65d8c8
RN
1092 break;
1093
1094 //each ip part must be between 0-255
1095 if( IsDigit(*++uri) && IsDigit(*++uri) &&
1096 //100 or less (note !)
846978d7
WS
1097 !( (*(uri-2) < wxT('2')) ||
1098 //240 or less
1099 (*(uri-2) == wxT('2') &&
ce321570 1100 (*(uri-1) < wxT('5') || (*(uri-1) == wxT('5') && *uri <= wxT('5')))
dd65d8c8
RN
1101 )
1102 )
1103 )
1104 {
1105 return false;
1106 }
1107 if(IsDigit(*uri))++uri;
1108 }
1109 }
1110 return iIPv4 == 4;
1111}
1112
dd65d8c8
RN
1113bool wxURI::ParseIPv6address(const wxChar*& uri)
1114{
1115 // IPv6address = 6( h16 ":" ) ls32
1116 // / "::" 5( h16 ":" ) ls32
1117 // / [ h16 ] "::" 4( h16 ":" ) ls32
1118 // / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1119 // / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1120 // / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1121 // / [ *4( h16 ":" ) h16 ] "::" ls32
1122 // / [ *5( h16 ":" ) h16 ] "::" h16
1123 // / [ *6( h16 ":" ) h16 ] "::"
1124
1125 size_t numPrefix = 0,
1126 maxPostfix;
1127
1128 bool bEndHex = false;
1129
1130 for( ; numPrefix < 6; ++numPrefix)
1131 {
1132 if(!ParseH16(uri))
1133 {
1134 --uri;
1135 bEndHex = true;
1136 break;
1137 }
846978d7 1138
ce321570 1139 if(*uri != wxT(':'))
dd65d8c8
RN
1140 {
1141 break;
1142 }
1143 }
1144
1145 if(!bEndHex && !ParseH16(uri))
1146 {
1147 --uri;
1148
1149 if (numPrefix)
1150 return false;
1151
ce321570 1152 if (*uri == wxT(':'))
dd65d8c8 1153 {
ce321570 1154 if (*++uri != wxT(':'))
dd65d8c8
RN
1155 return false;
1156
1157 maxPostfix = 5;
1158 }
1159 else
1160 maxPostfix = 6;
1161 }
1162 else
1163 {
ce321570 1164 if (*uri != wxT(':') || *(uri+1) != wxT(':'))
dd65d8c8
RN
1165 {
1166 if (numPrefix != 6)
1167 return false;
1168
ce321570 1169 while (*--uri != wxT(':')) {}
dd65d8c8
RN
1170 ++uri;
1171
1172 const wxChar* uristart = uri;
1173 //parse ls32
1174 // ls32 = ( h16 ":" h16 ) / IPv4address
846978d7 1175 if (ParseH16(uri) && *uri == wxT(':') && ParseH16(uri))
dd65d8c8
RN
1176 return true;
1177
1178 uri = uristart;
1179
1180 if (ParseIPv4address(uri))
1181 return true;
1182 else
1183 return false;
1184 }
1185 else
1186 {
1187 uri += 2;
846978d7 1188
dd65d8c8
RN
1189 if (numPrefix > 3)
1190 maxPostfix = 0;
1191 else
1192 maxPostfix = 4 - numPrefix;
1193 }
1194 }
1195
1196 bool bAllowAltEnding = maxPostfix == 0;
1197
1198 for(; maxPostfix != 0; --maxPostfix)
1199 {
ce321570 1200 if(!ParseH16(uri) || *uri != wxT(':'))
dd65d8c8
RN
1201 return false;
1202 }
1203
1204 if(numPrefix <= 4)
1205 {
1206 const wxChar* uristart = uri;
1207 //parse ls32
1208 // ls32 = ( h16 ":" h16 ) / IPv4address
846978d7 1209 if (ParseH16(uri) && *uri == wxT(':') && ParseH16(uri))
dd65d8c8
RN
1210 return true;
1211
1212 uri = uristart;
1213
1214 if (ParseIPv4address(uri))
1215 return true;
1216
1217 uri = uristart;
846978d7 1218
dd65d8c8
RN
1219 if (!bAllowAltEnding)
1220 return false;
1221 }
1222
1223 if(numPrefix <= 5 && ParseH16(uri))
1224 return true;
1225
1226 return true;
1227}
1228
1229bool wxURI::ParseIPvFuture(const wxChar*& uri)
1230{
1231 // IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
ce321570 1232 if (*++uri != wxT('v') || !IsHex(*++uri))
dd65d8c8
RN
1233 return false;
1234
1235 while (IsHex(*++uri)) {}
1236
ce321570 1237 if (*uri != wxT('.') || !(IsUnreserved(*++uri) || IsSubDelim(*uri) || *uri == wxT(':')))
dd65d8c8
RN
1238 return false;
1239
ce321570 1240 while(IsUnreserved(*++uri) || IsSubDelim(*uri) || *uri == wxT(':')) {}
dd65d8c8
RN
1241
1242 return true;
1243}
1244
1245
1246// ---------------------------------------------------------------------------
ce321570
RN
1247// CharToHex
1248//
846978d7 1249// Converts a character into a numeric hexidecimal value, or 0 if the
ce321570 1250// passed in character is not a valid hex character
dd65d8c8
RN
1251// ---------------------------------------------------------------------------
1252
ce321570 1253//static
409a7ba7 1254wxChar wxURI::CharToHex(const wxChar& c)
dd65d8c8 1255{
1676a194
WS
1256 if ((c >= wxT('A')) && (c <= wxT('Z'))) return wxChar(c - wxT('A') + 0x0A);
1257 if ((c >= wxT('a')) && (c <= wxT('z'))) return wxChar(c - wxT('a') + 0x0a);
1258 if ((c >= wxT('0')) && (c <= wxT('9'))) return wxChar(c - wxT('0') + 0x00);
dd65d8c8 1259
846978d7 1260 return 0;
dd65d8c8
RN
1261}
1262
ce321570
RN
1263// ---------------------------------------------------------------------------
1264// IsXXX
1265//
1266// Returns true if the passed in character meets the criteria of the method
1267// ---------------------------------------------------------------------------
1268
dd65d8c8
RN
1269//! unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
1270bool wxURI::IsUnreserved (const wxChar& c)
846978d7 1271{ return IsAlpha(c) || IsDigit(c) ||
ce321570
RN
1272 c == wxT('-') ||
1273 c == wxT('.') ||
1274 c == wxT('_') ||
1275 c == wxT('~') //tilde
846978d7 1276 ;
dd65d8c8
RN
1277}
1278
1279bool wxURI::IsReserved (const wxChar& c)
846978d7 1280{
dd65d8c8
RN
1281 return IsGenDelim(c) || IsSubDelim(c);
1282}
1283
1284//! gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
1285bool wxURI::IsGenDelim (const wxChar& c)
1286{
ce321570
RN
1287 return c == wxT(':') ||
1288 c == wxT('/') ||
1289 c == wxT('?') ||
1290 c == wxT('#') ||
1291 c == wxT('[') ||
1292 c == wxT(']') ||
1293 c == wxT('@');
dd65d8c8
RN
1294}
1295
1296//! sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
1297//! / "*" / "+" / "," / ";" / "="
1298bool wxURI::IsSubDelim (const wxChar& c)
1299{
ce321570
RN
1300 return c == wxT('!') ||
1301 c == wxT('$') ||
1302 c == wxT('&') ||
1303 c == wxT('\'') ||
1304 c == wxT('(') ||
1305 c == wxT(')') ||
1306 c == wxT('*') ||
1307 c == wxT('+') ||
1308 c == wxT(',') ||
1309 c == wxT(';') ||
846978d7 1310 c == wxT('=')
dd65d8c8
RN
1311 ;
1312}
1313
1314bool wxURI::IsHex(const wxChar& c)
ce321570 1315{ return IsDigit(c) || (c >= wxT('a') && c <= wxT('f')) || (c >= wxT('A') && c <= wxT('F')); }
dd65d8c8
RN
1316
1317bool wxURI::IsAlpha(const wxChar& c)
ce321570 1318{ return (c >= wxT('a') && c <= wxT('z')) || (c >= wxT('A') && c <= wxT('Z')); }
dd65d8c8
RN
1319
1320bool wxURI::IsDigit(const wxChar& c)
ce321570 1321{ return c >= wxT('0') && c <= wxT('9'); }
dd65d8c8
RN
1322
1323
dd65d8c8
RN
1324//end of uri.cpp
1325
1326
1327