uri_parser 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +6 -0
- data/.rvmrc +1 -0
- data/Gemfile +6 -0
- data/Rakefile +13 -0
- data/ext/uri_parser/basictypes.h +89 -0
- data/ext/uri_parser/extconf.h +6 -0
- data/ext/uri_parser/extconf.rb +50 -0
- data/ext/uri_parser/logging.h +5 -0
- data/ext/uri_parser/scoped_ptr.h +322 -0
- data/ext/uri_parser/string16.cc +95 -0
- data/ext/uri_parser/string16.h +194 -0
- data/ext/uri_parser/uri_parser.cc +87 -0
- data/ext/uri_parser/url_canon.h +872 -0
- data/ext/uri_parser/url_canon_etc.cc +392 -0
- data/ext/uri_parser/url_canon_fileurl.cc +215 -0
- data/ext/uri_parser/url_canon_host.cc +401 -0
- data/ext/uri_parser/url_canon_icu.cc +207 -0
- data/ext/uri_parser/url_canon_icu.h +63 -0
- data/ext/uri_parser/url_canon_internal.cc +427 -0
- data/ext/uri_parser/url_canon_internal.h +453 -0
- data/ext/uri_parser/url_canon_internal_file.h +157 -0
- data/ext/uri_parser/url_canon_ip.cc +737 -0
- data/ext/uri_parser/url_canon_ip.h +101 -0
- data/ext/uri_parser/url_canon_mailtourl.cc +137 -0
- data/ext/uri_parser/url_canon_path.cc +380 -0
- data/ext/uri_parser/url_canon_pathurl.cc +128 -0
- data/ext/uri_parser/url_canon_query.cc +189 -0
- data/ext/uri_parser/url_canon_relative.cc +572 -0
- data/ext/uri_parser/url_canon_stdstring.h +134 -0
- data/ext/uri_parser/url_canon_stdurl.cc +211 -0
- data/ext/uri_parser/url_common.h +48 -0
- data/ext/uri_parser/url_file.h +108 -0
- data/ext/uri_parser/url_parse.cc +760 -0
- data/ext/uri_parser/url_parse.h +336 -0
- data/ext/uri_parser/url_parse_file.cc +243 -0
- data/ext/uri_parser/url_parse_internal.h +112 -0
- data/ext/uri_parser/url_util.cc +553 -0
- data/ext/uri_parser/url_util.h +222 -0
- data/lib/uri_parser.rb +28 -0
- data/lib/uri_parser/version.rb +3 -0
- data/spec/spec_helper.rb +16 -0
- data/spec/uri_parser_spec.rb +54 -0
- data/uri_parser.gemspec +26 -0
- metadata +117 -0
@@ -0,0 +1,134 @@
|
|
1
|
+
// Copyright 2007, Google Inc.
|
2
|
+
// All rights reserved.
|
3
|
+
//
|
4
|
+
// Redistribution and use in source and binary forms, with or without
|
5
|
+
// modification, are permitted provided that the following conditions are
|
6
|
+
// met:
|
7
|
+
//
|
8
|
+
// * Redistributions of source code must retain the above copyright
|
9
|
+
// notice, this list of conditions and the following disclaimer.
|
10
|
+
// * Redistributions in binary form must reproduce the above
|
11
|
+
// copyright notice, this list of conditions and the following disclaimer
|
12
|
+
// in the documentation and/or other materials provided with the
|
13
|
+
// distribution.
|
14
|
+
// * Neither the name of Google Inc. nor the names of its
|
15
|
+
// contributors may be used to endorse or promote products derived from
|
16
|
+
// this software without specific prior written permission.
|
17
|
+
//
|
18
|
+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
19
|
+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
20
|
+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
21
|
+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
22
|
+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
23
|
+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
24
|
+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
25
|
+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
26
|
+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
27
|
+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
28
|
+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
29
|
+
|
30
|
+
// This header file defines a canonicalizer output method class for STL
|
31
|
+
// strings. Because the canonicalizer tries not to be dependent on the STL,
|
32
|
+
// we have segregated it here.
|
33
|
+
|
34
|
+
#ifndef GOOGLEURL_SRC_URL_CANON_STDSTRING_H__
|
35
|
+
#define GOOGLEURL_SRC_URL_CANON_STDSTRING_H__
|
36
|
+
|
37
|
+
#include <string>
|
38
|
+
#include "url_canon.h"
|
39
|
+
|
40
|
+
namespace url_canon {
|
41
|
+
|
42
|
+
// Write into a std::string given in the constructor. This object does not own
|
43
|
+
// the string itself, and the user must ensure that the string stays alive
|
44
|
+
// throughout the lifetime of this object.
|
45
|
+
//
|
46
|
+
// The given string will be appended to; any existing data in the string will
|
47
|
+
// be preserved. The caller should reserve() the amount of data in the string
|
48
|
+
// they expect to be written. We will resize if necessary, but that's slow.
|
49
|
+
//
|
50
|
+
// Note that when canonicalization is complete, the string will likely have
|
51
|
+
// unused space at the end because we make the string very big to start out
|
52
|
+
// with (by |initial_size|). This ends up being important because resize
|
53
|
+
// operations are slow, and because the base class needs to write directly
|
54
|
+
// into the buffer.
|
55
|
+
//
|
56
|
+
// Therefore, the user should call Complete() before using the string that
|
57
|
+
// this class wrote into.
|
58
|
+
class StdStringCanonOutput : public CanonOutput {
|
59
|
+
public:
|
60
|
+
StdStringCanonOutput(std::string* str)
|
61
|
+
: CanonOutput(),
|
62
|
+
str_(str) {
|
63
|
+
cur_len_ = static_cast<int>(str_->size()); // Append to existing data.
|
64
|
+
str_->resize(str_->capacity());
|
65
|
+
buffer_ = &(*str_)[0];
|
66
|
+
buffer_len_ = static_cast<int>(str_->size());
|
67
|
+
}
|
68
|
+
virtual ~StdStringCanonOutput() {
|
69
|
+
// Nothing to do, we don't own the string.
|
70
|
+
}
|
71
|
+
|
72
|
+
// Must be called after writing has completed but before the string is used.
|
73
|
+
void Complete() {
|
74
|
+
str_->resize(cur_len_);
|
75
|
+
buffer_len_ = cur_len_;
|
76
|
+
}
|
77
|
+
|
78
|
+
virtual void Resize(int sz) {
|
79
|
+
str_->resize(sz);
|
80
|
+
buffer_ = &(*str_)[0];
|
81
|
+
buffer_len_ = sz;
|
82
|
+
}
|
83
|
+
|
84
|
+
protected:
|
85
|
+
std::string* str_;
|
86
|
+
};
|
87
|
+
|
88
|
+
// An extension of the Replacements class that allows the setters to use
|
89
|
+
// standard strings.
|
90
|
+
//
|
91
|
+
// The strings passed as arguments are not copied and must remain valid until
|
92
|
+
// this class goes out of scope.
|
93
|
+
template<typename STR>
|
94
|
+
class StdStringReplacements :
|
95
|
+
public url_canon::Replacements<typename STR::value_type> {
|
96
|
+
public:
|
97
|
+
void SetSchemeStr(const STR& s) {
|
98
|
+
this->SetScheme(s.data(),
|
99
|
+
url_parse::Component(0, static_cast<int>(s.length())));
|
100
|
+
}
|
101
|
+
void SetUsernameStr(const STR& s) {
|
102
|
+
this->SetUsername(s.data(),
|
103
|
+
url_parse::Component(0, static_cast<int>(s.length())));
|
104
|
+
}
|
105
|
+
void SetPasswordStr(const STR& s) {
|
106
|
+
this->SetPassword(s.data(),
|
107
|
+
url_parse::Component(0, static_cast<int>(s.length())));
|
108
|
+
}
|
109
|
+
void SetHostStr(const STR& s) {
|
110
|
+
this->SetHost(s.data(),
|
111
|
+
url_parse::Component(0, static_cast<int>(s.length())));
|
112
|
+
}
|
113
|
+
void SetPortStr(const STR& s) {
|
114
|
+
this->SetPort(s.data(),
|
115
|
+
url_parse::Component(0, static_cast<int>(s.length())));
|
116
|
+
}
|
117
|
+
void SetPathStr(const STR& s) {
|
118
|
+
this->SetPath(s.data(),
|
119
|
+
url_parse::Component(0, static_cast<int>(s.length())));
|
120
|
+
}
|
121
|
+
void SetQueryStr(const STR& s) {
|
122
|
+
this->SetQuery(s.data(),
|
123
|
+
url_parse::Component(0, static_cast<int>(s.length())));
|
124
|
+
}
|
125
|
+
void SetRefStr(const STR& s) {
|
126
|
+
this->SetRef(s.data(),
|
127
|
+
url_parse::Component(0, static_cast<int>(s.length())));
|
128
|
+
}
|
129
|
+
};
|
130
|
+
|
131
|
+
} // namespace url_canon
|
132
|
+
|
133
|
+
#endif // GOOGLEURL_SRC_URL_CANON_STDSTRING_H__
|
134
|
+
|
@@ -0,0 +1,211 @@
|
|
1
|
+
// Copyright 2007, Google Inc.
|
2
|
+
// All rights reserved.
|
3
|
+
//
|
4
|
+
// Redistribution and use in source and binary forms, with or without
|
5
|
+
// modification, are permitted provided that the following conditions are
|
6
|
+
// met:
|
7
|
+
//
|
8
|
+
// * Redistributions of source code must retain the above copyright
|
9
|
+
// notice, this list of conditions and the following disclaimer.
|
10
|
+
// * Redistributions in binary form must reproduce the above
|
11
|
+
// copyright notice, this list of conditions and the following disclaimer
|
12
|
+
// in the documentation and/or other materials provided with the
|
13
|
+
// distribution.
|
14
|
+
// * Neither the name of Google Inc. nor the names of its
|
15
|
+
// contributors may be used to endorse or promote products derived from
|
16
|
+
// this software without specific prior written permission.
|
17
|
+
//
|
18
|
+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
19
|
+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
20
|
+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
21
|
+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
22
|
+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
23
|
+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
24
|
+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
25
|
+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
26
|
+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
27
|
+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
28
|
+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
29
|
+
|
30
|
+
// Functions to canonicalize "standard" URLs, which are ones that have an
|
31
|
+
// authority section including a host name.
|
32
|
+
|
33
|
+
#include "url_canon.h"
|
34
|
+
#include "url_canon_internal.h"
|
35
|
+
|
36
|
+
namespace url_canon {
|
37
|
+
|
38
|
+
namespace {
|
39
|
+
|
40
|
+
template<typename CHAR, typename UCHAR>
|
41
|
+
bool DoCanonicalizeStandardURL(const URLComponentSource<CHAR>& source,
|
42
|
+
const url_parse::Parsed& parsed,
|
43
|
+
CharsetConverter* query_converter,
|
44
|
+
CanonOutput* output,
|
45
|
+
url_parse::Parsed* new_parsed) {
|
46
|
+
// Scheme: this will append the colon.
|
47
|
+
bool success = CanonicalizeScheme(source.scheme, parsed.scheme,
|
48
|
+
output, &new_parsed->scheme);
|
49
|
+
|
50
|
+
// Authority (username, password, host, port)
|
51
|
+
bool have_authority;
|
52
|
+
if (parsed.username.is_valid() || parsed.password.is_valid() ||
|
53
|
+
parsed.host.is_nonempty() || parsed.port.is_valid()) {
|
54
|
+
have_authority = true;
|
55
|
+
|
56
|
+
// Only write the authority separators when we have a scheme.
|
57
|
+
if (parsed.scheme.is_valid()) {
|
58
|
+
output->push_back('/');
|
59
|
+
output->push_back('/');
|
60
|
+
}
|
61
|
+
|
62
|
+
// User info: the canonicalizer will handle the : and @.
|
63
|
+
success &= CanonicalizeUserInfo(source.username, parsed.username,
|
64
|
+
source.password, parsed.password,
|
65
|
+
output,
|
66
|
+
&new_parsed->username,
|
67
|
+
&new_parsed->password);
|
68
|
+
|
69
|
+
success &= CanonicalizeHost(source.host, parsed.host,
|
70
|
+
output, &new_parsed->host);
|
71
|
+
|
72
|
+
// Host must not be empty for standard URLs.
|
73
|
+
if (!parsed.host.is_nonempty())
|
74
|
+
success = false;
|
75
|
+
|
76
|
+
// Port: the port canonicalizer will handle the colon.
|
77
|
+
int default_port = DefaultPortForScheme(
|
78
|
+
&output->data()[new_parsed->scheme.begin], new_parsed->scheme.len);
|
79
|
+
success &= CanonicalizePort(source.port, parsed.port, default_port,
|
80
|
+
output, &new_parsed->port);
|
81
|
+
} else {
|
82
|
+
// No authority, clear the components.
|
83
|
+
have_authority = false;
|
84
|
+
new_parsed->host.reset();
|
85
|
+
new_parsed->username.reset();
|
86
|
+
new_parsed->password.reset();
|
87
|
+
new_parsed->port.reset();
|
88
|
+
success = false; // Standard URLs must have an authority.
|
89
|
+
}
|
90
|
+
|
91
|
+
// Path
|
92
|
+
if (parsed.path.is_valid()) {
|
93
|
+
success &= CanonicalizePath(source.path, parsed.path,
|
94
|
+
output, &new_parsed->path);
|
95
|
+
} else if (have_authority ||
|
96
|
+
parsed.query.is_valid() || parsed.ref.is_valid()) {
|
97
|
+
// When we have an empty path, make up a path when we have an authority
|
98
|
+
// or something following the path. The only time we allow an empty
|
99
|
+
// output path is when there is nothing else.
|
100
|
+
new_parsed->path = url_parse::Component(output->length(), 1);
|
101
|
+
output->push_back('/');
|
102
|
+
} else {
|
103
|
+
// No path at all
|
104
|
+
new_parsed->path.reset();
|
105
|
+
}
|
106
|
+
|
107
|
+
// Query
|
108
|
+
CanonicalizeQuery(source.query, parsed.query, query_converter,
|
109
|
+
output, &new_parsed->query);
|
110
|
+
|
111
|
+
// Ref: ignore failure for this, since the page can probably still be loaded.
|
112
|
+
CanonicalizeRef(source.ref, parsed.ref, output, &new_parsed->ref);
|
113
|
+
|
114
|
+
return success;
|
115
|
+
}
|
116
|
+
|
117
|
+
} // namespace
|
118
|
+
|
119
|
+
|
120
|
+
// Returns the default port for the given canonical scheme, or PORT_UNSPECIFIED
|
121
|
+
// if the scheme is unknown.
|
122
|
+
int DefaultPortForScheme(const char* scheme, int scheme_len) {
|
123
|
+
int default_port = url_parse::PORT_UNSPECIFIED;
|
124
|
+
switch (scheme_len) {
|
125
|
+
case 4:
|
126
|
+
if (!strncmp(scheme, "http", scheme_len))
|
127
|
+
default_port = 80;
|
128
|
+
break;
|
129
|
+
case 5:
|
130
|
+
if (!strncmp(scheme, "https", scheme_len))
|
131
|
+
default_port = 443;
|
132
|
+
break;
|
133
|
+
case 3:
|
134
|
+
if (!strncmp(scheme, "ftp", scheme_len))
|
135
|
+
default_port = 21;
|
136
|
+
else if (!strncmp(scheme, "wss", scheme_len))
|
137
|
+
default_port = 443;
|
138
|
+
break;
|
139
|
+
case 6:
|
140
|
+
if (!strncmp(scheme, "gopher", scheme_len))
|
141
|
+
default_port = 70;
|
142
|
+
break;
|
143
|
+
case 2:
|
144
|
+
if (!strncmp(scheme, "ws", scheme_len))
|
145
|
+
default_port = 80;
|
146
|
+
break;
|
147
|
+
}
|
148
|
+
return default_port;
|
149
|
+
}
|
150
|
+
|
151
|
+
bool CanonicalizeStandardURL(const char* spec,
|
152
|
+
int spec_len,
|
153
|
+
const url_parse::Parsed& parsed,
|
154
|
+
CharsetConverter* query_converter,
|
155
|
+
CanonOutput* output,
|
156
|
+
url_parse::Parsed* new_parsed) {
|
157
|
+
return DoCanonicalizeStandardURL<char, unsigned char>(
|
158
|
+
URLComponentSource<char>(spec), parsed, query_converter,
|
159
|
+
output, new_parsed);
|
160
|
+
}
|
161
|
+
|
162
|
+
bool CanonicalizeStandardURL(const char16* spec,
|
163
|
+
int spec_len,
|
164
|
+
const url_parse::Parsed& parsed,
|
165
|
+
CharsetConverter* query_converter,
|
166
|
+
CanonOutput* output,
|
167
|
+
url_parse::Parsed* new_parsed) {
|
168
|
+
return DoCanonicalizeStandardURL<char16, char16>(
|
169
|
+
URLComponentSource<char16>(spec), parsed, query_converter,
|
170
|
+
output, new_parsed);
|
171
|
+
}
|
172
|
+
|
173
|
+
// It might be nice in the future to optimize this so unchanged components don't
|
174
|
+
// need to be recanonicalized. This is especially true since the common case for
|
175
|
+
// ReplaceComponents is removing things we don't want, like reference fragments
|
176
|
+
// and usernames. These cases can become more efficient if we can assume the
|
177
|
+
// rest of the URL is OK with these removed (or only the modified parts
|
178
|
+
// recanonicalized). This would be much more complex to implement, however.
|
179
|
+
//
|
180
|
+
// You would also need to update DoReplaceComponents in url_util.cc which
|
181
|
+
// relies on this re-checking everything (see the comment there for why).
|
182
|
+
bool ReplaceStandardURL(const char* base,
|
183
|
+
const url_parse::Parsed& base_parsed,
|
184
|
+
const Replacements<char>& replacements,
|
185
|
+
CharsetConverter* query_converter,
|
186
|
+
CanonOutput* output,
|
187
|
+
url_parse::Parsed* new_parsed) {
|
188
|
+
URLComponentSource<char> source(base);
|
189
|
+
url_parse::Parsed parsed(base_parsed);
|
190
|
+
SetupOverrideComponents(base, replacements, &source, &parsed);
|
191
|
+
return DoCanonicalizeStandardURL<char, unsigned char>(
|
192
|
+
source, parsed, query_converter, output, new_parsed);
|
193
|
+
}
|
194
|
+
|
195
|
+
// For 16-bit replacements, we turn all the replacements into UTF-8 so the
|
196
|
+
// regular codepath can be used.
|
197
|
+
bool ReplaceStandardURL(const char* base,
|
198
|
+
const url_parse::Parsed& base_parsed,
|
199
|
+
const Replacements<char16>& replacements,
|
200
|
+
CharsetConverter* query_converter,
|
201
|
+
CanonOutput* output,
|
202
|
+
url_parse::Parsed* new_parsed) {
|
203
|
+
RawCanonOutput<1024> utf8;
|
204
|
+
URLComponentSource<char> source(base);
|
205
|
+
url_parse::Parsed parsed(base_parsed);
|
206
|
+
SetupUTF16OverrideComponents(base, replacements, &utf8, &source, &parsed);
|
207
|
+
return DoCanonicalizeStandardURL<char, unsigned char>(
|
208
|
+
source, parsed, query_converter, output, new_parsed);
|
209
|
+
}
|
210
|
+
|
211
|
+
} // namespace url_canon
|
@@ -0,0 +1,48 @@
|
|
1
|
+
// Copyright 2010, Google Inc.
|
2
|
+
// All rights reserved.
|
3
|
+
//
|
4
|
+
// Redistribution and use in source and binary forms, with or without
|
5
|
+
// modification, are permitted provided that the following conditions are
|
6
|
+
// met:
|
7
|
+
//
|
8
|
+
// * Redistributions of source code must retain the above copyright
|
9
|
+
// notice, this list of conditions and the following disclaimer.
|
10
|
+
// * Redistributions in binary form must reproduce the above
|
11
|
+
// copyright notice, this list of conditions and the following disclaimer
|
12
|
+
// in the documentation and/or other materials provided with the
|
13
|
+
// distribution.
|
14
|
+
// * Neither the name of Google Inc. nor the names of its
|
15
|
+
// contributors may be used to endorse or promote products derived from
|
16
|
+
// this software without specific prior written permission.
|
17
|
+
//
|
18
|
+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
19
|
+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
20
|
+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
21
|
+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
22
|
+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
23
|
+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
24
|
+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
25
|
+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
26
|
+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
27
|
+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
28
|
+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
29
|
+
|
30
|
+
#ifndef GOOGLEURL_SRC_URL_COMMON_H__
|
31
|
+
#define GOOGLEURL_SRC_URL_COMMON_H__
|
32
|
+
|
33
|
+
#if !defined(GURL_IMPLEMENTATION)
|
34
|
+
#define GURL_IMPLEMENTATION 0
|
35
|
+
#endif
|
36
|
+
|
37
|
+
#if defined(WIN32) && defined(GURL_DLL)
|
38
|
+
#if GURL_IMPLEMENTATION
|
39
|
+
#define GURL_API __declspec(dllexport)
|
40
|
+
#else
|
41
|
+
#define GURL_API __declspec(dllimport)
|
42
|
+
#endif
|
43
|
+
#else
|
44
|
+
#define GURL_API
|
45
|
+
#endif
|
46
|
+
|
47
|
+
#endif // GOOGLEURL_SRC_URL_COMMON_H__
|
48
|
+
|
@@ -0,0 +1,108 @@
|
|
1
|
+
// Copyright 2007, Google Inc.
|
2
|
+
// All rights reserved.
|
3
|
+
//
|
4
|
+
// Redistribution and use in source and binary forms, with or without
|
5
|
+
// modification, are permitted provided that the following conditions are
|
6
|
+
// met:
|
7
|
+
//
|
8
|
+
// * Redistributions of source code must retain the above copyright
|
9
|
+
// notice, this list of conditions and the following disclaimer.
|
10
|
+
// * Redistributions in binary form must reproduce the above
|
11
|
+
// copyright notice, this list of conditions and the following disclaimer
|
12
|
+
// in the documentation and/or other materials provided with the
|
13
|
+
// distribution.
|
14
|
+
// * Neither the name of Google Inc. nor the names of its
|
15
|
+
// contributors may be used to endorse or promote products derived from
|
16
|
+
// this software without specific prior written permission.
|
17
|
+
//
|
18
|
+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
19
|
+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
20
|
+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
21
|
+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
22
|
+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
23
|
+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
24
|
+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
25
|
+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
26
|
+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
27
|
+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
28
|
+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
29
|
+
|
30
|
+
// Provides shared functions used by the internals of the parser and
|
31
|
+
// canonicalizer for file URLs. Do not use outside of these modules.
|
32
|
+
|
33
|
+
#ifndef GOOGLEURL_SRC_URL_FILE_H__
|
34
|
+
#define GOOGLEURL_SRC_URL_FILE_H__
|
35
|
+
|
36
|
+
#include "url_parse_internal.h"
|
37
|
+
|
38
|
+
namespace url_parse {
|
39
|
+
|
40
|
+
#ifdef WIN32
|
41
|
+
|
42
|
+
// We allow both "c:" and "c|" as drive identifiers.
|
43
|
+
inline bool IsWindowsDriveSeparator(char16 ch) {
|
44
|
+
return ch == ':' || ch == '|';
|
45
|
+
}
|
46
|
+
inline bool IsWindowsDriveLetter(char16 ch) {
|
47
|
+
return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z');
|
48
|
+
}
|
49
|
+
|
50
|
+
#endif // WIN32
|
51
|
+
|
52
|
+
// Returns the index of the next slash in the input after the given index, or
|
53
|
+
// spec_len if the end of the input is reached.
|
54
|
+
template<typename CHAR>
|
55
|
+
inline int FindNextSlash(const CHAR* spec, int begin_index, int spec_len) {
|
56
|
+
int idx = begin_index;
|
57
|
+
while (idx < spec_len && !IsURLSlash(spec[idx]))
|
58
|
+
idx++;
|
59
|
+
return idx;
|
60
|
+
}
|
61
|
+
|
62
|
+
#ifdef WIN32
|
63
|
+
|
64
|
+
// Returns true if the start_offset in the given spec looks like it begins a
|
65
|
+
// drive spec, for example "c:". This function explicitly handles start_offset
|
66
|
+
// values that are equal to or larger than the spec_len to simplify callers.
|
67
|
+
//
|
68
|
+
// If this returns true, the spec is guaranteed to have a valid drive letter
|
69
|
+
// plus a colon starting at |start_offset|.
|
70
|
+
template<typename CHAR>
|
71
|
+
inline bool DoesBeginWindowsDriveSpec(const CHAR* spec, int start_offset,
|
72
|
+
int spec_len) {
|
73
|
+
int remaining_len = spec_len - start_offset;
|
74
|
+
if (remaining_len < 2)
|
75
|
+
return false; // Not enough room.
|
76
|
+
if (!IsWindowsDriveLetter(spec[start_offset]))
|
77
|
+
return false; // Doesn't start with a valid drive letter.
|
78
|
+
if (!IsWindowsDriveSeparator(spec[start_offset + 1]))
|
79
|
+
return false; // Isn't followed with a drive separator.
|
80
|
+
return true;
|
81
|
+
}
|
82
|
+
|
83
|
+
// Returns true if the start_offset in the given text looks like it begins a
|
84
|
+
// UNC path, for example "\\". This function explicitly handles start_offset
|
85
|
+
// values that are equal to or larger than the spec_len to simplify callers.
|
86
|
+
//
|
87
|
+
// When strict_slashes is set, this function will only accept backslashes as is
|
88
|
+
// standard for Windows. Otherwise, it will accept forward slashes as well
|
89
|
+
// which we use for a lot of URL handling.
|
90
|
+
template<typename CHAR>
|
91
|
+
inline bool DoesBeginUNCPath(const CHAR* text,
|
92
|
+
int start_offset,
|
93
|
+
int len,
|
94
|
+
bool strict_slashes) {
|
95
|
+
int remaining_len = len - start_offset;
|
96
|
+
if (remaining_len < 2)
|
97
|
+
return false;
|
98
|
+
|
99
|
+
if (strict_slashes)
|
100
|
+
return text[start_offset] == '\\' && text[start_offset + 1] == '\\';
|
101
|
+
return IsURLSlash(text[start_offset]) && IsURLSlash(text[start_offset + 1]);
|
102
|
+
}
|
103
|
+
|
104
|
+
#endif // WIN32
|
105
|
+
|
106
|
+
} // namespace url_parse
|
107
|
+
|
108
|
+
#endif // GOOGLEURL_SRC_URL_FILE_H__
|