staticlib_pion  2.0.2
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
http_request_reader.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015, alex at staticlibs.net
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // ---------------------------------------------------------------------
18 // pion: a Boost C++ framework for building lightweight HTTP interfaces
19 // ---------------------------------------------------------------------
20 // Copyright (C) 2007-2014 Splunk Inc. (https://github.com/splunk/pion)
21 //
22 // Distributed under the Boost Software License, Version 1.0.
23 // See http://www.boost.org/LICENSE_1_0.txt
24 //
25 
26 #ifndef STATICLIB_PION_HTTP_REQUEST_READER_HPP
27 #define STATICLIB_PION_HTTP_REQUEST_READER_HPP
28 
29 #include <chrono>
30 #include <functional>
31 #include <memory>
32 
33 #include "asio.hpp"
34 
35 #include "staticlib/config.hpp"
36 
40 
41 namespace staticlib {
42 namespace pion {
43 
44 class http_server;
45 
50 private:
54  http_server& server;
55 
59  tcp_connection_ptr tcp_conn;
60 
64  uint32_t read_timeout_millis;
65 
69  http_request_ptr request;
70 
71 public:
72 
79  http_request_reader(http_server& srv, tcp_connection_ptr& tcp_conn,
80  uint32_t read_timeout) :
81  http_parser(true),
82  server(srv),
83  tcp_conn(tcp_conn),
84  read_timeout_millis(read_timeout),
85  request(new http_request()) {
86  request->set_remote_ip(tcp_conn->get_remote_ip());
87  request->set_request_reader(this);
88  }
89 
96 
104 
108  static void receive(std::unique_ptr<http_request_reader> self);
109 
110 private:
111 
118  static void consume_bytes(std::unique_ptr<http_request_reader> self,
119  const std::error_code& read_error, std::size_t bytes_read);
120 
126  static void consume_bytes(std::unique_ptr<http_request_reader> self);
127 
133  static void read_bytes_with_timeout(std::unique_ptr<http_request_reader> self);
134 
140  void handle_read_error(const std::error_code& read_error);
141 
148  void finished_parsing_headers(const std::error_code& ec, sl::support::tribool& rc);
149 
155  void finished_reading(const std::error_code& ec);
156 
157 };
158 
159 } // namespace
160 }
161 
162 #endif // STATICLIB_PION_HTTP_REQUEST_READER_HPP