summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/web_socket_proxy_helper.cc
blob: 8b7b6a9a6b5dbde7c64770912383fa2a6bce7d1b (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
// Copyright (c) 2011 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 "chrome/browser/chromeos/web_socket_proxy_helper.h"

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

namespace chromeos {

// static
bool WebSocketProxyHelper::FetchPassportAddrNamePort(
      uint8* begin, uint8* end,
      std::string* passport, std::string* addr,
      std::string* hostname, int* port) {
  std::string input(begin, end);

  // At first, get rid of a colon at the end.
  if (input.empty() || input[input.length() - 1] != ':')
    return false;
  input.erase(input.length() - 1);

  // Fetch the passport from the start.
  if (!FetchToken(true, false, &input, passport) || passport->empty())
    return false;

  // Fetch the IP address from the start. Match '['-brackets if presented.
  if (!FetchToken(true, true, &input, addr))
    return false;

  // Fetch the port number from the end.
  std::string port_str;
  if (!FetchToken(false, false, &input, &port_str) || port_str.empty() ||
      !base::StringToInt(port_str, port) ||
      *port <= 0 || *port >= (1<<16))
    return false;

  // The hostname is everything remained.
  if (input.length() < 2 || input[input.length() - 1] != ':')
    return false;
  hostname->assign(input, 0, input.length() - 1);

  return true;
}

// static
bool WebSocketProxyHelper::FetchToken(bool forward,
                                      bool match_brackets,
                                      std::string* input,
                                      std::string* token ) {
  if (input->empty()) {
    token->clear();
  } else {
    std::string separator =
        (forward && match_brackets && input->at(0) == '[') ? "]:" : ":";
    size_t pos = forward ? input->find(separator) : input->rfind(separator);
    if (pos != std::string::npos) {
      size_t start = forward ? 0 : pos + 1;
      size_t end = forward ? pos : input->length();
      token->assign(*input, start, end - start + separator.length() - 1);
      input->erase(start, end - start + separator.length());
    } else {
      return false;
    }
  }
  return true;
}

}  // namespace chromeos