Skip to content

Commit 9c366f2

Browse files
committed
Added a workaround for the empty requests issue. This is still pending to be fixed, but for now the server is once again stable.
Updated the example form.pl, to show a full range of possibilities.
1 parent d74c554 commit 9c366f2

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

src/Worker.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,20 @@ void Worker::handle_request() {
4141
// TODO not sure if necessary
4242
if(errno == EINTR){
4343
this->logger->info("Recv() interrupted by signal");
44+
free(request);
4445
return;
4546
}
4647
return_code = HTTP_BAD_REQUEST;
4748
data = dHandler.get_error_file(return_code, "Empty request received");
4849
}
4950
else {
5051
std::string req_str = std::string(request);
52+
if (req_str.length() == 0) {
53+
this->logger->warn("Empty request received, ignoring");
54+
free(request);
55+
return;
56+
}
57+
5158
BasicHTTP::request req = httpHandler.parse_request(req_str);
5259

5360
if (req.valid) {

t/cgi/form.pl

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,18 @@ sub main {
3232
$output = "Top of $tod to you $name! It is very nice to meet you :)";
3333
}
3434

35-
# first print the cookie
36-
my $cookie;
37-
if ($name) {
38-
$cookie = bake_cookie($name);
35+
# prepare the cookie
36+
my $cookie = '';
37+
if ($cgi->param('reset')) {
38+
$cookie = drop_cookie();
39+
$output = "Goodbye $name :)";
3940
}
41+
else {
42+
$cookie = ($name ? bake_cookie($name) : '');
43+
}
44+
4045
# then the rest
41-
print_out($cookie, $output, $footer);
46+
printf get_template(), $cookie, $output, $footer;
4247

4348
return;
4449
}
@@ -50,37 +55,39 @@ sub fetch_from_cookie {
5055

5156
sub bake_cookie {
5257
my ($name) = @_;
53-
return "Set-Cookie: name=$name\n";
58+
return "Set-Cookie: name=$name; HttpOnly";
5459
}
5560

56-
sub print_out {
57-
my ($cookie, $output, $footer) = @_;
58-
59-
$cookie ||= '';
61+
sub drop_cookie {
62+
my ($name) = @_;
63+
return bake_cookie("deleted") . "; MaxAge=0; expires=Thu, 01 Jan 1970 00:00:00 GMT";
64+
}
6065

61-
print << "EOF";
66+
sub get_template {
67+
my $template = << "EOF";
6268
HTTP/1.x 200 OK
6369
Content-Type: text/html; charset=UTF-8;
64-
$cookie
70+
%s
6571
6672
<html>
6773
<head>
6874
<title>Created by form.pl</title>
6975
<link rel="stylesheet" type="text/css" href="/t/css/form.css">
7076
</head>
7177
<body>
72-
<h2>$output</h2>
78+
<h2>%s</h2>
7379
<p class="content">
7480
<img src="/t/images/hello.png" alt="hello"></img></br>
75-
<a href="/t/form.html">Back to the form</a>
76-
<a href="/t/cgi/form.pl">Show with no input params</a>
81+
<a href="/t/form.html">Back to the form</a><br/>
82+
<a href="/t/cgi/form.pl">Show with no input params</a><br/>
83+
<a href="/t/cgi/form.pl?reset=1">Drop cookie</a>
7784
</p>
78-
<p class="footer">$footer</p>
85+
<p class="footer">%s</p>
7986
</body>
8087
</html>
8188
EOF
8289

83-
return;
90+
return $template;
8491
}
8592

8693
main();

0 commit comments

Comments
 (0)