Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

basic GET arguments support (not perfect, parsing could be better) and example sketch #23

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions examples/HTTPEscapeSequence/HTTPEscapeSequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

#include <ArduinoHttpServer.h>


void setup() {
// just check to see if escaped characters are parsed correctly
Serial.begin(115200);
delay(100);
Serial.println("TEST - simple");
Serial.print("'index.php?key=value' check for key: ");
Serial.println(ArduinoHttpServer::HttpResource("index.php?key=value").getArgument("key"));
// + must get changed to space
Serial.println("TEST - with space");
Serial.print("'action_page.php?fname=with+space&lname=' check for fname: ");
Serial.println(ArduinoHttpServer::HttpResource("action_page.php?fname=with+space&lname=").getArgument("fname"));
Serial.println("TEST - with special char");
Serial.print("'action_page.php?fname=email%40server.com&lname=' check for fname: ");
Serial.println(ArduinoHttpServer::HttpResource("action_page.php?fname=email%40server.com&lname=").getArgument("fname"));
}

void loop() {
// put your main code here, to run repeatedly:

}
94 changes: 94 additions & 0 deletions examples/HelloGetArgument/HelloGetArgument.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@

#include <ArduinoHttpServer.h>

#include <Arduino.h>


#ifdef ESP8266 // This example is compatible with both, ATMega and ESP8266
#include <ESP8266WiFi.h>
#else
#include <SPI.h> //! \todo Temporary see fix: https://github.com/platformio/platformio/issues/48
// on older Arduinos use WiFi.h, on Arduino Uno R4 WiFi use WiFiS3.h
#include <WiFiS3.h>
#endif

const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASS";

WiFiServer wifiServer(80);

void setup()
{
Serial.begin(115200);
Serial.println("Starting Wifi Connection...");

WiFi.begin(const_cast<char*>(ssid), password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
Serial.println("Connected!");

wifiServer.begin();
}

void loop()
{
WiFiClient client( wifiServer.available() );
if (client.connected())
{
Serial.println("client connected!");
// Connected to client. Allocate and initialize StreamHttpRequest object.
ArduinoHttpServer::StreamHttpRequest<1024> httpRequest(client);

if (httpRequest.readRequest())
{
Serial.println("Done read request!");

// Retrieve HTTP resource / URL requested
Serial.println( httpRequest.getResource().toString() );

// Retrieve 1st part of HTTP resource.
// E.g.: "api" from "/api/sensors/on"
Serial.println(httpRequest.getResource()[0]);

ArduinoHttpServer::StreamHttpReply httpReply(client, "text/html");
String textField = httpRequest.getResource().getArgument("textfield");
Serial.print("Textfield is: ");
Serial.println(textField);

String reply = "<!DOCTYPE html>\n"
"<html>\n"
"<head>\n"
" <title>Minimal HTML Page</title>\n"
"</head>\n"
"<body>\n";
if(textField.length() > 0) {
reply += "<h3> Hello, ";
reply += textField;
reply += "!</h3>";
}
reply += " <form>\n"
" <label for=\"textfield\">Enter text:</label><br>\n"
" <input type=\"text\" id=\"textfield\" name=\"textfield\"><br><br>\n"
" <input type=\"submit\" value=\"Submit\">\n"
" </form>\n"
"</body>\n"
"</html>";
httpReply.send(reply);
}
else
{
// HTTP parsing failed. Client did not provide correct HTTP data or
// client requested an unsupported feature.
ArduinoHttpServer::StreamHttpErrorReply httpReply(client, httpRequest.getContentType());

const char *pErrorStr( httpRequest.getError().cStr() );
String errorStr(pErrorStr); //! \todo Make HttpReply FixString compatible.

httpReply.send( errorStr );
}
client.stop();
}

}
58 changes: 58 additions & 0 deletions src/internals/HttpResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,65 @@ 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".
Expand Down
1 change: 1 addition & 0 deletions src/internals/HttpResource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class HttpResource

bool isValid();
String operator[](const unsigned int index) const;
String getArgument(const char *key) const;
const String& toString() const;

private:
Expand Down