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 // read the chunk data - but not more then available
42 int read_max = len < open_chunk_len ? len : open_chunk_len;
43 int len_processed = client.read(str, read_max);
44 if (len_processed == -1) {
45 LOGE("HttpChunkReader: client.read result -1, open: %d",open_chunk_len);
46 return -1;
47 }
48 // update current unprocessed chunk
50
51 // remove traling CR LF from data
52 if (open_chunk_len <= 0) {
53 removeCRLF(client);
54 readChunkLen(client);
55 }
56
57 return len_processed;
58 }
59
61 int readln(Client& client, uint8_t* str, int len,
62 bool incl_nl = true) {
63 LOGD("HttpChunkReader: %s", "readln");
64 if (has_ended && open_chunk_len == 0) return 0;
65
66 int read_max = len < open_chunk_len ? len : open_chunk_len;
67 int len_processed = readlnInternal(client, str, read_max, incl_nl);
68 if (len_processed == -1) {
69 LOGD("HttpChunkReader: readln result -1");
70 return -1;
71 }
73
74 // the chunks are terminated by a final CRLF
75 if (open_chunk_len <= 0) {
76 removeCRLF(client);
77 readChunkLen(client);
78 }
79
80 return len_processed;
81 }
82
84 int available() {
85 int result = has_ended ? 0 : open_chunk_len;
86 LOGD("HttpChunkReader: available=>%d", result);
87 return result;
88 }
89
90 protected:
92 bool has_ended = false;
94
95 void removeCRLF(Client& client) {
96 LOGD("HttpChunkReader: %s", "removeCRLF");
97
98 // remove traling CR LF from data
99 if (client.peek() == '\r') {
100 LOGD("HttpChunkReader: %s", "removeCR");
101 client.read();
102 }
103 if (client.peek() == '\n') {
104 LOGD("HttpChunkReader: %s", "removeLF");
105 client.read();
106 }
107 }
108
109 // we read the chunk length which is indicated as hex value
110 bool readChunkLen(Client& client) {
111 LOGD("HttpChunkReader::readChunkLen");
113 if (readlnInternal(client, len_str, HTTP_CHUNKED_SIZE_MAX_LEN, false) < 0) {
114 LOGD("HttpChunkReader::readChunkLen readlnInternal result -1");
115 has_ended = true;
116 return false;
117 }
118 open_chunk_len = strtol((char*)len_str, nullptr, 16);
119
120 if (open_chunk_len == 0) {
121 has_ended = true;
122 LOGD("HttpChunkReader::readChunkLen %s", "last chunk received");
123 // processing of additinal final headers after the chunk end
124 if (http_header_ptr != nullptr) {
125 http_header_ptr->readExt(client);
126 }
127 }
128 return true;
129 }
130};
131
132} // namespace audio_tools
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define HTTP_CHUNKED_SIZE_MAX_LEN
Definition AudioToolsConfig.h:158
Definition Arduino.h:162
virtual int read(uint8_t *buffer, size_t len)
Definition Arduino.h:165
Http might reply with chunks. So we need to dechunk the data. see https://en.wikipedia....
Definition HttpChunkReader.h:14
int readln(Client &client, uint8_t *str, int len, bool incl_nl=true)
reads a single line from the chunks
Definition HttpChunkReader.h:61
bool has_ended
Definition HttpChunkReader.h:92
int available()
returns the number of bytes which are still available in the current chunk
Definition HttpChunkReader.h:84
void removeCRLF(Client &client)
Definition HttpChunkReader.h:95
HttpReplyHeader * http_header_ptr
Definition HttpChunkReader.h:93
bool readChunkLen(Client &client)
Definition HttpChunkReader.h:110
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:91
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)
reads up the the next CR LF - but never more then the indicated len.
Definition HttpLineReader.h:20
Reading and Writing of Http Replys.
Definition HttpHeader.h:464
void readExt(Client &in)
Definition HttpHeader.h:478
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:508