Skip to content

Commit 3915fd6

Browse files
committed
#44 #45 abort on HEAD, remove bot+mobil
1 parent 693b70d commit 3915fd6

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

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.4",
3+
"version": "0.9.5",
44
"description": "php-like server but with javascript",
55
"repository": {
66
"type": "git",

rtjscomp.js

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ const querystring_parse = require('querystring').decode;
2020
const request_ip_get = require('ipware')().get_ip;
2121

2222
// constants
23-
const AGENT_CHECK_BOT = /bot|googlebot|crawler|spider|robot|crawling|favicon/i;
24-
const AGENT_CHECK_MOBIL = /(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i;
2523
const GZIP_OPTIONS = {level: 9};
2624
const HTTP_LIST_REG = /,\s*/;
2725
const IMPORT_REG = /\bimport\(/g;
@@ -778,6 +776,7 @@ const request_handle = async (request, response, https) => {
778776
const request_method = request.method;
779777
const request_headers = request.headers;
780778
const request_ip = request_ip_get(request).clientIp;
779+
const request_method_head = request_method === 'HEAD';
781780

782781
if ('x-forwarded-proto' in request_headers) {
783782
https = request_headers['x-forwarded-proto'] === 'https';
@@ -866,6 +865,7 @@ const request_handle = async (request, response, https) => {
866865

867866
let file_gz_enabled = (
868867
gz_enabled &&
868+
!request_method_head &&
869869
'accept-encoding' in request_headers &&
870870
!type_raws.has(file_type) &&
871871
request_headers['accept-encoding'].split(HTTP_LIST_REG).includes('gzip')
@@ -876,7 +876,10 @@ const request_handle = async (request, response, https) => {
876876
!path_statics.has(path)
877877
);
878878

879-
if (request_method !== 'GET') {
879+
if (
880+
!request_method_head &&
881+
request_method !== 'GET'
882+
) {
880883
if (!file_dyn_enabled) throw 405;
881884
if ('content-length' in request_headers) {
882885
request_body_promise = new Promise(resolve => {
@@ -923,6 +926,12 @@ const request_handle = async (request, response, https) => {
923926
if (file_content.includes('\r')) {
924927
throw 'illegal line break, must be unix';
925928
}
929+
if (
930+
file_content.includes('response.write(') ||
931+
file_content.includes('response.end(')
932+
) {
933+
throw 'response.write() not allowed, use output.write()';
934+
}
926935
if (file_content.includes('globals.')) {
927936
log(`[deprecated] ${path}: uses globals object`);
928937
}
@@ -1120,14 +1129,15 @@ const request_handle = async (request, response, https) => {
11201129
}
11211130
}
11221131

1123-
const request_headers_user_agent = file_function_input['user_agent'] = request_headers['user-agent'];
1124-
file_function_input['bot'] = AGENT_CHECK_BOT.test(request_headers_user_agent);
1125-
file_function_input['mobil'] = AGENT_CHECK_MOBIL.test(request_headers_user_agent);
1126-
11271132
file_function_input['https'] = https;
11281133
file_function_input['ip'] = request_ip;
1129-
file_function_input['method'] = request_method.toLowerCase();
1134+
file_function_input['method'] = (
1135+
request_method_head
1136+
? 'get'
1137+
: request_method.toLowerCase()
1138+
);
11301139
file_function_input['path'] = request_url_parsed.pathname;
1140+
file_function_input['user_agent'] = request_headers['user-agent'];
11311141

11321142
let file_function_output;
11331143
response.setHeader('Cache-Control', 'no-cache, no-store');
@@ -1155,6 +1165,13 @@ const request_handle = async (request, response, https) => {
11551165
)
11561166
]);
11571167

1168+
if (request_method_head) {
1169+
file_function_output.write =
1170+
file_function_output.end = () => {
1171+
throw null;
1172+
}
1173+
}
1174+
11581175
await services_loaded_promise;
11591176

11601177
let returned;
@@ -1183,11 +1200,15 @@ const request_handle = async (request, response, https) => {
11831200
log(`[deprecated] ${path}: status code thrown, use return instead`);
11841201
returned = err;
11851202
}
1186-
else {
1203+
else if (err !== null) {
11871204
log(`[error] ${path}: invalid throw type: ${typeof err}`);
11881205
returned = 500;
11891206
}
11901207
}
1208+
if (request_method_head) {
1209+
delete file_function_output.write;
1210+
delete file_function_output.end;
1211+
}
11911212
if (returned != null) {
11921213
if (response.headersSent) {
11931214
if (
@@ -1244,7 +1265,12 @@ const request_handle = async (request, response, https) => {
12441265
response.setHeader('Content-Length', file_stat.size);
12451266
}
12461267

1247-
file_data.pipe(response);
1268+
if (request_method_head) {
1269+
response.end();
1270+
}
1271+
else {
1272+
file_data.pipe(response);
1273+
}
12481274
}
12491275
}
12501276
catch (err) {

0 commit comments

Comments
 (0)