-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from alcjzk/cleanup
Overall big refactor
- Loading branch information
Showing
43 changed files
with
1,668 additions
and
268 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,16 @@ | ||
#pragma once | ||
|
||
#ifndef CONFIG_H | ||
# define CONFIG_H | ||
|
||
# include <vector> | ||
# include <string> | ||
|
||
using namespace std; | ||
#include <vector> | ||
#include <string> | ||
|
||
class Config { | ||
public: | ||
Config(); | ||
|
||
const vector<string> &ports() const; | ||
int backlog() const; | ||
const std::vector<std::string>& ports() const; | ||
int backlog() const; | ||
|
||
private: | ||
vector<string> _ports; | ||
int _backlog; | ||
std::vector<std::string> _ports; | ||
int _backlog; | ||
}; | ||
|
||
#endif |
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include <cassert> | ||
#include "Error.hpp" | ||
|
||
Error::Error(Kind kind) : std::runtime_error(kind_str(kind)), _kind(kind) | ||
{ | ||
|
||
} | ||
|
||
const char* Error::what() const throw() | ||
{ | ||
return kind_str(_kind); | ||
} | ||
|
||
bool Error::operator==(const Error& other) const | ||
{ | ||
return _kind == other._kind; | ||
} | ||
|
||
bool Error::operator==(Kind kind) const | ||
{ | ||
return _kind == kind; | ||
} | ||
|
||
Error::Kind Error::kind() const | ||
{ | ||
return _kind; | ||
} | ||
|
||
const char* Error::kind_str(Kind kind) | ||
{ | ||
switch (kind) | ||
{ | ||
case CLOSED: | ||
return "Connection was closed by the client"; | ||
default: | ||
assert(false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#pragma once | ||
|
||
#include <stdexcept> | ||
|
||
class Error : public std::runtime_error | ||
{ | ||
public: | ||
typedef enum Kind | ||
{ | ||
CLOSED | ||
} Kind; | ||
|
||
Error(Kind kind); | ||
|
||
virtual const char* what() const throw(); | ||
|
||
bool operator==(const Error& other) const; | ||
bool operator==(Kind other) const; | ||
|
||
Kind kind() const; | ||
|
||
static const char* kind_str(Kind kind); | ||
private: | ||
Kind _kind; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include "HTTPError.hpp" | ||
|
||
HTTPError::HTTPError(Status status) : _status(status) | ||
{ | ||
// TODO: Disallow constructing from non-error status | ||
} | ||
|
||
const char* HTTPError::what() const throw() | ||
{ | ||
return _status.text(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#pragma once | ||
|
||
#include <exception> | ||
#include "Status.hpp" | ||
|
||
class HTTPError : public std::exception | ||
{ | ||
public: | ||
HTTPError(Status status); | ||
|
||
virtual const char* what() const throw(); | ||
|
||
private: | ||
Status _status; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#include <cstdlib> | ||
#include "HTTPError.hpp" | ||
#include "HTTPVersion.hpp" | ||
|
||
using std::string; | ||
using std::ostream; | ||
|
||
HTTPVersion::HTTPVersion(unsigned int major, unsigned int minor) throw() | ||
: _major(major), _minor(minor) | ||
{ | ||
|
||
} | ||
|
||
bool HTTPVersion::is_compatible_with(const HTTPVersion& other) const | ||
{ | ||
return (_major == other._major); | ||
} | ||
|
||
HTTPVersion::HTTPVersion(const string& version) | ||
{ | ||
if (version.find("HTTP/") != 0) | ||
throw HTTPError(Status::BAD_REQUEST); | ||
if (version.length() != 8) | ||
throw HTTPError(Status::BAD_REQUEST); | ||
string major_str = version.substr(5, 1); | ||
if (!std::isdigit(*major_str.c_str())) | ||
throw HTTPError(Status::BAD_REQUEST); | ||
_major = std::strtoul(major_str.c_str(), NULL, 10); | ||
if (version.at(6) != '.') | ||
throw HTTPError(Status::BAD_REQUEST); | ||
string minor_str = version.substr(7, 1); | ||
if (!std::isdigit(*minor_str.c_str())) | ||
throw HTTPError(Status::BAD_REQUEST); | ||
_minor = std::strtoul(minor_str.c_str(), NULL, 10); | ||
} | ||
|
||
unsigned int HTTPVersion::major() const | ||
{ | ||
return _major; | ||
} | ||
|
||
unsigned int HTTPVersion::minor() const | ||
{ | ||
return _minor; | ||
} | ||
|
||
ostream& operator<<(ostream& os, const HTTPVersion& version) | ||
{ | ||
return os << "HTTP/" << version.major() << '.' << version.minor(); | ||
} | ||
|
||
#ifdef TESTS | ||
|
||
void HTTPVersionTests::all() | ||
{ | ||
basic(); | ||
compatible(); | ||
} | ||
|
||
void HTTPVersionTests::basic() | ||
{ | ||
try { | ||
HTTPVersion version("HTTP/1.2"); | ||
if (version.major() != 1 || version.minor() != 2) | ||
throw TESTFAIL; | ||
} | ||
catch (...) { | ||
throw TESTFAIL; | ||
} | ||
} | ||
|
||
void HTTPVersionTests::compatible() | ||
{ | ||
try { | ||
HTTPVersion one_zero(1, 0); | ||
HTTPVersion one_one(1, 1); | ||
HTTPVersion two_zero(2, 0); | ||
if (!HTTPVersion(1, 0).is_compatible_with(HTTPVersion(1, 1))) | ||
throw TESTFAIL; | ||
if (HTTPVersion(2, 0).is_compatible_with(HTTPVersion(1, 0))) | ||
throw TESTFAIL; | ||
} | ||
catch (...) { | ||
throw TESTFAIL; | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <ostream> | ||
#include "Reader.hpp" | ||
|
||
class HTTPVersion | ||
{ | ||
public: | ||
HTTPVersion() = default; | ||
HTTPVersion(const std::string& version); | ||
HTTPVersion(unsigned int major, unsigned int minor) throw(); | ||
|
||
unsigned int major() const; | ||
unsigned int minor() const; | ||
|
||
bool is_compatible_with(const HTTPVersion& other) const; | ||
private: | ||
unsigned int _major; | ||
unsigned int _minor; | ||
}; | ||
|
||
std::ostream& operator<<(std::ostream& os, const HTTPVersion& version); | ||
|
||
#ifdef TESTS | ||
|
||
class HTTPVersionTests : public HTTPVersion | ||
{ | ||
public: | ||
static void all(); | ||
|
||
static void basic(); | ||
static void compatible(); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include "http.hpp" | ||
#include "HTTPError.hpp" | ||
#include "Header.hpp" | ||
|
||
using std::string; | ||
|
||
Header::Header(string name, string value) : | ||
_name(name), | ||
_value(value) {} | ||
|
||
Header::Header(const string& text) | ||
{ | ||
size_t start_pos = text.find_first_not_of(http::LWS); | ||
size_t end_pos = text.find_first_of(':', start_pos); | ||
|
||
if (end_pos == string::npos) | ||
{ | ||
throw HTTPError(Status::BAD_REQUEST); | ||
} | ||
_name = text.substr(0, end_pos); | ||
if (!http::is_token(_name)) | ||
{ | ||
throw HTTPError(Status::BAD_REQUEST); | ||
} | ||
start_pos = text.find_first_not_of(http::LWS, end_pos + 1); | ||
if (start_pos != string::npos) | ||
{ | ||
end_pos = text.find_last_not_of(http::LWS); | ||
_value = text.substr(start_pos, end_pos - start_pos + 1); | ||
} | ||
} | ||
|
||
void Header::append(const string& value) | ||
{ | ||
size_t start_pos = value.find_first_not_of(http::LWS); | ||
size_t end_pos = value.find_last_not_of(http::LWS); | ||
|
||
if (end_pos == string::npos) | ||
{ | ||
return ; // Ignore appending empty values | ||
} | ||
_value.append(1, http::SP); | ||
_value.append(value.substr(start_pos, end_pos - start_pos)); | ||
} | ||
|
||
std::ostream& operator<<(std::ostream& os, const Header& header) | ||
{ | ||
return os << header._name << ": " << header._value; | ||
} |
Oops, something went wrong.