Skip to content

Commit a7a99f3

Browse files
Merge pull request #38 from PythonGermany/add-server-tokens-directive
Add server_tokens config option
2 parents 5471d56 + 342abdf commit a7a99f3

File tree

7 files changed

+54
-13
lines changed

7 files changed

+54
-13
lines changed

README.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ The configuration file is used to define the configuration of the webserver. It
8989
| Config type | Options |
9090
| --- | --- |
9191
| Configuration file context | [http](#http) / [location](#location) / [server](#server) |
92-
| 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) |
92+
| 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) |
9393

9494
## Contexts
9595

@@ -110,13 +110,15 @@ http {
110110
log_to_terminal [on|off];
111111
log_level LEVEL;
112112
113+
server_tokens [on|off];
114+
113115
server {
114116
[directives]
115117
}
116118
}
117119
```
118120
Root context. It contains the global configuration of the webserver.
119-
**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)
121+
**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)
120122

121123
### Types
122124
```nginx
@@ -130,6 +132,8 @@ Types context. It contains the mime types of the server. The file [mime.types](c
130132
### Server
131133
```nginx
132134
server {
135+
server_tokens [on|off];
136+
133137
listen HOST:PORT;
134138
server_name NAME [NAME ...];
135139
root PATH;
@@ -147,7 +151,7 @@ server {
147151
}
148152
```
149153
Virtual server context. It contains the configuration of a virtual server.
150-
**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)
154+
**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)
151155

