arduino-audio-tools
Loading...
Searching...
No Matches
HttpLineReader.h
Go to the documentation of this file.
1#pragma once
2
3#include "AudioLogger.h"
4
5namespace audio_tools {
6
15 public:
17 HttpLineReader() = default;
18
19 // reads up the the next CR LF - but never more then the indicated len.
20 // returns the number of characters read including crlf
21 virtual int readlnInternal(Stream& client, uint8_t* str, int len,
22 bool incl_nl = true) {
23 int result = 0;
24 LOGD("HttpLineReader %s", "readlnInternal");
25 // if we do not have any data we stop
26 if (client.peek() == -1) {
27 LOGW("HttpLineReader %s", "readlnInternal->no Data");
28 str[0] = 0;
29 return -1;
30 }
31
32 // process characters until we find a new line
33 bool is_buffer_overflow = false;
34 int j = 0;
35 while (true) {
36 int c = client.read();
37 if (c == -1) {
38 break;
39 }
40
41 if (j < len) {
42 result++;
43 } else {
44 is_buffer_overflow = true;
45 }
46
47 if (c == '\n') {
48 if (incl_nl) {
49 str[j] = c;
50 break;
51 } else {
52 // remove cr lf
53 if (j >= 1) {
54 if (str[j - 1] == '\r') {
55 // remove cr
56 str[j - 1] = 0;
57 break;
58 } else {
59 // remove nl
60 str[j] = 0;
61 break;
62 }
63 }
64 }
65 }
66 if (!is_buffer_overflow) {
67 str[j] = c;
68 }
69 j++;
70 }
71 if (result > 0) {
72 str[result - 1] = 0;
73 } else {
74 str[0] = 0;
75 }
77 LOGE("HttpLineReader %s", "readlnInternal->cut off too long line");
78 }
79
80 return result;
81 }
82};
83
84} // namespace audio_tools
#define LOGW(...)
Definition AudioLoggerIDF.h:29
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define LOGE(...)
Definition AudioLoggerIDF.h:30
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
HttpLineReader()=default
Default constructor.
virtual int readlnInternal(Stream &client, uint8_t *str, int len, bool incl_nl=true)
Definition HttpLineReader.h:21
Definition NoArduino.h:142
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