1 // Copyright 2018 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 // The proxy functionality is implemented as a separate thread namely
6 // “quic proxy thread”, managed by an instance of the QuicHttpProxyBackend
7 // class. The QuicHttpProxyBackend instance also manages an instance of the
8 // class net::URLRequestContext, that manages a single context for all the
9 // HTTP calls made to the backend server. Finally, the QuicHttpProxyBackend
10 // instance owns (creates/ destroys) the instances of QuicHttpProxyBackendStream
11 // to avoid orphan pointers of QuicHttpProxyBackendStream when the corresponding
12 // QUIC connection is destroyed on the main thread due to several reasons. The
13 // QUIC connection management and protocol parsing is performed by the main/quic
14 // thread, in the same way as the toy QUIC server.
15 //
16 // quic_http_proxy_backend_stream.h has a description of threads, the flow
17 // of packets in QUIC proxy in the forward and reverse directions.
18 
19 #ifndef NET_TOOLS_QUIC_QUIC_HTTP_PROXY_BACKEND_H_
20 #define NET_TOOLS_QUIC_QUIC_HTTP_PROXY_BACKEND_H_
21 
22 #include <stdint.h>
23 
24 #include <memory>
25 #include <queue>
26 
27 #include "base/base64.h"
28 #include "base/callback.h"
29 #include "base/logging.h"
30 #include "base/macros.h"
31 #include "base/single_thread_task_runner.h"
32 #include "base/threading/thread.h"
33 #include "base/threading/thread_task_runner_handle.h"
34 #include "base/values.h"
35 #include "net/third_party/quiche/src/quic/tools/quic_simple_server_backend.h"
36 #include "net/url_request/url_request_context.h"
37 #include "net/url_request/url_request_context_builder.h"
38 #include "url/gurl.h"
39 
40 namespace base {
41 class SingleThreadTaskRunner;
42 }  // namespace base
43 
44 namespace quic {
45 class QuicSimpleServerBackend;
46 }  // namespace quic
47 
48 namespace net {
49 class QuicHttpProxyBackendStream;
50 
51 // Manages the context to proxy HTTP requests to the backend server
52 // Owns instance of net::URLRequestContext.
53 class QuicHttpProxyBackend : public quic::QuicSimpleServerBackend {
54  public:
55   explicit QuicHttpProxyBackend();
56   ~QuicHttpProxyBackend() override;
57 
58   // Must be called from the backend thread of the quic proxy
59   net::URLRequestContext* GetURLRequestContext();
60   scoped_refptr<base::SingleThreadTaskRunner> GetProxyTaskRunner() const;
61 
62   using ProxyBackendStreamMap =
63       std::unordered_map<quic::QuicSimpleServerBackend::RequestHandler*,
64                          std::unique_ptr<QuicHttpProxyBackendStream>>;
proxy_backend_streams_map()65   const ProxyBackendStreamMap* proxy_backend_streams_map() const {
66     return &backend_stream_map_;
67   }
68 
backend_url()69   GURL backend_url() const { return backend_url_; }
70 
71   // Implements the functions for interface quic::QuicSimpleServerBackend
72   bool InitializeBackend(const std::string& backend_url) override;
73   bool IsBackendInitialized() const override;
74   void FetchResponseFromBackend(
75       const spdy::SpdyHeaderBlock& request_headers,
76       const std::string& incoming_body,
77       quic::QuicSimpleServerBackend::RequestHandler* quic_stream) override;
78   void CloseBackendResponseStream(
79       quic::QuicSimpleServerBackend::RequestHandler* quic_stream) override;
80 
81  private:
82   // Maps quic streams in the frontend to the corresponding http streams
83   // managed by |this|
84   ProxyBackendStreamMap backend_stream_map_;
85 
86   bool ValidateBackendUrl(const std::string& backend_url);
87   void InitializeURLRequestContext();
88   QuicHttpProxyBackendStream* InitializeQuicProxyBackendStream(
89       quic::QuicSimpleServerBackend::RequestHandler* quic_server_stream);
90 
91   // URLRequestContext to make URL requests to the backend
92   std::unique_ptr<net::URLRequestContext> context_;  // owned by this
93 
94   bool thread_initialized_;
95   // <scheme://hostname:port/ for the backend HTTP server
96   GURL backend_url_;
97 
98   // Backend thread is owned by |this|
99   std::unique_ptr<base::Thread> proxy_thread_;
100   scoped_refptr<base::SingleThreadTaskRunner> proxy_task_runner_;
101 
102   // Protects against concurrent access from quic (main) and proxy
103   // threads for adding and clearing a backend request handler
104   base::Lock backend_stream_mutex_;
105 
106   DISALLOW_COPY_AND_ASSIGN(QuicHttpProxyBackend);
107 };
108 }  // namespace net
109 
110 #endif  // NET_TOOLS_QUIC_QUIC_HTTP_PROXY_BACKEND_H_
111