Initial commit
commit
1827b1c558
|
@ -0,0 +1,29 @@
|
|||
MODULES= test libraries/HttpServer
|
||||
|
||||
SOURCES ?= $(wildcard $(addsuffix /*.cpp, $(MODULES)))
|
||||
OBJECTS := $(addsuffix .o, $(addprefix .build/, $(basename $(SOURCES))))
|
||||
DEPFILES := $(subst .o,.dep, $(subst .build/,.deps/, $(OBJECTS)))
|
||||
TESTCPPFLAGS = -D_TEST_ -Ilibraries/HttpServer -Itest -Iarduino
|
||||
CPPDEPFLAGS = -MMD -MP -MF .deps/$(basename $<).dep
|
||||
TEST_TARGET=test_HttpServer
|
||||
|
||||
.PHONY: all test clean
|
||||
|
||||
all: test
|
||||
|
||||
|
||||
test: $(TEST_TARGET)
|
||||
./$(TEST_TARGET)
|
||||
|
||||
.build/%.o: %.cpp
|
||||
mkdir -p .deps/$(dir $<)
|
||||
mkdir -p .build/$(dir $<)
|
||||
$(COMPILE.cpp) $(TESTCPPFLAGS) $(CPPDEPFLAGS) -o $@ $<
|
||||
|
||||
$(TEST_TARGET): $(OBJECTS)
|
||||
$(CC) $(OBJECTS) -lstdc++ -o $@
|
||||
|
||||
clean:
|
||||
@rm -rf .deps/ .build/ $(TEST_TARGET)
|
||||
|
||||
-include $(DEPFILES)
|
|
@ -0,0 +1,72 @@
|
|||
#include "HttpHeaders.h"
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
#include <iostream>
|
||||
|
||||
|
||||
#ifndef HTTPHEADERS_SIZE
|
||||
#define HTTPHEADERS_SIZE 20
|
||||
#endif
|
||||
|
||||
|
||||
HttpHeaders::HttpHeaders() : _n(0) {
|
||||
headers = (HttpHeaderNode*) malloc(sizeof(HttpHeaderNode) * HTTPHEADERS_SIZE);
|
||||
}
|
||||
|
||||
void HttpHeaders::set(const char* name, const char* value) {
|
||||
for (int i = 0; i < count(); i++) {
|
||||
if (strcmp(name, headers[i].name) != 0)
|
||||
continue;
|
||||
free(headers[i].value);
|
||||
headers[i].value = (char *) malloc(sizeof(char) * strlen(value));
|
||||
strcpy(headers[i].value, value);
|
||||
return;
|
||||
}
|
||||
headers[_n].name = (char *) malloc(sizeof(char) * strlen(name));
|
||||
headers[_n].value = (char *) malloc(sizeof(char) * strlen(value));
|
||||
strcpy(headers[_n].name, name);
|
||||
strcpy(headers[_n].value, value);
|
||||
_n++;
|
||||
}
|
||||
|
||||
char* HttpHeaders::get(const char* name)
|
||||
{
|
||||
for (int i = 0; i < count(); i++) {
|
||||
if (strcmp(name, headers[i].name) == 0)
|
||||
return headers[i].value;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool HttpHeaders::has(const char* name)
|
||||
{
|
||||
for (int i = 0; i < count(); i++) {
|
||||
if (strcmp(name, headers[i].name) == 0)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int HttpHeaders::count()
|
||||
{
|
||||
return _n;
|
||||
}
|
||||
|
||||
int HttpHeaders::length()
|
||||
{
|
||||
int length = 0;
|
||||
for (int i = 0; i < count(); i++) {
|
||||
length += strlen(headers[i].name);
|
||||
length += strlen(headers[i].value);
|
||||
length += 4; // add ': ' and '\r\n'
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
HttpHeaders::~HttpHeaders() {
|
||||
for (int i = 0; i < count(); i++) {
|
||||
free(headers[i].name);
|
||||
free(headers[i].value);
|
||||
}
|
||||
free(headers);
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
#pragma once
|
||||
|
||||
typedef struct HttpHeaderNode
|
||||
{
|
||||
char* name;
|
||||
char* value;
|
||||
} HttpHeaderNode;
|
||||
|
||||
class HttpHeaders {
|
||||
public:
|
||||
HttpHeaders();
|
||||
~HttpHeaders();
|
||||
void set(const char * , const char *);
|
||||
char* get(const char*);
|
||||
bool has(const char*);
|
||||
int count();
|
||||
int length();
|
||||
void write(char *, int);
|
||||
|
||||
private:
|
||||
int _n;
|
||||
HttpHeaderNode* headers;
|
||||
};
|
||||
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,17 @@
|
|||
#ifndef DUMMYPRINT_H
|
||||
#define DUMMYPRINT_H
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
class DummyPrint
|
||||
{
|
||||
public:
|
||||
DummyPrint(char * buff): buffer(buff), pos(0) {};
|
||||
virtual size_t write(uint8_t c) { buffer[pos++]=(char) c; buffer[pos]='\0';return sizeof(c); }
|
||||
int pos;
|
||||
char * buffer;
|
||||
};
|
||||
|
||||
extern Print
|
||||
|
||||
#endif // DUMMYPRINT_H
|
|
@ -0,0 +1,17 @@
|
|||
#pragma once
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
class DummyStream: public Stream
|
||||
{
|
||||
public:
|
||||
DummyStream(char* buff): buffer(buffer), pos(0) {};
|
||||
char * buffer;
|
||||
int pos;
|
||||
virtual int available() { return (buffer[pos] != '\0'); }
|
||||
virtual int read() { return buffer[pos++]; }
|
||||
virtual int peek() { return buffer[pos]; }
|
||||
virtual size_t write(uint8_t c) { return 0; } // gone forever
|
||||
};
|
||||
|
||||
extern DummyStream Stream
|
|
@ -0,0 +1,2 @@
|
|||
#define CATCH_CONFIG_MAIN
|
||||
#include "catch.hpp"
|
|
@ -0,0 +1,51 @@
|
|||
#include "catch.hpp"
|
||||
#include "HttpHeaders.h"
|
||||
#include "string.h"
|
||||
|
||||
TEST_CASE("Empty HttpHeaders","[HttpHeaders]") {
|
||||
HttpHeaders headers;
|
||||
|
||||
CHECK(headers.count() == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("Get and set headers","[HttpHeaders]"){
|
||||
HttpHeaders headers;
|
||||
|
||||
// Check that getting non-existant header is a nullptr
|
||||
CHECK(headers.get("foo") == nullptr);
|
||||
|
||||
// Set 'foo: bar' and check it
|
||||
headers.set("foo","bar");
|
||||
CHECK(strcmp(headers.get("foo"),"bar") == 0);
|
||||
CHECK(headers.count() == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("Overwrite header with smaller header","[HttpHeaders]"){
|
||||
HttpHeaders headers;
|
||||
headers.set("foo","sushi");
|
||||
headers.set("foo","soy");
|
||||
|
||||
CHECK(strcmp(headers.get("foo"),"soy") == 0);
|
||||
CHECK(headers.count() == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("Overwrite header with larger header","[HttpHeaders]"){
|
||||
HttpHeaders headers;
|
||||
headers.set("foo","pear");
|
||||
headers.set("foo","watermelon");
|
||||
|
||||
CHECK(strcmp(headers.get("foo"),"watermelon") == 0);
|
||||
CHECK(headers.count()==1);
|
||||
}
|
||||
|
||||
TEST_CASE("Force headers to grow","[HttpHeaders]") {
|
||||
HttpHeaders headers;
|
||||
char * name = "header_";
|
||||
/*
|
||||
for (int i = 0; i < 30; i++) {
|
||||
name[6] = 0x41 + i;
|
||||
headers.set(name,"Foo");
|
||||
REQUIRE(strcmp(headers.get(name),"Foo") == 0);
|
||||
}
|
||||
*/
|
||||
}
|
Loading…
Reference in New Issue