Arduino DLNA Server
HttpParameters.h
Go to the documentation of this file.
1 #pragma once
2 #include <ctype.h>
3 #include <stdlib.h>
4 #include <string.h>
5 
6 #include "Stream.h"
7 #include "basic/Logger.h"
8 #include "basic/Str.h"
9 #include "basic/Vector.h"
10 
11 namespace tiny_dlna {
12 
18  struct HttpParameterEntry {
19  HttpParameterEntry() = default;
20  Str key = nullptr;
21  Str value = nullptr;
22  void clear() {
23  key.clear();
24  value.clear();
25  }
26  };
27 
28  public:
30  HttpParameters(const int maxLen = 256) { max_len = maxLen; };
31 
34 
36  void parse(Stream &in) {
37  char buffer[max_len];
38  while (in.available() > 0) {
39  memset(buffer, 0, max_len);
40  in.readBytesUntil('&', buffer, max_len);
41  StrView str(buffer);
42  DlnaLogger.log(DlnaInfo, "parameter: %s", buffer);
43  urldecode2(buffer, buffer);
44  DlnaLogger.log(DlnaInfo, "parameter decoded: %s", buffer);
45  int pos = str.indexOf("=");
46  if (pos > 0) {
47  buffer[pos] = 0; // delimit key
48  const char *key = buffer;
49  const char *value = buffer + pos + 1;
50  DlnaLogger.log(DlnaDebug, "key: %s", key);
51  DlnaLogger.log(DlnaDebug, "value: %s", value);
52  HttpParameterEntry *entry = getParameter(key);
53  if (entry != nullptr) {
54  entry->value = value;
55  } else {
56  entry = new HttpParameterEntry();
57  entry->key = key;
58  entry->value = value;
59  parameters.push_back(entry);
60  }
61  }
62  }
63  }
64 
67  void parse(Stream &in, void (*callback)(const char *key, const char *value)) {
68  char buffer[max_len];
69  while (in.available() > 0) {
70  memset(buffer, 0, max_len);
71  in.readBytesUntil('&', buffer, max_len);
72  StrView str(buffer);
73  DlnaLogger.log(DlnaInfo, "parameter: %s", buffer);
74  urldecode2(buffer, buffer);
75  DlnaLogger.log(DlnaInfo, "parameter decoded: %s", buffer);
76  int pos = str.indexOf("=");
77  if (pos > 0) {
78  buffer[pos] = 0; // delimit key
79  const char *key = buffer;
80  const char *value = buffer + pos + 1;
81  callback(key, value);
82  }
83  }
84  }
85 
87  bool hasKey(const char *key) {
88  for (auto &entry : parameters) {
89  if (entry->key.equals(key)) {
90  return true;
91  }
92  }
93  return false;
94  }
95 
97  HttpParameterEntry *getParameter(const char *key) {
98  for (auto &entry : parameters) {
99  if (entry->key.equals(key)) {
100  return entry;
101  }
102  }
103  return nullptr;
104  }
105 
107  const char *getValue(const char *key) {
108  for (auto &entry : parameters) {
109  if (entry->key.equals(key)) {
110  return entry->value.c_str();
111  }
112  }
113  return nullptr;
114  }
115 
117  float getFloat(const char *key) {
118  for (auto &entry : parameters) {
119  if (entry->key.equals(key)) {
120  return entry->value.toFloat();
121  }
122  }
123  return 0;
124  }
125 
127  int getInt(const char *key) {
128  for (auto &entry : parameters) {
129  if (entry->key.equals(key)) {
130  return entry->value.toInt();
131  }
132  }
133  return 0;
134  }
135 
137  void clear() {
138  for (auto entry : parameters) {
139  delete entry;
140  }
141  parameters.clear();
142  }
143 
144  protected:
146  int max_len;
147 
148  void urldecode2(char *dst, const char *src) {
149  char a, b;
150  while (*src) {
151  if ((*src == '%') && ((a = src[1]) && (b = src[2])) &&
152  (isxdigit(a) && isxdigit(b))) {
153  if (a >= 'a') a -= 'a' - 'A';
154  if (a >= 'A')
155  a -= ('A' - 10);
156  else
157  a -= '0';
158  if (b >= 'a') b -= 'a' - 'A';
159  if (b >= 'A')
160  b -= ('A' - 10);
161  else
162  b -= '0';
163  *dst++ = 16 * a + b;
164  src += 3;
165  } else if (*src == '+') {
166  *dst++ = ' ';
167  src++;
168  } else {
169  *dst++ = *src++;
170  }
171  }
172  *dst++ = '\0';
173  }
174 };
175 
176 } // namespace tiny_dlna
API for http parameters: key=value&key1=value1.
Definition: HttpParameters.h:17
void parse(Stream &in, void(*callback)(const char *key, const char *value))
Definition: HttpParameters.h:67
~HttpParameters()
Destructor.
Definition: HttpParameters.h:33
HttpParameters(const int maxLen=256)
Default Constructor.
Definition: HttpParameters.h:30
bool hasKey(const char *key)
Checks if the parameter exists.
Definition: HttpParameters.h:87
void urldecode2(char *dst, const char *src)
Definition: HttpParameters.h:148
void clear()
Clears all values.
Definition: HttpParameters.h:137
int max_len
Definition: HttpParameters.h:146
int getInt(const char *key)
Returns the value for a parameter id as int.
Definition: HttpParameters.h:127
HttpParameterEntry * getParameter(const char *key)
Returns a HttpParameterEntry for a parameter id.
Definition: HttpParameters.h:97
Vector< HttpParameterEntry * > parameters
Definition: HttpParameters.h:145
void parse(Stream &in)
Parses the parameters in the client stream.
Definition: HttpParameters.h:36
float getFloat(const char *key)
Returns the value for a parameter id as float.
Definition: HttpParameters.h:117
const char * getValue(const char *key)
Returns the value for a parameter id as string.
Definition: HttpParameters.h:107
void log(DlnaLogLevel current_level, const char *fmt...)
Print log message.
Definition: Logger.h:40
A simple wrapper to provide string functions on char*. If the underlying char* is a const we do not a...
Definition: StrView.h:25
virtual int indexOf(const char c, int start=0)
Definition: StrView.h:267
String implementation which keeps the data on the heap. We grow the allocated memory only if the copy...
Definition: Str.h:22
void clear() override
clears the string by setting the terminating 0 at the beginning
Definition: Str.h:164
void clear()
Definition: Vector.h:161
void push_back(T &&value)
Definition: Vector.h:167
Definition: Allocator.h:6
@ DlnaDebug
Definition: Logger.h:16
@ DlnaInfo
Definition: Logger.h:16
LoggerClass DlnaLogger
Definition: Logger.cpp:5