mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-11-23 08:06:13 +01:00
HTTPParser framework
Beware needs uplifted wifi buffer
This commit is contained in:
parent
c97d07608b
commit
68f47dae17
11
HTTPParser.cpp
Normal file
11
HTTPParser.cpp
Normal file
|
@ -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("<html><body>This is <b>not</b> a web server.<br/></body></html>"));
|
||||||
|
}
|
8
HTTPParser.h
Normal file
8
HTTPParser.h
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
#ifndef HTTPParser_h
|
||||||
|
#define HTTPParser_h
|
||||||
|
#include <Arduino.h>
|
||||||
|
class HTTPParser {
|
||||||
|
public:
|
||||||
|
static void parse(Print & stream, char * cmd);
|
||||||
|
};
|
||||||
|
#endif
|
|
@ -5,16 +5,16 @@
|
||||||
void StringFormatter::print( const __FlashStringHelper* input...) {
|
void StringFormatter::print( const __FlashStringHelper* input...) {
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, input);
|
va_start(args, input);
|
||||||
send(Serial,input,args);
|
send2(Serial,input,args);
|
||||||
}
|
}
|
||||||
|
|
||||||
void StringFormatter::send(Print & stream, const __FlashStringHelper* input...) {
|
void StringFormatter::send(Print & stream, const __FlashStringHelper* input...) {
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, input);
|
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
|
// thanks to Jan Turoň https://arduino.stackexchange.com/questions/56517/formatting-strings-in-arduino-for-output
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ class StringFormatter
|
||||||
static void print( const __FlashStringHelper* input...);
|
static void print( const __FlashStringHelper* input...);
|
||||||
static void send(Print & serial, const __FlashStringHelper* input...);
|
static void send(Print & serial, const __FlashStringHelper* input...);
|
||||||
private:
|
private:
|
||||||
static void send(Print & serial, const __FlashStringHelper* input,va_list args);
|
static void send2(Print & serial, const __FlashStringHelper* input,va_list args);
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -3,11 +3,12 @@
|
||||||
#include "DIAG.h"
|
#include "DIAG.h"
|
||||||
#include "StringFormatter.h"
|
#include "StringFormatter.h"
|
||||||
#include "WiThrottle.h"
|
#include "WiThrottle.h"
|
||||||
|
#include "HTTPParser.h"
|
||||||
const char PROGMEM READY_SEARCH[] ="\r\nready\r\n";
|
const char PROGMEM READY_SEARCH[] ="\r\nready\r\n";
|
||||||
const char PROGMEM OK_SEARCH[] ="\r\nOK\r\n";
|
const char PROGMEM OK_SEARCH[] ="\r\nOK\r\n";
|
||||||
const char PROGMEM END_DETAIL_SEARCH[] ="@ 1000";
|
const char PROGMEM END_DETAIL_SEARCH[] ="@ 1000";
|
||||||
const char PROGMEM PROMPT_SEARCH[] =">";
|
const char PROGMEM PROMPT_SEARCH[] =">";
|
||||||
|
const char PROGMEM SEND_OK_SEARCH[] ="\r\nSEND OK\r\n";
|
||||||
|
|
||||||
bool WifiInterface::connected=false;
|
bool WifiInterface::connected=false;
|
||||||
DCCEXParser WifiInterface::parser;
|
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.
|
// 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
|
// TODO ... tell JMRI parser that callbacks are diallowed because we dont want to handle the async
|
||||||
|
bool closeAfter=false;
|
||||||
if (buffer[0]=='<') parser.parse(streamer,buffer);
|
// 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);
|
else WiThrottle::getThrottle(streamer, connectionId)->parse(streamer, buffer);
|
||||||
|
|
||||||
|
|
||||||
if (streamer.available()) { // there is a reply to send
|
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');
|
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);
|
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
|
loopstate=0; // go back to looking for +IPD
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ class WifiInterface {
|
||||||
static byte loopstate;
|
static byte loopstate;
|
||||||
static int datalength;
|
static int datalength;
|
||||||
static int connectionId;
|
static int connectionId;
|
||||||
static const byte MAX_WIFI_BUFFER=64;
|
static const byte MAX_WIFI_BUFFER=250;
|
||||||
static byte buffer[MAX_WIFI_BUFFER];
|
static byte buffer[MAX_WIFI_BUFFER];
|
||||||
static MemStream streamer;
|
static MemStream streamer;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user