summaryrefslogtreecommitdiffstats
path: root/net/tools/flip_server/url_utilities.h
blob: 52a768a8ec8a439ba24fb11720a1fee4242f0e2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_TOOLS_FLIP_SERVER_URL_UTILITIES_H__
#define NET_TOOLS_FLIP_SERVER_URL_UTILITIES_H__

#include <string>

namespace net {

struct UrlUtilities {
  // Get the host from an url
  static std::string GetUrlHost(const std::string& url) {
    size_t b = url.find("//");
    if (b == std::string::npos)
      b = 0;
    else
      b += 2;
    size_t next_slash = url.find_first_of('/', b);
    size_t next_colon = url.find_first_of(':', b);
    if (next_slash != std::string::npos
        && next_colon != std::string::npos
        && next_colon < next_slash) {
      return std::string(url, b, next_colon - b);
    }
    if (next_slash == std::string::npos) {
      if (next_colon != std::string::npos) {
        return std::string(url, next_colon - b);
      } else {
        next_slash = url.size();
      }
    }
    return std::string(url, b, next_slash - b);
  }

  // Get the host + path portion of an url
  // e.g   http://www.foo.com/path
  //       returns www.foo.com/path
  static std::string GetUrlHostPath(const std::string& url) {
    size_t b = url.find("//");
    if (b == std::string::npos)
      b = 0;
    else
      b += 2;
    return std::string(url, b);
  }

  // Get the path portion of an url
  // e.g   http://www.foo.com/path
  //       returns /path
  static std::string GetUrlPath(const std::string& url) {
    size_t b = url.find("//");
    if (b == std::string::npos)
      b = 0;
    else
      b += 2;
    b = url.find("/", b+1);
    if (b == std::string::npos)
      return "/";

    return std::string(url, b);
  }
};

} // namespace net

#endif  // NET_TOOLS_FLIP_SERVER_URL_UTILITIES_H__