xref: /aosp_15_r20/external/libbrillo/brillo/url_utils.h (revision 1a96fba65179ea7d3f56207137718607415c5953)
1*1a96fba6SXin Li // Copyright 2014 The Chromium OS Authors. All rights reserved.
2*1a96fba6SXin Li // Use of this source code is governed by a BSD-style license that can be
3*1a96fba6SXin Li // found in the LICENSE file.
4*1a96fba6SXin Li 
5*1a96fba6SXin Li #ifndef LIBBRILLO_BRILLO_URL_UTILS_H_
6*1a96fba6SXin Li #define LIBBRILLO_BRILLO_URL_UTILS_H_
7*1a96fba6SXin Li 
8*1a96fba6SXin Li #include <string>
9*1a96fba6SXin Li #include <vector>
10*1a96fba6SXin Li 
11*1a96fba6SXin Li #include <base/compiler_specific.h>
12*1a96fba6SXin Li #include <base/macros.h>
13*1a96fba6SXin Li #include <brillo/brillo_export.h>
14*1a96fba6SXin Li #include <brillo/data_encoding.h>
15*1a96fba6SXin Li 
16*1a96fba6SXin Li namespace brillo {
17*1a96fba6SXin Li 
18*1a96fba6SXin Li namespace url {
19*1a96fba6SXin Li 
20*1a96fba6SXin Li // Appends a subpath to url and delimiting then with '/' if the path doesn't
21*1a96fba6SXin Li // end with it already. Also handles URLs with query parameters/fragment.
22*1a96fba6SXin Li BRILLO_EXPORT std::string Combine(
23*1a96fba6SXin Li     const std::string& url,
24*1a96fba6SXin Li     const std::string& subpath) WARN_UNUSED_RESULT;
25*1a96fba6SXin Li BRILLO_EXPORT std::string CombineMultiple(
26*1a96fba6SXin Li     const std::string& url,
27*1a96fba6SXin Li     const std::vector<std::string>& parts) WARN_UNUSED_RESULT;
28*1a96fba6SXin Li 
29*1a96fba6SXin Li // Removes the query string/fragment from |url| and returns the query string.
30*1a96fba6SXin Li // This method actually modifies |url|. So, if you call it on this:
31*1a96fba6SXin Li //    http://www.test.org/?foo=bar
32*1a96fba6SXin Li // it will modify |url| to "http://www.test.org/" and return "?foo=bar"
33*1a96fba6SXin Li BRILLO_EXPORT std::string TrimOffQueryString(std::string* url);
34*1a96fba6SXin Li 
35*1a96fba6SXin Li // Returns the query string, if available.
36*1a96fba6SXin Li // For example, for the following URL:
37*1a96fba6SXin Li //    http://server.com/path/to/object?k=v&foo=bar#fragment
38*1a96fba6SXin Li // Here:
39*1a96fba6SXin Li //    http://server.com/path/to/object - is the URL of the object,
40*1a96fba6SXin Li //    ?k=v&foo=bar                     - URL query string
41*1a96fba6SXin Li //    #fragment                        - URL fragment string
42*1a96fba6SXin Li // If |remove_fragment| is true, the function returns the query string without
43*1a96fba6SXin Li // the fragment. Otherwise the fragment is included as part of the result.
44*1a96fba6SXin Li BRILLO_EXPORT std::string GetQueryString(const std::string& url,
45*1a96fba6SXin Li                                          bool remove_fragment);
46*1a96fba6SXin Li 
47*1a96fba6SXin Li // Parses the query string into a set of key-value pairs.
48*1a96fba6SXin Li BRILLO_EXPORT data_encoding::WebParamList GetQueryStringParameters(
49*1a96fba6SXin Li     const std::string& url);
50*1a96fba6SXin Li 
51*1a96fba6SXin Li // Returns a value of the specified query parameter, or empty string if missing.
52*1a96fba6SXin Li BRILLO_EXPORT std::string GetQueryStringValue(
53*1a96fba6SXin Li     const std::string& url,
54*1a96fba6SXin Li     const std::string& name);
55*1a96fba6SXin Li BRILLO_EXPORT std::string GetQueryStringValue(
56*1a96fba6SXin Li     const data_encoding::WebParamList& params,
57*1a96fba6SXin Li     const std::string& name);
58*1a96fba6SXin Li 
59*1a96fba6SXin Li // Removes the query string and/or a fragment part from URL.
60*1a96fba6SXin Li // If |remove_fragment| is specified, the fragment is also removed.
61*1a96fba6SXin Li // For example:
62*1a96fba6SXin Li //    http://server.com/path/to/object?k=v&foo=bar#fragment
63*1a96fba6SXin Li // true  -> http://server.com/path/to/object
64*1a96fba6SXin Li // false -> http://server.com/path/to/object#fragment
65*1a96fba6SXin Li BRILLO_EXPORT std::string RemoveQueryString(
66*1a96fba6SXin Li     const std::string& url,
67*1a96fba6SXin Li     bool remove_fragment) WARN_UNUSED_RESULT;
68*1a96fba6SXin Li 
69*1a96fba6SXin Li // Appends a single query parameter to the URL.
70*1a96fba6SXin Li BRILLO_EXPORT std::string AppendQueryParam(
71*1a96fba6SXin Li     const std::string& url,
72*1a96fba6SXin Li     const std::string& name,
73*1a96fba6SXin Li     const std::string& value) WARN_UNUSED_RESULT;
74*1a96fba6SXin Li // Appends a list of query parameters to the URL.
75*1a96fba6SXin Li BRILLO_EXPORT std::string AppendQueryParams(
76*1a96fba6SXin Li     const std::string& url,
77*1a96fba6SXin Li     const data_encoding::WebParamList& params) WARN_UNUSED_RESULT;
78*1a96fba6SXin Li 
79*1a96fba6SXin Li // Checks if the URL has query parameters.
80*1a96fba6SXin Li BRILLO_EXPORT bool HasQueryString(const std::string& url);
81*1a96fba6SXin Li 
82*1a96fba6SXin Li }  // namespace url
83*1a96fba6SXin Li }  // namespace brillo
84*1a96fba6SXin Li 
85*1a96fba6SXin Li #endif  // LIBBRILLO_BRILLO_URL_UTILS_H_
86