25#include "../../Defines.h"
28#include <libb64/cencode.h>
29#include "WiFiServer.h"
30#include "WiFiClient.h"
37#define DEBUG_OUTPUT DEBUG_ESP_PORT
39#define DEBUG_OUTPUT Serial
103 if(authReq.startsWith(
"Basic")){
104 authReq = authReq.substring(6);
106 char toencodeLen = strlen(username)+strlen(password)+1;
107 char *toencode =
new char[toencodeLen + 1];
108 if(toencode == NULL){
112 char *encoded =
new char[base64_encode_expected_len(toencodeLen)+1];
118 sprintf(toencode,
"%s:%s", username, password);
119 if(base64_encode_chars(toencode, toencodeLen, encoded) > 0 && authReq.equals(encoded)){
134 sendHeader(
"WWW-Authenticate",
"Basic realm=\"Login Required\"");
176#ifdef DEBUG_ESP_HTTP_SERVER
177 SerialDebug.println(
"New client");
242 String headerLine = name;
245 headerLine +=
"\r\n";
261 response += String(code);
267 content_type =
"text/html";
269 sendHeader(
"Content-Type", content_type,
true);
271 sendHeader(
"Content-Length", String(contentLength));
299 size_t contentLength = 0;
301 if (content != NULL) {
302 contentLength = strlen_P(content);
307 memccpy_P((
void*)type, (PGM_VOID_P)content_type, 0,
sizeof(type));
316 memccpy_P((
void*)type, (PGM_VOID_P)content_type, 0,
sizeof(type));
323 send(code, (
const char*)content_type, content);
327 send(code, (
const char*)content_type.c_str(), content);
331 const char * footer =
"\r\n";
332 size_t len = content.length();
334 char * chunkSize = (
char *)malloc(11);
336 sprintf(chunkSize,
"%x%s", len, footer);
352 const char * footer =
"\r\n";
354 char * chunkSize = (
char *)malloc(11);
356 sprintf(chunkSize,
"%x%s", size, footer);
457 bool handled =
false;
459#ifdef DEBUG_ESP_HTTP_SERVER
460 SerialDebug.println(
"request handler not found");
465#ifdef DEBUG_ESP_HTTP_SERVER
467 SerialDebug.println(
"request handler failed to handle request");
486 case 100:
return F(
"Continue");
487 case 101:
return F(
"Switching Protocols");
488 case 200:
return F(
"OK");
489 case 201:
return F(
"Created");
490 case 202:
return F(
"Accepted");
491 case 203:
return F(
"Non-Authoritative Information");
492 case 204:
return F(
"No Content");
493 case 205:
return F(
"Reset Content");
494 case 206:
return F(
"Partial Content");
495 case 300:
return F(
"Multiple Choices");
496 case 301:
return F(
"Moved Permanently");
497 case 302:
return F(
"Found");
498 case 303:
return F(
"See Other");
499 case 304:
return F(
"Not Modified");
500 case 305:
return F(
"Use Proxy");
501 case 307:
return F(
"Temporary Redirect");
502 case 400:
return F(
"Bad Request");
503 case 401:
return F(
"Unauthorized");
504 case 402:
return F(
"Payment Required");
505 case 403:
return F(
"Forbidden");
506 case 404:
return F(
"Not Found");
507 case 405:
return F(
"Method Not Allowed");
508 case 406:
return F(
"Not Acceptable");
509 case 407:
return F(
"Proxy Authentication Required");
510 case 408:
return F(
"Request Time-out");
511 case 409:
return F(
"Conflict");
512 case 410:
return F(
"Gone");
513 case 411:
return F(
"Length Required");
514 case 412:
return F(
"Precondition Failed");
515 case 413:
return F(
"Request Entity Too Large");
516 case 414:
return F(
"Request-URI Too Large");
517 case 415:
return F(
"Unsupported Media Type");
518 case 416:
return F(
"Requested range not satisfiable");
519 case 417:
return F(
"Expectation Failed");
520 case 500:
return F(
"Internal Server Error");
521 case 501:
return F(
"Not Implemented");
522 case 502:
return F(
"Bad Gateway");
523 case 503:
return F(
"Service Unavailable");
524 case 504:
return F(
"Gateway Time-out");
525 case 505:
return F(
"HTTP Version not supported");
const char * AUTHORIZATION_HEADER
#define CONTENT_LENGTH_NOT_SET
#define HTTP_MAX_DATA_WAIT
#define HTTP_MAX_SEND_WAIT
#define HTTP_MAX_CLOSE_WAIT
#define CONTENT_LENGTH_UNKNOWN
virtual bool handle(WebServer &server, HTTPMethod requestMethod, String requestUri)
bool authenticate(const char *username, const char *password)
bool hasHeader(String name)
void _prepareHeader(String &response, int code, const char *content_type, size_t contentLength)
RequestHandler * _currentHandler
void on(const String &uri, THandlerFunction handler)
RequestArgument * _currentArgs
static String _responseCodeToString(int code)
void sendContent(const String &content)
void requestAuthentication()
HTTPMethod _currentMethod
HTTPClientStatus _currentStatus
WiFiClient _currentClient
void _addRequestHandler(RequestHandler *handler)
void addHandler(RequestHandler *handler)
RequestHandler * _firstHandler
void collectHeaders(const char *headerKeys[], const size_t headerKeysCount)
void send(int code, const char *content_type=NULL, const String &content=String(""))
unsigned long _statusChange
String header(String name)
void sendHeader(const String &name, const String &value, bool first=false)
void onNotFound(THandlerFunction fn)
WebServer(IPAddress addr, int port=80)
bool _parseRequest(WiFiClient &client)
void setContentLength(size_t contentLength)
THandlerFunction _fileUploadHandler
THandlerFunction _notFoundHandler
RequestHandler * _lastHandler
RequestArgument * _currentHeaders
void onFileUpload(THandlerFunction fn)
void send_P(int code, PGM_P content_type, PGM_P content)
void sendContent_P(PGM_P content)
void serveStatic(const char *uri, fs::FS &fs, const char *path, const char *cache_header=NULL)
std::function< void(void)> THandlerFunction