Alex Vakulenko | bda220a | 2014-04-18 15:25:44 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS 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. |
| 4 | |
| 5 | #ifndef BUFFET_URL_UTILS_H_ |
| 6 | #define BUFFET_URL_UTILS_H_ |
| 7 | |
Alex Vakulenko | bda220a | 2014-04-18 15:25:44 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 10 | |
| 11 | #include <base/basictypes.h> |
| 12 | |
Alex Vakulenko | bda220a | 2014-04-18 15:25:44 -0700 | [diff] [blame] | 13 | #include "buffet/data_encoding.h" |
| 14 | |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 15 | namespace buffet { |
Alex Vakulenko | bda220a | 2014-04-18 15:25:44 -0700 | [diff] [blame] | 16 | |
| 17 | namespace url { |
| 18 | |
| 19 | // Appends a subpath to url and delimiting then with '/' if the path doesn't |
| 20 | // end with it already. Also handles URLs with query parameters/fragment. |
| 21 | std::string Combine(const std::string& url, |
| 22 | const std::string& subpath) WARN_UNUSED_RESULT; |
| 23 | std::string CombineMultiple( |
| 24 | const std::string& url, |
| 25 | const std::vector<std::string>& parts) WARN_UNUSED_RESULT; |
| 26 | |
| 27 | // Removes the query string/fragment from |url| and returns the query string. |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 28 | // This method actually modifies |url|. So, if you call it on this: |
Alex Vakulenko | bda220a | 2014-04-18 15:25:44 -0700 | [diff] [blame] | 29 | // http://www.test.org/?foo=bar |
| 30 | // it will modify |url| to "http://www.test.org/" and return "?foo=bar" |
| 31 | std::string TrimOffQueryString(std::string* url); |
| 32 | |
| 33 | // Returns the query string, if available. |
| 34 | // For example, for the following URL: |
| 35 | // http://server.com/path/to/object?k=v&foo=bar#fragment |
| 36 | // Here: |
| 37 | // http://server.com/path/to/object - is the URL of the object, |
| 38 | // ?k=v&foo=bar - URL query string |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 39 | // #fragment - URL fragment string |
Alex Vakulenko | bda220a | 2014-04-18 15:25:44 -0700 | [diff] [blame] | 40 | // If |remove_fragment| is true, the function returns the query string without |
| 41 | // the fragment. Otherwise the fragment is included as part of the result. |
| 42 | std::string GetQueryString(const std::string& url, bool remove_fragment); |
| 43 | |
| 44 | // Parses the query string into a set of key-value pairs. |
| 45 | data_encoding::WebParamList GetQueryStringParameters(const std::string& url); |
| 46 | |
| 47 | // Returns a value of the specified query parameter, or empty string if missing. |
| 48 | std::string GetQueryStringValue(const std::string& url, |
| 49 | const std::string& name); |
| 50 | std::string GetQueryStringValue(const data_encoding::WebParamList& params, |
| 51 | const std::string& name); |
| 52 | |
| 53 | // Removes the query string and/or a fragment part from URL. |
| 54 | // If |remove_fragment| is specified, the fragment is also removed. |
| 55 | // For example: |
| 56 | // http://server.com/path/to/object?k=v&foo=bar#fragment |
| 57 | // true -> http://server.com/path/to/object |
| 58 | // false -> http://server.com/path/to/object#fragment |
| 59 | std::string RemoveQueryString(const std::string& url, |
| 60 | bool remove_fragment) WARN_UNUSED_RESULT; |
| 61 | |
| 62 | // Appends a single query parameter to the URL. |
| 63 | std::string AppendQueryParam(const std::string& url, |
| 64 | const std::string& name, |
| 65 | const std::string& value) WARN_UNUSED_RESULT; |
| 66 | // Appends a list of query parameters to the URL. |
| 67 | std::string AppendQueryParams( |
| 68 | const std::string& url, |
| 69 | const data_encoding::WebParamList& params) WARN_UNUSED_RESULT; |
| 70 | |
| 71 | // Checks if the URL has query parameters. |
| 72 | bool HasQueryString(const std::string& url); |
| 73 | |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 74 | } // namespace url |
| 75 | } // namespace buffet |
Alex Vakulenko | bda220a | 2014-04-18 15:25:44 -0700 | [diff] [blame] | 76 | |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 77 | #endif // BUFFET_URL_UTILS_H_ |