Bredbandskollen CLI  1.2
Asynchronous network task engine
httpconnection.h
1 // Copyright (c) 2018 IIS (The Internet Foundation in Sweden)
2 // Written by Göran Andersson <initgoran@gmail.com>
3 
4 // This class implements HTTP functionality common to server and client,
5 // mainly the websocket protocol.
6 
7 #pragma once
8 
9 #include "../framework/socketconnection.h"
10 
12 public:
13 
14  // 10 for HTTP/1.0, 11 for 1.1, 20 for 2.0 etc.
15  unsigned int httpVersion() {
16  return http_version;
17  }
18 
19  void sendWsMessage(const std::string &msg);
20  void sendWsBinary(const char *buf, size_t len);
21 
22  // Initiate sending a very large message. The owner task's sendWsData will
23  // be called repetedly until all data has been sent.
24  void startWsBinStream(size_t len) {
25  startWsStream(len, true);
26  }
27  void startWsTxtStream(size_t len) {
28  startWsStream(len, false);
29  }
30  void abortWsStream() {
31  closeMe();
32  }
33  void sendWsClose(uint16_t code, std::string msg);
34  void setOwner(Task *new_owner) override;
35 
36  // By default, incoming messages will be buffered and not delivered (through
37  // wsBinMessage or wsTextMessage) until the complete message has arrived.
38  // Call this from within the owner task's wsBinHeader or wsTextHeader
39  // callback to get the response "streamed", i.e. the response will be
40  // delivered in "chunks" to wsBinData/wsTextData as they arrive. Note that
41  // the data may be masked, and you will have to unmask it yourself.
42  void streamWsResponse() {
43  stream_incoming = true;
44  }
45  // Returns pointer to 4 byte incoming mask,
46  // or nullptr if the response isn't masked.
47  const unsigned char *responseMask() const {
48  if (incoming_is_masked)
49  return incoming_mask;
50  else
51  return nullptr;
52  }
53 
54  size_t wsIncomingBytesLeft() const {
55  return bytes_to_receive;
56  }
57  size_t wsBytesReceived() const {
58  return tot_to_receive-bytes_to_receive;
59  }
60  size_t wsOutgoingBytesLeft() const {
61  return bytes_to_send;
62  }
63  size_t wsBytesSent() const {
64  return tot_to_send-bytes_to_send;
65  }
66  bool isWebsocket() const {
67  return is_websocket;
68  }
69 protected:
70  HttpConnection(const std::string &label, Task *owner,
71  const std::string &hostname, uint16_t port,
72  uint16_t iptype = 0, struct addrinfo *local_addr = nullptr) :
73  SocketConnection(label, owner, hostname, port, iptype, local_addr) {
74  }
75  HttpConnection(const std::string &label, Task *owner, int fd,
76  const char *ip, uint16_t port) :
77  SocketConnection(label, owner, fd, ip, port) {
78  }
79 
80  void set_http_version(unsigned int major, unsigned int minor) {
81  http_version = 10U*major + minor;
82  }
83 
84  void send_ws_handshake(const std::string &key);
85  void send_ws_bin_header(size_t len);
86  void send_ws_txt_header(size_t len);
87  void send_ws_pong();
88  PollState incoming_ws_data(const char *buf, size_t len);
89  PollState incoming_ws_header(const char *buf, size_t len);
90  PollState wsReadData(const char *buf, size_t len);
91  PollState wsWriteData();
92 
93  // Incoming buffer
94  std::string buffer;
95 
96  // Everything below is websocket stuff:
97  bool is_websocket = false;
98 private:
99  void startWsStream(size_t len, bool is_binary = true);
100  // For fragmented messages:
101  unsigned char current_opcode = 0;
102  bool receiving_message = false;
103  bool sending_message = false;
104  bool output_is_binary = false;
105  bool stream_incoming = false;
106  bool incoming_is_masked = false;
107  bool incoming_is_binary = false;
108  size_t bytes_to_send, tot_to_send;
109  size_t bytes_to_receive, tot_to_receive;
110  unsigned char incoming_mask[4];
111  unsigned int http_version = 11; // 11 for 1.1, 20 for 2.0 etc.
112 };
Definition: httpconnection.h:11
void setOwner(Task *new_owner) override
Set the given task as owner of the socket.
Definition: httpconnection.cpp:358
std::string label() const
Return the object's log label.
Definition: logger.h:251
This class implements low-level socket connection operations. Inherit from it to implement protocols ...
Definition: socketconnection.h:47
SocketConnection(const std::string &label, Task *owner, const std::string &hostname, uint16_t port, uint16_t iptype=0, struct addrinfo *local_addr=nullptr)
Definition: socketconnection.cpp:25
Task * owner() const
Return task owning the socket.
Definition: socket.h:27
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
void closeMe()
Tell the network engine that the connection should be closed.
Definition: socket.h:178
The purpose of a task is to manage socket connections, and/or to execute timers.
Definition: task.h:39
PollState
Definition: pollstate.h:11