Arduino DLNA Server
Loading...
Searching...
No Matches
HttpChunkWriter.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdarg>
4#include <cstdint>
5#include <cstdio>
6#include <cstring>
7
8#include "Client.h"
9#include "Print.h"
10#include "dlna_config.h"
11#include "basic/Logger.h"
12#include "basic/StrView.h"
13
14namespace tiny_dlna {
15
20 public:
21 int writeChunk(Client& client, const char* str, int len,
22 const char* str1 = nullptr, int len1 = 0) {
23 DlnaLogger.log(DlnaLogLevel::Debug, "HttpChunkWriter", "writeChunk");
24 client.println(len + len1, HEX);
25 int result = client.write((const uint8_t*)str, len);
26 if (str1 != nullptr) {
27 result += client.write((const uint8_t*)str1, len1);
28 }
29 client.println();
30 return result;
31 }
32
33 int writeChunk(Client& client, const char* str) {
34 int len = strlen(str);
35 return writeChunk(client, str, len);
36 }
37
38 void writeEnd(Client& client) { writeChunk(client, "", 0); }
39};
40
56class ChunkPrint : public Print {
57 public:
58 ChunkPrint(Client& client) : client_ptr(&client) {}
59
60 size_t write(uint8_t ch) override {
61 char c = (char)ch;
63 return 1;
64 }
65
66 size_t write(const uint8_t* buffer, size_t size) override {
67 if (size == 0) return 0;
68 chunk_writer.writeChunk(*client_ptr, (const char*)buffer, (int)size);
69 return size;
70 }
71
72 size_t print(const char* str) {
73 if (!str) return 0;
74 int len = strlen(str);
75 // avoid emitting a zero-length chunk mid-response (which would signal
76 // the end of the chunked stream). If the string is empty, do nothing.
77 if (len == 0) return 0;
79 return len;
80 }
81
82 size_t printEscaped(const char* str) {
83 if (!str) return 0;
84 int len = strlen(str);
85 if (len == 0) return 0;
86 int max_len = len * 3 + 1; // worst-case expansion when replacing to &amp;
87 // allocate on heap to avoid large stack usage
88 char* buffer = new char[max_len];
89 // copy and perform replacements using StrView helper
90 strncpy(buffer, str, max_len - 1);
91 buffer[max_len - 1] = '\0';
92 StrView str_view{buffer, max_len, (int)strlen(buffer)};
93 str_view.replaceAll("&", "&amp;");
94 str_view.replaceAll("<", "&lt;");
95 str_view.replaceAll(">", "&gt;");
96 int new_len = str_view.length();
97 chunk_writer.writeChunk(*client_ptr, str_view.c_str(), new_len);
98 delete[] buffer;
99 return len;
100 }
101
102 size_t println(const char* str) {
103 int len = strlen(str);
104 // allocate on heap to avoid large VLA on stack
105 char* tmp = new char[len + 3];
106 strcpy(tmp, str);
107 strcat(tmp, "\r\n");
108 chunk_writer.writeChunk(*client_ptr, tmp, len + 2);
109 delete[] tmp;
110 return len + 2;
111 }
112
113 size_t println() {
115 return 2;
116 }
117
118 size_t printf(const char* fmt, ...) {
119 char buf[MAX_PRINTF_SIZE];
120 va_list ap;
121 va_start(ap, fmt);
122 int n = vsnprintf(buf, sizeof(buf), fmt, ap);
123 va_end(ap);
124 if (n <= 0) return 0;
125 if (n >= (int)sizeof(buf)) n = (int)sizeof(buf) - 1;
127 return (size_t)n;
128 }
129
131
132 protected:
133 Client* client_ptr;
135};
136
137} // namespace tiny_dlna
Print implementation for HTTP chunked transfer encoding.
Definition: HttpChunkWriter.h:56
size_t printf(const char *fmt,...)
Definition: HttpChunkWriter.h:118
size_t print(const char *str)
Definition: HttpChunkWriter.h:72
ChunkPrint(Client &client)
Definition: HttpChunkWriter.h:58
HttpChunkWriter chunk_writer
Definition: HttpChunkWriter.h:134
size_t printEscaped(const char *str)
Definition: HttpChunkWriter.h:82
size_t println(const char *str)
Definition: HttpChunkWriter.h:102
size_t write(uint8_t ch) override
Definition: HttpChunkWriter.h:60
size_t write(const uint8_t *buffer, size_t size) override
Definition: HttpChunkWriter.h:66
Client * client_ptr
Definition: HttpChunkWriter.h:133
void end()
Definition: HttpChunkWriter.h:130
size_t println()
Definition: HttpChunkWriter.h:113
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
int writeChunk(Client &client, const char *str)
Definition: HttpChunkWriter.h:33
void writeEnd(Client &client)
Definition: HttpChunkWriter.h:38
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 replaceAll(const char *toReplace, const char *replaced)
Replaces all instances of toReplace with replaced.
Definition: StrView.h:437
#define MAX_PRINTF_SIZE
Max printf buffer size.
Definition: dlna_config.h:50
Definition: Allocator.h:13