From 07767e0cd7183e55aa3c7c8b9ebf72ff63040fa8 Mon Sep 17 00:00:00 2001 From: Bart Friederichs Date: Tue, 15 Nov 2022 16:15:15 +0100 Subject: [PATCH 1/4] Add addHeader --- src/internals/StreamHttpReply.cpp | 5 ++++- src/internals/StreamHttpReply.hpp | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/internals/StreamHttpReply.cpp b/src/internals/StreamHttpReply.cpp index cfbaaa9..a189831 100644 --- a/src/internals/StreamHttpReply.cpp +++ b/src/internals/StreamHttpReply.cpp @@ -18,6 +18,10 @@ ArduinoHttpServer::AbstractStreamHttpReply::AbstractStreamHttpReply(Stream& stre } +void ArduinoHttpServer::AbstractStreamHttpReply::addHeader(const String& name, const String &value) { + +} + //------------------------------------------------------------------------------ //! \brief Send this reply / print this reply to stream. //! \todo: Accept char* also for data coming directly from flash. @@ -40,7 +44,6 @@ void ArduinoHttpServer::AbstractStreamHttpReply::send(const String& data, const DEBUG_ARDUINO_HTTP_SERVER_PRINTLN("done."); } - Stream& ArduinoHttpServer::AbstractStreamHttpReply::getStream() { return m_stream; diff --git a/src/internals/StreamHttpReply.hpp b/src/internals/StreamHttpReply.hpp index 11cbdb7..fc713f5 100644 --- a/src/internals/StreamHttpReply.hpp +++ b/src/internals/StreamHttpReply.hpp @@ -25,6 +25,7 @@ class AbstractStreamHttpReply { public: + void addHeader(const String& name, const String &value); virtual void send(const String& data, const String& title); protected: From 08d35afb0587191e30175466a245764e7d29c8c6 Mon Sep 17 00:00:00 2001 From: Bart Friederichs Date: Tue, 15 Nov 2022 16:22:32 +0100 Subject: [PATCH 2/4] Implement addHeader --- src/internals/StreamHttpReply.cpp | 7 ++++++- src/internals/StreamHttpReply.hpp | 7 +++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/internals/StreamHttpReply.cpp b/src/internals/StreamHttpReply.cpp index a189831..1e42334 100644 --- a/src/internals/StreamHttpReply.cpp +++ b/src/internals/StreamHttpReply.cpp @@ -19,7 +19,12 @@ ArduinoHttpServer::AbstractStreamHttpReply::AbstractStreamHttpReply(Stream& stre } void ArduinoHttpServer::AbstractStreamHttpReply::addHeader(const String& name, const String &value) { - + if (m_headerCount == MAX_HEADERS) return; + + m_headerNames[m_headerCount] = name; + m_headerValues[m_headerCount] = value; + + m_headerCount++; } //------------------------------------------------------------------------------ diff --git a/src/internals/StreamHttpReply.hpp b/src/internals/StreamHttpReply.hpp index fc713f5..e9f6c17 100644 --- a/src/internals/StreamHttpReply.hpp +++ b/src/internals/StreamHttpReply.hpp @@ -11,9 +11,10 @@ #define __ArduinoHttpServer__StreamHttpReply__ #include - #include "ArduinoHttpServerDebug.h" +#define MAX_HEADERS 3 + namespace ArduinoHttpServer { @@ -38,7 +39,9 @@ class AbstractStreamHttpReply constexpr static const char* CONTENT_TYPE_APPLICATION_JSON PROGMEM = "application/json"; private: - + String m_headerNames[MAX_HEADERS]; + String m_headerValues[MAX_HEADERS]; + int m_headerCount = 0; Stream& m_stream; String m_contentType; //!< Needs to be overridden to default when required. Therefore not const. const String m_code; From e6ab9f261c0c972cc3905f369e5105731be483fd Mon Sep 17 00:00:00 2001 From: Bart Friederichs Date: Tue, 15 Nov 2022 16:24:22 +0100 Subject: [PATCH 3/4] implement sending of extra headers --- src/internals/StreamHttpReply.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/internals/StreamHttpReply.cpp b/src/internals/StreamHttpReply.cpp index 1e42334..1b13aa0 100644 --- a/src/internals/StreamHttpReply.cpp +++ b/src/internals/StreamHttpReply.cpp @@ -43,6 +43,10 @@ void ArduinoHttpServer::AbstractStreamHttpReply::send(const String& data, const getStream().print( AHS_F("Connection: close\r\n") ); getStream().print( AHS_F("Content-Length: ") ); getStream().print( data.length()); getStream().print( AHS_F("\r\n") ); getStream().print( AHS_F("Content-Type: ") ); getStream().print( m_contentType ); getStream().print( AHS_F("\r\n") ); + for(int i = 0; i < m_headerCount; i++) { + getStream().print( m_headerNames[i] ); getStream().print(": "); getStream().print( m_headerValues[i] ); getStream().print( AHS_F("\r\n") ); + } + getStream().print( AHS_F("\r\n") ); getStream().print( data ); getStream().print( AHS_F("\r\n") ); From 0d927dbbbff103dc4e008244b04497c20cd6527c Mon Sep 17 00:00:00 2001 From: BF Date: Sat, 19 Nov 2022 15:26:28 +0100 Subject: [PATCH 4/4] Add OPTIONS method --- src/internals/ArduinoHttpServerDebug.h | 1 - src/internals/StreamHttpRequest.hpp | 13 ++++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/internals/ArduinoHttpServerDebug.h b/src/internals/ArduinoHttpServerDebug.h index 7177112..aa804b6 100644 --- a/src/internals/ArduinoHttpServerDebug.h +++ b/src/internals/ArduinoHttpServerDebug.h @@ -6,7 +6,6 @@ // Copyright (c) 2016 Sander van Woensel. All rights reserved. // //! Debug support - #ifdef ARDUINO_HTTP_SERVER_DEBUG #define DEBUG_ARDUINO_HTTP_SERVER_PRINT(...) Serial.print(__VA_ARGS__) #define DEBUG_ARDUINO_HTTP_SERVER_PRINTLN(...) Serial.println(__VA_ARGS__) diff --git a/src/internals/StreamHttpRequest.hpp b/src/internals/StreamHttpRequest.hpp index 58f493a..38666a7 100644 --- a/src/internals/StreamHttpRequest.hpp +++ b/src/internals/StreamHttpRequest.hpp @@ -28,7 +28,7 @@ namespace ArduinoHttpServer enum class Method : char { - Invalid, Get, Put, Post, Head, Delete + Invalid, Get, Put, Post, Head, Delete, Options }; @@ -240,8 +240,8 @@ void ArduinoHttpServer::StreamHttpRequest::parseMethod(char lineB if(m_error!=Error::OK) { return; } // First strtok call, initialize with cached line buffer. - // len("DELETE") + 1 for terminating null = 7 - FixString<7U> token(strtok_r(lineBuffer, " ", &m_lineBufferStrTokContext)); + // len("OPTIONS") + 1 for terminating null = 7 + FixString<8U> token(strtok_r(lineBuffer, " ", &m_lineBufferStrTokContext)); if(token == "GET") { @@ -263,6 +263,10 @@ void ArduinoHttpServer::StreamHttpRequest::parseMethod(char lineB { m_method = Method::Delete; } + else if (token == "OPTIONS") + { + m_method = Method::Options; + } else { m_method = Method::Invalid; @@ -290,7 +294,6 @@ void ArduinoHttpServer::StreamHttpRequest::parseVersion() else { setError(Error::PARSE_ERROR_INVALID_HTTP_VERSION, version); - } } @@ -319,7 +322,7 @@ template void ArduinoHttpServer::StreamHttpRequest::parseField(char lineBuffer[]) { if(m_error!=Error::OK) { return; } - + ArduinoHttpServer::HttpField httpField(lineBuffer); if(httpField.getType() == ArduinoHttpServer::HttpField::Type::CONTENT_TYPE)