ESP32 HTTPS Server: /home/frank/Projekte/esp32/https_server/esp32_https_server/src/HTTPConnection.hpp Source File

1 #ifndef SRC_HTTPCONNECTION_HPP_

2 #define SRC_HTTPCONNECTION_HPP_

3 

4 #include <Arduino.h>

5 #include <IPAddress.h>

6 

7 #include <string>

8 #include <mbedtls/base64.h>

9 #include <hwcrypto/sha.h>

10 #include <functional>

11 

12 

13 #include "lwip/netdb.h"

14 #undef read

15 #include "lwip/sockets.h"

16 

17 #include "HTTPSServerConstants.hpp"

18 #include "ConnectionContext.hpp"

19 

20 #include "HTTPHeaders.hpp"

21 #include "HTTPHeader.hpp"

22 

23 #include "ResourceResolver.hpp"

24 #include "ResolvedResource.hpp"

25 

26 #include "ResourceNode.hpp"

27 #include "HTTPRequest.hpp"

28 #include "HTTPResponse.hpp"

29 

30 #include "WebsocketHandler.hpp"

31 #include "WebsocketNode.hpp"

32 

34 

39 public:

42 

44  virtual void closeConnection();

45  virtual bool isSecure();

47 

48  void loop();

51 

52 protected:

56 

57  virtual size_t writeBuffer(byte* buffer, size_t length);

58  virtual size_t readBytesToBuffer(byte* buffer, size_t length);

59  virtual bool canReadData();

60  virtual size_t pendingByteCount();

61 

62 

63  unsigned long _lastTransmissionTS;

64 

65 

66  unsigned long _shutdownTS;

67 

68 

69 

70 

71 

72 

73 

74 

75 

76 

77 

78 

79 

80 

81 

82  enum {

83 

84 

85 

86  STATE_UNDEFINED,

87 

88  STATE_INITIAL,

89 

90  STATE_REQUEST_FINISHED,

91 

92  STATE_HEADERS_FINISHED,

93 

94  STATE_BODY_FINISHED,

95 

96  STATE_WEBSOCKET,

97 

98  STATE_CLOSING,

99 

100  STATE_CLOSED,

101 

102  STATE_ERROR

103  } _connectionState;

104 

105  enum {

106  CSTATE_UNDEFINED,

107  CSTATE_ACTIVE,

108  CSTATE_CLOSED

109  } _clientState;

110 

111 private:

112  void raiseError(uint16_t code, std::string reason);

113  void readLine(int lengthLimit);

114 

115  bool isTimeoutExceeded();

116  void refreshTimeout();

117 

118  int updateBuffer();

119  size_t pendingBufferSize();

120 

121  void signalClientClose();

122  void signalRequestError();

123  size_t readBuffer(byte* buffer, size_t length);

124  size_t getCacheSize();

125  bool checkWebsocket();

126 

127 

128  char _receiveBuffer[HTTPS_CONNECTION_DATA_CHUNK_SIZE];

129 

130 

131  int _bufferProcessed;

132 

133  int _bufferUnusedIdx;

134 

135 

136  struct sockaddr _sockAddr;

137  socklen_t _addrLen;

138  int _socket;

139 

140 

142 

143 

144  struct {

145  std::string text = "";

146  bool parsingFinished = false;

147  } _parserLine;

148 

149 

150  std::string _httpMethod;

151  std::string _httpResource;

153 

154 

156 

157 

158  bool _isKeepAlive;

159 

160 

162 

163 };

164 

166 

168 

170 

171 }

172 

173 #endif

virtual IPAddress getClientIP()

Definition: HTTPConnection.cpp:65

void handleWebsocketHandshake(HTTPRequest *req, HTTPResponse *res)

Definition: HTTPConnection.cpp:652

Represents the request stream for an HTTP request.

Definition: HTTPRequest.hpp:22

Represents the response stream of an HTTP request.

Definition: HTTPResponse.hpp:25

void validationMiddleware(HTTPRequest *req, HTTPResponse *res, std::function< void()> next)

Definition: HTTPConnection.cpp:622

bool isError()

Definition: HTTPConnection.cpp:99

Definition: WebsocketHandler.hpp:34

This class is used internally to resolve a string URL to the corresponding HTTPNode.

Definition: ResourceResolver.hpp:22

Definition: WebsocketInputStreambuf.hpp:21

Internal class to handle the state of a connection.

Definition: ConnectionContext.hpp:18

Represents a single open connection for the plain HTTPServer, without TLS.

Definition: HTTPConnection.hpp:38

bool isClosed()

Definition: HTTPConnection.cpp:92

std::string websocketKeyResponseHash(std::string const &key)

Definition: HTTPConnection.cpp:664

virtual int initialize(int serverSocketID, HTTPHeaders *defaultHeaders)

Definition: HTTPConnection.cpp:33

Definition: ConnectionContext.cpp:3