Arduino DLNA Server
HttpChunkReader.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "HttpHeader.h"
4 #include "HttpLineReader.h"
5 #include "basic/Logger.h"
6 
7 namespace tiny_dlna {
8 
14  public:
17  open_chunk_len = 0;
18  has_ended = false;
19  }
20 
23  http_heaer_ptr = &header;
24  open_chunk_len = 0;
25  has_ended = false;
26  }
27 
28  void open(Client &client) {
29  DlnaLogger.log(DlnaDebug, "HttpChunkReader %s", "open");
30  has_ended = false;
31  readChunkLen(client);
32  }
33 
34  // reads a block of data from the chunks
35  virtual int read(Client &client, uint8_t *str, int len) {
36  DlnaLogger.log(DlnaDebug, "HttpChunkReader %s", "read");
37  if (has_ended && open_chunk_len == 0) return 0;
38 
39  // read the chunk data - but not more then available
40  int read_max = len < open_chunk_len ? len : open_chunk_len;
41  int len_processed = client.read(str, read_max);
42  // update current unprocessed chunk
43  open_chunk_len -= len_processed;
44 
45  // remove traling CR LF from data
46  if (open_chunk_len <= 0) {
47  removeCRLF(client);
48  readChunkLen(client);
49  }
50 
51  return len_processed;
52  }
53 
54  // reads a single line from the chunks
55  virtual int readln(Client &client, uint8_t *str, int len,
56  bool incl_nl = true) {
57  DlnaLogger.log(DlnaDebug, "HttpChunkReader %s", "readln");
58  if (has_ended && open_chunk_len == 0) return 0;
59 
60  int read_max = len < open_chunk_len ? len : open_chunk_len;
61  int len_processed = readlnInternal(client, str, read_max, incl_nl);
62  open_chunk_len -= len_processed;
63 
64  // the chunks are terminated by a final CRLF
65  if (open_chunk_len <= 0) {
66  removeCRLF(client);
67  readChunkLen(client);
68  }
69 
70  return len_processed;
71  }
72 
73  int available() {
74  int result = has_ended ? 0 : open_chunk_len;
75  DlnaLogger.log(DlnaDebug, "HttpChunkReader available=>%d", result);
76 
77  return result;
78  }
79 
80  protected:
82  bool has_ended = false;
84 
85  void removeCRLF(Client &client) {
86  DlnaLogger.log(DlnaDebug, "HttpChunkReader %s", "removeCRLF");
87  // remove traling CR LF from data
88  if (client.peek() == '\r') {
89  DlnaLogger.log(DlnaDebug, "HttpChunkReader %s", "removeCR");
90  client.read();
91  }
92  if (client.peek() == '\n') {
93  DlnaLogger.log(DlnaDebug, "HttpChunkReader %s", "removeLF");
94  client.read();
95  }
96  }
97 
98  // we read the chunk length which is indicated as hex value
99  virtual void readChunkLen(Client &client) {
100  DlnaLogger.log(DlnaDebug, "HttpChunkReader::readChunkLen");
101  uint8_t len_str[51];
102  readlnInternal(client, len_str, 50, false);
103  DlnaLogger.log(DlnaDebug, "HttpChunkReader::readChunkLen %s",
104  (const char *)len_str);
105  DlnaLogger.log(DlnaDebug, "\n");
106  open_chunk_len = strtol((char *)len_str, nullptr, 16);
107 
108  char msg[40];
109  sprintf(msg, "chunk_len: %d", open_chunk_len);
110  DlnaLogger.log(DlnaDebug, "HttpChunkReader::readChunkLen->%s", msg);
111 
112  if (open_chunk_len == 0) {
113  has_ended = true;
114  DlnaLogger.log(DlnaDebug, "HttpChunkReader::readChunkLen %s",
115  "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 tiny_dlna
Http might reply with chunks. So we need to dechunk the data. see https://en.wikipedia....
Definition: HttpChunkReader.h:13
int open_chunk_len
Definition: HttpChunkReader.h:81
HttpChunkReader()
default constructor
Definition: HttpChunkReader.h:16
void removeCRLF(Client &client)
Definition: HttpChunkReader.h:85
void open(Client &client)
Definition: HttpChunkReader.h:28
HttpChunkReader(HttpReplyHeader &header)
constructor for processing final header information
Definition: HttpChunkReader.h:22
virtual int read(Client &client, uint8_t *str, int len)
Definition: HttpChunkReader.h:35
bool has_ended
Definition: HttpChunkReader.h:82
HttpReplyHeader * http_heaer_ptr
Definition: HttpChunkReader.h:83
virtual int readln(Client &client, uint8_t *str, int len, bool incl_nl=true)
Definition: HttpChunkReader.h:55
int available()
Definition: HttpChunkReader.h:73
virtual void readChunkLen(Client &client)
Definition: HttpChunkReader.h:99
We read a single line. A terminating 0 is added to the string to make it compliant for c string funct...
Definition: HttpLineReader.h:13
virtual int readlnInternal(Stream &client, uint8_t *str, int len, bool incl_nl=true)
Definition: HttpLineReader.h:18
Reading and Writing of Http Replys.
Definition: HttpHeader.h:411
void readExt(Client &in)
Definition: HttpHeader.h:425
void log(DlnaLogLevel current_level, const char *fmt...)
Print log message.
Definition: Logger.h:40
Definition: Allocator.h:6
@ DlnaDebug
Definition: Logger.h:16
LoggerClass DlnaLogger
Definition: Logger.cpp:5