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