@@ -38,7 +38,7 @@ HTTPSConnection::~HTTPSConnection() {
38
38
*
39
39
* The call WILL BLOCK if accept(serverSocketID) blocks. So use select() to check for that in advance.
40
40
*/
41
- int HTTPSConnection::initialize (int serverSocketID, SSL_CTX * sslCtx, HTTPHeaders *defaultHeaders) {
41
+ int HTTPSConnection::initialize (int serverSocketID, SSL_CTX * sslCtx, HTTPHeaders *defaultHeaders, bool isSSLSocket ) {
42
42
if (_connectionState == STATE_UNDEFINED) {
43
43
_defaultHeaders = defaultHeaders;
44
44
_socket = accept (serverSocketID, (struct sockaddr * )&_sockAddr, &_addrLen);
@@ -47,32 +47,45 @@ int HTTPSConnection::initialize(int serverSocketID, SSL_CTX * sslCtx, HTTPHeader
47
47
if (_socket >= 0 ) {
48
48
HTTPS_DLOGHEX (" [-->] New connection. Socket fid is: " , _socket);
49
49
50
- _ssl = SSL_new (sslCtx);
51
-
52
- if (_ssl) {
53
- // Bind SSL to the socket
54
- int success = SSL_set_fd (_ssl, _socket);
55
- if (success) {
56
-
57
- // Perform the handshake
58
- success = SSL_accept (_ssl);
59
- if (success) {
60
- _connectionState = STATE_INITIAL;
61
- _httpHeaders = new HTTPHeaders ();
62
- refreshTimeout ();
63
- return _socket;
64
- } else {
65
- HTTPS_DLOG (" [ERR] SSL_accept failed. Aborting handshake." );
66
- }
67
- } else {
68
- HTTPS_DLOG (" [ERR] SSL_set_fd failed. Aborting handshake." );
69
- }
70
- } else {
71
- HTTPS_DLOG (" [ERR] SSL_new failed. Aborting handshake." );
72
- }
50
+ if (isSSLSocket) { // HTTPS
51
+
52
+ _ssl = SSL_new (sslCtx);
53
+
54
+ if (_ssl) {
55
+ // Bind SSL to the socket
56
+ int success = SSL_set_fd (_ssl, _socket);
57
+ if (success) {
58
+
59
+ // Perform the handshake
60
+ success = SSL_accept (_ssl);
61
+ if (success) {
62
+ _connectionState = STATE_INITIAL;
63
+ _httpHeaders = new HTTPHeaders ();
64
+ refreshTimeout ();
65
+ return _socket;
66
+ } else {
67
+ HTTPS_DLOG (" [ERR] SSL_accept failed. Aborting handshake." );
68
+ }
69
+ } else {
70
+ HTTPS_DLOG (" [ERR] SSL_set_fd failed. Aborting handshake." );
71
+ }
72
+ } else {
73
+ HTTPS_DLOG (" [ERR] SSL_new failed. Aborting handshake." );
74
+ }
75
+
76
+ } else { // HTTP
77
+
78
+ _connectionState = STATE_INITIAL;
79
+ _httpHeaders = new HTTPHeaders ();
80
+ refreshTimeout ();
81
+ return _socket;
82
+
83
+ }
84
+
73
85
} else {
74
86
HTTPS_DLOG (" [ERR] Could not accept() new connection" );
75
87
}
88
+
76
89
_connectionState = STATE_ERROR;
77
90
_clientState = CSTATE_ACTIVE;
78
91
@@ -214,41 +227,90 @@ int HTTPSConnection::updateBuffer() {
214
227
// start at 0x1000, so we need to use _socket+1 here
215
228
select (_socket + 1 , &sockfds, NULL , NULL , &timeout);
216
229
217
- if (FD_ISSET (_socket, &sockfds) || SSL_pending (_ssl) > 0 ) {
218
-
219
- HTTPS_DLOGHEX (" [ ] There is data on the connection socket. fid=" , _socket)
220
-
221
- // The return code of SSL_read means:
222
- // > 0 : Length of the data that has been read
223
- // < 0 : Error
224
- // = 0 : Connection closed
225
- int readReturnCode = SSL_read (
226
- _ssl,
227
- // Only after the part of the buffer that has not been processed yet
228
- _receiveBuffer + sizeof (char ) * _bufferUnusedIdx,
229
- // Only append up to the end of the buffer
230
- HTTPS_CONNECTION_DATA_CHUNK_SIZE - _bufferUnusedIdx
231
- );
232
-
233
- if (readReturnCode > 0 ) {
234
- _bufferUnusedIdx += readReturnCode;
235
- refreshTimeout ();
236
- return readReturnCode;
237
-
238
- } else if (readReturnCode == 0 ) {
239
- // The connection has been closed by the client
240
- _clientState = CSTATE_CLOSED;
241
- HTTPS_DLOGHEX (" [ x ] Client closed connection, fid=" , _socket);
242
- // TODO: If we are in state websocket, we might need to do something here
243
- return 0 ;
244
- } else {
245
- // An error occured
246
- _connectionState = STATE_ERROR;
247
- HTTPS_DLOGHEX (" [ERR] An SSL error occured, fid=" , _socket);
248
- closeConnection ();
249
- return -1 ;
250
- }
251
- } // data pending
230
+ if (_ssl) { // HTTPS
231
+
232
+ if (FD_ISSET (_socket, &sockfds) || SSL_pending (_ssl) > 0 ) {
233
+
234
+ HTTPS_DLOGHEX (" [ ] There is data on the connection socket. fid=" , _socket)
235
+
236
+ int readReturnCode;
237
+
238
+ // The return code of SSL_read means:
239
+ // > 0 : Length of the data that has been read
240
+ // < 0 : Error
241
+ // = 0 : Connection closed
242
+ readReturnCode = SSL_read (
243
+ _ssl,
244
+ // Only after the part of the buffer that has not been processed yet
245
+ _receiveBuffer + sizeof (char ) * _bufferUnusedIdx,
246
+ // Only append up to the end of the buffer
247
+ HTTPS_CONNECTION_DATA_CHUNK_SIZE - _bufferUnusedIdx
248
+ );
249
+
250
+ if (readReturnCode > 0 ) {
251
+ _bufferUnusedIdx += readReturnCode;
252
+ refreshTimeout ();
253
+ return readReturnCode;
254
+
255
+ } else if (readReturnCode == 0 ) {
256
+ // The connection has been closed by the client
257
+ _clientState = CSTATE_CLOSED;
258
+ HTTPS_DLOGHEX (" [ x ] Client closed connection, fid=" , _socket);
259
+ // TODO: If we are in state websocket, we might need to do something here
260
+ return 0 ;
261
+ } else {
262
+ // An error occured
263
+ _connectionState = STATE_ERROR;
264
+ HTTPS_DLOGHEX (" [ERR] An SSL error occured, fid=" , _socket);
265
+ closeConnection ();
266
+ return -1 ;
267
+ }
268
+ } // data pending
269
+
270
+ } else { // HTTP
271
+
272
+ if (FD_ISSET (_socket, &sockfds)) {
273
+
274
+ HTTPS_DLOGHEX (" [ ] There is data on the connection socket. fid=" , _socket)
275
+
276
+ int readReturnCode;
277
+
278
+ // The return code of SSL_read means:
279
+ // > 0 : Length of the data that has been read
280
+ // < 0 : Error
281
+ // = 0 : Connection closed
282
+
283
+ readReturnCode = recv (
284
+ _socket,
285
+ // Only after the part of the buffer that has not been processed yet
286
+ _receiveBuffer + sizeof (char ) * _bufferUnusedIdx,
287
+ // Only append up to the end of the buffer
288
+ HTTPS_CONNECTION_DATA_CHUNK_SIZE - _bufferUnusedIdx,
289
+ MSG_WAITALL | MSG_DONTWAIT
290
+ );
291
+ if (readReturnCode > 0 ) {
292
+ _bufferUnusedIdx += readReturnCode;
293
+ refreshTimeout ();
294
+ return readReturnCode;
295
+
296
+ } else if (readReturnCode == 0 ) {
297
+ // The connection has been closed by the client
298
+ _clientState = CSTATE_CLOSED;
299
+ HTTPS_DLOGHEX (" [ x ] Client closed connection, fid=" , _socket);
300
+ // TODO: If we are in state websocket, we might need to do something here
301
+ return 0 ;
302
+ } else {
303
+ // An error occured
304
+ _connectionState = STATE_ERROR;
305
+ HTTPS_DLOGHEX (" [ERR] An recv error occured, fid=" , _socket);
306
+ closeConnection ();
307
+ return -1 ;
308
+ }
309
+
310
+ } // data pending
311
+
312
+ }
313
+
252
314
} // buffer can read more
253
315
}
254
316
return 0 ;
@@ -272,30 +334,40 @@ size_t HTTPSConnection::readBuffer(byte* buffer, size_t length) {
272
334
273
335
size_t HTTPSConnection::pendingBufferSize () {
274
336
updateBuffer ();
275
- return _bufferUnusedIdx - _bufferProcessed + SSL_pending (_ssl);
337
+
338
+ if (_ssl) // HTTPS
339
+ return _bufferUnusedIdx - _bufferProcessed + SSL_pending (_ssl);
340
+ else // HTTP
341
+ return _bufferUnusedIdx - _bufferProcessed;
276
342
}
277
343
278
344
void HTTPSConnection::serverError () {
279
- _connectionState = STATE_ERROR;
345
+ _connectionState = STATE_ERROR;
280
346
281
- // TODO: Write 500
282
- Serial.println (" Server error" );
283
- char staticResponse[] = " HTTP/1.1 500 Internal Server Error\r\n Server: esp32https\r\n Connection:close\r\n Content-Type: text/html\r\n Content-Length:34\r\n\r\n <h1>500 Internal Server Error</h1>" ;
284
- SSL_write (_ssl, staticResponse, strlen (staticResponse));
347
+ // TODO: Write 500
348
+ Serial.println (" Server error" );
349
+ char staticResponse[] = " HTTP/1.1 500 Internal Server Error\r\n Server: esp32https\r\n Connection:close\r\n Content-Type: text/html\r\n Content-Length:34\r\n\r\n <h1>500 Internal Server Error</h1>" ;
350
+ if (_ssl) // HTTPS
351
+ SSL_write (_ssl, staticResponse, strlen (staticResponse));
352
+ else // HTTP
353
+ send (_socket, staticResponse, strlen (staticResponse), 0 );
285
354
286
- closeConnection ();
355
+ closeConnection ();
287
356
}
288
357
289
358
290
359
void HTTPSConnection::clientError () {
291
- _connectionState = STATE_ERROR;
292
-
293
- // TODO: Write 400
294
- Serial.println (" Client error" );
295
- char staticResponse[] = " HTTP/1.1 400 Bad Request\r\n Server: esp32https\r\n Connection:close\r\n Content-Type: text/html\r\n Content-Length:26\r\n\r\n <h1>400 Bad Request</h1>" ;
296
- SSL_write (_ssl, staticResponse, strlen (staticResponse));
297
-
298
- closeConnection ();
360
+ _connectionState = STATE_ERROR;
361
+
362
+ // TODO: Write 400
363
+ Serial.println (" Client error" );
364
+ char staticResponse[] = " HTTP/1.1 400 Bad Request\r\n Server: esp32https\r\n Connection:close\r\n Content-Type: text/html\r\n Content-Length:26\r\n\r\n <h1>400 Bad Request</h1>" ;
365
+ if (_ssl) // HTTPS
366
+ SSL_write (_ssl, staticResponse, strlen (staticResponse));
367
+ else // HTTP
368
+ send (_socket, staticResponse, strlen (staticResponse), 0 );
369
+
370
+ closeConnection ();
299
371
}
300
372
301
373
void HTTPSConnection::readLine (int lengthLimit) {
@@ -528,4 +600,4 @@ void HTTPSConnection::loop() {
528
600
529
601
}
530
602
531
- } /* namespace httpsserver */
603
+ } /* namespace httpsserver */
0 commit comments