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