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