Bredbandskollen CLI  1.2
Asynchronous network task engine
webservertask.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 <stdexcept>
7 #include <map>
8 
9 #include "httptask.h"
10 #include "httpserverconnection.h"
11 #ifdef USE_GNUTLS
12 class SocketReceiver;
13 #endif
14 
16 class WebServerTask : public HttpTask {
17 public:
18  WebServerTask(const std::string &name, const TaskConfig &cfg=TaskConfig());
19 
20  double start() override;
21 
22  std::string headers(const std::string &code) {
23  return "HTTP/1.1 " + code + "\r\n" + fixed_response_headers;
24  }
25 
36  // "text/plain", data)
37  std::string setCookie(const std::string &name, const std::string &val,
38  long expiry = 0, const std::string &path = "/",
39  std::string domain = "") const;
40 
46  virtual HttpState newGetRequest(HttpServerConnection *,
47  const std::string &) {
48  return HttpState::CLOSE;
49  }
50 
56  virtual size_t sendResponseData(HttpServerConnection * /* conn */,
57  size_t /* bytes_left */) {
58  // Essentially, your overloaded code should do this:
59  // my_buffer = ...
60  // size_t sent = conn->sendData(my_buffer,
61  // std::min(bytes_left, sizeof my_buffer));
62  // return sent;
63  throw std::logic_error("sendResponseData not implemented");
64  }
65 
66  virtual HttpState newPostRequest(HttpServerConnection *conn,
67  const std::string &uri);
68 
73  virtual bool partialPostData(HttpServerConnection *conn,
74  const char *buffer, size_t len);
75 
78  virtual HttpState lastPostData(HttpServerConnection *conn,
79  const char *buffer, size_t len);
80 
85  virtual bool newWsRequest(HttpServerConnection *conn,
86  const std::string &uri);
87 
92  virtual void wsHandshakeFinished(HttpServerConnection *conn,
93  const std::string &uri);
94 
96  virtual HttpState preflightRequest(HttpServerConnection * /* conn */,
97  const std::string & /* uri */) {
98  return HttpState::CLOSE;
99  }
100 
101  void connAdded(SocketConnection *conn) override {
102  log() << "new connection id=" << conn->id();
103  }
104 
105  void connRemoved(SocketConnection *conn) override {
106  log() << "dropped connection " << conn->id();
107  }
108 
113  void setFixedHeaders(const std::string &hdr) {
114  fixed_response_headers = "Server: " + server_name + "\r\n" + hdr;
115  }
116  SocketConnection *newClient(int fd, const char *ip, uint16_t port,
117  ServerSocket *) final;
118 
121  static std::string corsHeader(HttpServerConnection *conn,
122  const std::string &domain);
123 
124  std::string webRoot() const {
125  return webroot;
126  }
127 
128  void setWebRoot(const std::string &path) {
129  webroot = path;
130  }
131 
132 #ifdef USE_GNUTLS
133  void newWorkerChannel(SocketReceiver *srv, unsigned int chan) override;
134 #endif
135 
136 protected:
137  TaskConfig the_config;
138 private:
139  std::string server_name, fixed_response_headers;
140  std::string webroot;
141 };
HTTP/1.1 server protocol.
Definition: httpserverconnection.h:28
Common API for HTTP server and client tasks.
Definition: httptask.h:12
std::ostream & log() const
Write a line of info log.
Definition: logger.h:328
Listen on a single socket for incoming connections.
Definition: serversocket.h:15
This class implements low-level socket connection operations. Inherit from it to implement protocols ...
Definition: socketconnection.h:47
Pass sockets and messages between processes.
Definition: socketreceiver.h:21
int id() const
Return unique connection ID if connected.
Definition: socket.h:79
Read configuration from file or string.
Definition: taskconfig.h:44
API for HTTP servers.
Definition: webservertask.h:16
virtual void wsHandshakeFinished(HttpServerConnection *conn, const std::string &uri)
Definition: webservertask.cpp:79
virtual HttpState preflightRequest(HttpServerConnection *, const std::string &)
Override this to implement a response to a preflight (OPTIONS) request.
Definition: webservertask.h:96
void connAdded(SocketConnection *conn) override
Definition: webservertask.h:101
void connRemoved(SocketConnection *conn) override
Definition: webservertask.h:105
std::string setCookie(const std::string &name, const std::string &val, long expiry=0, const std::string &path="/", std::string domain="") const
Return a string that may be inserted into the HTTP response headers.
Definition: webservertask.cpp:25
SocketConnection * newClient(int fd, const char *ip, uint16_t port, ServerSocket *) final
Definition: webservertask.cpp:52
virtual HttpState lastPostData(HttpServerConnection *conn, const char *buffer, size_t len)
Retrieve last part of a post message from client.
Definition: webservertask.cpp:68
virtual bool newWsRequest(HttpServerConnection *conn, const std::string &uri)
Request from client for a websocket upgrade.
Definition: webservertask.cpp:73
virtual bool partialPostData(HttpServerConnection *conn, const char *buffer, size_t len)
Retrieve part of a post message from client.
Definition: webservertask.cpp:63
virtual size_t sendResponseData(HttpServerConnection *, size_t)
Definition: webservertask.h:56
double start() override
Definition: webservertask.cpp:18
virtual HttpState newGetRequest(HttpServerConnection *, const std::string &)
Definition: webservertask.h:46
void newWorkerChannel(SocketReceiver *srv, unsigned int chan) override
Definition: webservertask.cpp:108
void setFixedHeaders(const std::string &hdr)
One or more HTTP response header lines that always should be sent.
Definition: webservertask.h:113
static std::string corsHeader(HttpServerConnection *conn, const std::string &domain)
Definition: webservertask.cpp:86