Skip to content

Commit 755e8e9

Browse files
committed
FEAT: HTTPD - Added possibility to stop server and return data from client (useful for OAuth2)
1 parent 8b2def7 commit 755e8e9

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

src/modules/httpd.r3

+29-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
Rebol [
22
Title: "HTTPD Scheme"
3-
Date: 10-May-2020
3+
Date: 02-Jul-2020
44
Author: ["Andreas Bolka" "Christopher Ross-Gill" "Oldes"]
55
File: %httpd.r3
66
Name: 'httpd
77
Type: 'module
8-
Version: 0.5.0
8+
Version: 0.6.0
99
Exports: [http-server decode-target to-CLF-idate]
1010
Rights: http://opensource.org/licenses/Apache-2.0
1111
Purpose: {
@@ -16,6 +16,7 @@ Rebol [
1616
* using _actors_ for main actions which may be customized
1717
* implemented `keep-alive` behaviour
1818
* sends `Not modified` response if file was not modified in given time
19+
* client can stop server
1920
}
2021
TODO: {
2122
* support for multidomain serving using `Host` header field
@@ -31,6 +32,7 @@ Rebol [
3132
https://gist.github.com/rgchris/73510e7d643eb0a6b9fa69b849cd9880}
3233
01-Apr-2019 "Oldes" {Rewritten to be usable in real life situations.}
3334
10-May-2020 "Oldes" {Implemented directory listing, logging and multipart POST processing}
35+
02-Jul-2020 "Oldes" {Added possibility to stop server and return data from client (useful for OAuth2)}
3436
]
3537
]
3638

@@ -570,14 +572,14 @@ sys/make-scheme [
570572
#"^/"
571573
]
572574
prin msg
573-
if file? ctx/config/log-access [
574-
write/append ctx/config/log-access msg
575+
if file? file: select ctx/config 'log-access [
576+
write/append file msg
575577
]
576578
if all [
577579
ctx/out/status >= 400
578-
file? ctx/config/log-errors
580+
file? file: select ctx/config 'log-errors
579581
][
580-
write/append ctx/config/log-errors msg
582+
write/append file msg
581583
]
582584
][
583585
print "** Failed to write a log"
@@ -701,9 +703,12 @@ sys/make-scheme [
701703
sys/log/debug 'HTTPD ["Awake (server):^[[22m" event/type]
702704
switch event/type [
703705
ACCEPT [ New-Client event/port ]
704-
CLOSE [ ]
706+
CLOSE [
707+
close event/port
708+
close event/port/locals/parent
709+
]
705710
]
706-
false
711+
true
707712
]
708713

709714

@@ -739,6 +744,7 @@ sys/make-scheme [
739744
]
740745
config: none
741746
timeout: none
747+
done?: none
742748
requests: 0 ; number of already served requests per connection
743749
]
744750
;? port
@@ -778,6 +784,11 @@ sys/make-scheme [
778784
;try [remove find clients port]
779785
]
780786
sys/log/debug 'HTTPD ["Ports open:" length? clients]
787+
if all [ctx/done? zero? length? clients][
788+
sys/log/info 'HTTPD "Server's job done, closing initiated"
789+
ctx/parent/data: ctx/done?
790+
Awake-Server make event! [type: 'CLOSE port: ctx/parent]
791+
]
781792
]
782793

783794
Check-Clients: func[
@@ -821,15 +832,21 @@ http-server: function [
821832
append server/locals/config spec
822833
]
823834

824-
? server/locals/config
835+
unless system/options/quiet [
836+
? server/locals/config
837+
]
825838

826839
if actor [
827-
if object? actions [ actions: body-of actions ]
828-
append server/actor actions
840+
append server/actor either block? actions [
841+
reduce/no-set actions
842+
][ body-of actions ]
829843
]
830844
unless no-wait [
831845
forever [
832-
wait [server 15]
846+
p: wait [server server/locals/subport 15]
847+
if all [port? p not open? p] [
848+
return p/data
849+
]
833850
server/scheme/Check-Clients server
834851
]
835852
]

0 commit comments

Comments
 (0)