Apache Mesos
http_proxy.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License
12 
13 #ifndef __PROCESS_HTTP_PROXY_HPP__
14 #define __PROCESS_HTTP_PROXY_HPP__
15 
16 #include <queue>
17 
18 #include <process/future.hpp>
19 #include <process/http.hpp>
20 #include <process/process.hpp>
21 #include <process/socket.hpp>
22 
23 #include <stout/option.hpp>
24 
25 namespace process {
26 
27 // Provides a process that manages sending HTTP responses so as to
28 // satisfy HTTP/1.1 pipelining. Each request should either enqueue a
29 // response, or ask the proxy to handle a future response. The process
30 // is responsible for making sure the responses are sent in the same
31 // order as the requests. Note that we use a 'Socket' in order to keep
32 // the underlying file descriptor from getting closed while there
33 // might still be outstanding responses even though the client might
34 // have closed the connection (see more discussion in
35 // SocketManager::close and SocketManager::proxy).
36 class HttpProxy : public Process<HttpProxy>
37 {
38 public:
39  explicit HttpProxy(const network::inet::Socket& _socket);
40 
41  ~HttpProxy() override {}
42 
43  // Enqueues the response to be sent once all previously enqueued
44  // responses have been processed (e.g., waited for and sent).
45  void enqueue(
46  const http::Response& response,
47  const http::Request& request);
48 
49  // Enqueues a future to a response that will get waited on (up to
50  // some timeout) and then sent once all previously enqueued
51  // responses have been processed (e.g., waited for and sent).
52  void handle(
53  const Future<http::Response>& future,
54  const http::Request& request);
55 
56 protected:
57  void finalize() override;
58 
59 private:
60  // Starts "waiting" on the next available future response.
61  void next();
62 
63  // Invoked once a future response has been satisfied.
64  void waited(const Future<http::Response>& future);
65 
66  // Demuxes and handles a response.
67  bool process(
68  const Future<http::Response>& future,
69  const http::Request& request);
70 
71  // Handles stream based responses.
72  void stream(
73  const Owned<http::Request>& request,
74  const Future<std::string>& chunk);
75 
76  network::inet::Socket socket; // Store the socket to keep it open.
77 
78  // Describes a queue "item" that wraps the future to the response
79  // and the original request.
80  // The original request contains needed information such as what encodings
81  // are acceptable and whether to persist the connection.
82  struct Item
83  {
84  Item(const http::Request& _request, const Future<http::Response>& _future)
85  : request(_request), future(_future) {}
86 
87  const http::Request request; // Make a copy.
88  Future<http::Response> future; // Make a copy.
89  };
90 
91  std::queue<Item*> items;
92 
93  Option<http::Pipe::Reader> pipe; // Current pipe, if streaming.
94 };
95 
96 } // namespace process {
97 
98 #endif // __PROCESS_HTTP_PROXY_HPP__
void finalize() override
Invoked when a process is terminated.
Future< Response > request(const Request &request, bool streamedResponse=false)
Asynchronously sends an HTTP request to the process and returns the HTTP response once the entire res...
HttpProxy(const network::inet::Socket &_socket)
Definition: http.hpp:533
Definition: http_proxy.hpp:36
Definition: executor.hpp:48
Definition: http.hpp:612
void handle(const Future< http::Response > &future, const http::Request &request)
~HttpProxy() override
Definition: http_proxy.hpp:41
Definition: owned.hpp:36
Definition: process.hpp:505
void enqueue(const http::Response &response, const http::Request &request)