11/****************************************************************************************************************************
2- * EthernetWebServer-impl.h - Dead simple web-server.
3- * For STM32 with built-in Ethernet (Nucleo-144, DISCOVERY, etc)
4- *
5- * EthernetWebServer_STM32 is a library for the STM32 run built-in Ethernet WebServer
6- *
7- * Forked and modified from ESP8266 https://github.com/esp8266/Arduino/releases
8- * Built by Khoi Hoang https://github.com/khoih-prog/ESP8266_AT_WebServer
9- * Licensed under MIT license
10- * Version: 1.0.1
11- *
12- * Original author:
13- * @file Esp8266WebServer.h
14- * @author Ivan Grokhotkov
15- *
16- * Version Modified By Date Comments
17- * ------- ----------- ---------- -----------
18- * 1.0.0 K Hoang 26/02/2020 Initial coding for STM32 with built-in Ethernet (Nucleo-144, DISCOVERY, etc) and ENC28J60
19- * 1.0.1 K Hoang 28/02/2020 Add W5x00 Ethernet shields using Ethernet library
2+ EthernetWebServer-impl.h - Dead simple web-server.
3+ For STM32 with built-in Ethernet (Nucleo-144, DISCOVERY, etc)
4+
5+ EthernetWebServer_STM32 is a library for the STM32 run built-in Ethernet WebServer
6+
7+ Forked and modified from ESP8266 https://github.com/esp8266/Arduino/releases
8+ Built by Khoi Hoang https://github.com/khoih-prog/ESP8266_AT_WebServer
9+ Licensed under MIT license
10+ Version: 1.0.1
11+
12+ Original author:
13+ @file Esp8266WebServer.h
14+ @author Ivan Grokhotkov
15+
16+ Version Modified By Date Comments
17+ ------- ----------- ---------- -----------
18+ 1.0.0 K Hoang 26/02/2020 Initial coding for STM32 with built-in Ethernet (Nucleo-144, DISCOVERY, etc) and ENC28J60
19+ 1.0.1 K Hoang 28/02/2020 Add W5x00 Ethernet shields using Ethernet library
2020 *****************************************************************************************************************************/
21-
21+
2222#ifndef EthernetWebServer_STM32_impl_h
2323#define EthernetWebServer_STM32_impl_h
2424
3131const char * AUTHORIZATION_HEADER = "Authorization" ;
3232
3333EthernetWebServer ::EthernetWebServer (int port )
34- : _server (port )
35- , _currentMethod (HTTP_ANY )
36- , _currentVersion (0 )
37- , _currentHandler (0 )
38- , _firstHandler (0 )
39- , _lastHandler (0 )
40- , _currentArgCount (0 )
41- , _currentArgs (0 )
42- , _headerKeysCount (0 )
43- , _currentHeaders (0 )
44- , _contentLength (0 )
45- , _chunked (false)
34+ : _server (port )
35+ , _currentMethod (HTTP_ANY )
36+ , _currentVersion (0 )
37+ , _currentHandler (0 )
38+ , _firstHandler (0 )
39+ , _lastHandler (0 )
40+ , _currentArgCount (0 )
41+ , _currentArgs (0 )
42+ , _headerKeysCount (0 )
43+ , _currentHeaders (0 )
44+ , _contentLength (0 )
45+ , _chunked (false)
4646{
4747}
4848
@@ -62,30 +62,30 @@ EthernetWebServer::~EthernetWebServer() {
6262void EthernetWebServer ::begin () {
6363 _currentStatus = HC_NONE ;
6464 _server .begin ();
65- if (!_headerKeysCount )
65+ if (!_headerKeysCount )
6666 collectHeaders (0 , 0 );
6767}
6868
69- bool EthernetWebServer ::authenticate (const char * username , const char * password ){
70- if (hasHeader (AUTHORIZATION_HEADER )){
69+ bool EthernetWebServer ::authenticate (const char * username , const char * password ) {
70+ if (hasHeader (AUTHORIZATION_HEADER )) {
7171 String authReq = header (AUTHORIZATION_HEADER );
72- if (authReq .startsWith ("Basic" )){
72+ if (authReq .startsWith ("Basic" )) {
7373 authReq = authReq .substring (6 );
7474 authReq .trim ();
75- char toencodeLen = strlen (username )+ strlen (password )+ 1 ;
75+ char toencodeLen = strlen (username ) + strlen (password ) + 1 ;
7676 char * toencode = new char [toencodeLen + 1 ];
77- if (toencode == NULL ){
77+ if (toencode == NULL ) {
7878 authReq = String ();
7979 return false;
8080 }
81- char * encoded = new char [base64_encode_expected_len (toencodeLen )+ 1 ];
82- if (encoded == NULL ){
81+ char * encoded = new char [base64_encode_expected_len (toencodeLen ) + 1 ];
82+ if (encoded == NULL ) {
8383 authReq = String ();
8484 delete [] toencode ;
8585 return false;
8686 }
8787 sprintf (toencode , "%s:%s" , username , password );
88- if (base64_encode_chars (toencode , toencodeLen , encoded ) > 0 && authReq .equals (encoded )){
88+ if (base64_encode_chars (toencode , toencodeLen , encoded ) > 0 && authReq .equals (encoded )) {
8989 authReq = String ();
9090 delete [] toencode ;
9191 delete [] encoded ;
@@ -99,7 +99,7 @@ bool EthernetWebServer::authenticate(const char * username, const char * passwor
9999 return false;
100100}
101101
102- void EthernetWebServer ::requestAuthentication (){
102+ void EthernetWebServer ::requestAuthentication () {
103103 sendHeader ("WWW-Authenticate" , "Basic realm=\"Login Required\"" );
104104 send (401 );
105105}
@@ -117,18 +117,18 @@ void EthernetWebServer::on(const String &uri, HTTPMethod method, EthernetWebServ
117117}
118118
119119void EthernetWebServer ::addHandler (RequestHandler * handler ) {
120- _addRequestHandler (handler );
120+ _addRequestHandler (handler );
121121}
122122
123123void EthernetWebServer ::_addRequestHandler (RequestHandler * handler ) {
124- if (!_lastHandler ) {
125- _firstHandler = handler ;
126- _lastHandler = handler ;
127- }
128- else {
129- _lastHandler -> next (handler );
130- _lastHandler = handler ;
131- }
124+ if (!_lastHandler ) {
125+ _firstHandler = handler ;
126+ _lastHandler = handler ;
127+ }
128+ else {
129+ _lastHandler -> next (handler );
130+ _lastHandler = handler ;
131+ }
132132}
133133
134134void EthernetWebServer ::handleClient () {
@@ -222,75 +222,75 @@ void EthernetWebServer::sendHeader(const String& name, const String& value, bool
222222}
223223
224224void EthernetWebServer ::setContentLength (size_t contentLength ) {
225- _contentLength = contentLength ;
225+ _contentLength = contentLength ;
226226}
227227
228228void EthernetWebServer ::_prepareHeader (String & response , int code , const char * content_type , size_t contentLength ) {
229- response = "HTTP /1. "+ String (_currentVersion )+ " ";
230- response += String (code );
231- response += " ";
232- response += _responseCodeToString (code );
233- response += "\r \n ";
234-
235- if (!content_type )
236- content_type = "text /html ";
237-
238- sendHeader ("Content - Type ", content_type , true);
239- if (_contentLength == CONTENT_LENGTH_NOT_SET ) {
240- sendHeader ("Content - Length ", String (contentLength ));
241- } else if (_contentLength != CONTENT_LENGTH_UNKNOWN ) {
242- sendHeader ("Content - Length ", String (_contentLength ));
243- } else if (_contentLength == CONTENT_LENGTH_UNKNOWN && _currentVersion ){ //HTTP/1.1 or above client
244- //let's do chunked
245- _chunked = true;
246- sendHeader ("Accept - Ranges ","none ");
247- sendHeader ("Transfer - Encoding ","chunked ");
248- }
249- sendHeader ("Connection ", "close ");
229+ response = "HTTP /1. " + String (_currentVersion ) + " ";
230+ response += String (code );
231+ response += " ";
232+ response += _responseCodeToString (code );
233+ response += "\r \n ";
234+
235+ if (!content_type )
236+ content_type = "text /html ";
237+
238+ sendHeader ("Content - Type ", content_type , true);
239+ if (_contentLength == CONTENT_LENGTH_NOT_SET ) {
240+ sendHeader ("Content - Length ", String (contentLength ));
241+ } else if (_contentLength != CONTENT_LENGTH_UNKNOWN ) {
242+ sendHeader ("Content - Length ", String (_contentLength ));
243+ } else if (_contentLength == CONTENT_LENGTH_UNKNOWN && _currentVersion ) { //HTTP/1.1 or above client
244+ //let's do chunked
245+ _chunked = true;
246+ sendHeader ("Accept - Ranges ", "none ");
247+ sendHeader ("Transfer - Encoding ", "chunked ");
248+ }
249+ sendHeader ("Connection ", "close ");
250250
251- response += _responseHeaders ;
252- response += "\r \n ";
253- _responseHeaders = String ();
251+ response += _responseHeaders ;
252+ response += "\r \n ";
253+ _responseHeaders = String ();
254254}
255255
256256void EthernetWebServer ::send (int code , const char * content_type , const String & content ) {
257- String header ;
258- // Can we asume the following?
259- //if(code == 200 && content.length() == 0 && _contentLength == CONTENT_LENGTH_NOT_SET)
260- // _contentLength = CONTENT_LENGTH_UNKNOWN;
261-
262- LOGDEBUG1 (F ("send1 : len = "), content .length ());
263- LOGDEBUG1 (F ("content = "), content );
264-
265- _prepareHeader (header , code , content_type , content .length ());
266-
267- _currentClient .write ((const uint8_t * )header .c_str (), header .length ());
268- if (content .length ())
269- {
270- //sendContent(content);
271- sendContent (content , content .length ());
272- }
257+ String header ;
258+ // Can we asume the following?
259+ //if(code == 200 && content.length() == 0 && _contentLength == CONTENT_LENGTH_NOT_SET)
260+ // _contentLength = CONTENT_LENGTH_UNKNOWN;
261+
262+ LOGDEBUG1 (F ("send1 : len = "), content .length ());
263+ LOGDEBUG1 (F ("content = "), content );
264+
265+ _prepareHeader (header , code , content_type , content .length ());
266+
267+ _currentClient .write ((const uint8_t * )header .c_str (), header .length ());
268+ if (content .length ())
269+ {
270+ //sendContent(content);
271+ sendContent (content , content .length ());
272+ }
273273}
274274
275275void EthernetWebServer ::send (int code , char * content_type , const String & content , size_t contentLength )
276276{
277- String header ;
278-
279- LOGDEBUG1 (F ("send2 : len = "), contentLength );
280- LOGDEBUG1 (F ("content = "), content );
281-
282- char type [64 ];
283- memccpy ((void * )type , content_type , 0 , sizeof (type ));
284- _prepareHeader (header , code , (const char * )type , contentLength );
285-
286- LOGDEBUG1 (F ("send2 : hdrlen = "), header .length ());
287- LOGDEBUG1 (F ("header = "), header );
288-
289- _currentClient .write ((const uint8_t * ) header .c_str (), header .length ());
290- if (contentLength )
291- {
292- sendContent (content , contentLength );
293- }
277+ String header ;
278+
279+ LOGDEBUG1 (F ("send2 : len = "), contentLength );
280+ LOGDEBUG1 (F ("content = "), content );
281+
282+ char type [64 ];
283+ memccpy ((void * )type , content_type , 0 , sizeof (type ));
284+ _prepareHeader (header , code , (const char * )type , contentLength );
285+
286+ LOGDEBUG1 (F ("send2 : hdrlen = "), header .length ());
287+ LOGDEBUG1 (F ("header = "), header );
288+
289+ _currentClient .write ((const uint8_t * ) header .c_str (), header .length ());
290+ if (contentLength )
291+ {
292+ sendContent (content , contentLength );
293+ }
294294}
295295
296296void EthernetWebServer ::send (int code , char * content_type , const String & content ) {
@@ -304,33 +304,33 @@ void EthernetWebServer::send(int code, const String& content_type, const String&
304304void EthernetWebServer ::sendContent (const String & content ) {
305305 const char * footer = "\r \n ";
306306 size_t len = content .length ();
307- if (_chunked ) {
307+ if (_chunked ) {
308308 char * chunkSize = (char * )malloc (11 );
309- if (chunkSize ){
309+ if (chunkSize ) {
310310 sprintf (chunkSize , "%x %s ", len , footer );
311311 _currentClient .write (chunkSize , strlen (chunkSize ));
312312 free (chunkSize );
313313 }
314314 }
315315 _currentClient .write (content .c_str (), len );
316- if (_chunked ){
316+ if (_chunked ) {
317317 _currentClient .write (footer , 2 );
318318 }
319319}
320320
321- void EthernetWebServer ::sendContent (const String & content , size_t size )
321+ void EthernetWebServer ::sendContent (const String & content , size_t size )
322322{
323323 const char * footer = "\r \n ";
324- if (_chunked ) {
324+ if (_chunked ) {
325325 char * chunkSize = (char * )malloc (11 );
326- if (chunkSize ){
326+ if (chunkSize ) {
327327 sprintf (chunkSize , "%x %s ", size , footer );
328328 _currentClient .write (chunkSize , strlen (chunkSize ));
329329 free (chunkSize );
330330 }
331331 }
332332 _currentClient .write (content .c_str (), size );
333- if (_chunked ){
333+ if (_chunked ) {
334334 _currentClient .write (footer , 2 );
335335 }
336336}
@@ -379,11 +379,11 @@ String EthernetWebServer::header(String name) {
379379void EthernetWebServer ::collectHeaders (const char * headerKeys [], const size_t headerKeysCount ) {
380380 _headerKeysCount = headerKeysCount + 1 ;
381381 if (_currentHeaders )
382- delete []_currentHeaders ;
382+ delete []_currentHeaders ;
383383 _currentHeaders = new RequestArgument [_headerKeysCount ];
384384 _currentHeaders [0 ].key = AUTHORIZATION_HEADER ;
385- for (int i = 1 ; i < _headerKeysCount ; i ++ ){
386- _currentHeaders [i ].key = headerKeys [i - 1 ];
385+ for (int i = 1 ; i < _headerKeysCount ; i ++ ) {
386+ _currentHeaders [i ].key = headerKeys [i - 1 ];
387387 }
388388}
389389
@@ -425,18 +425,18 @@ void EthernetWebServer::onNotFound(THandlerFunction fn) {
425425
426426void EthernetWebServer ::_handleRequest () {
427427 bool handled = false;
428- if (!_currentHandler ){
428+ if (!_currentHandler ) {
429429 LOGWARN (F ("request handler not found "));
430430 }
431431 else {
432432 handled = _currentHandler -> handle (* this , _currentMethod , _currentUri );
433433 if (!handled ) {
434- LOGWARN (F ("_handleRequest failed "));
434+ LOGWARN (F ("_handleRequest failed "));
435435 }
436436 }
437437
438438 if (!handled ) {
439- if (_notFoundHandler ) {
439+ if (_notFoundHandler ) {
440440 _notFoundHandler ();
441441 }
442442 else {
0 commit comments