Skip to content
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
1 change: 0 additions & 1 deletion src/internals/ArduinoHttpServerDebug.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down
14 changes: 13 additions & 1 deletion src/internals/StreamHttpReply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ 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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not a struct with a name and value pair?

m_headerValues[m_headerCount] = value;

m_headerCount++;
}

//------------------------------------------------------------------------------
//! \brief Send this reply / print this reply to stream.
//! \todo: Accept char* also for data coming directly from flash.
Expand All @@ -34,13 +43,16 @@ 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") );

DEBUG_ARDUINO_HTTP_SERVER_PRINTLN("done.");
}


Stream& ArduinoHttpServer::AbstractStreamHttpReply::getStream()
{
return m_stream;
Expand Down
8 changes: 6 additions & 2 deletions src/internals/StreamHttpReply.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
#define __ArduinoHttpServer__StreamHttpReply__

#include <Arduino.h>

#include "ArduinoHttpServerDebug.h"

#define MAX_HEADERS 3
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change it to modern C++ typesafe constexpr?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can give it a shot, but I haven't used those ever (I am not a native C++ speaker).


namespace ArduinoHttpServer
{

Expand All @@ -25,6 +26,7 @@ class AbstractStreamHttpReply
{

public:
void addHeader(const String& name, const String &value);
virtual void send(const String& data, const String& title);

protected:
Expand All @@ -37,7 +39,9 @@ class AbstractStreamHttpReply
constexpr static const char* CONTENT_TYPE_APPLICATION_JSON PROGMEM = "application/json";

private:

String m_headerNames[MAX_HEADERS];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't mind, I think I will rewrite it to become an array of HttpField objects.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course I don't mind. Your C++ is probably better than mine :).

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;
Expand Down
13 changes: 8 additions & 5 deletions src/internals/StreamHttpRequest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace ArduinoHttpServer

enum class Method : char
{
Invalid, Get, Put, Post, Head, Delete
Invalid, Get, Put, Post, Head, Delete, Options
};


Expand Down Expand Up @@ -240,8 +240,8 @@ void ArduinoHttpServer::StreamHttpRequest<MAX_BODY_SIZE>::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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// len("OPTIONS") + 1 for terminating null = 7
// len("OPTIONS") + 1 for terminating null = 8

FixString<8U> token(strtok_r(lineBuffer, " ", &m_lineBufferStrTokContext));

if(token == "GET")
{
Expand All @@ -263,6 +263,10 @@ void ArduinoHttpServer::StreamHttpRequest<MAX_BODY_SIZE>::parseMethod(char lineB
{
m_method = Method::Delete;
}
else if (token == "OPTIONS")
{
m_method = Method::Options;
}
else
{
m_method = Method::Invalid;
Expand Down Expand Up @@ -290,7 +294,6 @@ void ArduinoHttpServer::StreamHttpRequest<MAX_BODY_SIZE>::parseVersion()
else
{
setError(Error::PARSE_ERROR_INVALID_HTTP_VERSION, version);

}

}
Expand Down Expand Up @@ -319,7 +322,7 @@ template <size_t MAX_BODY_SIZE>
void ArduinoHttpServer::StreamHttpRequest<MAX_BODY_SIZE>::parseField(char lineBuffer[])
{
if(m_error!=Error::OK) { return; }

ArduinoHttpServer::HttpField httpField(lineBuffer);

if(httpField.getType() == ArduinoHttpServer::HttpField::Type::CONTENT_TYPE)
Expand Down