• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef GOOGLEURL_SRC_URL_CANON_H__
30 #define GOOGLEURL_SRC_URL_CANON_H__
31 
32 #include <memory.h>
33 #include <stdlib.h>
34 
35 #include "base/string16.h"
36 #include "googleurl/src/url_parse.h"
37 
38 namespace url_canon {
39 
40 // Canonicalizer output -------------------------------------------------------
41 
42 // Base class for the canonicalizer output, this maintains a buffer and
43 // supports simple resizing and append operations on it.
44 //
45 // It is VERY IMPORTANT that no virtual function calls be made on the common
46 // code path. We only have two virtual function calls, the destructor and a
47 // resize function that is called when the existing buffer is not big enough.
48 // The derived class is then in charge of setting up our buffer which we will
49 // manage.
50 template<typename T>
51 class CanonOutputT {
52  public:
CanonOutputT()53   CanonOutputT() : buffer_(NULL), buffer_len_(0), cur_len_(0) {
54   }
~CanonOutputT()55   virtual ~CanonOutputT() {
56   }
57 
58   // Implemented to resize the buffer. This function should update the buffer
59   // pointer to point to the new buffer, and any old data up to |cur_len_| in
60   // the buffer must be copied over.
61   //
62   // The new size |sz| must be larger than buffer_len_.
63   virtual void Resize(int sz) = 0;
64 
65   // Accessor for returning a character at a given position. The input offset
66   // must be in the valid range.
at(int offset)67   inline char at(int offset) const {
68     return buffer_[offset];
69   }
70 
71   // Sets the character at the given position. The given position MUST be less
72   // than the length().
set(int offset,int ch)73   inline void set(int offset, int ch) {
74     buffer_[offset] = ch;
75   }
76 
77   // Returns the number of characters currently in the buffer.
length()78   inline int length() const {
79     return cur_len_;
80   }
81 
82   // Returns the current capacity of the buffer. The length() is the number of
83   // characters that have been declared to be written, but the capacity() is
84   // the number that can be written without reallocation. If the caller must
85   // write many characters at once, it can make sure there is enough capacity,
86   // write the data, then use set_size() to declare the new length().
capacity()87   int capacity() const {
88     return buffer_len_;
89   }
90 
91   // Called by the user of this class to get the output. The output will NOT
92   // be NULL-terminated. Call length() to get the
93   // length.
data()94   const T* data() const {
95     return buffer_;
96   }
data()97   T* data() {
98     return buffer_;
99   }
100 
101   // Shortens the URL to the new length. Used for "backing up" when processing
102   // relative paths. This can also be used if an external function writes a lot
103   // of data to the buffer (when using the "Raw" version below) beyond the end,
104   // to declare the new length.
105   //
106   // This MUST NOT be used to expand the size of the buffer beyond capacity().
set_length(int new_len)107   void set_length(int new_len) {
108     cur_len_ = new_len;
109   }
110 
111   // This is the most performance critical function, since it is called for
112   // every character.
push_back(T ch)113   void push_back(T ch) {
114     // In VC2005, putting this common case first speeds up execution
115     // dramatically because this branch is predicted as taken.
116     if (cur_len_ < buffer_len_) {
117       buffer_[cur_len_] = ch;
118       cur_len_++;
119       return;
120     }
121 
122     // Grow the buffer to hold at least one more item. Hopefully we won't have
123     // to do this very often.
124     if (!Grow(1))
125       return;
126 
127     // Actually do the insertion.
128     buffer_[cur_len_] = ch;
129     cur_len_++;
130   }
131 
132   // Appends the given string to the output.
Append(const T * str,int str_len)133   void Append(const T* str, int str_len) {
134     if (cur_len_ + str_len > buffer_len_) {
135       if (!Grow(cur_len_ + str_len - buffer_len_))
136         return;
137     }
138     for (int i = 0; i < str_len; i++)
139       buffer_[cur_len_ + i] = str[i];
140     cur_len_ += str_len;
141   }
142 
143  protected:
144   // Grows the given buffer so that it can fit at least |min_additional|
145   // characters. Returns true if the buffer could be resized, false on OOM.
Grow(int min_additional)146   bool Grow(int min_additional) {
147     static const int kMinBufferLen = 16;
148     int new_len = (buffer_len_ == 0) ? kMinBufferLen : buffer_len_;
149     do {
150       if (new_len >= (1 << 30))  // Prevent overflow below.
151         return false;
152       new_len *= 2;
153     } while (new_len < buffer_len_ + min_additional);
154     Resize(new_len);
155     return true;
156   }
157 
158   T* buffer_;
159   int buffer_len_;
160 
161   // Used characters in the buffer.
162   int cur_len_;
163 };
164 
165 // Simple implementation of the CanonOutput using new[]. This class
166 // also supports a static buffer so if it is allocated on the stack, most
167 // URLs can be canonicalized with no heap allocations.
168 template<typename T, int fixed_capacity = 1024>
169 class RawCanonOutputT : public CanonOutputT<T> {
170  public:
RawCanonOutputT()171   RawCanonOutputT() : CanonOutputT<T>() {
172     this->buffer_ = fixed_buffer_;
173     this->buffer_len_ = fixed_capacity;
174   }
~RawCanonOutputT()175   virtual ~RawCanonOutputT() {
176     if (this->buffer_ != fixed_buffer_)
177       delete[] this->buffer_;
178   }
179 
Resize(int sz)180   virtual void Resize(int sz) {
181     T* new_buf = new T[sz];
182     memcpy(new_buf, this->buffer_,
183            sizeof(T) * (this->cur_len_ < sz ? this->cur_len_ : sz));
184     if (this->buffer_ != fixed_buffer_)
185       delete[] this->buffer_;
186     this->buffer_ = new_buf;
187     this->buffer_len_ = sz;
188   }
189 
190  protected:
191   T fixed_buffer_[fixed_capacity];
192 };
193 
194 // Normally, all canonicalization output is in narrow characters. We support
195 // the templates so it can also be used internally if a wide buffer is
196 // required.
197 typedef CanonOutputT<char> CanonOutput;
198 typedef CanonOutputT<char16> CanonOutputW;
199 
200 template<int fixed_capacity>
201 class RawCanonOutput : public RawCanonOutputT<char, fixed_capacity> {};
202 template<int fixed_capacity>
203 class RawCanonOutputW : public RawCanonOutputT<char16, fixed_capacity> {};
204 
205 // Character set converter ----------------------------------------------------
206 //
207 // Converts query strings into a custom encoding. The embedder can supply an
208 // implementation of this class to interface with their own character set
209 // conversion libraries.
210 //
211 // Embedders will want to see the unit test for the ICU version.
212 
213 class CharsetConverter {
214  public:
CharsetConverter()215   CharsetConverter() {}
~CharsetConverter()216   virtual ~CharsetConverter() {}
217 
218   // Converts the given input string from UTF-16 to whatever output format the
219   // converter supports. This is used only for the query encoding conversion,
220   // which does not fail. Instead, the converter should insert "invalid
221   // character" characters in the output for invalid sequences, and do the
222   // best it can.
223   //
224   // If the input contains a character not representable in the output
225   // character set, the converter should append the HTML entity sequence in
226   // decimal, (such as "&#20320;") with escaping of the ampersand, number
227   // sign, and semicolon (in the previous example it would be
228   // "%26%2320320%3B"). This rule is based on what IE does in this situation.
229   virtual void ConvertFromUTF16(const char16* input,
230                                 int input_len,
231                                 CanonOutput* output) = 0;
232 };
233 
234 // Whitespace -----------------------------------------------------------------
235 
236 // Searches for whitespace that should be removed from the middle of URLs, and
237 // removes it. Removed whitespace are tabs and newlines, but NOT spaces. Spaces
238 // are preserved, which is what most browsers do. A pointer to the output will
239 // be returned, and the length of that output will be in |output_len|.
240 //
241 // This should be called before parsing if whitespace removal is desired (which
242 // it normally is when you are canonicalizing).
243 //
244 // If no whitespace is removed, this function will not use the buffer and will
245 // return a pointer to the input, to avoid the extra copy. If modification is
246 // required, the given |buffer| will be used and the returned pointer will
247 // point to the beginning of the buffer.
248 //
249 // Therefore, callers should not use the buffer, since it may actuall be empty,
250 // use the computed pointer and |*output_len| instead.
251 const char* RemoveURLWhitespace(const char* input, int input_len,
252                                 CanonOutputT<char>* buffer,
253                                 int* output_len);
254 const char16* RemoveURLWhitespace(const char16* input, int input_len,
255                                   CanonOutputT<char16>* buffer,
256                                   int* output_len);
257 
258 // IDN ------------------------------------------------------------------------
259 
260 // Converts the Unicode input representing a hostname to ASCII using IDN rules.
261 // The output must fall in the ASCII range, but will be encoded in UTF-16.
262 //
263 // On success, the output will be filled with the ASCII host name and it will
264 // return true. Unlike most other canonicalization functions, this assumes that
265 // the output is empty. The beginning of the host will be at offset 0, and
266 // the length of the output will be set to the length of the new host name.
267 //
268 // On error, returns false. The output in this case is undefined.
269 bool IDNToASCII(const char16* src, int src_len, CanonOutputW* output);
270 
271 // Piece-by-piece canonicalizers ----------------------------------------------
272 //
273 // These individual canonicalizers append the canonicalized versions of the
274 // corresponding URL component to the given std::string. The spec and the
275 // previously-identified range of that component are the input. The range of
276 // the canonicalized component will be written to the output component.
277 //
278 // These functions all append to the output so they can be chained. Make sure
279 // the output is empty when you start.
280 //
281 // These functions returns boolean values indicating success. On failure, they
282 // will attempt to write something reasonable to the output so that, if
283 // displayed to the user, they will recognise it as something that's messed up.
284 // Nothing more should ever be done with these invalid URLs, however.
285 
286 // Scheme: Appends the scheme and colon to the URL. The output component will
287 // indicate the range of characters up to but not including the colon.
288 //
289 // Canonical URLs always have a scheme. If the scheme is not present in the
290 // input, this will just write the colon to indicate an empty scheme. Does not
291 // append slashes which will be needed before any authority components for most
292 // URLs.
293 //
294 // The 8-bit version requires UTF-8 encoding.
295 bool CanonicalizeScheme(const char* spec,
296                         const url_parse::Component& scheme,
297                         CanonOutput* output,
298                         url_parse::Component* out_scheme);
299 bool CanonicalizeScheme(const char16* spec,
300                         const url_parse::Component& scheme,
301                         CanonOutput* output,
302                         url_parse::Component* out_scheme);
303 
304 // User info: username/password. If present, this will add the delimiters so
305 // the output will be "<username>:<password>@" or "<username>@". Empty
306 // username/password pairs, or empty passwords, will get converted to
307 // nonexistant in the canonical version.
308 //
309 // The components for the username and password refer to ranges in the
310 // respective source strings. Usually, these will be the same string, which
311 // is legal as long as the two components don't overlap.
312 //
313 // The 8-bit version requires UTF-8 encoding.
314 bool CanonicalizeUserInfo(const char* username_source,
315                           const url_parse::Component& username,
316                           const char* password_source,
317                           const url_parse::Component& password,
318                           CanonOutput* output,
319                           url_parse::Component* out_username,
320                           url_parse::Component* out_password);
321 bool CanonicalizeUserInfo(const char16* username_source,
322                           const url_parse::Component& username,
323                           const char16* password_source,
324                           const url_parse::Component& password,
325                           CanonOutput* output,
326                           url_parse::Component* out_username,
327                           url_parse::Component* out_password);
328 
329 
330 // This structure holds detailed state exported from the IP/Host canonicalizers.
331 // Additional fields may be added as callers require them.
332 struct CanonHostInfo {
CanonHostInfoCanonHostInfo333   CanonHostInfo() : family(NEUTRAL), num_ipv4_components(0), out_host() {}
334 
335   // Convenience function to test if family is an IP address.
IsIPAddressCanonHostInfo336   bool IsIPAddress() const { return family == IPV4 || family == IPV6; }
337 
338   // This field summarizes how the input was classified by the canonicalizer.
339   enum Family {
340     NEUTRAL,   // - Doesn't resemble an IP address.  As far as the IP
341                //   canonicalizer is concerned, it should be treated as a
342                //   hostname.
343     BROKEN,    // - Almost an IP, but was not canonicalized.  This could be an
344                //   IPv4 address where truncation occurred, or something
345                //   containing the special characters :[] which did not parse
346                //   as an IPv6 address.  Never attempt to connect to this
347                //   address, because it might actually succeed!
348     IPV4,      // - Successfully canonicalized as an IPv4 address.
349     IPV6,      // - Successfully canonicalized as an IPv6 address.
350   };
351   Family family;
352 
353   // If |family| is IPV4, then this is the number of nonempty dot-separated
354   // components in the input text, from 1 to 4.  If |family| is not IPV4,
355   // this value is undefined.
356   int num_ipv4_components;
357 
358   // Location of host within the canonicalized output.
359   // CanonicalizeIPAddress() only sets this field if |family| is IPV4 or IPV6.
360   // CanonicalizeHostVerbose() always sets it.
361   url_parse::Component out_host;
362 };
363 
364 
365 // Host.
366 //
367 // The 8-bit version requires UTF-8 encoding.  Use this version when you only
368 // need to know whether canonicalization succeeded.
369 bool CanonicalizeHost(const char* spec,
370                       const url_parse::Component& host,
371                       CanonOutput* output,
372                       url_parse::Component* out_host);
373 bool CanonicalizeHost(const char16* spec,
374                       const url_parse::Component& host,
375                       CanonOutput* output,
376                       url_parse::Component* out_host);
377 
378 // Extended version of CanonicalizeHost, which returns additional information.
379 // Use this when you need to know whether the hostname was an IP address.
380 // A successful return is indicated by host_info->family != BROKEN.  See the
381 // definition of CanonHostInfo above for details.
382 void CanonicalizeHostVerbose(const char* spec,
383                              const url_parse::Component& host,
384                              CanonOutput* output,
385                              CanonHostInfo* host_info);
386 void CanonicalizeHostVerbose(const char16* spec,
387                              const url_parse::Component& host,
388                              CanonOutput* output,
389                              CanonHostInfo* host_info);
390 
391 
392 // IP addresses.
393 //
394 // Tries to interpret the given host name as an IPv4 or IPv6 address. If it is
395 // an IP address, it will canonicalize it as such, appending it to |output|.
396 // Additional status information is returned via the |*host_info| parameter.
397 // See the definition of CanonHostInfo above for details.
398 //
399 // This is called AUTOMATICALLY from the host canonicalizer, which ensures that
400 // the input is unescaped and name-prepped, etc. It should not normally be
401 // necessary or wise to call this directly.
402 void CanonicalizeIPAddress(const char* spec,
403                            const url_parse::Component& host,
404                            CanonOutput* output,
405                            CanonHostInfo* host_info);
406 void CanonicalizeIPAddress(const char16* spec,
407                            const url_parse::Component& host,
408                            CanonOutput* output,
409                            CanonHostInfo* host_info);
410 
411 // Port: this function will add the colon for the port if a port is present.
412 // The caller can pass url_parse::PORT_UNSPECIFIED as the
413 // default_port_for_scheme argument if there is no default port.
414 //
415 // The 8-bit version requires UTF-8 encoding.
416 bool CanonicalizePort(const char* spec,
417                       const url_parse::Component& port,
418                       int default_port_for_scheme,
419                       CanonOutput* output,
420                       url_parse::Component* out_port);
421 bool CanonicalizePort(const char16* spec,
422                       const url_parse::Component& port,
423                       int default_port_for_scheme,
424                       CanonOutput* output,
425                       url_parse::Component* out_port);
426 
427 // Returns the default port for the given canonical scheme, or PORT_UNSPECIFIED
428 // if the scheme is unknown.
429 int DefaultPortForScheme(const char* scheme, int scheme_len);
430 
431 // Path. If the input does not begin in a slash (including if the input is
432 // empty), we'll prepend a slash to the path to make it canonical.
433 //
434 // The 8-bit version assumes UTF-8 encoding, but does not verify the validity
435 // of the UTF-8 (i.e., you can have invalid UTF-8 sequences, invalid
436 // characters, etc.). Normally, URLs will come in as UTF-16, so this isn't
437 // an issue. Somebody giving us an 8-bit path is responsible for generating
438 // the path that the server expects (we'll escape high-bit characters), so
439 // if something is invalid, it's their problem.
440 bool CanonicalizePath(const char* spec,
441                       const url_parse::Component& path,
442                       CanonOutput* output,
443                       url_parse::Component* out_path);
444 bool CanonicalizePath(const char16* spec,
445                       const url_parse::Component& path,
446                       CanonOutput* output,
447                       url_parse::Component* out_path);
448 
449 // Canonicalizes the input as a file path. This is like CanonicalizePath except
450 // that it also handles Windows drive specs. For example, the path can begin
451 // with "c|\" and it will get properly canonicalized to "C:/".
452 // The string will be appended to |*output| and |*out_path| will be updated.
453 //
454 // The 8-bit version requires UTF-8 encoding.
455 bool FileCanonicalizePath(const char* spec,
456                           const url_parse::Component& path,
457                           CanonOutput* output,
458                           url_parse::Component* out_path);
459 bool FileCanonicalizePath(const char16* spec,
460                           const url_parse::Component& path,
461                           CanonOutput* output,
462                           url_parse::Component* out_path);
463 
464 // Query: Prepends the ? if needed.
465 //
466 // The 8-bit version requires the input to be UTF-8 encoding. Incorrectly
467 // encoded characters (in UTF-8 or UTF-16) will be replaced with the Unicode
468 // "invalid character." This function can not fail, we always just try to do
469 // our best for crazy input here since web pages can set it themselves.
470 //
471 // This will convert the given input into the output encoding that the given
472 // character set converter object provides. The converter will only be called
473 // if necessary, for ASCII input, no conversions are necessary.
474 //
475 // The converter can be NULL. In this case, the output encoding will be UTF-8.
476 void CanonicalizeQuery(const char* spec,
477                        const url_parse::Component& query,
478                        CharsetConverter* converter,
479                        CanonOutput* output,
480                        url_parse::Component* out_query);
481 void CanonicalizeQuery(const char16* spec,
482                        const url_parse::Component& query,
483                        CharsetConverter* converter,
484                        CanonOutput* output,
485                        url_parse::Component* out_query);
486 
487 // Ref: Prepends the # if needed. The output will be UTF-8 (this is the only
488 // canonicalizer that does not produce ASCII output). The output is
489 // guaranteed to be valid UTF-8.
490 //
491 // This function will not fail. If the input is invalid UTF-8/UTF-16, we'll use
492 // the "Unicode replacement character" for the confusing bits and copy the rest.
493 void CanonicalizeRef(const char* spec,
494                      const url_parse::Component& path,
495                      CanonOutput* output,
496                      url_parse::Component* out_path);
497 void CanonicalizeRef(const char16* spec,
498                      const url_parse::Component& path,
499                      CanonOutput* output,
500                      url_parse::Component* out_path);
501 
502 // Full canonicalizer ---------------------------------------------------------
503 //
504 // These functions replace any string contents, rather than append as above.
505 // See the above piece-by-piece functions for information specific to
506 // canonicalizing individual components.
507 //
508 // The output will be ASCII except the reference fragment, which may be UTF-8.
509 //
510 // The 8-bit versions require UTF-8 encoding.
511 
512 // Use for standard URLs with authorities and paths.
513 bool CanonicalizeStandardURL(const char* spec,
514                              int spec_len,
515                              const url_parse::Parsed& parsed,
516                              CharsetConverter* query_converter,
517                              CanonOutput* output,
518                              url_parse::Parsed* new_parsed);
519 bool CanonicalizeStandardURL(const char16* spec,
520                              int spec_len,
521                              const url_parse::Parsed& parsed,
522                              CharsetConverter* query_converter,
523                              CanonOutput* output,
524                              url_parse::Parsed* new_parsed);
525 
526 // Use for file URLs.
527 bool CanonicalizeFileURL(const char* spec,
528                          int spec_len,
529                          const url_parse::Parsed& parsed,
530                          CharsetConverter* query_converter,
531                          CanonOutput* output,
532                          url_parse::Parsed* new_parsed);
533 bool CanonicalizeFileURL(const char16* spec,
534                          int spec_len,
535                          const url_parse::Parsed& parsed,
536                          CharsetConverter* query_converter,
537                          CanonOutput* output,
538                          url_parse::Parsed* new_parsed);
539 
540 // Use for path URLs such as javascript. This does not modify the path in any
541 // way, for example, by escaping it.
542 bool CanonicalizePathURL(const char* spec,
543                          int spec_len,
544                          const url_parse::Parsed& parsed,
545                          CanonOutput* output,
546                          url_parse::Parsed* new_parsed);
547 bool CanonicalizePathURL(const char16* spec,
548                          int spec_len,
549                          const url_parse::Parsed& parsed,
550                          CanonOutput* output,
551                          url_parse::Parsed* new_parsed);
552 
553 // Use for mailto URLs. This "canonicalizes" the url into a path and query
554 // component. It does not attempt to merge "to" fields. It uses UTF-8 for
555 // the query encoding if there is a query. This is because a mailto URL is
556 // really intended for an external mail program, and the encoding of a page,
557 // etc. which would influence a query encoding normally are irrelevant.
558 bool CanonicalizeMailtoURL(const char* spec,
559                            int spec_len,
560                            const url_parse::Parsed& parsed,
561                            CanonOutput* output,
562                            url_parse::Parsed* new_parsed);
563 bool CanonicalizeMailtoURL(const char16* spec,
564                            int spec_len,
565                            const url_parse::Parsed& parsed,
566                            CanonOutput* output,
567                            url_parse::Parsed* new_parsed);
568 
569 // Part replacer --------------------------------------------------------------
570 
571 // Internal structure used for storing separate strings for each component.
572 // The basic canonicalization functions use this structure internally so that
573 // component remplacement (different strings for different components) can be
574 // treated on the same code path as regular canonicalization (the same string
575 // for each component).
576 //
577 // A url_parse::Parsed structure usually goes along with this. Those
578 // components identify offsets within these strings, so that they can all be
579 // in the same string, or spread arbitrarily across different ones.
580 //
581 // This structures does not own any data. It is the caller's responsibility to
582 // ensure that the data the pointers point to stays in scope and is not
583 // modified.
584 template<typename CHAR>
585 struct URLComponentSource {
586   // Constructor normally used by callers wishing to replace components. This
587   // will make them all NULL, which is no replacement. The caller would then
588   // override the compoents they want to replace.
URLComponentSourceURLComponentSource589   URLComponentSource()
590       : scheme(NULL),
591         username(NULL),
592         password(NULL),
593         host(NULL),
594         port(NULL),
595         path(NULL),
596         query(NULL),
597         ref(NULL) {
598   }
599 
600   // Constructor normally used internally to initialize all the components to
601   // point to the same spec.
URLComponentSourceURLComponentSource602   explicit URLComponentSource(const CHAR* default_value)
603       : scheme(default_value),
604         username(default_value),
605         password(default_value),
606         host(default_value),
607         port(default_value),
608         path(default_value),
609         query(default_value),
610         ref(default_value) {
611   }
612 
613   const CHAR* scheme;
614   const CHAR* username;
615   const CHAR* password;
616   const CHAR* host;
617   const CHAR* port;
618   const CHAR* path;
619   const CHAR* query;
620   const CHAR* ref;
621 };
622 
623 // This structure encapsulates information on modifying a URL. Each component
624 // may either be left unchanged, replaced, or deleted.
625 //
626 // By default, each component is unchanged. For those components that should be
627 // modified, call either Set* or Clear* to modify it.
628 //
629 // The string passed to Set* functions DOES NOT GET COPIED AND MUST BE KEPT
630 // IN SCOPE BY THE CALLER for as long as this object exists!
631 //
632 // Prefer the 8-bit replacement version if possible since it is more efficient.
633 template<typename CHAR>
634 class Replacements {
635  public:
Replacements()636   Replacements() {
637   }
638 
639   // Scheme
SetScheme(const CHAR * s,const url_parse::Component & comp)640   void SetScheme(const CHAR* s, const url_parse::Component& comp) {
641     sources_.scheme = s;
642     components_.scheme = comp;
643   }
644   // Note: we don't have a ClearScheme since this doesn't make any sense.
IsSchemeOverridden()645   bool IsSchemeOverridden() const { return sources_.scheme != NULL; }
646 
647   // Username
SetUsername(const CHAR * s,const url_parse::Component & comp)648   void SetUsername(const CHAR* s, const url_parse::Component& comp) {
649     sources_.username = s;
650     components_.username = comp;
651   }
ClearUsername()652   void ClearUsername() {
653     sources_.username = Placeholder();
654     components_.username = url_parse::Component();
655   }
IsUsernameOverridden()656   bool IsUsernameOverridden() const { return sources_.username != NULL; }
657 
658   // Password
SetPassword(const CHAR * s,const url_parse::Component & comp)659   void SetPassword(const CHAR* s, const url_parse::Component& comp) {
660     sources_.password = s;
661     components_.password = comp;
662   }
ClearPassword()663   void ClearPassword() {
664     sources_.password = Placeholder();
665     components_.password = url_parse::Component();
666   }
IsPasswordOverridden()667   bool IsPasswordOverridden() const { return sources_.password != NULL; }
668 
669   // Host
SetHost(const CHAR * s,const url_parse::Component & comp)670   void SetHost(const CHAR* s, const url_parse::Component& comp) {
671     sources_.host = s;
672     components_.host = comp;
673   }
ClearHost()674   void ClearHost() {
675     sources_.host = Placeholder();
676     components_.host = url_parse::Component();
677   }
IsHostOverridden()678   bool IsHostOverridden() const { return sources_.host != NULL; }
679 
680   // Port
SetPort(const CHAR * s,const url_parse::Component & comp)681   void SetPort(const CHAR* s, const url_parse::Component& comp) {
682     sources_.port = s;
683     components_.port = comp;
684   }
ClearPort()685   void ClearPort() {
686     sources_.port = Placeholder();
687     components_.port = url_parse::Component();
688   }
IsPortOverridden()689   bool IsPortOverridden() const { return sources_.port != NULL; }
690 
691   // Path
SetPath(const CHAR * s,const url_parse::Component & comp)692   void SetPath(const CHAR* s, const url_parse::Component& comp) {
693     sources_.path = s;
694     components_.path = comp;
695   }
ClearPath()696   void ClearPath() {
697     sources_.path = Placeholder();
698     components_.path = url_parse::Component();
699   }
IsPathOverridden()700   bool IsPathOverridden() const { return sources_.path != NULL; }
701 
702   // Query
SetQuery(const CHAR * s,const url_parse::Component & comp)703   void SetQuery(const CHAR* s, const url_parse::Component& comp) {
704     sources_.query = s;
705     components_.query = comp;
706   }
ClearQuery()707   void ClearQuery() {
708     sources_.query = Placeholder();
709     components_.query = url_parse::Component();
710   }
IsQueryOverridden()711   bool IsQueryOverridden() const { return sources_.query != NULL; }
712 
713   // Ref
SetRef(const CHAR * s,const url_parse::Component & comp)714   void SetRef(const CHAR* s, const url_parse::Component& comp) {
715     sources_.ref = s;
716     components_.ref = comp;
717   }
ClearRef()718   void ClearRef() {
719     sources_.ref = Placeholder();
720     components_.ref = url_parse::Component();
721   }
IsRefOverridden()722   bool IsRefOverridden() const { return sources_.ref != NULL; }
723 
724   // Getters for the itnernal data. See the variables below for how the
725   // information is encoded.
sources()726   const URLComponentSource<CHAR>& sources() const { return sources_; }
components()727   const url_parse::Parsed& components() const { return components_; }
728 
729  private:
730   // Returns a pointer to a static empty string that is used as a placeholder
731   // to indicate a component should be deleted (see below).
Placeholder()732   const CHAR* Placeholder() {
733     static const CHAR empty_string = 0;
734     return &empty_string;
735   }
736 
737   // We support three states:
738   //
739   // Action                 | Source                Component
740   // -----------------------+--------------------------------------------------
741   // Don't change component | NULL                  (unused)
742   // Replace component      | (replacement string)  (replacement component)
743   // Delete component       | (non-NULL)            (invalid component: (0,-1))
744   //
745   // We use a pointer to the empty string for the source when the component
746   // should be deleted.
747   URLComponentSource<CHAR> sources_;
748   url_parse::Parsed components_;
749 };
750 
751 // The base must be an 8-bit canonical URL.
752 bool ReplaceStandardURL(const char* base,
753                         const url_parse::Parsed& base_parsed,
754                         const Replacements<char>& replacements,
755                         CharsetConverter* query_converter,
756                         CanonOutput* output,
757                         url_parse::Parsed* new_parsed);
758 bool ReplaceStandardURL(const char* base,
759                         const url_parse::Parsed& base_parsed,
760                         const Replacements<char16>& replacements,
761                         CharsetConverter* query_converter,
762                         CanonOutput* output,
763                         url_parse::Parsed* new_parsed);
764 
765 // Replacing some parts of a file URL is not permitted. Everything except
766 // the host, path, query, and ref will be ignored.
767 bool ReplaceFileURL(const char* base,
768                     const url_parse::Parsed& base_parsed,
769                     const Replacements<char>& replacements,
770                     CharsetConverter* query_converter,
771                     CanonOutput* output,
772                     url_parse::Parsed* new_parsed);
773 bool ReplaceFileURL(const char* base,
774                     const url_parse::Parsed& base_parsed,
775                     const Replacements<char16>& replacements,
776                     CharsetConverter* query_converter,
777                     CanonOutput* output,
778                     url_parse::Parsed* new_parsed);
779 
780 // Path URLs can only have the scheme and path replaced. All other components
781 // will be ignored.
782 bool ReplacePathURL(const char* base,
783                     const url_parse::Parsed& base_parsed,
784                     const Replacements<char>& replacements,
785                     CanonOutput* output,
786                     url_parse::Parsed* new_parsed);
787 bool ReplacePathURL(const char* base,
788                     const url_parse::Parsed& base_parsed,
789                     const Replacements<char16>& replacements,
790                     CanonOutput* output,
791                     url_parse::Parsed* new_parsed);
792 
793 // Mailto URLs can only have the scheme, path, and query replaced.
794 // All other components will be ignored.
795 bool ReplaceMailtoURL(const char* base,
796                       const url_parse::Parsed& base_parsed,
797                       const Replacements<char>& replacements,
798                       CanonOutput* output,
799                       url_parse::Parsed* new_parsed);
800 bool ReplaceMailtoURL(const char* base,
801                       const url_parse::Parsed& base_parsed,
802                       const Replacements<char16>& replacements,
803                       CanonOutput* output,
804                       url_parse::Parsed* new_parsed);
805 
806 // Relative URL ---------------------------------------------------------------
807 
808 // Given an input URL or URL fragment |fragment|, determines if it is a
809 // relative or absolute URL and places the result into |*is_relative|. If it is
810 // relative, the relevant portion of the URL will be placed into
811 // |*relative_component| (there may have been trimmed whitespace, for example).
812 // This value is passed to ResolveRelativeURL. If the input is not relative,
813 // this value is UNDEFINED (it may be changed by the functin).
814 //
815 // Returns true on success (we successfully determined the URL is relative or
816 // not). Failure means that the combination of URLs doesn't make any sense.
817 //
818 // The base URL should always be canonical, therefore is ASCII.
819 bool IsRelativeURL(const char* base,
820                    const url_parse::Parsed& base_parsed,
821                    const char* fragment,
822                    int fragment_len,
823                    bool is_base_hierarchical,
824                    bool* is_relative,
825                    url_parse::Component* relative_component);
826 bool IsRelativeURL(const char* base,
827                    const url_parse::Parsed& base_parsed,
828                    const char16* fragment,
829                    int fragment_len,
830                    bool is_base_hierarchical,
831                    bool* is_relative,
832                    url_parse::Component* relative_component);
833 
834 // Given a canonical parsed source URL, a URL fragment known to be relative,
835 // and the identified relevant portion of the relative URL (computed by
836 // IsRelativeURL), this produces a new parsed canonical URL in |output| and
837 // |out_parsed|.
838 //
839 // It also requires a flag indicating whether the base URL is a file: URL
840 // which triggers additional logic.
841 //
842 // The base URL should be canonical and have a host (may be empty for file
843 // URLs) and a path. If it doesn't have these, we can't resolve relative
844 // URLs off of it and will return the base as the output with an error flag.
845 // Becausee it is canonical is should also be ASCII.
846 //
847 // The query charset converter follows the same rules as CanonicalizeQuery.
848 //
849 // Returns true on success. On failure, the output will be "something
850 // reasonable" that will be consistent and valid, just probably not what
851 // was intended by the web page author or caller.
852 bool ResolveRelativeURL(const char* base_url,
853                         const url_parse::Parsed& base_parsed,
854                         bool base_is_file,
855                         const char* relative_url,
856                         const url_parse::Component& relative_component,
857                         CharsetConverter* query_converter,
858                         CanonOutput* output,
859                         url_parse::Parsed* out_parsed);
860 bool ResolveRelativeURL(const char* base_url,
861                         const url_parse::Parsed& base_parsed,
862                         bool base_is_file,
863                         const char16* relative_url,
864                         const url_parse::Component& relative_component,
865                         CharsetConverter* query_converter,
866                         CanonOutput* output,
867                         url_parse::Parsed* out_parsed);
868 
869 }  // namespace url_canon
870 
871 #endif  // GOOGLEURL_SRC_URL_CANON_H__
872