7 #include "HardwareSerial.h"
42 static IPAddress address;
43 address = WiFi.localIP();
49 bool begin(
int port,
const char* ssid,
const char* password) {
50 if (WiFi.status() != WL_CONNECTED && ssid !=
nullptr &&
51 password !=
nullptr) {
52 WiFi.begin(ssid, password);
53 while (WiFi.status() != WL_CONNECTED) {
59 Serial.print(
"Started Server at ");
60 Serial.print(WiFi.localIP());
82 void rewrite(
const char* from,
const char* to) {
90 void* ctx[] =
nullptr,
int ctxCount = 0) {
97 memmove(hl->
context, ctx, ctxCount *
sizeof(
void*));
116 const char* result) {
122 if (hl->contextCount < 2) {
126 const char* mime = (
const char*)hl->context[0];
127 const char* msg = (
const char*)hl->context[1];
132 hl->
context[1] = (
void*)result;
141 const uint8_t* data,
int len) {
147 if (hl->contextCount < 3) {
151 const char* mime =
static_cast<char*
>(hl->context[0]);
152 const uint8_t* data =
static_cast<uint8_t*
>(hl->context[1]);
153 int* p_len = (
int*)hl->context[2];
173 if (hl->contextCount < 1) {
179 Url* url =
static_cast<Url*
>(hl->context[0]);
205 if (p_tunnel ==
nullptr) {
210 const char* mime = hl->mime;
212 Stream* p_in = p_tunnel->
get();
213 if (p_in ==
nullptr) {
219 StrView content_len_str{content_len};
221 server_ptr->reply(mime, *p_in, content_len_str.toInt());
244 nullstr(handler_line_ptr->path.c_str()),
245 methods[handler_line_ptr->method],
246 nullstr(handler_line_ptr->mime));
248 if (pathStr.
matches(handler_line_ptr->path.c_str()) &&
253 nullstr(handler_line_ptr->path.c_str()));
254 handler_line_ptr->fn(
this, path, handler_line_ptr);
269 int status = 200,
const char* msg =
SUCCESS) {
272 while (inputStream.available()) {
293 void reply(
const char* contentType, Stream& inputStream,
int size,
294 int status = 200,
const char* msg =
SUCCESS) {
302 while (inputStream.available()) {
311 void reply(
const char* contentType,
void (*callback)(Stream& out),
312 int status = 200,
const char* msg =
SUCCESS) {
324 void reply(
const char* contentType,
void (*callback)(Print& out),
325 int status = 200,
const char* msg =
SUCCESS) {
337 void reply(
const char* contentType,
const char* str,
int status = 200,
340 int len = strlen(str);
350 void reply(
const char* contentType,
const uint8_t* str,
int len,
351 int status = 200,
const char* msg =
SUCCESS) {
368 reply(404,
"Page Not Found");
372 void reply(
int status,
const char* msg) {
417 if (
client.available() > 5) {
466 const char*
nullstr(
const char* in) {
return in ==
nullptr ?
"" : in; }
473 reply_header.
clear();
489 if (
rewrite->from.matches(from)) {
498 bool matchesMime(
const char* handler_mime,
const char* request_mime) {
499 if (handler_mime ==
nullptr || request_mime ==
nullptr) {
Writes the data chunked to the actual client.
Definition: HttpChunkWriter.h:12
int writeChunk(Client &client, const char *str, int len, const char *str1=nullptr, int len1=0)
Definition: HttpChunkWriter.h:14
void writeEnd(Client &client)
Definition: HttpChunkWriter.h:31
Used to register and process callbacks.
Definition: HttpRequestHandlerLine.h:19
web_callback_fn fn
Definition: HttpRequestHandlerLine.h:38
Str path
Definition: HttpRequestHandlerLine.h:36
TinyMethodID method
Definition: HttpRequestHandlerLine.h:35
void ** context
Definition: HttpRequestHandlerLine.h:39
const char * mime
Definition: HttpRequestHandlerLine.h:37
int contextCount
Definition: HttpRequestHandlerLine.h:40
Object which information about the rewrite rule.
Definition: HttpRequestRewrite.h:11
virtual HttpReplyHeader & reply()
Definition: HttpRequest.h:109
A Simple Header only implementation of Http Server that allows the registration of callback functions...
Definition: HttpServer.h:24
bool is_active
Definition: HttpServer.h:460
Client & client()
Provides the current client.
Definition: HttpServer.h:436
HttpRequestHeader & requestHeader()
provides the request header
Definition: HttpServer.h:380
bool begin(int port, const char *ssid, const char *password)
Definition: HttpServer.h:49
void setNoConnectDelay(int delay)
Definition: HttpServer.h:449
Client * client_ptr
Definition: HttpServer.h:458
bool doLoop()
Legacy method: same as copy();.
Definition: HttpServer.h:404
Vector< char > buffer
Definition: HttpServer.h:461
const char * resolveRewrite(const char *from)
Definition: HttpServer.h:485
void on(const char *url, TinyMethodID method, web_callback_fn fn, void *ctx[]=nullptr, int ctxCount=0)
register a generic handler
Definition: HttpServer.h:89
void replyOK()
write OK reply with 200 SUCCESS
Definition: HttpServer.h:363
List< HttpRequestRewrite * > rewrite_collection
Definition: HttpServer.h:457
bool copy()
Call this method from your loop!
Definition: HttpServer.h:407
void on(const char *url, TinyMethodID method, Url &redirect)
register a redirection
Definition: HttpServer.h:169
void endClient()
closes the connection to the current client_ptr
Definition: HttpServer.h:386
List< HttpRequestHandlerLine * > handler_collection
Definition: HttpServer.h:455
bool onRequest(const char *path)
Definition: HttpServer.h:235
const char * local_host
Definition: HttpServer.h:462
void reply(const char *contentType, void(*callback)(Print &out), int status=200, const char *msg=SUCCESS)
write reply - using callback that writes to stream
Definition: HttpServer.h:324
bool matchesMime(const char *handler_mime, const char *request_mime)
Definition: HttpServer.h:498
void processRequest()
Definition: HttpServer.h:469
void reply(const char *contentType, Stream &inputStream, int size, int status=200, const char *msg=SUCCESS)
write reply - copies data from input stream with header size
Definition: HttpServer.h:293
HttpRequestHeader request_header
Definition: HttpServer.h:453
IPAddress & localIP()
Provides the local ip address.
Definition: HttpServer.h:41
WiFiServer * server_ptr
Definition: HttpServer.h:459
void addHandler(HttpRequestHandlerLine *handlerLinePtr)
adds a new handler
Definition: HttpServer.h:399
void on(const char *url, TinyMethodID method, const char *mime, const char *result)
register a handler which provides the indicated string
Definition: HttpServer.h:115
void on(const char *url, TinyMethodID method, HttpTunnel &tunnel)
register a redirection
Definition: HttpServer.h:198
const char * localHost()
Determines the local ip address.
Definition: HttpServer.h:442
void reply(int status, const char *msg)
Writes the status and message to the reply.
Definition: HttpServer.h:372
HttpReplyHeader reply_header
Definition: HttpServer.h:454
void end()
stops the server_ptr
Definition: HttpServer.h:76
void replyNotFound()
write 404 reply
Definition: HttpServer.h:366
void crlf()
print a CR LF
Definition: HttpServer.h:393
void reply(const char *contentType, const char *str, int status=200, const char *msg=SUCCESS)
write reply - string with header size
Definition: HttpServer.h:337
void rewrite(const char *from, const char *to)
adds a rewrite rule
Definition: HttpServer.h:82
void reply(const char *contentType, void(*callback)(Stream &out), int status=200, const char *msg=SUCCESS)
write reply - using callback that writes to stream
Definition: HttpServer.h:311
int no_connect_delay
Definition: HttpServer.h:463
const char * nullstr(const char *in)
Converts null to an empty string.
Definition: HttpServer.h:466
void replyChunked(const char *contentType, int status=200, const char *msg=SUCCESS)
start of chunked reply: use HttpChunkWriter to provde the data
Definition: HttpServer.h:282
void reply(const char *contentType, const uint8_t *str, int len, int status=200, const char *msg=SUCCESS)
Definition: HttpServer.h:350
void replyChunked(const char *contentType, Stream &inputStream, int status=200, const char *msg=SUCCESS)
chunked reply with data from an input stream
Definition: HttpServer.h:268
HttpServer(WiFiServer &server, int bufferSize=1024)
Definition: HttpServer.h:26
void on(const char *url, TinyMethodID method, const char *mime, const uint8_t *data, int len)
register a handler which provides the indicated string
Definition: HttpServer.h:140
HttpReplyHeader & replyHeader()
provides the reply header
Definition: HttpServer.h:383
~HttpServer()
Definition: HttpServer.h:32
void on(const char *url, TinyMethodID method, const char *mime, web_callback_fn fn)
register a handler with mime
Definition: HttpServer.h:103
bool begin(int port)
Starts the server on the indicated port.
Definition: HttpServer.h:68
Forwards a request to a destination URL and provides a pointer to the result stream.
Definition: HttpTunnel.h:12
Stream * get()
Executes the get request.
Definition: HttpTunnel.h:21
const char * mime()
Definition: HttpTunnel.h:31
HttpRequest & request()
Definition: HttpTunnel.h:29
Double linked list.
Definition: List.h:19
void log(DlnaLogLevel current_level, const char *fmt...)
Print log message.
Definition: Logger.h:40
A simple wrapper to provide string functions on char*. If the underlying char* is a const we do not a...
Definition: StrView.h:25
virtual bool matches(const char *pattern)
Definition: StrView.h:200
virtual bool contains(const char *str)
checks if the string contains a substring
Definition: StrView.h:277
URL parser which breaks a full url string up into its individual parts.
Definition: Url.h:18
const char * url()
Definition: Url.h:46
bool resize(int newSize, T value)
Definition: Vector.h:247
int size()
Definition: Vector.h:163
T * data()
Definition: Vector.h:294
Definition: Allocator.h:6
const char * CONTENT_TYPE
Definition: HttpHeader.h:16
const char * CON_KEEP_ALIVE
Definition: HttpHeader.h:20
void(* web_callback_fn)(HttpServer *server, const char *requestPath, HttpRequestHandlerLine *handlerLine)
Definition: HttpRequestHandlerLine.h:12
@ DlnaDebug
Definition: Logger.h:16
@ DlnaInfo
Definition: Logger.h:16
@ DlnaWarning
Definition: Logger.h:16
@ DlnaError
Definition: Logger.h:16
const char * methods[]
Definition: HttpHeader.h:47
const char * CONTENT_LENGTH
Definition: HttpHeader.h:17
TinyMethodID
Definition: HttpHeader.h:35
const char * TRANSFER_ENCODING
Definition: HttpHeader.h:21
const char * SUCCESS
Definition: HttpHeader.h:25
LoggerClass DlnaLogger
Definition: Logger.cpp:5
const char * CHUNKED
Definition: HttpHeader.h:22
const char * CONNECTION
Definition: HttpHeader.h:18
const char * LOCATION
Definition: HttpHeader.h:32