Bredbandskollen CLI  1.2
Asynchronous network task engine
httpclienttask.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 "httptask.h"
7 #include "cookiemanager.h"
8 #include "httpclientconnection.h"
9 
11 class HttpClientTask : public HttpTask {
12 public:
13  HttpClientTask(const std::string &name, const HttpHost &httpserver);
14 
16  virtual void newRequest(HttpClientConnection *) {
17  }
18 
27  virtual bool headerComplete(HttpClientConnection *);
28 
37  virtual bool websocketUpgrade(HttpClientConnection *);
38 
41 
48  virtual void payload(HttpClientConnection *, char *, size_t ) {
49  }
50 
58  virtual size_t doPost(HttpClientConnection *conn, size_t len);
59 
61  std::string serverHost() const {
62  return peer.hostname;
63  }
64 
72  virtual std::string httpHeaderLines(const std::string &uri) const {
73  if (!peer.cmgr)
74  return user_agent_header;
75  return peer.cmgr->httpHeaderLine(peer.hostname, uri) +
76  user_agent_header;
77  }
78 
79  void setCookie(const std::string &header_line, const std::string &uri) {
80  if (peer.cmgr)
81  peer.cmgr->setCookie(header_line, peer.hostname, uri);
82  }
83  virtual std::string cacheLabel() {
84  return peer.hostname + std::to_string(peer.port);
85  }
86  void setUserAgentString(const std::string &s) {
87  if (s.find_first_of("\r\n") == std::string::npos)
88  user_agent_header = "User-Agent: " + s + "\r\n";
89  }
90 
95  static bool setLocalAddress(const std::string &addr, uint16_t iptype);
96 
97  static const std::string &getLocalAddress() {
98  return local_address;
99  }
100 protected:
101  bool createNewConnection();
102 
103  std::string proxyHost() const {
104  return peer.proxyHost;
105  }
106  uint32_t proxyPort() const {
107  return peer.proxyPort;
108  }
109  CookieManager *cookieMgr() const {
110  return peer.cmgr;
111  }
112  void setServer(const std::string hostname, uint16_t port_number = 80) {
113  peer.hostname = hostname;
114  peer.port = port_number;
115  }
116 private:
117  HttpHost peer;
118  std::string user_agent_header;
119  std::string real_server;
120  static std::string local_address;
121  static struct addrinfo *local_ip;
122 };
Definition: cookiemanager.h:14
HTTP/1.1 client protocol.
Definition: httpclientconnection.h:39
API for HTTP clients.
Definition: httpclienttask.h:11
virtual bool requestComplete(HttpClientConnection *)=0
Called when response has been fully read.
virtual void payload(HttpClientConnection *, char *, size_t)
Data has arrived from the server.
Definition: httpclienttask.h:48
std::string serverHost() const
Return server's host name.
Definition: httpclienttask.h:61
static bool setLocalAddress(const std::string &addr, uint16_t iptype)
Bind all subsequent client sockets to the given local ip address.
Definition: httpclienttask.cpp:43
virtual bool headerComplete(HttpClientConnection *)
Called when response headers are fully read and parsed, except for websocket upgrades,...
Definition: httpclienttask.cpp:28
virtual bool websocketUpgrade(HttpClientConnection *)
Called after succesful websocket upgrade.
Definition: httpclienttask.cpp:36
virtual std::string httpHeaderLines(const std::string &uri) const
Extra header lines to be added to outgoing requests.
Definition: httpclienttask.h:72
virtual void newRequest(HttpClientConnection *)
Initiate next request, or ignore to close connection.
Definition: httpclienttask.h:16
virtual size_t doPost(HttpClientConnection *conn, size_t len)
Send POST data, return number of bytes sent.
Definition: httpclienttask.cpp:32
The host name and port number of a HTTP host.
Definition: httphost.h:17
Common API for HTTP server and client tasks.
Definition: httptask.h:12