Bredbandskollen CLI  1.2
Asynchronous network task engine
httpclientconnection.h
1 // Copyright (c) 2018 IIS (The Internet Foundation in Sweden)
2 // Written by Göran Andersson <initgoran@gmail.com>
3 
4 #pragma once
5 
6 #include <map>
7 #include <sstream>
8 
9 #include "httpconnection.h"
10 
11 class HttpClientTask;
12 
40 public:
41  // TODO: take a HttpHost as a parameter!
42  HttpClientConnection(const std::string &label, HttpClientTask *task,
43  const std::string &hostname, uint16_t port,
44  const std::string &http_hostname = std::string(),
45  uint16_t iptype = 0,
46  struct addrinfo *local_addr = nullptr);
47 
48  PollState connected() final;
49  PollState readData(char *buf, size_t len) final;
50 
52  PollState writeData() final;
53 
59  buffer_response = false;
60  }
61 
64  unsigned int httpStatus() const {
65  return response_http_status;
66  }
67 
69  std::string contentType() const {
70  auto p = http_response_headers.find("content-type");
71  if (p == http_response_headers.end())
72  return std::string();
73  else
74  return p->second;
75  }
76 
77  const std::string &rawHttpHeader() const {
78  return response_header;
79  }
80 
81  const std::multimap<std::string, std::string> &responseHeaders() const {
82  return http_response_headers;
83  }
84 
85  size_t remainingContentLength() const {
86  return bytes_left_to_read;
87  }
88 
89  std::string contents() const {
90  return buffer;
91  }
92 
94  void get(const std::string &url);
95 
99  void post(const std::string &url, size_t len);
100 
101  // As above, but with first chunk of data available. Using this method
102  // may save one call to doPost(), i.e. one less send system call.
103  /*
104  void post(const std::string &url, size_t len,
105  const char *buf, size_t buffer_len, size_t lspace = 0);
106  */
107 
110  void post(const std::string &url, const std::string &data);
111 
113  void ws_get(const std::string &url);
114 
117  void pass() {
118  current_request = "pass";
119  }
120  bool idle() const {
121  return current_request.empty() ||
122  current_request == "pass";
123  }
124 
125  static std::string urlencode(const std::string &val);
126  static void addUrlPars(std::string &url,
127  const std::map<std::string, std::string> &pars);
128  std::string cacheLabel() override;
129 
130  void setOwner(Task *new_owner) override;
131 private:
132  std::string real_hostname;
133  // A weak pointer to the owner task. Dangerous. However, the EventLoop will
134  // kill all connections (i.e. us) before killing the owner task.
135  HttpClientTask *owner_task;
136  std::string proto_hostname;
137  std::string current_request, current_uri;
138 
139  // Set if this is a websocket connection:
140  std::string websocket_rkey;
141 
142  // Used when reading response headers:
143  std::string response_header;
144 
145  const std::string::size_type max_header_length = 20000;
146 
147  bool response_header_complete = false;
148  // Set when response headers are parsed:
149  bool response_is_chunked;
150  bool final_chunk;
151  bool buffer_response = true;
152  size_t bytes_left_to_read = 0;
153  size_t bytes_left_to_post = 0;
154  unsigned int response_http_status = 0;
155  std::string chunked_info;
156 
157  bool parseResponseHeaders();
158  std::multimap<std::string, std::string> http_response_headers;
159  bool parseChunkedEncodingLength(char *&buf, size_t &len);
160 };
HTTP/1.1 client protocol.
Definition: httpclientconnection.h:39
PollState writeData() final
For HTTP POST.
Definition: httpclientconnection.cpp:272
void doStreamResponse()
Definition: httpclientconnection.h:58
void post(const std::string &url, size_t len)
Definition: httpclientconnection.cpp:376
unsigned int httpStatus() const
Definition: httpclientconnection.h:64
void get(const std::string &url)
May be called in the newRequest callback:
Definition: httpclientconnection.cpp:317
void ws_get(const std::string &url)
May be called in the newRequest callback to open a websocket.
Definition: httpclientconnection.cpp:328
std::string contentType() const
Return value of first Content-Type header, empty string on failure.
Definition: httpclientconnection.h:69
PollState connected() final
Will be called when the connection is established.
Definition: httpclientconnection.cpp:31
PollState readData(char *buf, size_t len) final
Callback, called when data has arrived; len > 0.
Definition: httpclientconnection.cpp:102
std::string cacheLabel() override
Return the socket's cache group, or an empty string.
Definition: httpclientconnection.cpp:388
void setOwner(Task *new_owner) override
Set the given task as owner of the socket.
Definition: httpclientconnection.cpp:392
void pass()
Definition: httpclientconnection.h:117
API for HTTP clients.
Definition: httpclienttask.h:11
Definition: httpconnection.h:11
std::string label() const
Return the object's log label.
Definition: logger.h:251
std::string hostname() const
Return name of the host to which the socket is supposed to connect.
Definition: socket.h:32
uint16_t port() const
Return port number to which the socket is supposed to connect.
Definition: socket.h:37
The purpose of a task is to manage socket connections, and/or to execute timers.
Definition: task.h:39
PollState
Definition: pollstate.h:11