Skip to content
Merged
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
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ The configuration file is used to define the configuration of the webserver. It
| Config type | Options |
| --- | --- |
| Configuration file context | [http](#http) / [location](#location) / [server](#server) |
| Configuration file directives | [access_log](#access_log) / [alias](#alias) / [allow](#allow) / [autoindex](#autoindex) / [cgi](#cgi) / [cgi_timeout](#cgi_timeout) / [client_timeout](#client_timout) / [error_log](#error_log) / [error_page](#error_page) / [include](#include) / [index](#index) / [listen](#listen) / [log_level](#log_level) / [log_to_terminal](#log_to_terminal) / [max_client_body_size](#max_client_body_size) / [redirect](#redirect) / [root](#root) / [server_name](#server_name) / [type](#type) |
| Configuration file directives | [access_log](#access_log) / [alias](#alias) / [allow](#allow) / [autoindex](#autoindex) / [cgi](#cgi) / [cgi_timeout](#cgi_timeout) / [client_timeout](#client_timout) / [error_log](#error_log) / [error_page](#error_page) / [include](#include) / [index](#index) / [listen](#listen) / [log_level](#log_level) / [log_to_terminal](#log_to_terminal) / [max_client_body_size](#max_client_body_size) / [redirect](#redirect) / [root](#root) / [server_name](#server_name) / [server_tokens](#server_tokens) / [type](#type) |

## Contexts

Expand All @@ -110,13 +110,15 @@ http {
log_to_terminal [on|off];
log_level LEVEL;

server_tokens [on|off];

server {
[directives]
}
}
```
Root context. It contains the global configuration of the webserver.
**Allowed tokens:** [access_log](#access_log) / [cgi_timeout](#cgi_timeout) / [client_timeout](#client_timout)/ [error_log](#error_log) / [include](#include) / [log_level](#log_level) / [log_to_terminal](#log_to_terminal) / [server](#server) / [types](#types)
**Allowed tokens:** [access_log](#access_log) / [cgi_timeout](#cgi_timeout) / [client_timeout](#client_timout)/ [error_log](#error_log) / [include](#include) / [log_level](#log_level) / [log_to_terminal](#log_to_terminal) / [server](#server) / [server_tokens](#server_tokens) / [types](#types)

### Types
```nginx
Expand All @@ -130,6 +132,8 @@ Types context. It contains the mime types of the server. The file [mime.types](c
### Server
```nginx
server {
server_tokens [on|off];

listen HOST:PORT;
server_name NAME [NAME ...];
root PATH;
Expand All @@ -147,7 +151,7 @@ server {
}
```
Virtual server context. It contains the configuration of a virtual server.
**Allowed tokens:** [allow](#allow) / [autoindex](#autoindex) / [cgi](#cgi) / [error_page](#error_page) / [index](#index) / [listen](#listen) / [location](#location) / [max_client_body_size](#max_client_body_size) / [root](#root) / [server_name](#server_name)
**Allowed tokens:** [allow](#allow) / [autoindex](#autoindex) / [cgi](#cgi) / [error_page](#error_page) / [index](#index) / [listen](#listen) / [location](#location) / [max_client_body_size](#max_client_body_size) / [root](#root) / [server_name](#server_name) / [server_tokens](#server_tokens)

### Location
```nginx
Expand Down Expand Up @@ -303,6 +307,13 @@ server_name NAME [NAME ...];
Sets the server names.
**Allowed in:** [Server](#server)

### server_tokens
```
server_tokens [on|off];
```
Enables or disables emitting server version in error messages and in the Server response header field.
**Allowed in:** [Http](#http) / [Server](#server)

### type
```nginx
type MIME_TYPE EXTENSION [EXTENSION ...];
Expand Down Expand Up @@ -343,21 +354,27 @@ http {
# File -> /etc/webserv/sites-enabled/server.conf
server {
listen 8080;

server_tokens on;

root /var/www/html;
index index.html index.htm;
autoindex on;

max_client_body_size 1m;
allow GET HEAD OPTIONS;
error_page 404 /404.html;

cgi php /usr/bin/php-cgi;

location /example {
alias /www; # Request: GET /example/file -> ROOT/www/file
index example.html;

allow GET HEAD OPTIONS PUT DELETE;
autoindex off;
}
location /redirect {
location /search {
redirect https://www.duckduckgo.com;
}
}
Expand Down
4 changes: 3 additions & 1 deletion conf/sites-available/config_test.conf
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ server {

allow GET HEAD OPTIONS;

server_tokens on;

index lol😀.html;
autoindex on;

Expand Down Expand Up @@ -66,4 +68,4 @@ server {
cgi py /usr/bin/python3;
cgi php /usr/bin/php-cgi;
}
}
}
2 changes: 2 additions & 0 deletions include/config/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const token_t tokens_g[] = {
{"log_level", "http", false, 0, 1, 1, 1, isLogLevel},
{"log_to_terminal", "http", false, 0, 1, 1, 1, isBoolean},
{"server", "http", true, 1, static_cast<size_t>(-1), 0, 0, NULL},
{"server_tokens", "http", false, 0, 1, 1, 1, isBoolean},
{"types", "http", true, 1, 1, 0, 0, NULL},

// Mime type context
Expand All @@ -72,6 +73,7 @@ const token_t tokens_g[] = {
{"root", "server", false, 1, 1, 1, 1, NULL},
{"server_name", "server", false, 0, static_cast<size_t>(-1), 1,
static_cast<size_t>(-1), NULL},
{"server_tokens", "server", false, 0, 1, 1, 1, isBoolean},
{"max_client_head_size", "server", false, 0, 1, 1, 1, isMemorySize},
{"max_client_body_size", "server", false, 0, 1, 1, 1, isMemorySize},

Expand Down
6 changes: 5 additions & 1 deletion include/http/VirtualHost.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class VirtualHost {
static std::vector<VirtualHost> _virtualHosts;
static std::map<std::string, std::string> _mimeTypes;
Context _context;
std::string _externalServerId;
std::set<Address> _resolvedListenDirective;

public:
Expand All @@ -25,17 +26,20 @@ class VirtualHost {
// Setters/Adders
static void add(const VirtualHost &virtualHost);
static void setMimeTypes(std::map<std::string, std::string> &mimeTypes);
void setContext(const Context &context);

// Getters
static std::vector<VirtualHost> &getVirtualHosts();
static std::string getMimeType(std::string extension);
std::string const &getAddress();
std::set<Address> const &getResolvedAddress() const;
Context &getContext();
std::string const &getExternalServerId() const;

static VirtualHost *matchVirtualHost(Address &address, std::string host);
Context *matchLocation(const std::string &uri);

private:
void setExternalServerId();
};

#endif
4 changes: 3 additions & 1 deletion include/webserv.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#ifndef WEBSERV_HPP
#define WEBSERV_HPP

#define WEBSERV_NAME "webserv"
#define WEBSERV_VERSION "0.0.2"
#define WEBSERV_ID "webserv/" WEBSERV_VERSION

#define WEBSERV_ID WEBSERV_NAME "/" WEBSERV_VERSION

#endif
7 changes: 4 additions & 3 deletions src/http/Http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ void Http::processCgi(std::string contentLength) {
std::vector<std::string> env;
// const values:
env.push_back("GATEWAY_INTERFACE=CGI/1.1");
env.push_back("SERVER_SOFTWARE=" WEBSERV_ID);
env.push_back("SERVER_SOFTWARE=" + _virtualHost->getExternalServerId());
env.push_back("SERVER_PROTOCOL=" PROTOCOL "/" HTTP_VERSION);

// request specific values:
Expand Down Expand Up @@ -526,7 +526,8 @@ void Http::checkResourceValidity(const File &file, const std::string &uri) {
std::string Http::getDefaultBody(std::string code, std::string reason) const {
return "<html>\r\n<head><title>" + code + " " + reason +
"</title></head>\r\n<body>\r\n<center><h1>" + code + " " + reason +
"</h1></center>\r\n<hr><center>" WEBSERV_ID
"</h1></center>\r\n<hr><center>" +
_virtualHost->getExternalServerId() +
"</center>\r\n</body>\r\n</"
"html>\r\n";
}
Expand All @@ -538,7 +539,7 @@ void Http::sendResponse() {
}

// Set default header values
_response.setHeader("Server", WEBSERV_ID);
_response.setHeader("Server", _virtualHost->getExternalServerId());
if (_response.getHeader("Content-Length").empty())
_response.setHeader("Content-Length", "0");
if (_response.getHeader("Content-type").empty())
Expand Down
19 changes: 16 additions & 3 deletions src/http/VirtualHost.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#include "VirtualHost.hpp"

#include "utils.hpp"
#include "webserv.hpp"

std::vector<VirtualHost> VirtualHost::_virtualHosts;

std::map<std::string, std::string> VirtualHost::_mimeTypes;

VirtualHost::VirtualHost() {}
VirtualHost::VirtualHost() : _externalServerId(WEBSERV_NAME) {}

VirtualHost::VirtualHost(const Context &context) {
_context = context;
setExternalServerId();

std::vector<std::vector<std::string> > &listens =
_context.getDirective("listen");
Expand All @@ -22,6 +24,7 @@ VirtualHost::VirtualHost(const VirtualHost &rhs) { *this = rhs; }
VirtualHost &VirtualHost::operator=(const VirtualHost &rhs) {
if (this == &rhs) return *this;
_context = rhs._context;
_externalServerId = rhs._externalServerId;
_resolvedListenDirective = rhs._resolvedListenDirective;
return *this;
}
Expand All @@ -36,8 +39,6 @@ void VirtualHost::setMimeTypes(std::map<std::string, std::string> &mimeTypes) {
_mimeTypes = mimeTypes;
}

void VirtualHost::setContext(const Context &context) { _context = context; }

std::vector<VirtualHost> &VirtualHost::getVirtualHosts() {
return _virtualHosts;
}
Expand All @@ -57,6 +58,10 @@ std::set<Address> const &VirtualHost::getResolvedAddress() const {

Context &VirtualHost::getContext() { return _context; }

const std::string &VirtualHost::getExternalServerId() const {
return _externalServerId;
}

VirtualHost *VirtualHost::matchVirtualHost(Address &address, std::string host) {
std::vector<VirtualHost *> possibleHosts;

Expand Down Expand Up @@ -110,3 +115,11 @@ Context *VirtualHost::matchLocation(const std::string &uri) {
}
return match;
}

void VirtualHost::setExternalServerId() {
if (_context.exists("server_tokens", true) &&
_context.getDirective("server_tokens", true)[0][0] == "on")
_externalServerId = WEBSERV_ID;
else
_externalServerId = WEBSERV_NAME;
}