blob: fd66dd6741f09c31dcb51ad3b490afeacfbfe701 [file] [log] [blame]
Alex Vakulenkoa629bed2014-08-20 16:16:34 -07001// 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 Vakulenkofed60b02015-10-27 09:53:05 -07005#ifndef LIBBRILLO_BRILLO_URL_UTILS_H_
6#define LIBBRILLO_BRILLO_URL_UTILS_H_
Alex Vakulenkoa629bed2014-08-20 16:16:34 -07007
8#include <string>
9#include <vector>
10
Alex Vakulenko7fb5e542014-12-10 12:52:31 -080011#include <base/compiler_specific.h>
Alex Vakulenkof2418e52014-09-04 08:26:42 -070012#include <base/macros.h>
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070013#include <brillo/brillo_export.h>
14#include <brillo/data_encoding.h>
Alex Vakulenkoa629bed2014-08-20 16:16:34 -070015
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070016namespace brillo {
Alex Vakulenkoa629bed2014-08-20 16:16:34 -070017
18namespace 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 Vakulenko9ed0cab2015-10-12 15:21:28 -070022BRILLO_EXPORT std::string Combine(
Alex Vakulenko847b8712014-08-29 10:43:06 -070023 const std::string& url,
24 const std::string& subpath) WARN_UNUSED_RESULT;
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070025BRILLO_EXPORT std::string CombineMultiple(
Alex Vakulenkoa629bed2014-08-20 16:16:34 -070026 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 Vakulenko9ed0cab2015-10-12 15:21:28 -070033BRILLO_EXPORT std::string TrimOffQueryString(std::string* url);
Alex Vakulenkoa629bed2014-08-20 16:16:34 -070034
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 Vakulenko9ed0cab2015-10-12 15:21:28 -070044BRILLO_EXPORT std::string GetQueryString(const std::string& url,
45 bool remove_fragment);
Alex Vakulenkoa629bed2014-08-20 16:16:34 -070046
47// Parses the query string into a set of key-value pairs.
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070048BRILLO_EXPORT data_encoding::WebParamList GetQueryStringParameters(
Alex Vakulenko847b8712014-08-29 10:43:06 -070049 const std::string& url);
Alex Vakulenkoa629bed2014-08-20 16:16:34 -070050
51// Returns a value of the specified query parameter, or empty string if missing.
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070052BRILLO_EXPORT std::string GetQueryStringValue(
Alex Vakulenko847b8712014-08-29 10:43:06 -070053 const std::string& url,
54 const std::string& name);
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070055BRILLO_EXPORT std::string GetQueryStringValue(
Alex Vakulenko847b8712014-08-29 10:43:06 -070056 const data_encoding::WebParamList& params,
57 const std::string& name);
Alex Vakulenkoa629bed2014-08-20 16:16:34 -070058
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 Vakulenko9ed0cab2015-10-12 15:21:28 -070065BRILLO_EXPORT std::string RemoveQueryString(
Alex Vakulenko847b8712014-08-29 10:43:06 -070066 const std::string& url,
67 bool remove_fragment) WARN_UNUSED_RESULT;
Alex Vakulenkoa629bed2014-08-20 16:16:34 -070068
69// Appends a single query parameter to the URL.
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070070BRILLO_EXPORT std::string AppendQueryParam(
Alex Vakulenko847b8712014-08-29 10:43:06 -070071 const std::string& url,
72 const std::string& name,
73 const std::string& value) WARN_UNUSED_RESULT;
Alex Vakulenkoa629bed2014-08-20 16:16:34 -070074// Appends a list of query parameters to the URL.
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070075BRILLO_EXPORT std::string AppendQueryParams(
Alex Vakulenkoa629bed2014-08-20 16:16:34 -070076 const std::string& url,
77 const data_encoding::WebParamList& params) WARN_UNUSED_RESULT;
78
79// Checks if the URL has query parameters.
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070080BRILLO_EXPORT bool HasQueryString(const std::string& url);
Alex Vakulenkoa629bed2014-08-20 16:16:34 -070081
82} // namespace url
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070083} // namespace brillo
Alex Vakulenkoa629bed2014-08-20 16:16:34 -070084
Alex Vakulenkofed60b02015-10-27 09:53:05 -070085#endif // LIBBRILLO_BRILLO_URL_UTILS_H_