Home | History | Annotate | Download | only in http
      1 // Copyright (c) 2012 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 #ifndef NET_HTTP_HTTP_PROXY_CLIENT_SOCKET_H_
      6 #define NET_HTTP_HTTP_PROXY_CLIENT_SOCKET_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/memory/ref_counted.h"
     12 #include "net/base/completion_callback.h"
     13 #include "net/base/host_port_pair.h"
     14 #include "net/base/load_timing_info.h"
     15 #include "net/base/net_log.h"
     16 #include "net/http/http_auth_controller.h"
     17 #include "net/http/http_request_headers.h"
     18 #include "net/http/http_request_info.h"
     19 #include "net/http/http_response_info.h"
     20 #include "net/http/proxy_client_socket.h"
     21 #include "net/socket/ssl_client_socket.h"
     22 
     23 class GURL;
     24 
     25 namespace net {
     26 
     27 class AddressList;
     28 class ClientSocketHandle;
     29 class GrowableIOBuffer;
     30 class HttpAuthCache;
     31 class HttpStream;
     32 class HttpStreamParser;
     33 class IOBuffer;
     34 
     35 class HttpProxyClientSocket : public ProxyClientSocket {
     36  public:
     37   // Takes ownership of |transport_socket|, which should already be connected
     38   // by the time Connect() is called.  If tunnel is true then on Connect()
     39   // this socket will establish an Http tunnel.
     40   HttpProxyClientSocket(ClientSocketHandle* transport_socket,
     41                         const GURL& request_url,
     42                         const std::string& user_agent,
     43                         const HostPortPair& endpoint,
     44                         const HostPortPair& proxy_server,
     45                         HttpAuthCache* http_auth_cache,
     46                         HttpAuthHandlerFactory* http_auth_handler_factory,
     47                         bool tunnel,
     48                         bool using_spdy,
     49                         NextProto protocol_negotiated,
     50                         bool is_https_proxy);
     51 
     52   // On destruction Disconnect() is called.
     53   virtual ~HttpProxyClientSocket();
     54 
     55   // ProxyClientSocket implementation.
     56   virtual const HttpResponseInfo* GetConnectResponseInfo() const OVERRIDE;
     57   virtual HttpStream* CreateConnectResponseStream() OVERRIDE;
     58   virtual int RestartWithAuth(const CompletionCallback& callback) OVERRIDE;
     59   virtual const scoped_refptr<HttpAuthController>& GetAuthController() const
     60       OVERRIDE;
     61   virtual bool IsUsingSpdy() const OVERRIDE;
     62   virtual NextProto GetProtocolNegotiated() const OVERRIDE;
     63 
     64   // StreamSocket implementation.
     65   virtual int Connect(const CompletionCallback& callback) OVERRIDE;
     66   virtual void Disconnect() OVERRIDE;
     67   virtual bool IsConnected() const OVERRIDE;
     68   virtual bool IsConnectedAndIdle() const OVERRIDE;
     69   virtual const BoundNetLog& NetLog() const OVERRIDE;
     70   virtual void SetSubresourceSpeculation() OVERRIDE;
     71   virtual void SetOmniboxSpeculation() OVERRIDE;
     72   virtual bool WasEverUsed() const OVERRIDE;
     73   virtual bool UsingTCPFastOpen() const OVERRIDE;
     74   virtual bool WasNpnNegotiated() const OVERRIDE;
     75   virtual NextProto GetNegotiatedProtocol() const OVERRIDE;
     76   virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE;
     77 
     78   // Socket implementation.
     79   virtual int Read(IOBuffer* buf,
     80                    int buf_len,
     81                    const CompletionCallback& callback) OVERRIDE;
     82   virtual int Write(IOBuffer* buf,
     83                     int buf_len,
     84                     const CompletionCallback& callback) OVERRIDE;
     85   virtual bool SetReceiveBufferSize(int32 size) OVERRIDE;
     86   virtual bool SetSendBufferSize(int32 size) OVERRIDE;
     87   virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE;
     88   virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE;
     89 
     90  private:
     91   enum State {
     92     STATE_NONE,
     93     STATE_GENERATE_AUTH_TOKEN,
     94     STATE_GENERATE_AUTH_TOKEN_COMPLETE,
     95     STATE_SEND_REQUEST,
     96     STATE_SEND_REQUEST_COMPLETE,
     97     STATE_READ_HEADERS,
     98     STATE_READ_HEADERS_COMPLETE,
     99     STATE_DRAIN_BODY,
    100     STATE_DRAIN_BODY_COMPLETE,
    101     STATE_TCP_RESTART,
    102     STATE_TCP_RESTART_COMPLETE,
    103     STATE_DONE,
    104   };
    105 
    106   // The size in bytes of the buffer we use to drain the response body that
    107   // we want to throw away.  The response body is typically a small error
    108   // page just a few hundred bytes long.
    109   static const int kDrainBodyBufferSize = 1024;
    110 
    111   int PrepareForAuthRestart();
    112   int DidDrainBodyForAuthRestart(bool keep_alive);
    113 
    114   void LogBlockedTunnelResponse() const;
    115 
    116   void DoCallback(int result);
    117   void OnIOComplete(int result);
    118 
    119   int DoLoop(int last_io_result);
    120   int DoGenerateAuthToken();
    121   int DoGenerateAuthTokenComplete(int result);
    122   int DoSendRequest();
    123   int DoSendRequestComplete(int result);
    124   int DoReadHeaders();
    125   int DoReadHeadersComplete(int result);
    126   int DoDrainBody();
    127   int DoDrainBodyComplete(int result);
    128   int DoTCPRestart();
    129   int DoTCPRestartComplete(int result);
    130 
    131   CompletionCallback io_callback_;
    132   State next_state_;
    133 
    134   // Stores the callback to the layer above, called on completing Connect().
    135   CompletionCallback user_callback_;
    136 
    137   HttpRequestInfo request_;
    138   HttpResponseInfo response_;
    139 
    140   scoped_refptr<GrowableIOBuffer> parser_buf_;
    141   scoped_ptr<HttpStreamParser> http_stream_parser_;
    142   scoped_refptr<IOBuffer> drain_buf_;
    143 
    144   // Stores the underlying socket.
    145   scoped_ptr<ClientSocketHandle> transport_;
    146 
    147   // The hostname and port of the endpoint.  This is not necessarily the one
    148   // specified by the URL, due to Alternate-Protocol or fixed testing ports.
    149   const HostPortPair endpoint_;
    150   scoped_refptr<HttpAuthController> auth_;
    151   const bool tunnel_;
    152   // If true, then the connection to the proxy is a SPDY connection.
    153   const bool using_spdy_;
    154   // Protocol negotiated with the server.
    155   NextProto protocol_negotiated_;
    156   // If true, then SSL is used to communicate with this proxy
    157   const bool is_https_proxy_;
    158 
    159   std::string request_line_;
    160   HttpRequestHeaders request_headers_;
    161 
    162   // Used only for redirects.
    163   bool redirect_has_load_timing_info_;
    164   LoadTimingInfo redirect_load_timing_info_;
    165 
    166   const BoundNetLog net_log_;
    167 
    168   DISALLOW_COPY_AND_ASSIGN(HttpProxyClientSocket);
    169 };
    170 
    171 }  // namespace net
    172 
    173 #endif  // NET_HTTP_HTTP_PROXY_CLIENT_SOCKET_H_
    174