2017-02-11 22:16:49 -05:00
|
|
|
\ 1991
|
2017-02-01 22:26:33 -05:00
|
|
|
|
2017-02-11 22:16:49 -05:00
|
|
|
include unix/socket.fs
|
|
|
|
|
2017-02-13 22:32:00 -05:00
|
|
|
\ User-defined routing
|
|
|
|
wordlist constant routes
|
|
|
|
: find-route ( addr u -- data )
|
|
|
|
routes search-wordlist if
|
|
|
|
>body @
|
|
|
|
else 0 then ;
|
|
|
|
: register-route ( data addr u -- )
|
|
|
|
2dup routes search-wordlist if
|
|
|
|
routes drop nip nip
|
|
|
|
>body !
|
|
|
|
else
|
|
|
|
routes get-current >r set-current \ switch definition word lists
|
|
|
|
nextname create ,
|
|
|
|
r> set-current
|
|
|
|
then ;
|
|
|
|
|
2017-02-15 12:22:38 -05:00
|
|
|
\ Public directory
|
|
|
|
: pubvar create 0 , 0 , ;
|
|
|
|
pubvar public
|
|
|
|
: set-public-path ( addr u -- )
|
|
|
|
public 2! ;
|
|
|
|
: get-public-path ( -- addr u )
|
|
|
|
public 2@ ;
|
2017-02-13 22:32:00 -05:00
|
|
|
|
|
|
|
\ Internal request handling
|
2017-02-11 22:16:49 -05:00
|
|
|
: read-request ( socket -- addr u ) pad 4096 read-socket ;
|
|
|
|
|
|
|
|
: send-response ( addr u socket -- )
|
2017-02-14 21:36:17 -05:00
|
|
|
dup >r write-socket r> close-socket ;
|
|
|
|
|
|
|
|
: requested-route ( addr u -- routeaddr routeu )
|
|
|
|
bl scan 1- swap 1+ swap 2dup bl scan swap drop - ;
|
|
|
|
|
2017-02-15 12:22:38 -05:00
|
|
|
: file-exists? ( addr u -- addr u bool )
|
|
|
|
2dup file-status nip 0= ;
|
|
|
|
|
|
|
|
: serve-file ( addr u -- addr u )
|
|
|
|
slurp-file ;
|
|
|
|
|
2017-02-14 21:36:17 -05:00
|
|
|
: either-resolve ( addr u -- resolveaddr resolveu )
|
|
|
|
s" GET" search if
|
|
|
|
requested-route
|
2017-02-15 12:22:38 -05:00
|
|
|
2dup find-route dup if
|
|
|
|
>r 2drop r> \ keep xt, drop the route string
|
|
|
|
execute \ execute the user's route handler
|
2017-02-14 21:36:17 -05:00
|
|
|
else
|
2017-02-15 12:22:38 -05:00
|
|
|
drop \ drop the xt
|
|
|
|
get-public-path 2swap s+ \ see if route exists in public dir
|
|
|
|
file-exists? if
|
|
|
|
serve-file \ collect file contents
|
|
|
|
else
|
|
|
|
exit \ continue to 404
|
|
|
|
then
|
2017-02-14 21:36:17 -05:00
|
|
|
then
|
|
|
|
s\" HTTP/1.1 200 OK\n Content-Type: text/html\n\n" 2swap s+
|
|
|
|
rdrop exit then ;
|
|
|
|
|
2017-02-15 12:22:38 -05:00
|
|
|
: or-404 ( addr u -- 404addr 404u )
|
|
|
|
2drop
|
|
|
|
s\" HTTP/1.1 404 Not Found\n Content-Type: text/plain\n\n 404" ;
|
|
|
|
|
2017-02-14 21:36:17 -05:00
|
|
|
: prepare-response ( addr u -- returnaddr returnu)
|
|
|
|
either-resolve or-404 ;
|
2017-02-11 22:16:49 -05:00
|
|
|
|
|
|
|
: start-server { server client }
|
|
|
|
begin
|
|
|
|
server 255 listen
|
|
|
|
server accept-socket to client
|
|
|
|
|
2017-02-14 21:36:17 -05:00
|
|
|
client read-request prepare-response client send-response
|
2017-02-11 22:16:49 -05:00
|
|
|
again ;
|
|
|
|
|
2017-02-13 22:32:00 -05:00
|
|
|
|
|
|
|
\ Userland
|
|
|
|
: 1991: ( port -- )
|
|
|
|
create-server 0 start-server ;
|
2017-02-14 21:36:17 -05:00
|
|
|
: /1991 ( "<path> <word>" -- )
|
2017-02-13 22:32:00 -05:00
|
|
|
bl word ' swap count register-route ;
|
2017-02-11 22:16:49 -05:00
|
|
|
|
|
|
|
|
|
|
|
\ App demo:
|
2017-02-14 21:36:17 -05:00
|
|
|
: handle-/ s" fff" ;
|
|
|
|
: handle-hi s" hi!" ;
|
2017-02-11 22:16:49 -05:00
|
|
|
|
2017-02-14 21:36:17 -05:00
|
|
|
/1991 / handle-/
|
|
|
|
/1991 /hi handle-hi
|
2017-02-11 22:16:49 -05:00
|
|
|
|
2017-02-14 21:36:17 -05:00
|
|
|
8080 1991:
|