Paul Stewart | 58a577b | 2012-01-10 11:18:52 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef SHILL_HTTP_PROXY_ |
| 6 | #define SHILL_HTTP_PROXY_ |
| 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 11 | #include <base/cancelable_callback.h> |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 12 | #include <base/memory/ref_counted.h> |
| 13 | #include <base/memory/scoped_ptr.h> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 14 | #include <base/memory/weak_ptr.h> |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 15 | |
| 16 | #include "shill/byte_string.h" |
| 17 | #include "shill/refptr_types.h" |
| 18 | |
| 19 | namespace shill { |
| 20 | |
| 21 | class AsyncConnection; |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 22 | class DNSClient; |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 23 | class Error; |
| 24 | class EventDispatcher; |
Liam McLoughlin | f4baef2 | 2012-08-01 19:08:25 -0700 | [diff] [blame] | 25 | struct InputData; |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 26 | class IOHandler; |
| 27 | class IPAddress; |
| 28 | class Sockets; |
| 29 | |
| 30 | // The HTTPProxy class implements a simple web proxy that |
| 31 | // is bound to a specific interface and name server. This |
| 32 | // allows us to specify which connection a URL should be |
| 33 | // fetched through, even though many connections |
| 34 | // could be active at the same time. |
| 35 | // |
| 36 | // This service is meant to be low-performance, since we |
| 37 | // do not want to divert resources from the rest of the |
| 38 | // connection manager. As such, we serve one client request |
| 39 | // at a time. This is probably okay since the use case is |
| 40 | // limited -- only portal detection, activation and Cashew |
| 41 | // are planned to be full-time users. |
| 42 | class HTTPProxy { |
| 43 | public: |
| 44 | enum State { |
| 45 | kStateIdle, |
| 46 | kStateWaitConnection, |
| 47 | kStateReadClientHeader, |
| 48 | kStateLookupServer, |
| 49 | kStateConnectServer, |
| 50 | kStateTunnelData, |
| 51 | kStateFlushResponse, |
| 52 | }; |
| 53 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 54 | explicit HTTPProxy(ConnectionRefPtr connection); |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 55 | virtual ~HTTPProxy(); |
| 56 | |
| 57 | // Start HTTP proxy. |
| 58 | bool Start(EventDispatcher *dispatcher, Sockets *sockets); |
| 59 | |
| 60 | // Shutdown. |
| 61 | void Stop(); |
| 62 | |
| 63 | int proxy_port() const { return proxy_port_; } |
| 64 | |
| 65 | private: |
| 66 | friend class HTTPProxyTest; |
| 67 | |
| 68 | // Time to wait for initial headers from client. |
| 69 | static const int kClientHeaderTimeoutSeconds; |
| 70 | // Time to wait for connection to remote server. |
| 71 | static const int kConnectTimeoutSeconds; |
| 72 | // Time to wait for DNS server. |
| 73 | static const int kDNSTimeoutSeconds; |
| 74 | // Default port on remote server to connect to. |
| 75 | static const int kDefaultServerPort; |
| 76 | // Time to wait for any input from either server or client. |
| 77 | static const int kInputTimeoutSeconds; |
| 78 | // Maximum clients to be kept waiting. |
| 79 | static const size_t kMaxClientQueue; |
| 80 | // Maximum number of header lines to accept. |
| 81 | static const size_t kMaxHeaderCount; |
| 82 | // Maximum length of an individual header line. |
| 83 | static const size_t kMaxHeaderSize; |
| 84 | // Timeout for whole transaction. |
| 85 | static const int kTransactionTimeoutSeconds; |
| 86 | |
Paul Stewart | 58a577b | 2012-01-10 11:18:52 -0800 | [diff] [blame] | 87 | static const char kHTTPMethodConnect[]; |
| 88 | static const char kHTTPMethodTerminator[]; |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 89 | static const char kHTTPURLDelimiters[]; |
| 90 | static const char kHTTPURLPrefix[]; |
| 91 | static const char kHTTPVersionPrefix[]; |
| 92 | static const char kHTTPVersionErrorMsg[]; |
| 93 | static const char kInternalErrorMsg[]; // Message to send on failure. |
| 94 | |
| 95 | void AcceptClient(int fd); |
| 96 | bool ConnectServer(const IPAddress &address, int port); |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 97 | void GetDNSResult(const Error &error, const IPAddress &address); |
Paul Stewart | 5f06a0e | 2012-12-20 11:11:33 -0800 | [diff] [blame] | 98 | void OnReadError(const Error &error); |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 99 | void OnConnectCompletion(bool success, int fd); |
| 100 | bool ParseClientRequest(); |
| 101 | bool ProcessLastHeaderLine(); |
| 102 | bool ReadClientHeaders(InputData *data); |
| 103 | bool ReadClientHostname(std::string *header); |
Paul Stewart | 58a577b | 2012-01-10 11:18:52 -0800 | [diff] [blame] | 104 | bool ReadClientHTTPMethod(std::string *header); |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 105 | bool ReadClientHTTPVersion(std::string *header); |
| 106 | void ReadFromClient(InputData *data); |
| 107 | void ReadFromServer(InputData *data); |
Paul Stewart | 58a577b | 2012-01-10 11:18:52 -0800 | [diff] [blame] | 108 | void SetClientResponse(int code, const std::string &type, |
| 109 | const std::string &content_type, |
| 110 | const std::string &message); |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 111 | void SendClientError(int code, const std::string &error); |
| 112 | void StartIdleTimeout(); |
| 113 | void StartReceive(); |
| 114 | void StartTransmit(); |
| 115 | void StopClient(); |
| 116 | void WriteToClient(int fd); |
| 117 | void WriteToServer(int fd); |
| 118 | |
| 119 | // State held for the lifetime of the proxy. |
| 120 | State state_; |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 121 | ConnectionRefPtr connection_; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 122 | base::WeakPtrFactory<HTTPProxy> weak_ptr_factory_; |
| 123 | base::Callback<void(int)> accept_callback_; |
| 124 | base::Callback<void(bool, int)> connect_completion_callback_; |
| 125 | base::Callback<void(const Error &, const IPAddress &)> dns_client_callback_; |
| 126 | base::Callback<void(InputData *)> read_client_callback_; |
| 127 | base::Callback<void(InputData *)> read_server_callback_; |
| 128 | base::Callback<void(int)> write_client_callback_; |
| 129 | base::Callback<void(int)> write_server_callback_; |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 130 | |
| 131 | // State held while proxy is started (even if no transaction is active). |
| 132 | scoped_ptr<IOHandler> accept_handler_; |
| 133 | EventDispatcher *dispatcher_; |
| 134 | scoped_ptr<DNSClient> dns_client_; |
| 135 | int proxy_port_; |
| 136 | int proxy_socket_; |
| 137 | scoped_ptr<AsyncConnection> server_async_connection_; |
| 138 | Sockets *sockets_; |
| 139 | |
| 140 | // State held while proxy is started and a transaction is active. |
| 141 | int client_socket_; |
Paul Stewart | 58a577b | 2012-01-10 11:18:52 -0800 | [diff] [blame] | 142 | std::string client_method_; |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 143 | std::string client_version_; |
| 144 | int server_port_; |
| 145 | int server_socket_; |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 146 | bool is_route_requested_; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 147 | base::CancelableClosure idle_timeout_; |
Paul Stewart | f582b50 | 2012-04-04 21:39:22 -0700 | [diff] [blame] | 148 | base::CancelableClosure transaction_timeout_; |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 149 | std::vector<std::string> client_headers_; |
| 150 | std::string server_hostname_; |
| 151 | ByteString client_data_; |
| 152 | ByteString server_data_; |
| 153 | scoped_ptr<IOHandler> read_client_handler_; |
| 154 | scoped_ptr<IOHandler> write_client_handler_; |
| 155 | scoped_ptr<IOHandler> read_server_handler_; |
| 156 | scoped_ptr<IOHandler> write_server_handler_; |
| 157 | |
| 158 | DISALLOW_COPY_AND_ASSIGN(HTTPProxy); |
| 159 | }; |
| 160 | |
| 161 | } // namespace shill |
| 162 | |
| 163 | #endif // SHILL_HTTP_PROXY_ |