Skip to content

Commit 5c05a74

Browse files
committed
Initial commit of server code
1 parent ac4320c commit 5c05a74

24 files changed

Lines changed: 1545 additions & 0 deletions

‎https/ConnectionContext.cpp‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* ConnectionContext.cpp
3+
*
4+
* Created on: Dec 15, 2017
5+
* Author: frank
6+
*/
7+
8+
#include "ConnectionContext.hpp"
9+
10+
namespace httpsserver {
11+
12+
ConnectionContext::ConnectionContext() {
13+
// TODO Auto-generated constructor stub
14+
15+
}
16+
17+
ConnectionContext::~ConnectionContext() {
18+
// TODO Auto-generated destructor stub
19+
}
20+
21+
} /* namespace httpsserver */

‎https/ConnectionContext.hpp‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* ConnectionContext.hpp
3+
*
4+
* Created on: Dec 15, 2017
5+
* Author: frank
6+
*/
7+
8+
#ifndef HTTPS_CONNECTIONCONTEXT_HPP_
9+
#define HTTPS_CONNECTIONCONTEXT_HPP_
10+
11+
#include <Arduino.h>
12+
13+
// Required for SSL
14+
#include "openssl/ssl.h"
15+
#undef read
16+
17+
namespace httpsserver {
18+
19+
class ConnectionContext {
20+
public:
21+
ConnectionContext();
22+
virtual ~ConnectionContext();
23+
24+
virtual SSL* ssl() = 0;
25+
};
26+
27+
} /* namespace httpsserver */
28+
29+
#endif /* HTTPS_CONNECTIONCONTEXT_HPP_ */

‎https/HTTPHeader.cpp‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* HTTPHeader.cpp
3+
*
4+
* Created on: Dec 12, 2017
5+
* Author: frank
6+
*/
7+
8+
#include "HTTPHeader.hpp"
9+
10+
namespace httpsserver {
11+
12+
HTTPHeader::HTTPHeader(const std::string name, const std::string value):
13+
_name(std::move(name)),
14+
_value(std::move(value)) {
15+
16+
}
17+
18+
HTTPHeader::~HTTPHeader() {
19+
20+
}
21+
22+
std::string HTTPHeader::print() {
23+
return _name + ": " + _value;
24+
}
25+
26+
} /* namespace httpsserver */

‎https/HTTPHeader.hpp‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* HTTPHeader.hpp
3+
*
4+
* Created on: Dec 12, 2017
5+
* Author: frank
6+
*/
7+
8+
#ifndef HTTPS_HTTPHEADER_HPP_
9+
#define HTTPS_HTTPHEADER_HPP_
10+
11+
#include <string>
12+
13+
namespace httpsserver {
14+
15+
class HTTPHeader {
16+
public:
17+
HTTPHeader(const std::string name, const std::string value);
18+
virtual ~HTTPHeader();
19+
const std::string _name;
20+
const std::string _value;
21+
std::string print();
22+
};
23+
24+
} /* namespace httpsserver */
25+
26+
#endif /* HTTPS_HTTPHEADER_HPP_ */

‎https/HTTPHeaders.cpp‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* HTTPHeaders.cpp
3+
*
4+
* Created on: Dec 13, 2017
5+
* Author: frank
6+
*/
7+
8+
#include "HTTPHeaders.hpp"
9+
10+
namespace httpsserver {
11+
12+
HTTPHeaders::HTTPHeaders() {
13+
_headers = new std::vector<HTTPHeader *>();
14+
15+
}
16+
17+
HTTPHeaders::~HTTPHeaders() {
18+
for(std::vector<HTTPHeader*>::iterator header = _headers->begin(); header != _headers->end(); ++header) {
19+
delete (*header);
20+
}
21+
delete _headers;
22+
}
23+
24+
HTTPHeader * HTTPHeaders::get(const std::string name) {
25+
for(std::vector<HTTPHeader*>::iterator header = _headers->begin(); header != _headers->end(); ++header) {
26+
if ((*header)->_name.compare(name)==0) {
27+
return (*header);
28+
}
29+
}
30+
return NULL;
31+
}
32+
33+
void HTTPHeaders::set(HTTPHeader * header) {
34+
for(int i = 0; i < _headers->size(); i++) {
35+
if ((*_headers)[i]->_name.compare(header->_name)==0) {
36+
delete (*_headers)[i];
37+
(*_headers)[i] = header;
38+
return;
39+
}
40+
}
41+
_headers->push_back(header);
42+
}
43+
44+
std::vector<HTTPHeader *> * HTTPHeaders::getAll() {
45+
return _headers;
46+
}
47+
48+
} /* namespace httpsserver */

‎https/HTTPHeaders.hpp‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* HTTPHeaders.hpp
3+
*
4+
* Created on: Dec 13, 2017
5+
* Author: frank
6+
*/
7+
8+
#ifndef HTTPS_HTTPHEADERS_HPP_
9+
#define HTTPS_HTTPHEADERS_HPP_
10+
11+
#include <string>
12+
// Arduino declares it's own min max, incompatible with the stl...
13+
#undef min
14+
#undef max
15+
#include <vector>
16+
17+
#include "HTTPSServerConstants.hpp"
18+
#include "HTTPHeader.hpp"
19+
20+
namespace httpsserver {
21+
22+
class HTTPHeaders {
23+
public:
24+
HTTPHeaders();
25+
virtual ~HTTPHeaders();
26+
27+
HTTPHeader * get(std::string name);
28+
void set(HTTPHeader * header);
29+
30+
std::vector<HTTPHeader *> * getAll();
31+
32+
private:
33+
std::vector<HTTPHeader*> * _headers;
34+
};
35+
36+
} /* namespace httpsserver */
37+
38+
#endif /* HTTPS_HTTPHEADERS_HPP_ */

