Skip to content

Commit 29f1977

Browse files
committed
#48 upload limit
1 parent fabe7aa commit 29f1977

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ it is only needed if you want to change default settings. it has the following p
4444
- `type_dynamics`: dynamic file types, see [api](#api)
4545
- `type_mimes`: file type to mime type map (most common are already set, only overrides)
4646
- `type_raws`: array of file types that are sent uncompressed
47+
- `upload_limit`: max upload size in mebibytes, default is 10
4748
- `zstd_level`: compression level for zstd, 0-19, default is 3
4849

4950
here is an example for a customized setup:

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rtjscomp",
3-
"version": "0.9.7",
3+
"version": "0.9.8",
44
"description": "php-like server but with javascript",
55
"repository": {
66
"type": "git",

rtjscomp.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const log_verbose_flag = process.argv.includes('-v');
5151
let log_verbose = log_verbose_flag;
5252
let port_http = 0;
5353
let port_https = 0;
54+
let upload_limit = 10 * 1024 * 1024;
5455
let compression_enabled = true;
5556
let exiting = false;
5657
/// any path -> file
@@ -1063,6 +1064,17 @@ const request_handle = async (request, response, https) => {
10631064
) {
10641065
if (!file_dyn_enabled) throw 405;
10651066
if ('content-length' in request_headers) {
1067+
const content_length = Number(request_headers['content-length']);
1068+
if (
1069+
isNaN(content_length) ||
1070+
content_length < 0 ||
1071+
content_length % 1 > 0
1072+
) {
1073+
throw 400;
1074+
}
1075+
if (content_length > upload_limit) {
1076+
throw 413;
1077+
}
10661078
request_body_promise = new Promise(resolve => {
10671079
const request_body_chunks = [];
10681080
request.on('data', chunk => {
@@ -1539,6 +1551,9 @@ const request_handle = async (request, response, https) => {
15391551
http.STATUS_CODES[err] || 'Error'
15401552
}</h1></body></html>`);
15411553
}
1554+
if ('content-length' in request_headers) {
1555+
request.socket.destroy();
1556+
}
15421557
}
15431558
}
15441559

@@ -1821,6 +1836,7 @@ await file_keep_new('rtjscomp.json', data => {
18211836
const type_dynamics_new = get_prop_list(data, 'type_dynamics');
18221837
const type_mimes_new = get_prop_map(data, 'type_mimes');
18231838
const type_raws_new = get_prop_list(data, 'type_raws');
1839+
const upload_limit_new = get_prop_uint(data, 'upload_limit', 10);
18241840
const zstd_level_new = get_prop_uint(data, 'zstd_level', 3);
18251841

18261842
if (data) {
@@ -1832,20 +1848,21 @@ await file_keep_new('rtjscomp.json', data => {
18321848
if (gzip_level_new > 9) {
18331849
throw 'gzip_level > 9';
18341850
}
1835-
if (zstd_level_new > 19) {
1836-
throw 'zstd_level > 19';
1837-
}
18381851
if (
18391852
port_http_new > 65535 ||
18401853
port_https_new > 65535
18411854
) {
18421855
throw 'port > 65535';
18431856
}
1857+
if (zstd_level_new > 19) {
1858+
throw 'zstd_level > 19';
1859+
}
18441860

18451861
compression_enabled = compression_enabled_new;
18461862
GZIP_OPTIONS.level = compression_enabled ? gzip_level_new : 0;
18471863
ZSTD_OPTIONS.params[ZSTD_c_compressionLevel] = zstd_level_new;
18481864
log_verbose = log_verbose_new;
1865+
upload_limit = upload_limit_new * 1024 * 1024;
18491866
if (path_ghosts_new) {
18501867
path_ghosts.clear();
18511868
for (const key of path_ghosts_new) {

0 commit comments

Comments
 (0)