6#include "HardwareSerial.h"
34template <
typename ClientType,
typename ServerType>
40 this->
buffer.resize(bufferSize);
53 static IPAddress address;
54 address = WiFi.localIP();
73 void rewrite(
const char* from,
const char* to)
override {
81 void* ctx[] =
nullptr,
int ctxCount = 0)
override {
88 memmove(hl->
context, ctx, ctxCount *
sizeof(
void*));
107 const char* result)
override {
113 if (hl->contextCount < 2) {
117 const char* mime = (
const char*)hl->context[0];
118 const char* msg = (
const char*)hl->context[1];
123 hl->
context[1] = (
void*)result;
132 const uint8_t* data,
int len)
override {
138 if (hl->contextCount < 3) {
142 const char* mime =
static_cast<char*
>(hl->context[0]);
143 const uint8_t* data =
static_cast<uint8_t*
>(hl->context[1]);
144 int* p_len = (
int*)hl->context[2];
164 if (hl->contextCount < 1) {
170 Url* url =
static_cast<Url*
>(hl->context[0]);
200 nullstr(handler_line_ptr->path.c_str()),
201 methods[handler_line_ptr->method],
202 nullstr(handler_line_ptr->mime));
204 if (pathStr.
matches(handler_line_ptr->path.c_str()) &&
210 nullstr(handler_line_ptr->path.c_str()));
211 handler_line_ptr->fn(
this, path, handler_line_ptr);
226 int status = 200,
const char* msg =
SUCCESS)
override {
229 while (inputStream.available()) {
230 int len = inputStream.readBytes(
buffer.data(),
buffer.size());
240 const char* msg =
SUCCESS)
override {
250 void reply(
const char* contentType, Stream& inputStream,
int size,
251 int status = 200,
const char* msg =
SUCCESS)
override {
259 while (inputStream.available()) {
260 int len = inputStream.readBytes(
buffer.data(),
buffer.size());
268 void reply(
const char* contentType,
size_t (*callback)(Print& out,
void*
ref),
269 int status = 200,
const char* msg =
SUCCESS,
270 void*
ref =
nullptr)
override {
273 size_t size = callback(nop,
ref);
281 callback(Serial,
ref);
283#if DLNA_CHECK_XML_LENGTH
285 size_t test_len = callback(test,
ref);
286 if (strlen(test.
c_str()) != size) {
288 "HttpServer wrote %d bytes: expected %d / strlen: %d", test_len, size, strlen(test.
c_str()));
296 void reply(
const char* contentType,
const char* str,
int status = 200,
297 const char* msg =
SUCCESS)
override {
299 int len = strlen(str);
309 void reply(
const char* contentType,
const uint8_t* str,
int len,
310 int status = 200,
const char* msg =
SUCCESS)
override {
327 reply(404,
"Page Not Found");
330 void replyError(
int err,
const char* msg =
"Internal Server Error")
override {
474 if ((!(*it).connected())) {
490 void reply(
int status,
const char* msg) {
497 const char*
nullstr(
const char* in) {
return in ==
nullptr ?
"" : in; }
504 reply_header.
clear();
520 if (
rewrite->from.matches(from)) {
529 bool matchesMime(
const char* handler_mime,
const char* request_mime) {
532 if (
StrView(handler_mime).isEmpty() ||
StrView(request_mime).isEmpty()) {
Writes the data chunked to the actual client.
Definition: HttpChunkWriter.h:19
int writeChunk(Client &client, const char *str, int len, const char *str1=nullptr, int len1=0)
Definition: HttpChunkWriter.h:21
void writeEnd(Client &client)
Definition: HttpChunkWriter.h:38
Used to register and process callbacks.
Definition: HttpRequestHandlerLine.h:15
web_callback_fn fn
Definition: HttpRequestHandlerLine.h:39
Str path
Definition: HttpRequestHandlerLine.h:37
TinyMethodID method
Definition: HttpRequestHandlerLine.h:36
void ** context
Definition: HttpRequestHandlerLine.h:40
const char * mime
Definition: HttpRequestHandlerLine.h:38
int contextCount
Definition: HttpRequestHandlerLine.h:41
Object which information about the rewrite rule.
Definition: HttpRequestRewrite.h:11
Header-only HTTP server wrapper that registers callback handlers.
Definition: HttpServer.h:35
ClientType * client_ptr
Definition: HttpServer.h:459
void processRequest()
Definition: HttpServer.h:500
void setReference(void *reference) override
Definesa reference/context object.
Definition: HttpServer.h:445
void reply(const char *contentType, size_t(*callback)(Print &out, void *ref), int status=200, const char *msg=SUCCESS, void *ref=nullptr) override
write reply - using callback that writes to stream
Definition: HttpServer.h:268
void replyNotFound() override
write 404 reply
Definition: HttpServer.h:325
bool copy() override
Call this method from your loop!
Definition: HttpServer.h:363
void replyOK() override
write OK reply with 200 SUCCESS
Definition: HttpServer.h:322
void reply(int status, const char *msg)
Writes the status and message to the reply.
Definition: HttpServer.h:490
HttpReplyHeader reply_header
Definition: HttpServer.h:453
void on(const char *url, TinyMethodID method, web_callback_fn fn, void *ctx[]=nullptr, int ctxCount=0) override
register a generic handler
Definition: HttpServer.h:80
void replyChunked(const char *contentType, int status=200, const char *msg=SUCCESS) override
start of chunked reply: use HttpChunkWriter to provde the data
Definition: HttpServer.h:239
List< ClientType >::Iterator current_client_iterator
Definition: HttpServer.h:457
void on(const char *url, TinyMethodID method, Url &redirect) override
register a redirection
Definition: HttpServer.h:160
bool isActive() override
Check if server is active.
Definition: HttpServer.h:420
void setNoConnectDelay(int delay) override
Set no-connect delay.
Definition: HttpServer.h:430
void removeClosedClients()
Definition: HttpServer.h:471
bool onRequest(const char *path) override
Definition: HttpServer.h:190
bool matchesMime(const char *handler_mime, const char *request_mime)
Definition: HttpServer.h:529
void crlf() override
print a CR LF
Definition: HttpServer.h:349
List< ClientType > open_clients
Definition: HttpServer.h:456
void on(const char *url, TinyMethodID method, const char *mime, const uint8_t *data, int len) override
register a handler which provides the indicated string
Definition: HttpServer.h:131
void on(const char *url, TinyMethodID method, const char *mime, const char *result) override
register a handler which provides the indicated string
Definition: HttpServer.h:106
HttpRequestHeader & requestHeader() override
provides the request header
Definition: HttpServer.h:336
HttpServer(ServerType &server, int bufferSize=1024)
Definition: HttpServer.h:37
int no_connect_delay
Definition: HttpServer.h:464
void rewrite(const char *from, const char *to) override
adds a rewrite rule
Definition: HttpServer.h:73
ServerType * server_ptr
Definition: HttpServer.h:460
bool doLoop() override
Legacy method: same as copy();.
Definition: HttpServer.h:360
ClientType & client() override
Provides the current client.
Definition: HttpServer.h:415
void reply(const char *contentType, Stream &inputStream, int size, int status=200, const char *msg=SUCCESS) override
write reply - copies data from input stream with header size
Definition: HttpServer.h:250
bool is_active
Definition: HttpServer.h:461
~HttpServer() override
Definition: HttpServer.h:43
List< HttpRequestRewrite * > rewrite_collection
Definition: HttpServer.h:455
HttpRequestHeader request_header
Definition: HttpServer.h:452
const char * nullstr(const char *in)
Converts null to an empty string.
Definition: HttpServer.h:497
IPAddress & localIP() override
Provides the local ip address.
Definition: HttpServer.h:52
void end() override
stops the server_ptr
Definition: HttpServer.h:67
void replyChunked(const char *contentType, Stream &inputStream, int status=200, const char *msg=SUCCESS) override
chunked reply with data from an input stream
Definition: HttpServer.h:225
void replyError(int err, const char *msg="Internal Server Error") override
Send error response with status code.
Definition: HttpServer.h:330
const char * local_host
Definition: HttpServer.h:463
void reply(const char *contentType, const uint8_t *str, int len, int status=200, const char *msg=SUCCESS) override
Send binary data response.
Definition: HttpServer.h:309
void addHandler(HttpRequestHandlerLine *handlerLinePtr) override
adds a new handler
Definition: HttpServer.h:355
bool begin() override
Starts the server.
Definition: HttpServer.h:59
List< HttpRequestHandlerLine * > handler_collection
Definition: HttpServer.h:454
const char * resolveRewrite(const char *from)
Definition: HttpServer.h:516
void * ref
Definition: HttpServer.h:465
void * getReference() override
Provides access to a reference/context object.
Definition: HttpServer.h:448
void reply(const char *contentType, const char *str, int status=200, const char *msg=SUCCESS) override
write reply - string with header size
Definition: HttpServer.h:296
HttpReplyHeader & replyHeader() override
provides the reply header
Definition: HttpServer.h:339
Vector< char > buffer
Definition: HttpServer.h:462
void on(const char *url, TinyMethodID method, const char *mime, web_callback_fn fn) override
register a handler with mime
Definition: HttpServer.h:94
Str contentStr() override
converts the client content to a string
Definition: HttpServer.h:433
const char * localHost() override
Determines the local ip address.
Definition: HttpServer.h:423
void endClient() override
closes the connection to the current client_ptr
Definition: HttpServer.h:342
Abstract interface for HTTP server functionality.
Definition: IHttpServer.h:30
List Iterator.
Definition: List.h:29
Double linked list.
Definition: List.h:19
Iterator begin()
Definition: List.h:309
bool empty()
Definition: List.h:331
bool push_back(T &data)
Definition: List.h:128
Iterator end()
Definition: List.h:314
Iterator erase(Iterator it)
Definition: List.h:290
Class with does not do any output: it can be used to determine the length of the output.
Definition: NullPrint.h:12
Print to a dynamic string.
Definition: StrPrint.h:13
const char * c_str()
Definition: StrPrint.h:39
A simple wrapper to provide string functions on char*. If the underlying char* is a const we do not a...
Definition: StrView.h:18
virtual bool matches(const char *pattern)
Definition: StrView.h:207
virtual bool replace(const char *toReplace, const int replaced)
Replaces the first instance of toReplace with replaced.
Definition: StrView.h:395
virtual bool contains(const char *str)
checks if the string contains a substring
Definition: StrView.h:284
Heap-backed string utility used throughout tiny_dlna.
Definition: Str.h:27
void add(const char *append)
Append C-string (ignored if nullptr)
Definition: Str.h:96
URL parser which breaks a full url string up into its individual parts.
Definition: Url.h:18
const char * url()
Definition: Url.h:39
Lightweight wrapper around std::vector with Arduino-friendly helpers and a pluggable allocator.
Definition: Vector.h:39
#define DLNA_HTTP_REQUEST_TIMEOUT_MS
Define the default http request timeout.
Definition: dlna_config.h:20
Definition: Allocator.h:13
const char * CONTENT_TYPE
Definition: HttpHeader.h:16
const char * CON_KEEP_ALIVE
Definition: HttpHeader.h:20
const char * methods[]
Definition: HttpHeader.h:50
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
void(*)(IHttpServer *server, const char *requestPath, HttpRequestHandlerLine *handlerLine) web_callback_fn
Definition: IHttpServer.h:20
const char * CHUNKED
Definition: HttpHeader.h:22
const char * CONNECTION
Definition: HttpHeader.h:18
const char * LOCATION
Definition: HttpHeader.h:32