[GH-ISSUE #1658] Bug with the main code of WiFiManager.cpp #1410

Closed
opened 2026-02-28 01:29:57 +03:00 by kerem · 1 comment
Owner

Originally created by @mtsSwitzerland on GitHub (Sep 21, 2023).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/1658

Basic Infos

Problem with libary code.

Hardware

WiFimanager Branch/Release: Master

Esp32:

Hardware: ESP32

Core Version: 2.4.0, staging

Description

I had a problem with the libary as soon I've add the libary by pubspec instead of adding the whole folder to the project.
While I had the folder included in the platformIO project, everything went well. But after putting the libary in by pubspec I go some errors:

Settings in IDE

Module: [env:esp32doit-devkit-v1]
platform = espressif32
board = esp32doit-devkit-v1
framework = arduino
monitor_speed = 115200
upload_speed = 921600

Additional libraries:

Sketch

Debug Messages

Compiling .pio\build\esp32doit-devkit-v1\FrameworkArduino\IPv6Address.cpp.o
Compiling .pio\build\esp32doit-devkit-v1\FrameworkArduino\MD5Builder.cpp.o
.pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp:2425:18: error: too many decimal points in number
if(serverloc = 0.0.0.0){
^~~~~~~
.pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp: In member function 'boolean WiFiManager::captivePortal()':
.pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp:2425:6: error: 'serverloc' was not declared in this scope
if(serverloc = 0.0.0.0){
^~~~~~~~~
.pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp:2425:6: note: suggested alternative: 'serverLoc'
if(serverloc = 0.0.0.0){
^~~~~~~~~
serverLoc
.pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp: At global scope:
.pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp:2433:3: error: expected unqualified-id before 'if'
if(_httpPort != 80) serverLoc += ":" + (String)_httpPort; // add port if not default
^~
.pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp:2434:21: error: 'serverLoc' was not declared in this scope
bool doredirect = serverLoc != server->hostHeader(); // redirect if hostheader not server ip, prevent redirect loops
^~~~~~~~~
.pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp:2434:21: note: suggested alternative: 'server_h'
bool doredirect = serverLoc != server->hostHeader(); // redirect if hostheader not server ip, prevent redirect loops
^~~~~~~~~
server_h
.pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp:2434:34: error: 'server' was not declared in this scope
bool doredirect = serverLoc != server->hostHeader(); // redirect if hostheader not server ip, prevent redirect loops
^~~~~~
.pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp:2434:34: note: suggested alternative: 'Server'
bool doredirect = serverLoc != server->hostHeader(); // redirect if hostheader not server ip, prevent redirect loops
^~~~~~
Server
.pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp:2436:3: error: expected unqualified-id before 'if'
if (doredirect) {
^~
.pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp:2445:3: error: expected unqualified-id before 'return'
return false;
^~~~~~
.pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp:2446:1: error: expected declaration before '}' token
}
^
.pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp: In member function 'boolean WiFiManager::captivePortal()':
.pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp:2422:60: warning: control reaches end of non-void function [-Wreturn-type]
String serverLoc = toStringIp(server->client().localIP());
^
*** [.pio\build\esp32doit-devkit-v1\lib9f8\WiFiManager\WiFiManager.cpp.o] Error 1
====================================================================================================== [FAILED] Took 12.41 seconds
messages here

The code, wich was causing the error, was in WiFiManager.cpp was like this:
/**
 * HTTPD redirector
 * Redirect to captive portal if we got a request for another domain. 
 * Return true in that case so the page handler do not try to handle the request again. 
 */
boolean WiFiManager::captivePortal() {
  #ifdef WM_DEBUG_LEVEL
  DEBUG_WM(WM_DEBUG_MAX,"-> " + server->hostHeader());
  #endif
  
  if(!_enableCaptivePortal) return false; // skip redirections, @todo maybe allow redirection even when no cp ? might be useful
  
  String serverLoc =  toStringIp(server->client().localIP());

  // fallback for ipv6 bug
  if(serverloc = 0.0.0.0){
    if ((WiFi.status()) != WL_CONNECTED)
      serverLoc = toStringIp(WiFi.softAPIP());
    else
      serverLoc = toStringIp(WiFi.localIP());
    }
  }
  
  if(_httpPort != 80) serverLoc += ":" + (String)_httpPort; // add port if not default
  bool doredirect = serverLoc != server->hostHeader(); // redirect if hostheader not server ip, prevent redirect loops
  
  if (doredirect) {
    #ifdef WM_DEBUG_LEVEL
    DEBUG_WM(WM_DEBUG_VERBOSE,F("<- Request redirected to captive portal"));
    #endif
    server->sendHeader(F("Location"), (String)F("http://") + serverLoc, true); // @HTTPHEAD send redirect
    server->send ( 302, FPSTR(HTTP_HEAD_CT2), ""); // Empty content inhibits Content-length header so we have to close the socket ourselves.
    server->client().stop(); // Stop is needed because we sent no content length
    return true;
  }
  return false;
}

I have changed it to this:
boolean WiFiManager::captivePortal() {
  #ifdef WM_DEBUG_LEVEL
  DEBUG_WM(WM_DEBUG_MAX,"-> " + server->hostHeader());
  #endif
  
  if(!_enableCaptivePortal) return false;
  
  String serverLoc = toStringIp(server->client().localIP());

  // fallback for ipv6 bug
  if(serverLoc == "0.0.0.0"){
    if ((WiFi.status()) != WL_CONNECTED)
      serverLoc = toStringIp(WiFi.softAPIP());
    else
      serverLoc = toStringIp(WiFi.localIP());
  }
  
  if(_httpPort != 80) serverLoc += ":" + (String)_httpPort;
  
  bool doredirect = serverLoc != server->hostHeader();
  
  if (doredirect) {
    #ifdef WM_DEBUG_LEVEL
    DEBUG_WM(WM_DEBUG_VERBOSE,F("<- Request redirected to captive portal"));
    #endif
    server->sendHeader(F("Location"), (String)F("http://") + serverLoc, true);
    server->send(302, FPSTR(HTTP_HEAD_CT2), "");
    server->client().stop();
    return true;
  }
  return false;
}

now it works but I'm not sure if this is correct.

Cheers Oli

Originally created by @mtsSwitzerland on GitHub (Sep 21, 2023). Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/1658 ### Basic Infos Problem with libary code. #### Hardware WiFimanager Branch/Release: Master Esp32: Hardware: ESP32 Core Version: 2.4.0, staging ### Description I had a problem with the libary as soon I've add the libary by pubspec instead of adding the whole folder to the project. While I had the folder included in the platformIO project, everything went well. But after putting the libary in by pubspec I go some errors: ### Settings in IDE Module: [env:esp32doit-devkit-v1] platform = espressif32 board = esp32doit-devkit-v1 framework = arduino monitor_speed = 115200 upload_speed = 921600 Additional libraries: ### Sketch ### Debug Messages Compiling .pio\build\esp32doit-devkit-v1\FrameworkArduino\IPv6Address.cpp.o Compiling .pio\build\esp32doit-devkit-v1\FrameworkArduino\MD5Builder.cpp.o .pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp:2425:18: error: too many decimal points in number if(serverloc = 0.0.0.0){ ^~~~~~~ .pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp: In member function 'boolean WiFiManager::captivePortal()': .pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp:2425:6: error: 'serverloc' was not declared in this scope if(serverloc = 0.0.0.0){ ^~~~~~~~~ .pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp:2425:6: note: suggested alternative: 'serverLoc' if(serverloc = 0.0.0.0){ ^~~~~~~~~ serverLoc .pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp: At global scope: .pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp:2433:3: error: expected unqualified-id before 'if' if(_httpPort != 80) serverLoc += ":" + (String)_httpPort; // add port if not default ^~ .pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp:2434:21: error: 'serverLoc' was not declared in this scope bool doredirect = serverLoc != server->hostHeader(); // redirect if hostheader not server ip, prevent redirect loops ^~~~~~~~~ .pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp:2434:21: note: suggested alternative: 'server_h' bool doredirect = serverLoc != server->hostHeader(); // redirect if hostheader not server ip, prevent redirect loops ^~~~~~~~~ server_h .pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp:2434:34: error: 'server' was not declared in this scope bool doredirect = serverLoc != server->hostHeader(); // redirect if hostheader not server ip, prevent redirect loops ^~~~~~ .pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp:2434:34: note: suggested alternative: 'Server' bool doredirect = serverLoc != server->hostHeader(); // redirect if hostheader not server ip, prevent redirect loops ^~~~~~ Server .pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp:2436:3: error: expected unqualified-id before 'if' if (doredirect) { ^~ .pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp:2445:3: error: expected unqualified-id before 'return' return false; ^~~~~~ .pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp:2446:1: error: expected declaration before '}' token } ^ .pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp: In member function 'boolean WiFiManager::captivePortal()': .pio/libdeps/esp32doit-devkit-v1/WiFiManager/WiFiManager.cpp:2422:60: warning: control reaches end of non-void function [-Wreturn-type] String serverLoc = toStringIp(server->client().localIP()); ^ *** [.pio\build\esp32doit-devkit-v1\lib9f8\WiFiManager\WiFiManager.cpp.o] Error 1 ====================================================================================================== [FAILED] Took 12.41 seconds messages here ``` The code, wich was causing the error, was in WiFiManager.cpp was like this: /** * HTTPD redirector * Redirect to captive portal if we got a request for another domain. * Return true in that case so the page handler do not try to handle the request again. */ boolean WiFiManager::captivePortal() { #ifdef WM_DEBUG_LEVEL DEBUG_WM(WM_DEBUG_MAX,"-> " + server->hostHeader()); #endif if(!_enableCaptivePortal) return false; // skip redirections, @todo maybe allow redirection even when no cp ? might be useful String serverLoc = toStringIp(server->client().localIP()); // fallback for ipv6 bug if(serverloc = 0.0.0.0){ if ((WiFi.status()) != WL_CONNECTED) serverLoc = toStringIp(WiFi.softAPIP()); else serverLoc = toStringIp(WiFi.localIP()); } } if(_httpPort != 80) serverLoc += ":" + (String)_httpPort; // add port if not default bool doredirect = serverLoc != server->hostHeader(); // redirect if hostheader not server ip, prevent redirect loops if (doredirect) { #ifdef WM_DEBUG_LEVEL DEBUG_WM(WM_DEBUG_VERBOSE,F("<- Request redirected to captive portal")); #endif server->sendHeader(F("Location"), (String)F("http://") + serverLoc, true); // @HTTPHEAD send redirect server->send ( 302, FPSTR(HTTP_HEAD_CT2), ""); // Empty content inhibits Content-length header so we have to close the socket ourselves. server->client().stop(); // Stop is needed because we sent no content length return true; } return false; } I have changed it to this: boolean WiFiManager::captivePortal() { #ifdef WM_DEBUG_LEVEL DEBUG_WM(WM_DEBUG_MAX,"-> " + server->hostHeader()); #endif if(!_enableCaptivePortal) return false; String serverLoc = toStringIp(server->client().localIP()); // fallback for ipv6 bug if(serverLoc == "0.0.0.0"){ if ((WiFi.status()) != WL_CONNECTED) serverLoc = toStringIp(WiFi.softAPIP()); else serverLoc = toStringIp(WiFi.localIP()); } if(_httpPort != 80) serverLoc += ":" + (String)_httpPort; bool doredirect = serverLoc != server->hostHeader(); if (doredirect) { #ifdef WM_DEBUG_LEVEL DEBUG_WM(WM_DEBUG_VERBOSE,F("<- Request redirected to captive portal")); #endif server->sendHeader(F("Location"), (String)F("http://") + serverLoc, true); server->send(302, FPSTR(HTTP_HEAD_CT2), ""); server->client().stop(); return true; } return false; } now it works but I'm not sure if this is correct. Cheers Oli
kerem closed this issue 2026-02-28 01:29:57 +03:00
Author
Owner

@ayavilevich commented on GitHub (Sep 21, 2023):

Also see: github.com/tzapu/WiFiManager@5a8e869a7e (r127834085)

<!-- gh-comment-id:1729099783 --> @ayavilevich commented on GitHub (Sep 21, 2023): Also see: https://github.com/tzapu/WiFiManager/commit/5a8e869a7ee0b7998bd5aa82d279a2717caa7270#r127834085
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/WiFiManager#1410
No description provided.