Skip to content

Commit 9f2548d

Browse files
committed
FIX: improved error handling in HTTPd (catches invalid requests)
1 parent 3d068ab commit 9f2548d

File tree

1 file changed

+37
-31
lines changed

1 file changed

+37
-31
lines changed

src/modules/httpd.r3

+37-31
Original file line numberDiff line numberDiff line change
@@ -547,41 +547,47 @@ sys/make-scheme [
547547
out/content: none
548548
]
549549

550-
if error? err: try [
550+
try/except [
551551
write port buffer
552552
][
553553
;@@TODO: handle it without `print`; using on-error?
554-
print "Write failed!"
555-
probe copy/part buffer 100
554+
print "** Write failed!"
555+
;probe copy/part buffer 100
556556
Awake-Client make event! [type: 'close port: port ]
557557
]
558558
]
559559

560560
Do-log: function [ctx][
561-
msg: ajoin [
562-
ctx/remote-ip
563-
{ - - [} to-CLF-idate now {] "}
564-
ctx/inp/method #" "
565-
to string! ctx/inp/target/original
566-
" HTTP/" ctx/inp/version {" }
567-
ctx/out/status #" "
568-
any [ctx/out/header/Content-Length #"-"]
569-
#"^/"
570-
]
571-
prin msg
572-
if file? ctx/config/log-access [
573-
try [write/append ctx/config/log-access msg]
574-
]
575-
if all [
576-
ctx/out/status >= 400
577-
file? ctx/config/log-errors
561+
try/except [
562+
msg: ajoin [
563+
ctx/remote-ip
564+
{ - - [} to-CLF-idate now {] "}
565+
ctx/inp/method #" "
566+
to string! ctx/inp/target/original
567+
" HTTP/" ctx/inp/version {" }
568+
ctx/out/status #" "
569+
any [ctx/out/header/Content-Length #"-"]
570+
#"^/"
571+
]
572+
prin msg
573+
if file? ctx/config/log-access [
574+
write/append ctx/config/log-access msg
575+
]
576+
if all [
577+
ctx/out/status >= 400
578+
file? ctx/config/log-errors
579+
][
580+
write/append ctx/config/log-errors msg
581+
]
578582
][
579-
try [write/append ctx/config/log-errors msg]
583+
print "** Failed to write a log"
584+
print system/state/last-error
580585
]
581586
]
582587

583588
Awake-Client: wrap [
584-
from-method: ["GET" | "POST" | "HEAD" | "PUT" | "DELETE" | "TRACE" | "CONNECT" | "OPTIONS"]
589+
chars-method: #[bitset! #{00000000000000007FFFFFE0}] ; #"A" - #"Z"
590+
;from-method: ["GET" | "POST" | "HEAD" | "PUT" | "DELETE" | "TRACE" | "CONNECT" | "OPTIONS"]
585591
chars: complement union space: charset " " charset [#"^@" - #"^_"]
586592
CRLF2BIN: #{0D0A0D0A}
587593

@@ -601,12 +607,12 @@ sys/make-scheme [
601607
READ [
602608
sys/log/more 'HTTPD ["bytes:^[[1m" length? port/data]
603609
either header-end: find/tail port/data CRLF2BIN [
604-
if error? err: try [
610+
try/except [
605611
if none? ctx/state [
606612
with inp [
607613
parse copy/part port/data header-end [
608-
copy method from-method some space
609-
copy target some chars some space
614+
copy method some chars-method some space
615+
copy target some chars some space
610616
"HTTP/" copy version some chars thru CRLF
611617
copy header to end
612618
(
@@ -630,7 +636,7 @@ sys/make-scheme [
630636
]
631637
actor/on-read port/locals
632638
][
633-
print err
639+
print system/state/last-error
634640
ctx/state: 'error
635641
ctx/out/status: 500 ; Internal Server Error
636642
]
@@ -659,10 +665,10 @@ sys/make-scheme [
659665
close out/content ; closing source port
660666
End-Client port
661667
][
662-
if error? err: try [
668+
try/except [
663669
write port buffer
664670
][
665-
print "Write failed (2)!"
671+
print "** Write failed (2)!"
666672
;probe buffer
667673
End-Client port
668674
]
@@ -740,9 +746,9 @@ sys/make-scheme [
740746
append port/locals/clients client
741747

742748
sys/log/info 'HTTPD ["New client:^[[1;31m" client/locals/remote]
743-
if error? err: try [read client][
744-
print ["Failed to read new client:" client/locals/remote]
745-
print err
749+
try/except [read client][
750+
print ["** Failed to read new client:" client/locals/remote]
751+
print system/state/last-error
746752
]
747753
]
748754

0 commit comments

Comments
 (0)