‎https/HTTPRequest.cpp‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* HTTPRequest.cpp
3+
*
4+
* Created on: Dec 13, 2017
5+
* Author: frank
6+
*/
7+
8+
#include "HTTPRequest.hpp"
9+
10+
namespace httpsserver {
11+
12+
HTTPRequest::HTTPRequest(ConnectionContext * con, HTTPHeaders * headers):
13+
_con(con),
14+
_headers(headers) {
15+
16+
}
17+
18+
HTTPRequest::~HTTPRequest() {
19+
20+
}
21+
22+
std::string HTTPRequest::getHeader(std::string name) {
23+
HTTPHeader * h = _headers->get(name);
24+
if (h != NULL) {
25+
return h->_value;
26+
} else {
27+
return NULL;
28+
}
29+
}
30+
31+
} /* namespace httpsserver */

‎https/HTTPRequest.hpp‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* HTTPRequest.hpp
3+
*
4+
* Created on: Dec 13, 2017
5+
* Author: frank
6+
*/
7+
8+
#ifndef HTTPS_HTTPREQUEST_HPP_
9+
#define HTTPS_HTTPREQUEST_HPP_
10+
11+
#include <Arduino.h>
12+
#include <string>
13+
14+
#include "ConnectionContext.hpp"
15+
#include "HTTPHeaders.hpp"
16+
#include "HTTPHeader.hpp"
17+
18+
namespace httpsserver {
19+
20+
class HTTPRequest {
21+
public:
22+
HTTPRequest(ConnectionContext * con, HTTPHeaders * headers);
23+
virtual ~HTTPRequest();
24+
25+
std::string getHeader(std::string name);
26+
private:
27+
ConnectionContext * _con;
28+
29+
HTTPHeaders * _headers;
30+
};
31+
32+
} /* namespace httpsserver */
33+
34+
#endif /* HTTPS_HTTPREQUEST_HPP_ */

‎https/HTTPResponse.cpp‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* HTTPResponse.cpp
3+
*
4+
* Created on: Dec 13, 2017
5+
* Author: frank
6+
*/
7+
8+
#include "HTTPResponse.hpp"
9+
10+
namespace httpsserver {
11+
12+
HTTPResponse::HTTPResponse(ConnectionContext * con):
13+
_con(con) {
14+
15+
// Default status code is 200 OK
16+
_statusCode = 200;
17+
_statusText = "OK";
18+
_headerWritten = false;
19+
}
20+
21+
HTTPResponse::~HTTPResponse() {
22+
23+
}
24+
25+
void HTTPResponse::setStatusCode(uint16_t statusCode) {
26+
_statusCode = statusCode;
27+
}
28+
29+
void HTTPResponse::setStatusText(std::string statusText) {
30+
_statusText = statusText;
31+
}
32+
33+
void HTTPResponse::setHeader(std::string name, std::string value) {
34+
_headers.set(new HTTPHeader(name, value));
35+
}
36+
37+
bool HTTPResponse::isHeaderWritten() {
38+
return _headerWritten;
39+
}
40+
41+
/**
42+
* Writes a string to the response. May be called several times.
43+
*/
44+
void HTTPResponse::print(const std::string &str) {
45+
printHeader();
46+
printInternal(str);
47+
}
48+
49+
/**
50+
* Writes bytes to the response. May be called several times.
51+
*/
52+
void HTTPResponse::writeBytes(const void * data, int length) {
53+
printHeader();
54+
writeBytesInternal(data, length);
55+
}
56+
57+
/**
58+
* If not already done, writes the header.
59+
*/
60+
void HTTPResponse::printHeader() {
61+
if (!_headerWritten) {
62+
// FIXME: The usage of ostringstream increses the binary size by 200kB.
63+
// std::to_string is only available for the 2011 standard, but maybe
64+
// one can change this to a int-string-conversion this is performed manually
65+
std::ostringstream statusLine;
66+
statusLine << "HTTP/1.1" << _statusCode << " " << _statusText << "\r\n";
67+
printInternal(statusLine.str());
68+
std::vector<HTTPHeader *> * headers = _headers.getAll();
69+
for(std::vector<HTTPHeader*>::iterator header = headers->begin(); header != headers->end(); ++header) {
70+
printInternal((*header)->print()+"\r\n");
71+
}
72+
printInternal("\r\n");
73+
74+
// TODO: If we wanted to use Connection: keep-alive, we need to know the content size now.
75+
76+
_headerWritten=true;
77+
}
78+
}
79+
80+
void HTTPResponse::printInternal(const std::string &str) {
81+
writeBytesInternal(str.c_str(), str.length());
82+
}
83+
84+
void HTTPResponse::writeBytesInternal(const void * data, int length) {
85+
SSL_write(_con->ssl(), data, length);
86+
}
87+
88+
} /* namespace httpsserver */

0 commit comments

Comments
 (0)