Arduino DLNA Server
Loading...
Searching...
No Matches
HttpClientHandler.h
Go to the documentation of this file.
1#pragma once
2
3#include <stddef.h>
4#include <string.h>
5
6#include "HttpChunkWriter.h"
7#include "HttpHeader.h"
8#include "IHttpServer.h"
9#include "basic/RetryPrint.h"
10#include "basic/StrPrint.h"
11
12namespace tiny_dlna {
13
29template <typename ClientT>
31 public:
32 // Default constructor for use in HttpServer
33 HttpClientHandler() = default;
34
37 virtual ~HttpClientHandler() = default;
38
39 void setClient(ClientT* client) { p_client = client; }
40
43 if (!p_client) return;
44 Client& client = *p_client;
45 int count = 50;
46 while (client.available() == 0) {
47 delay(20);
48 if (count-- <= 0) {
49 break;
50 }
51 }
53 // provide reply with empty header
55 }
56
57 // Helper for writing status and message to the reply (uses p_client)
58 void reply(int status, const char* msg) {
59 if (!p_client) return;
60 reply_header.setValues(status, msg);
62 endClient();
63 }
64
65 void replyChunked(const char* contentType, Stream& inputStream,
66 int status = 200, const char* msg = SUCCESS) override {
67 if (!p_client) return;
68 replyChunked(contentType, status, msg);
69 HttpChunkWriter chunk_writer;
70 uint8_t buffer[buffer_size];
71 while (inputStream.available()) {
72 int len = inputStream.readBytes(buffer, buffer_size);
73 chunk_writer.writeChunk(*p_client, (const char*)buffer, len);
74 }
75 chunk_writer.writeEnd(*p_client);
76 endClient();
77 }
78
79 void replyChunked(const char* contentType, int status = 200,
80 const char* msg = SUCCESS) override {
81 if (!p_client) return;
82 DlnaLogger.log(DlnaLogLevel::Info, "reply %s", "replyChunked");
83 reply_header.setValues(status, msg);
85 reply_header.put(CONTENT_TYPE, contentType);
88 }
89
90 void reply(const char* contentType, Stream& inputStream, int size,
91 int status = 200, const char* msg = SUCCESS) override {
92 if (!p_client) return;
93 DlnaLogger.log(DlnaLogLevel::Info, "reply %s", "stream");
94 reply_header.setValues(status, msg);
96 reply_header.put(CONTENT_TYPE, contentType);
99 uint8_t buffer[buffer_size];
100 while (inputStream.available()) {
101 int len = inputStream.readBytes(buffer, buffer_size);
102 p_client->write((const uint8_t*)buffer, len);
103 }
104 endClient();
105 }
106
107 void reply(const char* contentType, size_t (*callback)(Print& out, void* ref),
108 int status = 200, const char* msg = SUCCESS,
109 void* ref = nullptr) override {
110 if (!p_client) return;
111 DlnaLogger.log(DlnaLogLevel::Info, "reply %s", "via callback");
112 NullPrint nop;
113 size_t size = callback(nop, ref);
114 reply_header.setValues(status, msg);
115 reply_header.put(CONTENT_TYPE, contentType);
119 size_t size_eff = callback(*p_client, ref);
120 if (size_eff != size) {
121 DlnaLogger.log(DlnaLogLevel::Warning,
122 "HttpServer callback wrote %d bytes; expected %d",
123 size_eff, size);
124 }
125#if DLNA_LOG_XML
126 callback(Serial, ref);
127#endif
128
129 #if DLNA_CHECK_XML_LENGTH
130 StrPrint test;
131 size_t test_len = callback(test, ref);
132 if (strlen(test.c_str()) != size) {
133 DlnaLogger.log(DlnaLogLevel::Error,
134 "HttpServer wrote %d bytes: expected %d / strlen: %d",
135 test_len, size, strlen(test.c_str()));
136 }
137#endif
138 endClient();
139 }
140
141 void reply(const char* contentType, const char* str, int status = 200,
142 const char* msg = SUCCESS) override {
143 if (!p_client) return;
144 DlnaLogger.log(DlnaLogLevel::Info, "reply %s", str);
145 int len = strlen(str);
146 reply_header.setValues(status, msg);
148 reply_header.put(CONTENT_TYPE, contentType);
151 p_client->write((const uint8_t*)str, len);
152 endClient();
153 }
154
155 void reply(const char* contentType, const uint8_t* str, int len,
156 int status = 200, const char* msg = SUCCESS) override {
157 if (!p_client) return;
158 DlnaLogger.log(DlnaLogLevel::Info, "reply %s: %d bytes", contentType, len);
159 reply_header.setValues(status, msg);
161 reply_header.put(CONTENT_TYPE, contentType);
164 p_client->write((const uint8_t*)str, len);
165 endClient();
166 }
167
168 void replyOK() override { reply("text/plain", "SUCCESS", 200, SUCCESS); }
169
170 void replyNotFound() override {
171 DlnaLogger.log(DlnaLogLevel::Info, "reply %s", "404");
172 reply("text/plain", "Page Not Found", 404, "Page Not Found");
173 }
174
175 void replyError(int err, const char* msg = "Internal Server Error") override {
176 DlnaLogger.log(DlnaLogLevel::Info, "reply %s", "error");
177 reply("text/plain", msg, err, msg);
178 }
179
180 void endClient() override {
181 if (!p_client) return;
182 p_client->flush();
183 p_client->stop();
184 }
185
186 void crlf() override {
187 if (p_client) p_client->println();
188 }
189
190 Client* client() override { return p_client; }
191
193
195
196 // Allows resizing the internal buffer
197 void resize(size_t newSize) { buffer_size = newSize; }
198
199 protected:
200 ClientT* p_client = nullptr;
203 int buffer_size = 512;
204};
205
206} // namespace tiny_dlna
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
Handles HTTP client connections and responses for the DLNA HTTP server.
Definition: HttpClientHandler.h:30
HttpReplyHeader reply_header
Definition: HttpClientHandler.h:202
virtual ~HttpClientHandler()=default
void reply(const char *contentType, size_t(*callback)(Print &out, void *ref), int status=200, const char *msg=SUCCESS, void *ref=nullptr) override
Definition: HttpClientHandler.h:107
void replyChunked(const char *contentType, int status=200, const char *msg=SUCCESS) override
Definition: HttpClientHandler.h:79
void replyError(int err, const char *msg="Internal Server Error") override
Definition: HttpClientHandler.h:175
int buffer_size
Definition: HttpClientHandler.h:203
HttpRequestHeader & requestHeader()
Definition: HttpClientHandler.h:192
HttpClientHandler(ClientT *client)
Definition: HttpClientHandler.h:36
void reply(const char *contentType, Stream &inputStream, int size, int status=200, const char *msg=SUCCESS) override
Definition: HttpClientHandler.h:90
void reply(int status, const char *msg)
Definition: HttpClientHandler.h:58
void readHttpHeader()
Reads the http header info from the client.
Definition: HttpClientHandler.h:42
HttpReplyHeader & replyHeader()
Definition: HttpClientHandler.h:194
HttpClientHandler(ClientT &client)
Definition: HttpClientHandler.h:35
void setClient(ClientT *client)
Definition: HttpClientHandler.h:39
ClientT * p_client
Definition: HttpClientHandler.h:200
Client * client() override
Definition: HttpClientHandler.h:190
void replyOK() override
Definition: HttpClientHandler.h:168
void replyChunked(const char *contentType, Stream &inputStream, int status=200, const char *msg=SUCCESS) override
Definition: HttpClientHandler.h:65
void reply(const char *contentType, const uint8_t *str, int len, int status=200, const char *msg=SUCCESS) override
Definition: HttpClientHandler.h:155
void replyNotFound() override
Definition: HttpClientHandler.h:170
HttpRequestHeader request_header
Definition: HttpClientHandler.h:201
void reply(const char *contentType, const char *str, int status=200, const char *msg=SUCCESS) override
Definition: HttpClientHandler.h:141
void resize(size_t newSize)
Definition: HttpClientHandler.h:197
void crlf() override
Definition: HttpClientHandler.h:186
void endClient() override
Definition: HttpClientHandler.h:180
void read(Client &in)
Definition: HttpHeader.h:233
HttpHeader & clear(bool activeFlag=true)
clears the data - usually we do not delete but we just set the active flag
Definition: HttpHeader.h:86
HttpHeader & put(const char *key, const char *value)
Definition: HttpHeader.h:105
void write(Client &out)
Definition: HttpHeader.h:267
Reading and Writing of Http Replys.
Definition: HttpHeader.h:409
void setValues(int statusCode, const char *msg="", const char *protocol=nullptr)
Definition: HttpHeader.h:412
Reading and writing of Http Requests.
Definition: HttpHeader.h:355
Definition: IHttpServer.h:19
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
Definition: Allocator.h:13
const char * CONTENT_TYPE
Definition: HttpHeader.h:16
const char * CON_KEEP_ALIVE
Definition: HttpHeader.h:20
const char * CONTENT_LENGTH
Definition: HttpHeader.h:17
const char * TRANSFER_ENCODING
Definition: HttpHeader.h:21
const char * SUCCESS
Definition: HttpHeader.h:25
const char * CHUNKED
Definition: HttpHeader.h:22
const char * CONNECTION
Definition: HttpHeader.h:18