-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathHttpResource.cpp
More file actions
131 lines (112 loc) · 3.08 KB
/
HttpResource.cpp
File metadata and controls
131 lines (112 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
//! \file
// ArduinoHttpServer
//
// Created by Sander van Woensel on 23-01-16.
// Copyright (c) 2016 Sander van Woensel. All rights reserved.
//
//! The URL/path provided after the HTTP method.
#include "HttpResource.hpp"
#include <WString.h>
ArduinoHttpServer::HttpResource::HttpResource(const String& resource) :
m_resource(resource)
{
}
ArduinoHttpServer::HttpResource::HttpResource() :
m_resource()
{
}
// TODO: default?
ArduinoHttpServer::HttpResource& ArduinoHttpServer::HttpResource::operator=(const ArduinoHttpServer::HttpResource& other)
{
m_resource = other.m_resource;
return *this;
}
bool ArduinoHttpServer::HttpResource::isValid()
{
return m_resource.length() > 0;
}
//! Retrieve the GET argument value for given string key
//! \details E.g. HttpResource("/index.php&key=value").getArgument("key")
//! returns "value".
//! \returns Empty string when index specified is out of range.
String ArduinoHttpServer::HttpResource::getArgument(const char *key) const {
int queryStart = m_resource.indexOf('?');
if (queryStart == -1) {
return "";
}
queryStart++;
// FIXME: adding = will ensure that = is directly after key name,
// but we still may get incorrect values for strings like "akey" vs "key"...
String keyStr = String(key) + '=';
int keyStart = m_resource.indexOf(keyStr, queryStart);
if (keyStart == -1) {
return "";
}
int valueEnd = m_resource.indexOf('&', keyStart);
if (valueEnd == -1) {
valueEnd = m_resource.length();
}
#if 0
String ret = m_resource.substring(keyStart + keyStr.length(), valueEnd);
#else
int a, b;
String ret = "";
for(int i = keyStart + keyStr.length(); i < valueEnd; i++) {
if (i < valueEnd-3 && (m_resource[i] == '%') &&
((a = m_resource[i+1]) && (b = m_resource[i+2])) &&
(isxdigit(a) && isxdigit(b))) {
if (a >= 'a')
a -= 'a' - 'A';
if (a >= 'A')
a -= ('A' - 10);
else
a -= '0';
if (b >= 'a')
b -= 'a' - 'A';
if (b >= 'A')
b -= ('A' - 10);
else
b -= '0';
ret += (char)(16 * a + b);
i += 2;
}
else if (m_resource[i] == '+') {
ret += ' ';
}
else {
ret += m_resource[i];
}
}
#endif
return ret;
}
//! Retrieve resource part at the specified index.
//! \details E.g. HttpResource("/api/sensors/1/state")[1]
//! returns "sensors".
//! \returns Empty string when index specified is out of range.
String ArduinoHttpServer::HttpResource::operator[](const unsigned int index) const
{
int fromOffset(0);
// Forward till we reach desired index.
for (unsigned int currentIndex=0; currentIndex <= index; ++currentIndex)
{
fromOffset = m_resource.indexOf(RESOURCE_SEPERATOR, fromOffset);
if(fromOffset == -1)
{
return String("");
}
++fromOffset; // Seek past '/'.
}
// Find next possible '/' or end.
int toOffset( m_resource.indexOf(RESOURCE_SEPERATOR, fromOffset) );
if(toOffset == -1)
{
toOffset = m_resource.length();
}
return m_resource.substring(fromOffset, toOffset);
}
const String& ArduinoHttpServer::HttpResource::toString() const
{
return m_resource;
}