/[pcsx2_0.9.7]/trunk/3rdparty/wxWidgets/src/common/uri.cpp
ViewVC logotype

Contents of /trunk/3rdparty/wxWidgets/src/common/uri.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22