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