152156
### Location
153157
```nginx
@@ -303,6 +307,13 @@ server_name NAME [NAME ...];
303307
Sets the server names.
304308
**Allowed in:** [Server](#server)
305309

310+
### server_tokens
311+
```
312+
server_tokens [on|off];
313+
```
314+
Enables or disables emitting server version in error messages and in the Server response header field.
315+
**Allowed in:** [Http](#http) / [Server](#server)
316+
306317
### type
307318
```nginx
308319
type MIME_TYPE EXTENSION [EXTENSION ...];
@@ -343,21 +354,27 @@ http {
343354
# File -> /etc/webserv/sites-enabled/server.conf
344355
server {
345356
listen 8080;
357+
358+
server_tokens on;
359+
346360
root /var/www/html;
347361
index index.html index.htm;
348362
autoindex on;
363+
349364
max_client_body_size 1m;
350365
allow GET HEAD OPTIONS;
351366
error_page 404 /404.html;
367+
352368
cgi php /usr/bin/php-cgi;
353369
354370
location /example {
355371
alias /www; # Request: GET /example/file -> ROOT/www/file
356372
index example.html;
373+
357374
allow GET HEAD OPTIONS PUT DELETE;
358375
autoindex off;
359376
}
360-
location /redirect {
377+
location /search {
361378
redirect https://www.duckduckgo.com;
362379
}
363380
}

conf/sites-available/config_test.conf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ server {
66

77
allow GET HEAD OPTIONS;
88

9+
server_tokens on;
10+
911
index lol😀.html;
1012
autoindex on;
1113

@@ -66,4 +68,4 @@ server {
6668
cgi py /usr/bin/python3;
6769
cgi php /usr/bin/php-cgi;
6870
}
69-
}
71+
}

include/config/Config.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const token_t tokens_g[] = {
5353
{"log_level", "http", false, 0, 1, 1, 1, isLogLevel},
5454
{"log_to_terminal", "http", false, 0, 1, 1, 1, isBoolean},
5555
{"server", "http", true, 1, static_cast<size_t>(-1), 0, 0, NULL},
56+
{"server_tokens", "http", false, 0, 1, 1, 1, isBoolean},
5657
{"types", "http", true, 1, 1, 0, 0, NULL},
5758

5859
// Mime type context
@@ -72,6 +73,7 @@ const token_t tokens_g[] = {
7273
{"root", "server", false, 1, 1, 1, 1, NULL},
7374
{"server_name", "server", false, 0, static_cast<size_t>(-1), 1,
7475
static_cast<size_t>(-1), NULL},
76+
{"server_tokens", "server", false, 0, 1, 1, 1, isBoolean},
7577
{"max_client_head_size", "server", false, 0, 1, 1, 1, isMemorySize},
7678
{"max_client_body_size", "server", false, 0, 1, 1, 1, isMemorySize},
7779

include/http/VirtualHost.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class VirtualHost {
1313
static std::vector<VirtualHost> _virtualHosts;
1414
static std::map<std::string, std::string> _mimeTypes;
1515
Context _context;
16+
std::string _externalServerId;
1617
std::set<Address> _resolvedListenDirective;
1718

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

3030
// Getters
3131
static std::vector<VirtualHost> &getVirtualHosts();
3232
static std::string getMimeType(std::string extension);
3333
std::string const &getAddress();
3434
std::set<Address> const &getResolvedAddress() const;
3535
Context &getContext();
36+
std::string const &getExternalServerId() const;
3637

3738
static VirtualHost *matchVirtualHost(Address &address, std::string host);
3839
Context *matchLocation(const std::string &uri);
40+
41+
private:
42+
void setExternalServerId();
3943
};
4044

4145
#endif

include/webserv.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#ifndef WEBSERV_HPP
22
#define WEBSERV_HPP
33

4+
#define WEBSERV_NAME "webserv"
45
#define WEBSERV_VERSION "0.0.2"
5-
#define WEBSERV_ID "webserv/" WEBSERV_VERSION
6+
7+
#define WEBSERV_ID WEBSERV_NAME "/" WEBSERV_VERSION
68

79
#endif

src/http/Http.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ void Http::processCgi(std::string contentLength) {
263263
std::vector<std::string> env;
264264
// const values:
265265
env.push_back("GATEWAY_INTERFACE=CGI/1.1");
266-
env.push_back("SERVER_SOFTWARE=" WEBSERV_ID);
266+
env.push_back("SERVER_SOFTWARE=" + _virtualHost->getExternalServerId());
267267
env.push_back("SERVER_PROTOCOL=" PROTOCOL "/" HTTP_VERSION);
268268

269269
// request specific values:
@@ -526,7 +526,8 @@ void Http::checkResourceValidity(const File &file, const std::string &uri) {
526526
std::string Http::getDefaultBody(std::string code, std::string reason) const {
527527
return "<html>\r\n<head><title>" + code + " " + reason +
528528
"</title></head>\r\n<body>\r\n<center><h1>" + code + " " + reason +
529-
"</h1></center>\r\n<hr><center>" WEBSERV_ID
529+
"</h1></center>\r\n<hr><center>" +
530+
_virtualHost->getExternalServerId() +
530531
"</center>\r\n</body>\r\n</"
531532
"html>\r\n";
532533
}
@@ -538,7 +539,7 @@ void Http::sendResponse() {
538539
}
539540

540541
// Set default header values
541-
_response.setHeader("Server", WEBSERV_ID);
542+
_response.setHeader("Server", _virtualHost->getExternalServerId());
542543
if (_response.getHeader("Content-Length").empty())
543544
_response.setHeader("Content-Length", "0");
544545
if (_response.getHeader("Content-type").empty())

src/http/VirtualHost.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
#include "VirtualHost.hpp"
22

33
#include "utils.hpp"
4+
#include "webserv.hpp"
45

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

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

9-
VirtualHost::VirtualHost() {}
10+
VirtualHost::VirtualHost() : _externalServerId(WEBSERV_NAME) {}
1011

1112
VirtualHost::VirtualHost(const Context &context) {
1213
_context = context;
14+
setExternalServerId();
1315

1416
std::vector<std::vector<std::string> > &listens =
1517
_context.getDirective("listen");
@@ -22,6 +24,7 @@ VirtualHost::VirtualHost(const VirtualHost &rhs) { *this = rhs; }
2224
VirtualHost &VirtualHost::operator=(const VirtualHost &rhs) {
2325
if (this == &rhs) return *this;
2426
_context = rhs._context;
27+
_externalServerId = rhs._externalServerId;
2528
_resolvedListenDirective = rhs._resolvedListenDirective;
2629
return *this;
2730
}
@@ -36,8 +39,6 @@ void VirtualHost::setMimeTypes(std::map<std::string, std::string> &mimeTypes) {
3639
_mimeTypes = mimeTypes;
3740
}
3841

39-
void VirtualHost::setContext(const Context &context) { _context = context; }
40-
4142
std::vector<VirtualHost> &VirtualHost::getVirtualHosts() {
4243
return _virtualHosts;
4344
}
@@ -57,6 +58,10 @@ std::set<Address> const &VirtualHost::getResolvedAddress() const {
5758

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

61+
const std::string &VirtualHost::getExternalServerId() const {
62+
return _externalServerId;
63+
}
64+
6065
VirtualHost *VirtualHost::matchVirtualHost(Address &address, std::string host) {
6166
std::vector<VirtualHost *> possibleHosts;
6267

@@ -110,3 +115,11 @@ Context *VirtualHost::matchLocation(const std::string &uri) {
110115
}
111116
return match;
112117
}
118+
119+
void VirtualHost::setExternalServerId() {
120+
if (_context.exists("server_tokens", true) &&
121+
_context.getDirective("server_tokens", true)[0][0] == "on")
122+
_externalServerId = WEBSERV_ID;
123+
else
124+
_externalServerId = WEBSERV_NAME;
125+
}

0 commit comments

Comments
 (0)