-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathHttpResource.cpp
More file actions
97 lines (76 loc) · 2.27 KB
/
Copy pathHttpResource.cpp
File metadata and controls
97 lines (76 loc) · 2.27 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
//
//! \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;
}
// not a perfect solution, may return incorrect argument if one is a substring (at the start) of another
String ArduinoHttpServer::HttpResource::getArgument(const char *key) const {
int queryStart = m_resource.indexOf('?');
if (queryStart == -1) {
return "";
}
queryStart++;
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();
}
String value = m_resource.substring(keyStart + keyStr.length(), valueEnd);
return value;
}
//! 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;
}