|
5 | 5 | namespace audio_tools {
|
6 | 6 |
|
7 | 7 | /**
|
8 |
| - * @brief We read a single line. A terminating 0 is added to the string to make it |
9 |
| - * compliant for c string functions. |
10 |
| - * @author Phil Schatzmann |
11 |
| - * @copyright GPLv3 |
12 |
| - */ |
| 8 | +* @brief We read a single line. A terminating 0 is added to the string to make it |
| 9 | +* compliant for c string functions. |
| 10 | +* @author Phil Schatzmann |
| 11 | +* @copyright GPLv3 |
| 12 | +*/ |
13 | 13 |
|
14 | 14 | class HttpLineReader {
|
15 |
| - public: |
16 |
| - HttpLineReader(){} |
17 |
| - // reads up the the next CR LF - but never more then the indicated len. returns the number of characters read including crlf |
18 |
| - virtual int readlnInternal(Stream &client, uint8_t* str, int len, bool incl_nl=true){ |
19 |
| - int result = 0; |
20 |
| - LOGD( "HttpLineReader %s","readlnInternal"); |
21 |
| - // wait for first character |
22 |
| - for (int w=0;w<20 && client.available()==0; w++){ |
23 |
| - delay(100); |
24 |
| - } |
25 |
| - // if we do not have any data we stop |
26 |
| - if (client.available()==0) { |
27 |
| - LOGW( "HttpLineReader %s","readlnInternal->no Data"); |
28 |
| - str[0]=0; |
29 |
| - return 0; |
30 |
| - } |
| 15 | + public: |
| 16 | + HttpLineReader(){} |
| 17 | + // reads up the the next CR LF - but never more then the indicated len. returns the number of characters read including crlf |
| 18 | + virtual int readlnInternal(Stream &client, uint8_t* str, int len, bool incl_nl=true){ |
| 19 | + int result = 0; |
| 20 | + LOGD( "HttpLineReader %s","readlnInternal"); |
| 21 | + // wait for first character |
| 22 | + for (int w=0;w<20 && client.available()==0; w++){ |
| 23 | + delay(100); |
| 24 | + } |
| 25 | + // if we do not have any data we stop |
| 26 | + if (client.available()==0) { |
| 27 | + LOGW( "HttpLineReader %s","readlnInternal->no Data"); |
| 28 | + str[0]=0; |
| 29 | + return 0; |
| 30 | + } |
31 | 31 |
|
32 |
| - // process characters |
33 |
| - bool is_buffer_owerflow = false; |
34 |
| - for (int j=0;j<len;j++){ |
35 |
| - int c = client.read(); |
36 |
| - if (c==-1){ |
37 |
| - break; |
38 |
| - } |
| 32 | + // process characters until we find a new line |
| 33 | + bool is_buffer_owerflow = false; |
| 34 | + int j=0; |
| 35 | + while (true){ |
| 36 | + int c = client.read(); |
| 37 | + if (c==-1){ |
| 38 | + break; |
| 39 | + } |
39 | 40 |
|
40 |
| - if (j<len){ |
41 |
| - result++; |
42 |
| - } else { |
43 |
| - is_buffer_owerflow = true; |
44 |
| - } |
| 41 | + if (j<len){ |
| 42 | + result++; |
| 43 | + } else { |
| 44 | + is_buffer_owerflow = true; |
| 45 | + } |
45 | 46 |
|
46 |
| - if (c=='\n'){ |
47 |
| - if (incl_nl){ |
48 |
| - str[j]=c; |
49 |
| - break; |
50 |
| - } else { |
51 |
| - // remove cl lf |
52 |
| - if (j>=1){ |
53 |
| - if (str[j-1]=='\r'){ |
54 |
| - // remove cr |
55 |
| - str[j-1]=0; |
56 |
| - break;; |
57 |
| - } else { |
58 |
| - // remove nl |
59 |
| - str[j]=0; |
60 |
| - break; |
61 |
| - } |
62 |
| - } |
63 |
| - } |
64 |
| - } |
65 |
| - if (!is_buffer_owerflow){ |
66 |
| - str[j] = c; |
67 |
| - } |
68 |
| - } |
69 |
| - str[result]=0; |
70 |
| - if (is_buffer_owerflow){ |
71 |
| - LOGE("Line cut off"); |
72 |
| - } |
| 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_owerflow){ |
| 67 | + str[j] = c; |
| 68 | + } |
| 69 | + j++; |
| 70 | + } |
| 71 | + str[result-1]=0; |
| 72 | + if (is_buffer_owerflow){ |
| 73 | + LOGE("Line cut off: %s", str); |
| 74 | + } |
73 | 75 |
|
74 |
| - return result; |
75 |
| - } |
| 76 | + return result; |
| 77 | + } |
76 | 78 | };
|
77 | 79 |
|
78 | 80 | }
|
0 commit comments