diff options
author | ericroman@google.com <ericroman@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-19 08:11:42 +0000 |
---|---|---|
committer | ericroman@google.com <ericroman@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-19 08:11:42 +0000 |
commit | f6fb2de77993bbd88931ba205b0cc1e3423111f6 (patch) | |
tree | 831dbc093048f60b7d3cfa6548d42cedc340b4d7 /net/proxy/proxy_server.h | |
parent | 77c846f5d8043e56543668971fff356d58380cf7 (diff) | |
download | chromium_src-f6fb2de77993bbd88931ba205b0cc1e3423111f6.zip chromium_src-f6fb2de77993bbd88931ba205b0cc1e3423111f6.tar.gz chromium_src-f6fb2de77993bbd88931ba205b0cc1e3423111f6.tar.bz2 |
Add parsing for PAC result strings -- ProxyInfo::UsePacString().
Added the support class ProxyServer to avoid losing the proxy server's type information (http, socks) when populating the list.
The format of the configuration strings has been extended accordingly to include an optional [<scheme>"://"] prefix.
(i.e. "http://", "socks4://", "socks5://").
Review URL: http://codereview.chromium.org/20398
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@10010 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/proxy/proxy_server.h')
-rw-r--r-- | net/proxy/proxy_server.h | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/net/proxy/proxy_server.h b/net/proxy/proxy_server.h new file mode 100644 index 0000000..59f0718 --- /dev/null +++ b/net/proxy/proxy_server.h @@ -0,0 +1,118 @@ +// 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_PROXY_SERVER_H_ +#define NET_PROXY_SERVER_H_ + +#include <string> + +namespace net { + +// ProxyServer encodes the {type, host, port} of a proxy server. +// ProxyServer is immutable. +class ProxyServer { + public: + // The type of proxy. These are defined as bit flags so they can be ORed + // together to pass as the |scheme_bit_field| argument to + // ProxyService::RemoveProxiesWithoutScheme(). + enum Scheme { + SCHEME_INVALID = 1 << 0, + SCHEME_DIRECT = 1 << 1, + SCHEME_HTTP = 1 << 2, + SCHEME_SOCKS4 = 1 << 3, + SCHEME_SOCKS5 = 1 << 4, + }; + + // Default copy-constructor and assignment operator are OK! + + // Constructs an invalid ProxyServer. + ProxyServer() : scheme_(SCHEME_INVALID), port_(-1) {} + + ProxyServer(Scheme scheme, const std::string& host, int port) + : scheme_(scheme), host_(host), port_(port) {} + + bool is_valid() const { return scheme_ != SCHEME_INVALID; } + + // Gets the proxy's scheme (i.e. SOCKS4, SOCKS5, HTTP} + Scheme scheme() const { return scheme_; } + + // Returns true if this ProxyServer is actually just a DIRECT connection. + bool is_direct() const { return scheme_ == SCHEME_DIRECT; } + + // Returns true if this ProxyServer is an HTTP proxy. + bool is_http() const { return scheme_ == SCHEME_HTTP; } + + // Returns true if this ProxyServer is a SOCKS proxy. + bool is_socks() const { + return scheme_ == SCHEME_SOCKS4 || scheme_ == SCHEME_SOCKS5; + } + + // Gets the host portion of the proxy server. + const std::string& host() const; + + // Gets the port portion of the proxy server. + int port() const; + + // Returns the <host>":"<port> string for the proxy server. + std::string host_and_port() const; + + // Parse from an input with format: + // [<scheme>"://"]<server>[":"<port>] + // + // Both <scheme> and <port> are optional. If <scheme> is omitted, it will + // be assumed as "http". If <port> is omitted, it will be assumed as + // the default port for the chosen scheme (80 for "http", 1080 for "socks"). + // + // If parsing fails the instance will be set to invalid. + // + // Examples: + // "foopy" {scheme=HTTP, host="foopy", port=80} + // "socks4://foopy" {scheme=SOCKS4, host="foopy", port=1080} + // "socks5://foopy" {scheme=SOCKS5, host="foopy", port=1080} + // "http://foopy:17" {scheme=HTTP, host="foopy", port=17} + // "direct://" {scheme=DIRECT} + // "foopy:X" INVALID -- bad port. + static ProxyServer FromURI(const std::string& uri); + static ProxyServer FromURI(std::string::const_iterator uri_begin, + std::string::const_iterator uri_end); + + // Format as a URI string. This does the reverse of FromURI. + std::string ToURI() const; + + // Parses from a PAC string result. + // + // If <port> is omitted, it will be assumed as the default port for the + // chosen scheme (80 for "http", 1080 for "socks"). + // + // If parsing fails the instance will be set to invalid. + // + // Examples: + // "PROXY foopy:19" {scheme=HTTP, host="foopy", port=19} + // "DIRECT" {scheme=DIRECT} + // "SOCKS5 foopy" {scheme=SOCKS5, host="foopy", port=1080} + // "BLAH xxx:xx" INVALID + static ProxyServer FromPacString(const std::string& pac_string); + static ProxyServer FromPacString(std::string::const_iterator pac_string_begin, + std::string::const_iterator pac_string_end); + + // Format as a PAC result entry. This does the reverse of FromPacString(). + std::string ToPacString() const; + + private: + // Create a ProxyServer given a scheme, and host/port string. If parsing the + // host/port string fails, the returned instance will be invalid. + static ProxyServer FromSchemeHostAndPort( + Scheme scheme, + std::string::const_iterator host_and_port_begin, + std::string::const_iterator host_and_port_end); + + Scheme scheme_; + std::string host_; + int port_; +}; + +} // namespace net + +#endif // NET_PROXY_SERVER_H_ + |