forked from fhessel/esp32_https_server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTPServer.hpp
More file actions
127 lines (110 loc) · 3.68 KB
/
HTTPServer.hpp
File metadata and controls
127 lines (110 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#ifndef SRC_HTTPSERVER_HPP_
#define SRC_HTTPSERVER_HPP_
// Standard library
#include <string>
// Arduino stuff
#include <Arduino.h>
#ifndef HTTPS_DISABLE_IPV4
#include <IPAddress.h>
#ifdef HTTPS_DISABLE_IPV4
#error You cannot HTTPS_DISABLE_IPV4 and HTTPS_DISABLE_IPV6 at the same time
#endif
#endif
#ifndef HTTPS_DISABLE_IPV6
#include <IPv6Address.h>
#ifdef HTTPS_DISABLE_IPV4
#error You cannot HTTPS_DISABLE_IPV4 and HTTPS_DISABLE_IPV6 at the same time
#endif
#endif
// Required for sockets
#include "lwip/netdb.h"
#undef read
#include "lwip/sockets.h"
#include "lwip/inet.h"
// Internal includes
#include "HTTPSServerConstants.hpp"
#include "HTTPHeaders.hpp"
#include "HTTPHeader.hpp"
#include "ResourceNode.hpp"
#include "ResourceResolver.hpp"
#include "ResolvedResource.hpp"
#include "HTTPConnection.hpp"
namespace httpsserver {
/**
* \brief Main implementation for the plain HTTP server. Use HTTPSServer for TLS support
*/
class HTTPServer : public ResourceResolver {
public:
#ifndef HTTPS_DISABLE_IPV4
/**
* \brief Create a server instance that binds to an IPv4 address
*
* \param bindAddress IPAddress to bind to. Use IPAddress() to bind to all IPv4 interfaces.
* \param port TCP port to run the server on. Defaults to 80 (HTTP default)
* \param maxConnections Maximum number of parallel connections handled by the server. Defaults to 8.
*/
HTTPServer(const IPAddress bindAddress = IPAddress(), const uint16_t port = 80, const uint8_t maxConnections = 8);
#endif
#ifndef HTTPS_DISABLE_IPV6
#ifdef HTTPS_DISABLE_IPV4
HTTPServer(const IPv6Address bindAddress, const uint16_t port = 80, const uint8_t maxConnections = 8);
#else
/**
* \brief Create a server instance that binds to an IPv6 address
*
* \param bindAddress IPv6Address to bind to. Use IPv6Address() to bind to all IPv6 interfaces.
* \param port TCP port to run the server on. Defaults to 80 (HTTP default)
* \param maxConnections Maximum number of parallel connections handled by the server. Defaults to 8.
*/
HTTPServer(const IPv6Address bindAddress = IPv6Address(), const uint16_t port = 80, const uint8_t maxConnections = 8);
#endif
#endif
virtual ~HTTPServer();
uint8_t start();
void stop();
bool isRunning();
void loop();
void setDefaultHeader(std::string name, std::string value);
protected:
// Static configuration. Port, keys, etc. ====================
// Certificate that should be used (includes private key)
const uint16_t _port;
// Max parallel connections that the server will accept
const uint8_t _maxConnections;
#ifndef HTTPS_DISABLE_IPV4
/**
* \brief IPv4 addresses to bind to (0 = all IPv4 interfaces)
*
* To enable IPv4, add at least one element to this vector.
* The content cannot be changed while the server is _running
*/
std::vector<in_addr_t> _bindAddressesV4;
#endif
#ifndef HTTPS_DISABLE_IPV6
/**
* \brief IPv6 addresses to bind to (0 = all IPv6 interfaces)
*
* To enable IPv6, add at least one element to this vector.
* The content cannot be changed while the server is _running
*/
std::vector<in6_addr> _bindAddressesV6;
#endif
//// Runtime data ============================================
// The array of connections that are currently active
HTTPConnection ** _connections;
// Status of the server: Are we running, or not?
boolean _running;
// The server socket
int _socket;
// The server socket address, that our service is bound to
sockaddr _sock_addr;
// Headers that are included in every response
HTTPHeaders _defaultHeaders;
// Setup functions
virtual uint8_t setupSocket();
virtual void teardownSocket();
// Helper functions
virtual int createConnection(int idx);
};
}
#endif /* SRC_HTTPSERVER_HPP_ */