Skip to content

Commit ead47c0

Browse files
committed
FEAT: HTTPD server now have ON-ACCEPT actor which can be used to limit connection to specified IPs only
1 parent 7ba3dc2 commit ead47c0

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

src/modules/httpd.r3

+8-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ Rebol [
2020
TODO: {
2121
* support for multidomain serving using `Host` header field
2222
* add support for other methods - PUT, DELETE, TRACE, CONNECT, OPTIONS?
23-
* limit connections per IP
2423
* better error handling
2524
* standard access log
2625
* add list-dir action which shows content of a directory, if allowed
@@ -117,6 +116,7 @@ sys/make-scheme [
117116
close port/locals/subport
118117
]
119118

119+
On-Accept: func [ctx [object!]][ true ]
120120
On-Header: func [ctx [object!]][] ;= placeholder; user can use it for early request processing
121121

122122
On-Get: func [
@@ -495,8 +495,14 @@ sys/make-scheme [
495495

496496
New-Client: func[port [port!] /local client info err][
497497
client: first port
498-
client/awake: :Awake-Client
499498
info: query client
499+
unless Actor/On-Accept info [
500+
; connection not allowed
501+
sys/log/info 'HTTPD ["Client not accepted:^[[22m" info/remote-ip]
502+
close client
503+
return false
504+
]
505+
client/awake: :Awake-Client
500506
client/locals: make object! [
501507
state: none
502508
parent: port

src/tests/httpd-root/form.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</head>
77
<body>
88
<h1>Just a simple test form</h1>
9-
<form action="form.html" method="post">
9+
<form action="/result/" method="post">
1010
First name: <input type="text" name="fname"><br>
1111
Last name: <input type="text" name="lname"><br>
1212
<input type="submit" value="Submit">

src/tests/test-httpd.r3

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ do %../modules/httpd.r3
1919
system/options/log/httpd: 3 ; for verbose output
2020

2121
my-actor: object [
22+
On-Accept: func [info [object!]][
23+
; allow only connections from localhost
24+
; TRUE = accepted, FALSE = refuse
25+
find [ 127.0.0.1 ] info/remote-ip
26+
]
2227
On-Header: func [ctx [object!]][
2328
switch ctx/inp/target/file [
2429
%form/ [
@@ -34,7 +39,6 @@ my-actor: object [
3439
; request processing will stop with redirection response
3540
]
3641
]
37-
? ctx
3842
]
3943
On-Post-Received: func [ctx [object!]][
4044
ctx/out/header/Content-Type: "text/html; charset=UTF-8"

0 commit comments

Comments
 (0)