blob: d9bce25814e3fcb808787fc390896e6eec085e5f [file] [log] [blame]
Alex Vakulenkobda220a2014-04-18 15:25:44 -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
5#ifndef BUFFET_URL_UTILS_H_
6#define BUFFET_URL_UTILS_H_
7
Alex Vakulenkobda220a2014-04-18 15:25:44 -07008#include <string>
9#include <vector>
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070010
11#include <base/basictypes.h>
12
Alex Vakulenkobda220a2014-04-18 15:25:44 -070013#include "buffet/data_encoding.h"
14
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070015namespace buffet {
Alex Vakulenkobda220a2014-04-18 15:25:44 -070016
17namespace 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.
21std::string Combine(const std::string& url,
22 const std::string& subpath) WARN_UNUSED_RESULT;
23std::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 Vakulenkoaf23b322014-05-08 16:25:45 -070028// This method actually modifies |url|. So, if you call it on this:
Alex Vakulenkobda220a2014-04-18 15:25:44 -070029// http://www.test.org/?foo=bar
30// it will modify |url| to "http://www.test.org/" and return "?foo=bar"
31std::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 Vakulenkoaf23b322014-05-08 16:25:45 -070039// #fragment - URL fragment string
Alex Vakulenkobda220a2014-04-18 15:25:44 -070040// 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.
42std::string GetQueryString(const std::string& url, bool remove_fragment);
43
44// Parses the query string into a set of key-value pairs.
45data_encoding::WebParamList GetQueryStringParameters(const std::string& url);
46
47// Returns a value of the specified query parameter, or empty string if missing.
48std::string GetQueryStringValue(const std::string& url,
49 const std::string& name);
50std::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
59std::string RemoveQueryString(const std::string& url,
60 bool remove_fragment) WARN_UNUSED_RESULT;
61
62// Appends a single query parameter to the URL.
63std::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.
67std::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.
72bool HasQueryString(const std::string& url);
73
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070074} // namespace url
75} // namespace buffet
Alex Vakulenkobda220a2014-04-18 15:25:44 -070076
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070077#endif // BUFFET_URL_UTILS_H_