Home | History | Annotate | Download | only in brillo
      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 LIBBRILLO_BRILLO_URL_UTILS_H_
      6 #define LIBBRILLO_BRILLO_URL_UTILS_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include <base/compiler_specific.h>
     12 #include <base/macros.h>
     13 #include <brillo/brillo_export.h>
     14 #include <brillo/data_encoding.h>
     15 
     16 namespace brillo {
     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.
     22 BRILLO_EXPORT std::string Combine(
     23     const std::string& url,
     24     const std::string& subpath) WARN_UNUSED_RESULT;
     25 BRILLO_EXPORT std::string CombineMultiple(
     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"
     33 BRILLO_EXPORT std::string TrimOffQueryString(std::string* url);
     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.
     44 BRILLO_EXPORT std::string GetQueryString(const std::string& url,
     45                                          bool remove_fragment);
     46 
     47 // Parses the query string into a set of key-value pairs.
     48 BRILLO_EXPORT data_encoding::WebParamList GetQueryStringParameters(
     49     const std::string& url);
     50 
     51 // Returns a value of the specified query parameter, or empty string if missing.
     52 BRILLO_EXPORT std::string GetQueryStringValue(
     53     const std::string& url,
     54     const std::string& name);
     55 BRILLO_EXPORT std::string GetQueryStringValue(
     56     const data_encoding::WebParamList& params,
     57     const std::string& name);
     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
     65 BRILLO_EXPORT std::string RemoveQueryString(
     66     const std::string& url,
     67     bool remove_fragment) WARN_UNUSED_RESULT;
     68 
     69 // Appends a single query parameter to the URL.
     70 BRILLO_EXPORT std::string AppendQueryParam(
     71     const std::string& url,
     72     const std::string& name,
     73     const std::string& value) WARN_UNUSED_RESULT;
     74 // Appends a list of query parameters to the URL.
     75 BRILLO_EXPORT std::string AppendQueryParams(
     76     const std::string& url,
     77     const data_encoding::WebParamList& params) WARN_UNUSED_RESULT;
     78 
     79 // Checks if the URL has query parameters.
     80 BRILLO_EXPORT bool HasQueryString(const std::string& url);
     81 
     82 }  // namespace url
     83 }  // namespace brillo
     84 
     85 #endif  // LIBBRILLO_BRILLO_URL_UTILS_H_
     86