@@ -60,10 +60,12 @@ function to process requests, decode URLs, serve files, etc. etc.
6060#include "wifi_manager.h"
6161
6262
63- EventGroupHandle_t http_server_event_group = NULL ;
64- EventBits_t uxBits ;
63+ // EventGroupHandle_t http_server_event_group = NULL;
64+ // EventBits_t uxBits;
6565
6666static const char TAG [] = "http_server" ;
67+ static TaskHandle_t task_http_server = NULL ;
68+
6769
6870/* embedded binary data */
6971extern const uint8_t style_css_start [] asm("_binary_style_css_start" );
@@ -90,21 +92,14 @@ const static char http_redirect_hdr_end[] = "/\n\n";
9092
9193
9294
93- void http_server_set_event_start (){
94- if (!http_server_event_group ) http_server_event_group = xEventGroupCreate ();
95- xEventGroupSetBits (http_server_event_group , HTTP_SERVER_START_BIT_0 );
95+ void http_server_start (){
96+ if (task_http_server == NULL ){
97+ xTaskCreate (& http_server , "http_server" , 2048 , NULL , WIFI_MANAGER_TASK_PRIORITY - 1 , & task_http_server );
98+ }
9699}
97100
98-
99101void http_server (void * pvParameters ) {
100102
101- if (!http_server_event_group ) http_server_event_group = xEventGroupCreate ();
102-
103- /* do not start the task until wifi_manager says it's safe to do so! */
104- ESP_LOGD (TAG , "waiting for start bit" );
105- uxBits = xEventGroupWaitBits (http_server_event_group , HTTP_SERVER_START_BIT_0 , pdFALSE , pdTRUE , portMAX_DELAY );
106- ESP_LOGD (TAG , "received start bit, starting server" );
107-
108103 struct netconn * conn , * newconn ;
109104 err_t err ;
110105 conn = netconn_new (NETCONN_TCP );
@@ -117,10 +112,12 @@ void http_server(void *pvParameters) {
117112 http_server_netconn_serve (newconn );
118113 netconn_delete (newconn );
119114 }
120- vTaskDelay ( ( TickType_t ) 10 ); /* allows the freeRTOS scheduler to take over if needed */
115+ taskYIELD (); /* allows the freeRTOS scheduler to take over if needed. */
121116 } while (err == ERR_OK );
122117 netconn_close (conn );
123118 netconn_delete (conn );
119+
120+ vTaskDelete ( NULL );
124121}
125122
126123
@@ -162,98 +159,104 @@ void http_server_netconn_serve(struct netconn *conn) {
162159
163160 if (line ) {
164161
165-
166- /* captive portal functionality: redirect to the access point IP addresss */
162+ /* captive portal functionality: redirect to access point IP for HOST that are not the access point IP OR the STA IP */
167163 int lenH = 0 ;
168164 char * host = http_server_get_header (save_ptr , "Host: " , & lenH );
169- if ((sizeof (host ) > 0 ) && !strstr (host , DEFAULT_AP_IP )) {
165+ /* determine if Host is from the STA IP address */
166+ wifi_manager_lock_sta_ip_string (portMAX_DELAY );
167+ bool access_from_sta_ip = lenH > 0 ?strstr (host , wifi_manager_get_sta_ip_string ()):false;
168+ wifi_manager_unlock_sta_ip_string ();
169+
170+ if (lenH > 0 && !strstr (host , DEFAULT_AP_IP ) && !access_from_sta_ip ) {
170171 netconn_write (conn , http_redirect_hdr_start , sizeof (http_redirect_hdr_start ) - 1 , NETCONN_NOCOPY );
171172 netconn_write (conn , DEFAULT_AP_IP , sizeof (DEFAULT_AP_IP ) - 1 , NETCONN_NOCOPY );
172173 netconn_write (conn , http_redirect_hdr_end , sizeof (http_redirect_hdr_end ) - 1 , NETCONN_NOCOPY );
173174 }
174- /* default page */
175- else if (strstr (line , "GET / " )) {
176- netconn_write (conn , http_html_hdr , sizeof (http_html_hdr ) - 1 , NETCONN_NOCOPY );
177- netconn_write (conn , index_html_start , index_html_end - index_html_start , NETCONN_NOCOPY );
178- }
179- else if (strstr (line , "GET /jquery.js " )) {
180- netconn_write (conn , http_jquery_gz_hdr , sizeof (http_jquery_gz_hdr ) - 1 , NETCONN_NOCOPY );
181- netconn_write (conn , jquery_gz_start , jquery_gz_end - jquery_gz_start , NETCONN_NOCOPY );
182- }
183- else if (strstr (line , "GET /code.js " )) {
184- netconn_write (conn , http_js_hdr , sizeof (http_js_hdr ) - 1 , NETCONN_NOCOPY );
185- netconn_write (conn , code_js_start , code_js_end - code_js_start , NETCONN_NOCOPY );
186- }
187- else if (strstr (line , "GET /ap.json " )) {
188- /* if we can get the mutex, write the last version of the AP list */
189- if (wifi_manager_lock_json_buffer (( TickType_t ) 10 )){
190- netconn_write (conn , http_ok_json_no_cache_hdr , sizeof (http_ok_json_no_cache_hdr ) - 1 , NETCONN_NOCOPY );
191- char * buff = wifi_manager_get_ap_list_json ();
192- netconn_write (conn , buff , strlen (buff ), NETCONN_NOCOPY );
193- wifi_manager_unlock_json_buffer ();
175+ else {
176+ /* default page */
177+ if (strstr (line , "GET / " )) {
178+ netconn_write (conn , http_html_hdr , sizeof (http_html_hdr ) - 1 , NETCONN_NOCOPY );
179+ netconn_write (conn , index_html_start , index_html_end - index_html_start , NETCONN_NOCOPY );
194180 }
195- else {
196- netconn_write (conn , http_503_hdr , sizeof (http_503_hdr ) - 1 , NETCONN_NOCOPY );
197- ESP_LOGD ( TAG , "http_server_netconn_serve: GET /ap.json failed to obtain mutex" );
181+ else if ( strstr ( line , "GET /jquery.js " )) {
182+ netconn_write (conn , http_jquery_gz_hdr , sizeof (http_jquery_gz_hdr ) - 1 , NETCONN_NOCOPY );
183+ netconn_write ( conn , jquery_gz_start , jquery_gz_end - jquery_gz_start , NETCONN_NOCOPY );
198184 }
199- /* request a wifi scan */
200- wifi_manager_scan_async ();
201- }
202- else if (strstr (line , "GET /style.css " )) {
203- netconn_write (conn , http_css_hdr , sizeof (http_css_hdr ) - 1 , NETCONN_NOCOPY );
204- netconn_write (conn , style_css_start , style_css_end - style_css_start , NETCONN_NOCOPY );
205- }
206- else if (strstr (line , "GET /status.json " )){
207- if (wifi_manager_lock_json_buffer (( TickType_t ) 10 )){
208- char * buff = wifi_manager_get_ip_info_json ();
209- if (buff ){
185+ else if (strstr (line , "GET /code.js " )) {
186+ netconn_write (conn , http_js_hdr , sizeof (http_js_hdr ) - 1 , NETCONN_NOCOPY );
187+ netconn_write (conn , code_js_start , code_js_end - code_js_start , NETCONN_NOCOPY );
188+ }
189+ else if (strstr (line , "GET /ap.json " )) {
190+ /* if we can get the mutex, write the last version of the AP list */
191+ if (wifi_manager_lock_json_buffer (( TickType_t ) 10 )){
210192 netconn_write (conn , http_ok_json_no_cache_hdr , sizeof (http_ok_json_no_cache_hdr ) - 1 , NETCONN_NOCOPY );
193+ char * buff = wifi_manager_get_ap_list_json ();
211194 netconn_write (conn , buff , strlen (buff ), NETCONN_NOCOPY );
212195 wifi_manager_unlock_json_buffer ();
213196 }
214197 else {
215198 netconn_write (conn , http_503_hdr , sizeof (http_503_hdr ) - 1 , NETCONN_NOCOPY );
199+ ESP_LOGD (TAG , "http_server_netconn_serve: GET /ap.json failed to obtain mutex" );
216200 }
201+ /* request a wifi scan */
202+ wifi_manager_scan_async ();
217203 }
218- else {
219- netconn_write (conn , http_503_hdr , sizeof (http_503_hdr ) - 1 , NETCONN_NOCOPY );
220- ESP_LOGD ( TAG , "http_server_netconn_serve: GET /status failed to obtain mutex" );
204+ else if ( strstr ( line , "GET /style.css " )) {
205+ netconn_write (conn , http_css_hdr , sizeof (http_css_hdr ) - 1 , NETCONN_NOCOPY );
206+ netconn_write ( conn , style_css_start , style_css_end - style_css_start , NETCONN_NOCOPY );
221207 }
222- }
223- else if (strstr (line , "DELETE /connect.json " )) {
224- ESP_LOGD (TAG , "http_server_netconn_serve: DELETE /connect.json" );
225- /* request a disconnection from wifi and forget about it */
226- wifi_manager_disconnect_async ();
227- netconn_write (conn , http_ok_json_no_cache_hdr , sizeof (http_ok_json_no_cache_hdr ) - 1 , NETCONN_NOCOPY ); /* 200 ok */
228- }
229- else if (strstr (line , "POST /connect.json " )) {
230- ESP_LOGD (TAG , "http_server_netconn_serve: POST /connect.json" );
231-
232- bool found = false;
233- int lenS = 0 , lenP = 0 ;
234- char * ssid = NULL , * password = NULL ;
235- ssid = http_server_get_header (save_ptr , "X-Custom-ssid: " , & lenS );
236- password = http_server_get_header (save_ptr , "X-Custom-pwd: " , & lenP );
237-
238- if (ssid && lenS <= MAX_SSID_SIZE && password && lenP <= MAX_PASSWORD_SIZE ){
239- wifi_config_t * config = wifi_manager_get_wifi_sta_config ();
240- memset (config , 0x00 , sizeof (wifi_config_t ));
241- memcpy (config -> sta .ssid , ssid , lenS );
242- memcpy (config -> sta .password , password , lenP );
243- ESP_LOGD (TAG , "http_server_netconn_serve: wifi_manager_connect_async() call" );
244- wifi_manager_connect_async ();
245- netconn_write (conn , http_ok_json_no_cache_hdr , sizeof (http_ok_json_no_cache_hdr ) - 1 , NETCONN_NOCOPY ); //200ok
246- found = true;
208+ else if (strstr (line , "GET /status.json " )){
209+ if (wifi_manager_lock_json_buffer (( TickType_t ) 10 )){
210+ char * buff = wifi_manager_get_ip_info_json ();
211+ if (buff ){
212+ netconn_write (conn , http_ok_json_no_cache_hdr , sizeof (http_ok_json_no_cache_hdr ) - 1 , NETCONN_NOCOPY );
213+ netconn_write (conn , buff , strlen (buff ), NETCONN_NOCOPY );
214+ wifi_manager_unlock_json_buffer ();
215+ }
216+ else {
217+ netconn_write (conn , http_503_hdr , sizeof (http_503_hdr ) - 1 , NETCONN_NOCOPY );
218+ }
219+ }
220+ else {
221+ netconn_write (conn , http_503_hdr , sizeof (http_503_hdr ) - 1 , NETCONN_NOCOPY );
222+ ESP_LOGD (TAG , "http_server_netconn_serve: GET /status failed to obtain mutex" );
223+ }
224+ }
225+ else if (strstr (line , "DELETE /connect.json " )) {
226+ ESP_LOGD (TAG , "http_server_netconn_serve: DELETE /connect.json" );
227+ /* request a disconnection from wifi and forget about it */
228+ wifi_manager_disconnect_async ();
229+ netconn_write (conn , http_ok_json_no_cache_hdr , sizeof (http_ok_json_no_cache_hdr ) - 1 , NETCONN_NOCOPY ); /* 200 ok */
247230 }
231+ else if (strstr (line , "POST /connect.json " )) {
232+ ESP_LOGD (TAG , "http_server_netconn_serve: POST /connect.json" );
233+
234+ bool found = false;
235+ int lenS = 0 , lenP = 0 ;
236+ char * ssid = NULL , * password = NULL ;
237+ ssid = http_server_get_header (save_ptr , "X-Custom-ssid: " , & lenS );
238+ password = http_server_get_header (save_ptr , "X-Custom-pwd: " , & lenP );
239+
240+ if (ssid && lenS <= MAX_SSID_SIZE && password && lenP <= MAX_PASSWORD_SIZE ){
241+ wifi_config_t * config = wifi_manager_get_wifi_sta_config ();
242+ memset (config , 0x00 , sizeof (wifi_config_t ));
243+ memcpy (config -> sta .ssid , ssid , lenS );
244+ memcpy (config -> sta .password , password , lenP );
245+ ESP_LOGD (TAG , "http_server_netconn_serve: wifi_manager_connect_async() call" );
246+ wifi_manager_connect_async ();
247+ netconn_write (conn , http_ok_json_no_cache_hdr , sizeof (http_ok_json_no_cache_hdr ) - 1 , NETCONN_NOCOPY ); //200ok
248+ found = true;
249+ }
250+
251+ if (!found ){
252+ /* bad request the authentification header is not complete/not the correct format */
253+ netconn_write (conn , http_400_hdr , sizeof (http_400_hdr ) - 1 , NETCONN_NOCOPY );
254+ }
248255
249- if (! found ){
250- /* bad request the authentification header is not complete/not the correct format */
256+ }
257+ else {
251258 netconn_write (conn , http_400_hdr , sizeof (http_400_hdr ) - 1 , NETCONN_NOCOPY );
252259 }
253-
254- }
255- else {
256- netconn_write (conn , http_400_hdr , sizeof (http_400_hdr ) - 1 , NETCONN_NOCOPY );
257260 }
258261 }
259262 else {
0 commit comments