Arduino DLNA Server
HttpLineReader.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "basic/Logger.h"
4 
5 namespace tiny_dlna {
6 
14  public:
16  // reads up the the next CR LF - but never more then the indicated len.
17  // returns the number of characters read including crlf
18  virtual int readlnInternal(Stream& client, uint8_t* str, int len,
19  bool incl_nl = true) {
20  int result = 0;
21  DlnaLogger.log(DlnaDebug, "HttpLineReader", "readlnInternal");
22  int count = 0;
23  // wait for first character
24  for (int w = 0; w < 20 && client.available() == 0; w++) {
25  delay(10);
26  if (count++ > 5) break;
27  }
28  // if we do not have any data we stop
29  if (client.available() == 0) {
30  DlnaLogger.log(DlnaWarning, "HttpLineReader", "readlnInternal->no Data");
31  str[0] = 0;
32  return 0;
33  }
34 
35  // process characters until we find a new line
36  bool is_buffer_owerflow = false;
37  int j = 0;
38  while (true) {
39  int c = client.read();
40  if (c == -1) {
41  break;
42  }
43 
44  if (j < len) {
45  result++;
46  } else {
47  is_buffer_owerflow = true;
48  }
49  if (c == '\n') {
50  if (incl_nl) {
51  str[j] = c;
52  break;
53  } else {
54  // remove cl lf
55  if (j >= 1) {
56  if (str[j - 1] == '\r') {
57  // remove cr
58  str[j - 1] = 0;
59  break;
60  ;
61  } else {
62  // remove nl
63  str[j] = 0;
64  break;
65  }
66  }
67  }
68  }
69  if (!is_buffer_owerflow) {
70  str[j] = c;
71  }
72  j++;
73  }
74  str[result] = 0;
75  str[result - 1] = 0;
76  if (is_buffer_owerflow) {
77  DlnaLogger.log(DlnaError, "Line cut off:", (const char*)str);
78  }
79  return result;
80  }
81 };
82 
83 } // namespace tiny_dlna
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
HttpLineReader()
Definition: HttpLineReader.h:15
virtual int readlnInternal(Stream &client, uint8_t *str, int len, bool incl_nl=true)
Definition: HttpLineReader.h:18
void log(DlnaLogLevel current_level, const char *fmt...)
Print log message.
Definition: Logger.h:40
Definition: Allocator.h:6
@ DlnaDebug
Definition: Logger.h:16
@ DlnaWarning
Definition: Logger.h:16
@ DlnaError
Definition: Logger.h:16
LoggerClass DlnaLogger
Definition: Logger.cpp:5