summaryrefslogtreecommitdiff
path: root/chromium/net/tools/dump_cache/url_utilities.cc
blob: 76044943ce744a6aca8a54d3286973db2457ebc3 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) 2010 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.

#include "net/tools/dump_cache/url_utilities.h"

#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"

namespace net {

std::string UrlUtilities::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, b, next_colon - b);
    } else {
      next_slash = url.size();
    }
  }
  return std::string(url, b, next_slash - b);
}

std::string UrlUtilities::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);
}

std::string UrlUtilities::GetUrlPath(const std::string& url) {
  size_t b = url.find("//");
  if (b == std::string::npos)
    b = 0;
  else
    b += 2;
  b = url.find("/", b);
  if (b == std::string::npos)
    return "/";

  size_t e = url.find("#", b+1);
  if (e != std::string::npos)
    return std::string(url, b, (e - b));
  return std::string(url, b);
}

namespace {

// Parsing states for UrlUtilities::Unescape
enum UnescapeState {
  NORMAL,   // We are not in the middle of parsing an escape.
  ESCAPE1,  // We just parsed % .
  ESCAPE2   // We just parsed %X for some hex digit X.
};

}  // namespace

std::string UrlUtilities::Unescape(const std::string& escaped_url) {
  std::string unescaped_url, escape_text;
  int escape_value;
  UnescapeState state = NORMAL;
  std::string::const_iterator iter = escaped_url.begin();
  while (iter < escaped_url.end()) {
    char c = *iter;
    switch (state) {
      case NORMAL:
        if (c == '%') {
          escape_text.clear();
          state = ESCAPE1;
        } else {
          unescaped_url.push_back(c);
        }
        ++iter;
        break;
      case ESCAPE1:
        if (IsHexDigit(c)) {
          escape_text.push_back(c);
          state = ESCAPE2;
          ++iter;
        } else {
          // Unexpected, % followed by non-hex chars, pass it through.
          unescaped_url.push_back('%');
          state = NORMAL;
        }
        break;
      case ESCAPE2:
        if (IsHexDigit(c)) {
          escape_text.push_back(c);
          bool ok = base::HexStringToInt(escape_text, &escape_value);
          DCHECK(ok);
          unescaped_url.push_back(static_cast<unsigned char>(escape_value));
          state = NORMAL;
          ++iter;
        } else {
          // Unexpected, % followed by non-hex chars, pass it through.
          unescaped_url.push_back('%');
          unescaped_url.append(escape_text);
          state = NORMAL;
        }
        break;
    }
  }
  // Unexpected, % followed by end of string, pass it through.
  if (state == ESCAPE1 || state == ESCAPE2) {
    unescaped_url.push_back('%');
    unescaped_url.append(escape_text);
  }
  return unescaped_url;
}

}  // namespace net