From 68f47dae17aca1a674e844c0dcee3b3b11ebe6a1 Mon Sep 17 00:00:00 2001 From: Asbelos Date: Sun, 28 Jun 2020 12:45:31 +0100 Subject: [PATCH] HTTPParser framework Beware needs uplifted wifi buffer --- HTTPParser.cpp | 11 +++++++++++ HTTPParser.h | 8 ++++++++ StringFormatter.cpp | 6 +++--- StringFormatter.h | 2 +- WifiInterface.cpp | 24 ++++++++++++++++++------ WifiInterface.h | 2 +- 6 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 HTTPParser.cpp create mode 100644 HTTPParser.h diff --git a/HTTPParser.cpp b/HTTPParser.cpp new file mode 100644 index 0000000..27208f0 --- /dev/null +++ b/HTTPParser.cpp @@ -0,0 +1,11 @@ +#include "HTTPParser.h" +#include "StringFormatter.h" + +void HTTPParser::parse(Print & stream, char * cmd) { + + // BEWARE - As soon as you start responding, the cmd buffer is trashed! + // You must get everything you need from it before using StringFormatter::send! + + StringFormatter::send(stream,F("HTTP/1.1 200 OK\nContent-Type: text/html\nConnnection: close\n\n")); + StringFormatter::send(stream,F("This is not a web server.
")); +} diff --git a/HTTPParser.h b/HTTPParser.h new file mode 100644 index 0000000..39a5083 --- /dev/null +++ b/HTTPParser.h @@ -0,0 +1,8 @@ +#ifndef HTTPParser_h +#define HTTPParser_h +#include +class HTTPParser { + public: + static void parse(Print & stream, char * cmd); +}; +#endif diff --git a/StringFormatter.cpp b/StringFormatter.cpp index 5c64027..cab55b5 100644 --- a/StringFormatter.cpp +++ b/StringFormatter.cpp @@ -5,16 +5,16 @@ void StringFormatter::print( const __FlashStringHelper* input...) { va_list args; va_start(args, input); - send(Serial,input,args); + send2(Serial,input,args); } void StringFormatter::send(Print & stream, const __FlashStringHelper* input...) { va_list args; va_start(args, input); - send(stream,input,args); + send2(stream,input,args); } -void StringFormatter::send(Print & stream,const __FlashStringHelper* format, va_list args) { +void StringFormatter::send2(Print & stream,const __FlashStringHelper* format, va_list args) { // thanks to Jan TuroĊˆ https://arduino.stackexchange.com/questions/56517/formatting-strings-in-arduino-for-output diff --git a/StringFormatter.h b/StringFormatter.h index e3b9139..76dd605 100644 --- a/StringFormatter.h +++ b/StringFormatter.h @@ -8,7 +8,7 @@ class StringFormatter static void print( const __FlashStringHelper* input...); static void send(Print & serial, const __FlashStringHelper* input...); private: - static void send(Print & serial, const __FlashStringHelper* input,va_list args); + static void send2(Print & serial, const __FlashStringHelper* input,va_list args); }; diff --git a/WifiInterface.cpp b/WifiInterface.cpp index 59bd6d4..713a0fc 100644 --- a/WifiInterface.cpp +++ b/WifiInterface.cpp @@ -3,11 +3,12 @@ #include "DIAG.h" #include "StringFormatter.h" #include "WiThrottle.h" - +#include "HTTPParser.h" const char PROGMEM READY_SEARCH[] ="\r\nready\r\n"; const char PROGMEM OK_SEARCH[] ="\r\nOK\r\n"; const char PROGMEM END_DETAIL_SEARCH[] ="@ 1000"; const char PROGMEM PROMPT_SEARCH[] =">"; +const char PROGMEM SEND_OK_SEARCH[] ="\r\nSEND OK\r\n"; bool WifiInterface::connected=false; DCCEXParser WifiInterface::parser; @@ -126,17 +127,28 @@ void WifiInterface::loop(Stream & wifiStream) { // Otherwise we would have to copy the buffer elsewhere and RAM is in short supply. // TODO ... tell JMRI parser that callbacks are diallowed because we dont want to handle the async - - if (buffer[0]=='<') parser.parse(streamer,buffer); + bool closeAfter=false; + // Intercept HTTP requests + if (strstr(buffer," HTTP/1.1")) { + HTTPParser::parse(streamer,buffer); + closeAfter=true; + } + else if (buffer[0]=='<') parser.parse(streamer,buffer); else WiThrottle::getThrottle(streamer, connectionId)->parse(streamer, buffer); if (streamer.available()) { // there is a reply to send - DIAG(F("WiFiInterface Responding (%d) %s\n"),connectionId,buffer); - - StringFormatter::send(wifiStream,F("AT+CIPSEND=%d,%d\r\n"),connectionId,streamer.available()); streamer.write('\0'); + DIAG(F("WiFiInterface Responding client (%d) l(%d) %s\n"),connectionId,streamer.available()-1,buffer); + + StringFormatter::send(wifiStream,F("AT+CIPSEND=%d,%d\r\n"),connectionId,streamer.available()-1); if (checkForOK(wifiStream,1000,PROMPT_SEARCH,true)) wifiStream.print((char *) buffer); + checkForOK(wifiStream,3000,SEND_OK_SEARCH,true); } + if (closeAfter) { + StringFormatter::send(wifiStream,F("AT+CIPCLOSE=%d\r\n"),connectionId); + checkForOK(wifiStream,2000,OK_SEARCH,true); + } + loopstate=0; // go back to looking for +IPD } diff --git a/WifiInterface.h b/WifiInterface.h index 80c2e8a..aae7010 100644 --- a/WifiInterface.h +++ b/WifiInterface.h @@ -21,7 +21,7 @@ class WifiInterface { static byte loopstate; static int datalength; static int connectionId; - static const byte MAX_WIFI_BUFFER=64; + static const byte MAX_WIFI_BUFFER=250; static byte buffer[MAX_WIFI_BUFFER]; static MemStream streamer; };