OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium 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 #include "chrome/browser/chromeos/web_socket_proxy_helper.h" |
| 6 |
| 7 #include "base/string_number_conversions.h" |
| 8 #include "base/logging.h" |
| 9 |
| 10 namespace chromeos { |
| 11 |
| 12 // static |
| 13 bool WebSocketProxyHelper::FetchPassportAddrNamePort( |
| 14 uint8* begin, uint8* end, |
| 15 std::string* passport, std::string* addr, |
| 16 std::string* hostname, int* port) { |
| 17 std::string input(begin, end); |
| 18 |
| 19 // At first, get rid of a colon at the end. |
| 20 if (input.empty() || input[input.length() - 1] != ':') |
| 21 return false; |
| 22 input.erase(input.length() - 1); |
| 23 |
| 24 // Fetch the passport from the start. |
| 25 if (!FetchToken(true, false, &input, passport) || passport->empty()) |
| 26 return false; |
| 27 |
| 28 // Fetch the IP address from the start. Match '['-brackets if presented. |
| 29 if (!FetchToken(true, true, &input, addr)) |
| 30 return false; |
| 31 |
| 32 // Fetch the port number from the end. |
| 33 std::string port_str; |
| 34 if (!FetchToken(false, false, &input, &port_str) || port_str.empty() || |
| 35 !base::StringToInt(port_str, port) || |
| 36 *port <= 0 || *port >= (1<<16)) |
| 37 return false; |
| 38 |
| 39 // The hostname is everything remained. |
| 40 if (input.length() < 2 || input[input.length() - 1] != ':') |
| 41 return false; |
| 42 hostname->assign(input, 0, input.length() - 1); |
| 43 |
| 44 return true; |
| 45 } |
| 46 |
| 47 // static |
| 48 bool WebSocketProxyHelper::FetchToken(bool forward, |
| 49 bool match_brackets, |
| 50 std::string* input, |
| 51 std::string* token ) { |
| 52 if (input->empty()) { |
| 53 token->clear(); |
| 54 } else { |
| 55 std::string separator = |
| 56 (forward && match_brackets && input->at(0) == '[') ? "]:" : ":"; |
| 57 size_t pos = forward ? input->find(separator) : input->rfind(separator); |
| 58 if (pos != std::string::npos) { |
| 59 size_t start = forward ? 0 : pos + 1; |
| 60 size_t end = forward ? pos : input->length(); |
| 61 token->assign(*input, start, end - start + separator.length() - 1); |
| 62 input->erase(start, end - start + separator.length()); |
| 63 } else { |
| 64 return false; |
| 65 } |
| 66 } |
| 67 return true; |
| 68 } |
| 69 |
| 70 } // namespace chromeos |
OLD | NEW |