Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Dec 29, 2023. It is now read-only.

Latest commit

 

History

History
History
188 lines (159 loc) · 4.7 KB

File metadata and controls

188 lines (159 loc) · 4.7 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "HTTPResponse.hpp"
#include <Arduino.h>
#include "lwip/sockets.h"
namespace httpsserver {
HTTPResponse::HTTPResponse(ConnectionContext * con):
_con(con) {
// Default status code is 200 OK
_statusCode = 200;
_statusText = "OK";
_headerWritten = false;
_isError = false;
_responseCacheSize = con->getCacheSize();
_responseCachePointer = 0;
if (_responseCacheSize > 0) {
HTTPS_LOGD("Creating buffered response, size: %d", _responseCacheSize);
_responseCache = new byte[_responseCacheSize];
} else {
HTTPS_LOGD("Creating non-buffered response");
_responseCache = NULL;
}
}
HTTPResponse::~HTTPResponse() {
if (_responseCache != NULL) {
delete[] _responseCache;
}
_headers.clearAll();
}
void HTTPResponse::setStatusCode(uint16_t statusCode) {
_statusCode = statusCode;
}
void HTTPResponse::setStatusText(std::string const &statusText) {
_statusText = statusText;
}
uint16_t HTTPResponse::getStatusCode() {
return _statusCode;
}
std::string HTTPResponse::getStatusText() {
return _statusText;
}
void HTTPResponse::setHeader(std::string const &name, std::string const &value) {
_headers.set(new HTTPHeader(name, value));
}
std::string HTTPResponse::getHeader(std::string const &name) {
HTTPHeader * h = _headers.get(name);
if (h != NULL) {
return h->_value;
} else {
return std::string();
}
}
bool HTTPResponse::isHeaderWritten() {
return _headerWritten;
}
bool HTTPResponse::isResponseBuffered() {
return _responseCache != NULL;
}
void HTTPResponse::finalize() {
if (isResponseBuffered()) {
drainBuffer();
}
}
/**
* Writes a string to the response. May be called several times.
*/
void HTTPResponse::printStd(const std::string &str) {
write((uint8_t*)str.c_str(), str.length());
}
/**
* Writes bytes to the response. May be called several times.
*/
size_t HTTPResponse::write(const uint8_t *buffer, size_t size) {
if(!isResponseBuffered()) {
printHeader();
}
return writeBytesInternal(buffer, size);
}
/**
* Writes a single byte to the response.
*/
size_t HTTPResponse::write(uint8_t b) {
if(!isResponseBuffered()) {
printHeader();
}
byte ba[] = {b};
return writeBytesInternal(ba, 1);
}
/**
* If not already done, writes the header.
*/
void HTTPResponse::printHeader() {
if (!_headerWritten) {
HTTPS_LOGD("Printing headers");
// Status line, like: "HTTP/1.1 200 OK\r\n"
std::string statusLine = "HTTP/1.1 " + intToString(_statusCode) + " " + _statusText + "\r\n";
printInternal(statusLine, true);
// Each header, like: "Host: myEsp32\r\n"
std::vector<HTTPHeader *> * headers = _headers.getAll();
for(std::vector<HTTPHeader*>::iterator header = headers->begin(); header != headers->end(); ++header) {
printInternal((*header)->print()+"\r\n", true);
}
printInternal("\r\n", true);
_headerWritten=true;
}
}
/**
* This method can be called to cancel the ongoing transmission and send the error page (if possible)
*/
void HTTPResponse::error() {
_con->signalRequestError();
}
void HTTPResponse::printInternal(const std::string &str, bool skipBuffer) {
writeBytesInternal((uint8_t*)str.c_str(), str.length(), skipBuffer);
}
size_t HTTPResponse::writeBytesInternal(const void * data, int length, bool skipBuffer) {
if (!_isError) {
if (isResponseBuffered() && !skipBuffer) {
// We are buffering ...
if(length <= _responseCacheSize - _responseCachePointer) {
// ... and there is space left in the buffer -> Write to buffer
size_t end = _responseCachePointer + length;
size_t i = 0;
while(_responseCachePointer < end) {
_responseCache[_responseCachePointer++] = ((byte*)data)[i++];
}
// Returning skips the SSL_write below
return length;
} else {
// .., and the buffer is too small. This is the point where we switch from
// caching to streaming
if (!_headerWritten) {
setHeader("Connection", "close");
}
drainBuffer(true);
}
}
return _con->writeBuffer((byte*)data, length);
} else {
return 0;
}
}
void HTTPResponse::drainBuffer(bool onOverflow) {
if (!_headerWritten) {
if (_responseCache != NULL && !onOverflow) {
_headers.set(new HTTPHeader("Content-Length", intToString(_responseCachePointer)));
}
printHeader();
}
if (_responseCache != NULL) {
HTTPS_LOGD("Draining response buffer");
// Check for 0 as it may be an overflow reaction without any data that has been written earlier
if(_responseCachePointer > 0) {
// FIXME: Return value?
_con->writeBuffer((byte*)_responseCache, _responseCachePointer);
}
delete[] _responseCache;
_responseCache = NULL;
}
}
} /* namespace httpsserver */
Morty Proxy This is a proxified and sanitized view of the page, visit original site.