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_heaer_ptr = &header;
26 has_ended = false;
27 }
28
29 void open(Client &client) {
30 LOGD("HttpChunkReader: %s", "open");
31 has_ended = false;
32 readChunkLen(client);
33 }
34
35 // reads a block of data from the chunks
36 virtual int read(Client &client, uint8_t *str, int len) {
37 LOGD("HttpChunkReader: %s", "read");
38 if (has_ended && open_chunk_len == 0) return 0;
39
40 // read the chunk data - but not more then available
41 int read_max = len < open_chunk_len ? len : open_chunk_len;
42 int len_processed = client.read(str, read_max);
43 // update current unprocessed chunk
45
46 // remove traling CR LF from data
47 if (open_chunk_len <= 0) {
48 removeCRLF(client);
49 readChunkLen(client);
50 }
51
52 return len_processed;
53 }
54
55 // reads a single line from the chunks
56 virtual int readln(Client &client, uint8_t *str, int len,
57 bool incl_nl = true) {
58 LOGD("HttpChunkReader: %s", "readln");
59 if (has_ended && open_chunk_len == 0) return 0;
60
61 int read_max = len < open_chunk_len ? len : open_chunk_len;
62 int len_processed = readlnInternal(client, str, read_max, incl_nl);
64
65 // the chunks are terminated by a final CRLF
66 if (open_chunk_len <= 0) {
67 removeCRLF(client);
68 readChunkLen(client);
69 }
70
71 return len_processed;
72 }
73
74 int available() {
75 int result = has_ended ? 0 : open_chunk_len;
76 char msg[50];
77 snprintf(msg, 50, "available=>%d", result);
78 LOGD("HttpChunkReader: %s", msg);
79
80 return result;
81 }
82
83 protected:
85 bool has_ended = false;
87
88 void removeCRLF(Client &client) {
89 LOGD("HttpChunkReader: %s", "removeCRLF");
90 // remove traling CR LF from data
91 if (client.peek() == '\r') {
92 LOGD("HttpChunkReader: %s", "removeCR");
93 client.read();
94 }
95 if (client.peek() == '\n') {
96 LOGD("HttpChunkReader: %s", "removeLF");
97 client.read();
98 }
99 }
100
101 // we read the chunk length which is indicated as hex value
102 virtual void readChunkLen(Client &client) {
103 LOGD("HttpChunkReader::readChunkLen");
106 LOGD("HttpChunkReader::readChunkLen %s", (const char *)len_str);
107 open_chunk_len = strtol((char *)len_str, nullptr, 16);
108
109 char msg[80] = {0};
110 snprintf(msg, 80, "chunk_len: %d", open_chunk_len);
111 LOGD("HttpChunkReader::readChunkLen->%s", msg);
112
113 if (open_chunk_len == 0) {
114 has_ended = true;
115 LOGD("HttpChunkReader::readChunkLen %s", "last chunk received");
116 // processing of additinal final headers after the chunk end
117 if (http_heaer_ptr != nullptr) {
118 http_heaer_ptr->readExt(client);
119 }
120 }
121 }
122};
123
124} // namespace audio_tools
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#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
virtual int readln(Client &client, uint8_t *str, int len, bool incl_nl=true)
Definition HttpChunkReader.h:56
bool has_ended
Definition HttpChunkReader.h:85
virtual void readChunkLen(Client &client)
Definition HttpChunkReader.h:102
int available()
Definition HttpChunkReader.h:74
void removeCRLF(Client &client)
Definition HttpChunkReader.h:88
void open(Client &client)
Definition HttpChunkReader.h:29
virtual int read(Client &client, uint8_t *str, int len)
Definition HttpChunkReader.h:36
HttpChunkReader(HttpReplyHeader &header)
constructor for processing final header information
Definition HttpChunkReader.h:23
int open_chunk_len
Definition HttpChunkReader.h:84
HttpReplyHeader * http_heaer_ptr
Definition HttpChunkReader.h:86
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:19
Reading and Writing of Http Replys.
Definition HttpHeader.h:444
void readExt(Client &in)
Definition HttpHeader.h:458
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:512