Skip to content

Commit

Permalink
Merge pull request #3 from alcjzk/cleanup
Browse files Browse the repository at this point in the history
Overall big refactor
  • Loading branch information
alcjzk authored Jan 6, 2024
2 parents 264aa35 + a772ae8 commit e5807c8
Show file tree
Hide file tree
Showing 43 changed files with 1,668 additions and 268 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[Makefile]
indent_style = tab
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
NAME = webserv

TEST = test

OBJ_DIR = ./obj
SRC_DIR = ./src/

Expand All @@ -9,10 +11,13 @@ OPT = 0
WARN = all extra error no-unused-variable no-unused-parameter
EXTRA = -g -MP -MMD

CFLAGS = -I$(SRC_DIR) $(EXTRA) $(WARN:%=-W%) -O$(OPT) -std=c++98
CFLAGS = -I$(SRC_DIR) $(EXTRA) $(WARN:%=-W%) -O$(OPT) -std=c++11 -D LOG_ENABLE -D LOGLEVEL_INFO

SHELL = /bin/sh

SRCS = main.cpp Config.cpp Server.cpp StatusCode.cpp Method.cpp
SRCS = main.cpp Config.cpp Server.cpp Runtime.cpp Task.cpp Reader.cpp Method.cpp HTTPVersion.cpp URI.cpp RequestLine.cpp Error.cpp Status.cpp HTTPError.cpp Response.cpp Log.cpp Request.cpp Header.cpp http.cpp

SRCS_TEST = test.cpp Reader.cpp HTTPVersion.cpp Method.cpp RequestLine.cpp URI.cpp

all: $(NAME)

Expand All @@ -30,6 +35,9 @@ re: fclean all
$(NAME): $(SRCS:%.cpp=$(OBJ_DIR)/%.o)
$(CC) $(CFLAGS) -o $@ $^

$(TEST): $(SRCS_TEST:%.cpp=$(OBJ_DIR)/%.o)
$(CC) $(CFLAGS) -o $@ $^

$(OBJ_DIR)/%.o: %.cpp | $(OBJ_DIR)
$(CC) -c $(CFLAGS) -o $@ $<

Expand Down
Empty file removed ref.cpp
Empty file.
10 changes: 7 additions & 3 deletions src/Config.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#include "Config.hpp"

using std::vector;
using std::string;

Config::Config()
: _ports(vector<string>(1, string("8000"))),
_backlog(128) // OSX capped value for listen(2)
{
_ports = vector<string>(1, string("8000"));
_backlog = 20;

}

const vector<string> &Config::ports() const
const vector<string>& Config::ports() const
{
return _ports;
}
Expand Down
20 changes: 7 additions & 13 deletions src/Config.hpp
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 removed src/Connection.cpp
Empty file.
13 changes: 0 additions & 13 deletions src/Connection.hpp

This file was deleted.

38 changes: 38 additions & 0 deletions src/Error.cpp
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);
}
}
25 changes: 25 additions & 0 deletions src/Error.hpp
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;
};
11 changes: 11 additions & 0 deletions src/HTTPError.cpp
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();
}
15 changes: 15 additions & 0 deletions src/HTTPError.hpp
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;
};
88 changes: 88 additions & 0 deletions src/HTTPVersion.cpp
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
36 changes: 36 additions & 0 deletions src/HTTPVersion.hpp
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
49 changes: 49 additions & 0 deletions src/Header.cpp
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;
}
Loading

0 comments on commit e5807c8

Please sign in to comment.