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