arduino-audio-tools
Loading...
Searching...
No Matches
HttpChunkReader.h
Go to the documentation of this file.
1#pragma once
2
3#include "HttpHeader.h"
4#include "HttpLineReader.h"
5
6namespace audio_tools {
7
15 public:
19 has_ended = false;
20 }
21
24 http_header_ptr = &header;
26 has_ended = false;
27 }
28
30 bool open(Client& client) {
31 LOGD("HttpChunkReader: %s", "open");
32 has_ended = false;
33 return readChunkLen(client);
34 }
35
37 int read(Client& client, uint8_t* str, int len) {
38 LOGD("HttpChunkReader: %s", "read");
39 if (has_ended && open_chunk_len == 0) return 0;
40
41 // wait for data
42 auto start_time = millis();
43 client.setTimeout(timeout);
44 int result = client.peek();
45 auto time_eff = millis() - start_time;
46 if (result == -1 && time_eff < timeout) {
47 LOGE("Timout %d not working - waited: %d ms", timeout);
48 }
49
50
51 // read the chunk data - but not more then available
52 int read_max = len < open_chunk_len ? len : open_chunk_len;
53 int len_processed = client.read(str, read_max);
54 if (len_processed == -1) {
55 LOGE("HttpChunkReader: client.read result -1, open: %d for timeout %d",open_chunk_len, timeout);
56 return -1;
57 }
58 // update current unprocessed chunk
60
61 // remove traling CR LF from data
62 if (open_chunk_len <= 0) {
63 removeCRLF(client);
64 readChunkLen(client);
65 }
66
67 return len_processed;
68 }
69
71 int readln(Client& client, uint8_t* str, int len,
72 bool incl_nl = true) {
73 LOGD("HttpChunkReader: %s", "readln");
74 if (has_ended && open_chunk_len == 0) return 0;
75
76 int read_max = len < open_chunk_len ? len : open_chunk_len;
77 int len_processed = readlnInternal(client, str, read_max, incl_nl);
78 if (len_processed == -1) {
79 LOGD("HttpChunkReader: readln result -1");
80 return -1;
81 }
83
84 // the chunks are terminated by a final CRLF
85 if (open_chunk_len <= 0) {
86 removeCRLF(client);
87 readChunkLen(client);
88 }
89
90 return len_processed;
91 }
92
94 int available() {
95 int result = has_ended ? 0 : open_chunk_len;
96 LOGD("HttpChunkReader: available=>%d", result);
97 return result;
98 }
99
103 }
104
105 protected:
107 bool has_ended = false;
109 int timeout = 0;
110
111 void removeCRLF(Client& client) {
112 LOGD("HttpChunkReader: %s", "removeCRLF");
113
114 // remove traling CR LF from data
115 if (client.peek() == '\r') {
116 LOGD("HttpChunkReader: %s", "removeCR");
117 client.read();
118 }
119 if (client.peek() == '\n') {
120 LOGD("HttpChunkReader: %s", "removeLF");
121 client.read();
122 }
123 }
124
125 // we read the chunk length which is indicated as hex value
126 bool readChunkLen(Client& client) {
127 LOGD("HttpChunkReader::readChunkLen");
129 if (readlnInternal(client, len_str, HTTP_CHUNKED_SIZE_MAX_LEN, false) < 0) {
130 LOGD("HttpChunkReader::readChunkLen readlnInternal result -1");
131 has_ended = true;
132 return false;
133 }
134 open_chunk_len = strtol((char*)len_str, nullptr, 16);
135
136 if (open_chunk_len == 0) {
137 has_ended = true;
138 LOGD("HttpChunkReader::readChunkLen %s", "last chunk received");
139 // processing of additinal final headers after the chunk end
140 if (http_header_ptr != nullptr) {
141 http_header_ptr->readExt(client);
142 }
143 }
144 return true;
145 }
146};
147
148} // namespace audio_tools
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define HTTP_CHUNKED_SIZE_MAX_LEN
Definition AudioToolsConfig.h:148
Definition NoArduino.h:169
virtual int read(uint8_t *buffer, size_t len)
Definition NoArduino.h:172
Http might reply with chunks. So we need to dechunk the data. see https://en.wikipedia....
Definition HttpChunkReader.h:14
void setTimeout(int timeoutMs)
Timout is just used for logging.
Definition HttpChunkReader.h:101
int readln(Client &client, uint8_t *str, int len, bool incl_nl=true)
reads a single line from the chunks
Definition HttpChunkReader.h:71
bool has_ended
Definition HttpChunkReader.h:107
int available()
returns the number of bytes which are still available in the current chunk
Definition HttpChunkReader.h:94
int timeout
Definition HttpChunkReader.h:109
void removeCRLF(Client &client)
Definition HttpChunkReader.h:111
HttpReplyHeader * http_header_ptr
Definition HttpChunkReader.h:108
bool readChunkLen(Client &client)
Definition HttpChunkReader.h:126
HttpChunkReader(HttpReplyHeader &header)
constructor for processing final header information
Definition HttpChunkReader.h:23
bool open(Client &client)
opens the chunk reader and reads the first chunk length
Definition HttpChunkReader.h:30
int open_chunk_len
Definition HttpChunkReader.h:106
int read(Client &client, uint8_t *str, int len)
reads a block of data from the chunks
Definition HttpChunkReader.h:37
HttpChunkReader()
default constructor
Definition HttpChunkReader.h:17
We read a single line. A terminating 0 is added to the string to make it compliant for c string funct...
Definition HttpLineReader.h:14
virtual int readlnInternal(Stream &client, uint8_t *str, int len, bool incl_nl=true)
Definition HttpLineReader.h:21
Reading and Writing of Http Replys.
Definition HttpHeader.h:453
void readExt(Client &in)
Definition HttpHeader.h:467
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
uint32_t millis()
Returns the milliseconds since the start.
Definition Time.h:12
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:512