MakerLabs ACM
request_handler.h
Go to the documentation of this file.
1 /*
2  * Copyright Paul Reimer, 2018
3  *
4  * This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 Unported License.
5  * To view a copy of this license, visit
6  * https://creativecommons.org/licenses/by-nc-sa/4.0/
7  * or send a letter to
8  * Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
9  */
10 
11 #pragma once
12 
13 #include "requests.h"
14 
16 
17 #if REQUESTS_SUPPORT_JSON
18 #include "json_emitter.h"
19 
20 #if REQUESTS_SUPPORT_JSON_TO_FLATBUFFERS
21 #include "json_to_flatbuffers_converter.h"
22 #endif // REQUESTS_SUPPORT_JSON_TO_FLATBUFFERS
23 #endif // REQUESTS_SUPPORT_JSON
24 
25 #include <experimental/string_view>
26 #include <string>
27 #include <vector>
28 
29 #ifdef REQUESTS_USE_CURL
30 #include "curl/curl.h"
31 using curl_slist = struct curl_slist;
32 #endif // REQUESTS_USE_CURL
33 
34 namespace Requests {
35 
36 using ResponseFlatbuffer = flatbuffers::DetachedBuffer;
37 using ServerSentEventFlatbuffer = flatbuffers::DetachedBuffer;
38 
39 class RequestManager;
40 
42 {
43  friend class RequestManager;
44 
45  using string = std::string;
47 
48  using flatbuf = std::vector<uint8_t>;
49 
51  const RequestIntentFlatbufferRef& _request_intent_buf_ref
52  );
54 
55  // Move-only class with default move behaviour
56  RequestHandler(RequestHandler&&) = default;
58  RequestHandler(const RequestHandler&) = delete;
59  RequestHandler& operator= (const RequestHandler&) = delete;
60 
63 
64  string errbuf;
65  short response_code = -1;
67 
68 #ifdef REQUESTS_USE_CURL
69  curl_slist *slist = nullptr;
70 #endif // REQUESTS_USE_CURL
71 
72 #ifdef REQUESTS_USE_SH2LIB
73  bool connected = false;
74  string connected_hostname;
75  bool finished = false;
76  int body_sent_byte_count = 0;
77  string_view _path;
78 #endif // REQUESTS_USE_SH2LIB
79 
80  // Must be public to be accessible from c-style callback
81 #ifdef REQUESTS_USE_SH2LIB
82  auto header_callback(const string_view k, const string_view v)
83  -> size_t;
84  auto read_callback(const size_t max_chunk_size)
85  -> string_view;
86 #endif // REQUESTS_USE_SH2LIB
87 #ifdef REQUESTS_USE_CURL
88  auto header_callback(const string_view chunk)
89  -> size_t;
90 #endif // REQUESTS_USE_CURL
91  auto write_callback(const string_view chunk)
92  -> size_t;
93  auto finish_callback()
94  -> void;
95 
96 private:
97  auto create_partial_response(const string_view chunk)
99 
100  std::unique_ptr<ServerSentEventsEmitter> server_sent_events_emitter;
101 
102 #if REQUESTS_SUPPORT_JSON
103  using JsonEmitter = Json::JsonEmitter;
104  std::unique_ptr<JsonEmitter> json_path_emitter;
105 
106 #if REQUESTS_SUPPORT_JSON_TO_FLATBUFFERS
107  using JsonToFlatbuffersConverter = JsonFlatbuffers::JsonToFlatbuffersConverter;
108  std::unique_ptr<JsonToFlatbuffersConverter> flatbuffers_path_emitter;
109 #endif // REQUESTS_SUPPORT_JSON_TO_FLATBUFFERS
110 #endif // REQUESTS_SUPPORT_JSON
111 
112  string _req_url;
113 };
114 
115 } // namespace Requests
flatbuffers::DetachedBuffer ServerSentEventFlatbuffer
Definition: request_handler.h:37
RequestHandler & operator=(RequestHandler &&)=default
std::vector< uint8_t > MutableRequestIntentFlatbuffer
Definition: requests.h:29
flatbuffers::BufferRef< RequestIntent > RequestIntentFlatbufferRef
Definition: requests.h:28
Definition: request_manager.h:39
flatbuffers::DetachedBuffer ResponseFlatbuffer
Definition: request_handler.h:36
Definition: json_emitter.h:27
Definition: request_handler.h:41
string response_buffer
Definition: request_handler.h:66
std::string string
Definition: oom_killer_actor_behaviour.cpp:24
std::experimental::string_view string_view
Definition: actor_model.cpp:16
RequestIntent * request_intent
Definition: request_handler.h:61
Definition: requests.fbs:68
string errbuf
Definition: request_handler.h:64
auto write_callback(const string_view chunk) -> size_t
Definition: request_handler.cpp:118
short response_code
Definition: request_handler.h:65
Definition: curl_library_info.cpp:15
std::vector< uint8_t > flatbuf
Definition: request_handler.h:48
std::experimental::string_view string_view
Definition: request_handler.h:46
auto finish_callback() -> void
Definition: request_handler.cpp:279
MutableRequestIntentFlatbuffer request_intent_mutable_buf
Definition: request_handler.h:62
~RequestHandler()
Definition: request_handler.cpp:108
RequestHandler(const RequestIntentFlatbufferRef &_request_intent_buf_ref)
Definition: request_handler.cpp:30