arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
HttpChunkReader.h
1#pragma once
2
3#include "HttpHeader.h"
4#include "HttpLineReader.h"
5
6namespace audio_tools {
7
15 public:
18 open_chunk_len = 0;
19 has_ended = false;
20 }
21
24 http_heaer_ptr = &header;
25 open_chunk_len = 0;
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
44 open_chunk_len -= len_processed;
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);
63 open_chunk_len -= len_processed;
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:
84 int open_chunk_len;
85 bool has_ended = false;
86 HttpReplyHeader *http_heaer_ptr;
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");
104 uint8_t len_str[HTTP_CHUNKED_SIZE_MAX_LEN+1] = {0};
105 readlnInternal(client, len_str, HTTP_CHUNKED_SIZE_MAX_LEN, false);
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
Definition NoArduino.h:167
Http might reply with chunks. So we need to dechunk the data. see https://en.wikipedia....
Definition HttpChunkReader.h:14
HttpChunkReader(HttpReplyHeader &header)
constructor for processing final header information
Definition HttpChunkReader.h:23
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
Reading and Writing of Http Replys.
Definition HttpHeader.h:444
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioConfig.h:885