Skip to content

Commit 4844d68

Browse files
refactor: generate chunk sizes dynamically
Signed-off-by: Ar Rakin <rakinar2@onesoftnet.eu.org>
1 parent e1aac54 commit 4844d68

4 files changed

Lines changed: 88 additions & 42 deletions

File tree

res/index_end.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
%zx%c%c </tbody>
1+
</tbody>
22
<tfoot>
33
<tr>
44
<td colspan="4">
@@ -13,4 +13,3 @@
1313
</body>
1414
</html>
1515

16-
%c%c

res/index_start.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
%zx%c%c<!DOCTYPE html>
1+
<!DOCTYPE html>
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8" />
@@ -88,4 +88,4 @@ <h1>Index of %.*s</h1>
8888
</th>
8989
</tr>
9090
</thead>
91-
<tbody>%c%c
91+
<tbody>

src/modules/autoindex.c

Lines changed: 83 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,22 @@
1919
#include "autoindex.h"
2020
#include "log/log.h"
2121

22-
bool
23-
fh_autoindex_handle (struct fh_autoindex *autoindex)
22+
static struct fh_link end_chunk_link = {
23+
.is_eos = true,
24+
.is_start = false,
25+
.next = NULL,
26+
.buf = & (struct fh_buf) {
27+
.type = FH_BUF_DATA,
28+
.freeable = false,
29+
.attrs.mem.cap = 5,
30+
.attrs.mem.len = 5,
31+
.attrs.mem.rd_only = true,
32+
.attrs.mem.data = (uint8_t *) "0\r\n\r\n",
33+
},
34+
};
35+
36+
static bool
37+
fh_autoindex_handle_chunked (struct fh_autoindex *autoindex)
2438
{
2539
const struct fh_request *request = autoindex->request;
2640
struct fh_response *response = autoindex->response;
@@ -43,8 +57,8 @@ fh_autoindex_handle (struct fh_autoindex *autoindex)
4357
pool_t *pool = autoindex->response->pool;
4458
uint16_t port = autoindex->conn->extra->port;
4559
size_t index_start_chunk_len
46-
= resource_index_start_html_len - 23 + (3 * request->uri_len);
47-
size_t index_end_chunk_len = resource_index_end_html_len - 17
60+
= resource_index_start_html_len - 12 + (3 * request->uri_len);
61+
size_t index_end_chunk_len = resource_index_end_html_len - 6
4862
+ autoindex->conn->extra->host_len
4963
+ (port < 10 ? 1
5064
: port < 100 ? 2
@@ -56,12 +70,10 @@ fh_autoindex_handle (struct fh_autoindex *autoindex)
5670
size_t index_end_len = index_end_chunk_len + 32 + 4;
5771

5872
struct fh_link *end_link;
59-
struct fh_link *end_chunk_link;
60-
struct fh_link *start_link = fh_pool_alloc (
61-
pool,
62-
(sizeof (*start_link) + sizeof (*start_link->buf) + index_start_len + 1
63-
+ sizeof (*end_link) + sizeof (*end_link->buf) + index_end_len + 1
64-
+ sizeof (*end_chunk_link) + sizeof (*end_chunk_link->buf)));
73+
struct fh_link *start_link
74+
= fh_pool_alloc (pool, (sizeof (*start_link) + sizeof (*start_link->buf)
75+
+ index_start_len + 1 + sizeof (*end_link)
76+
+ sizeof (*end_link->buf) + index_end_len + 1));
6577

6678
if (!start_link)
6779
{
@@ -83,44 +95,57 @@ fh_autoindex_handle (struct fh_autoindex *autoindex)
8395
end_link->next = NULL;
8496
end_link->is_eos = false;
8597

86-
end_chunk_link = (struct fh_link *) (end_link->buf->attrs.mem.data
87-
+ index_end_len + 1);
88-
end_chunk_link->buf = (struct fh_buf *) (end_chunk_link + 1);
89-
end_chunk_link->buf->attrs.mem.data = (uint8_t *) "0\r\n\r\n";
90-
end_chunk_link->buf->type = FH_BUF_DATA;
91-
end_chunk_link->buf->attrs.mem.cap = end_chunk_link->buf->attrs.mem.len = 5;
92-
end_chunk_link->next = NULL;
93-
end_chunk_link->is_eos = true;
94-
end_chunk_link->buf->attrs.mem.rd_only = true;
95-
96-
int rc;
97-
98-
if ((rc = snprintf (
99-
(char *) start_link->buf->attrs.mem.data, index_start_len + 1,
100-
resource_index_start_html, index_start_chunk_len, '\r', '\n',
101-
(int) request->uri_len, request->uri, (int) request->uri_len,
102-
request->uri, (int) request->uri_len, request->uri, '\r', '\n'))
98+
end_chunk_link.buf->attrs.mem.data = (uint8_t *) "0\r\n\r\n";
99+
end_chunk_link.buf->attrs.mem.cap = end_chunk_link.buf->attrs.mem.len = 5;
100+
101+
int rc1, rc2;
102+
103+
if ((rc1 = snprintf ((char *) start_link->buf->attrs.mem.data, 34,
104+
"%zx\r\n", index_start_chunk_len))
105+
< 0)
106+
{
107+
response->status = FH_STATUS_INTERNAL_SERVER_ERROR;
108+
return true;
109+
}
110+
111+
if ((rc2 = snprintf ((char *) start_link->buf->attrs.mem.data + rc1,
112+
index_start_len + 1 - rc1, resource_index_start_html,
113+
(int) request->uri_len, request->uri,
114+
(int) request->uri_len, request->uri,
115+
(int) request->uri_len, request->uri))
103116
< 0)
104117
{
105118
response->status = FH_STATUS_INTERNAL_SERVER_ERROR;
106119
return true;
107120
}
108121

122+
start_link->buf->attrs.mem.data[rc1 + rc2] = '\r';
123+
start_link->buf->attrs.mem.data[rc1 + rc2 + 1] = '\n';
109124
start_link->buf->attrs.mem.cap = start_link->buf->attrs.mem.len
110-
= (size_t) rc;
125+
= (size_t) (rc1 + rc2 + 2);
126+
127+
if ((rc1 = snprintf ((char *) end_link->buf->attrs.mem.data, 34, "%zx\r\n",
128+
index_end_chunk_len))
129+
< 0)
130+
{
131+
response->status = FH_STATUS_INTERNAL_SERVER_ERROR;
132+
return true;
133+
}
111134

112-
if ((rc
113-
= snprintf ((char *) end_link->buf->attrs.mem.data, index_end_len + 1,
114-
resource_index_end_html, index_end_chunk_len, '\r', '\n',
115-
(int) autoindex->conn->extra->host_len,
116-
autoindex->conn->extra->host, port, '\r', '\n'))
135+
if ((rc2 = snprintf ((char *) end_link->buf->attrs.mem.data + rc1,
136+
index_end_len + 1 - rc1, resource_index_end_html,
137+
(int) autoindex->conn->extra->host_len,
138+
autoindex->conn->extra->host, port))
117139
< 0)
118140
{
119141
response->status = FH_STATUS_INTERNAL_SERVER_ERROR;
120142
return true;
121143
}
122144

123-
end_link->buf->attrs.mem.cap = end_link->buf->attrs.mem.len = (size_t) rc;
145+
end_link->buf->attrs.mem.data[rc1 + rc2] = '\r';
146+
end_link->buf->attrs.mem.data[rc1 + rc2 + 1] = '\n';
147+
end_link->buf->attrs.mem.cap = end_link->buf->attrs.mem.len
148+
= (size_t) (rc1 + rc2 + 2);
124149

125150
struct dirent64 **namelist;
126151
int namelist_count = 0;
@@ -309,8 +334,8 @@ fh_autoindex_handle (struct fh_autoindex *autoindex)
309334
tail->next = end_link;
310335
tail = end_link;
311336

312-
tail->next = end_chunk_link;
313-
tail = end_chunk_link;
337+
tail->next = &end_chunk_link;
338+
tail = &end_chunk_link;
314339

315340
response->encoding = FH_ENCODING_CHUNKED;
316341
response->body_start = start_link;
@@ -320,3 +345,25 @@ fh_autoindex_handle (struct fh_autoindex *autoindex)
320345
fh_pr_debug ("Response generated successfully");
321346
return true;
322347
}
348+
349+
static bool
350+
fh_autoindex_handle_plain (struct fh_autoindex *autoindex)
351+
{
352+
const struct fh_request *request = autoindex->request;
353+
struct fh_response *response = autoindex->response;
354+
355+
return true;
356+
}
357+
358+
bool
359+
fh_autoindex_handle (struct fh_autoindex *autoindex)
360+
{
361+
switch (autoindex->request->protocol)
362+
{
363+
case FH_PROTOCOL_HTTP_1_0:
364+
return fh_autoindex_handle_plain (autoindex);
365+
366+
default:
367+
return fh_autoindex_handle_chunked (autoindex);
368+
}
369+
}

tests/valgrind-benchmark.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ valgrind \
3535

3636
pid=$!
3737

38-
trap 'kill -0 $pid > /dev/null 2>&1 && kill -9 $pid && ps aux | grep freehttpd | awk "{ print \$2 }" | xargs kill -9' EXIT
38+
trap 'kill -0 $pid > /dev/null 2>&1 && kill -9 $pid && ps aux | grep valgrind | awk "{ print \$2 }" | xargs kill -9' EXIT
3939
trap "exit 1" INT TERM
4040

4141
echo "Starting stress test with Valgrind in 2 seconds..."
@@ -51,7 +51,7 @@ fi
5151
echo "Benchmark completed successfully"
5252

5353
kill -9 $pid
54-
ps aux | grep freehttpd | awk "{ print \$2 }" | xargs kill -9
54+
ps aux | grep valgrind | awk "{ print \$2 }" | xargs kill -9
5555
wait $pid
5656

5757
echo "Valgrind completed successfully"

0 commit comments

Comments
 (0)