2017-02-12 14:16:49 +11:00
|
|
|
\ 1991
|
2017-02-02 14:26:33 +11:00
|
|
|
|
2017-02-12 14:16:49 +11:00
|
|
|
include unix/socket.fs
|
|
|
|
|
2017-02-16 15:13:46 +11:00
|
|
|
\ Helper words
|
|
|
|
: +s ( addr1 u1 addr2 u2 -- addr3 u3 ) \ like s+ but prepend rather than append.
|
|
|
|
2swap s+ ;
|
|
|
|
: exchange ( a1 a2 -- )
|
|
|
|
2dup c@ swap c@ rot c! swap c! ;
|
|
|
|
: reverse ( caddr u -- ) \ reverse a string
|
|
|
|
1- bounds begin 2dup > while
|
|
|
|
2dup exchange
|
|
|
|
-1 /string
|
|
|
|
repeat 2drop ;
|
2017-02-18 14:46:43 +11:00
|
|
|
: sourcedir ( -- saddr su )
|
|
|
|
\ Returns the directory in which the file
|
|
|
|
\ invoking the word finds itself
|
|
|
|
\ relative to gforth's execution directory.
|
|
|
|
\ Useful for specifying in which dir to find
|
|
|
|
\ specific files (e.g., public/, views/).
|
|
|
|
sourcefilename \ get the name of our file
|
2017-02-19 01:46:33 +11:00
|
|
|
pad dup >r place \ copy the string so we don't
|
|
|
|
r> count \ modify sourcefilename.
|
2017-02-18 14:46:43 +11:00
|
|
|
2dup reverse \ reverse and search for first /
|
|
|
|
s" /" search if \ if found, reverse string to
|
2017-02-19 01:46:33 +11:00
|
|
|
2dup reverse \ strip the filename but keep dir.
|
2017-02-18 14:46:43 +11:00
|
|
|
else
|
|
|
|
2drop \ no slash,
|
|
|
|
s" ./" \ same dir execution.
|
|
|
|
then ;
|
2017-02-16 15:13:46 +11:00
|
|
|
|
2017-02-14 14:32:00 +11: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
|
2017-02-16 15:13:46 +11:00
|
|
|
routes get-current >r set-current \ switch definition word lists
|
2017-02-14 14:32:00 +11:00
|
|
|
nextname create ,
|
|
|
|
r> set-current
|
|
|
|
then ;
|
|
|
|
|
2017-02-16 04:22:38 +11: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-18 14:46:43 +11:00
|
|
|
sourcedir s" public" s+ set-public-path
|
|
|
|
|
|
|
|
\ Views directory
|
|
|
|
pubvar views
|
|
|
|
: set-view-path ( addr u -- )
|
|
|
|
views 2! ;
|
|
|
|
: get-view-path ( -- addr u )
|
|
|
|
views 2@ ;
|
|
|
|
sourcedir s" views" s+ set-view-path
|
2017-02-14 14:32:00 +11:00
|
|
|
|
2017-02-18 01:10:06 +11:00
|
|
|
\ Query params
|
|
|
|
pubvar queryString
|
|
|
|
: set-query-string ( addr u -- )
|
|
|
|
queryString 2! ;
|
|
|
|
: get-query-string ( -- addr u )
|
|
|
|
queryString 2@ ;
|
|
|
|
|
2017-02-16 15:13:46 +11:00
|
|
|
\ Request's Content-Type
|
|
|
|
pubvar RequestContentType
|
|
|
|
: set-content-type ( addr u -- )
|
|
|
|
RequestContentType 2! ;
|
|
|
|
: get-content-type ( -- addr u )
|
|
|
|
RequestContentType 2@ ;
|
|
|
|
|
2017-02-17 15:03:37 +11:00
|
|
|
: filetype: ( addr u "extension" -- ) \ takes a content-type and the extension
|
|
|
|
create here over 1+ allot place
|
|
|
|
does> count ;
|
|
|
|
|
|
|
|
: get-filetype ( addr u -- caddr cu ) \ takes an extension, looks to find a definition
|
|
|
|
find-name dup if
|
|
|
|
name>int
|
|
|
|
execute
|
2017-02-16 15:13:46 +11:00
|
|
|
else
|
2017-02-17 15:03:37 +11:00
|
|
|
drop
|
2017-02-16 15:13:46 +11:00
|
|
|
s" text/plain"
|
|
|
|
then ;
|
|
|
|
|
2017-02-17 15:03:37 +11:00
|
|
|
s" text/plain" filetype: txt \ txt should always be defined
|
|
|
|
s" text/html" filetype: html
|
|
|
|
s" text/css" filetype: css
|
|
|
|
s" text/javascript" filetype: js
|
|
|
|
s" image/png" filetype: png
|
|
|
|
s" image/gif" filetype: gif
|
|
|
|
s" image/jpeg" filetype: jpg
|
|
|
|
s" image/jpeg" filetype: jpeg
|
|
|
|
s" image/x-icon" filetype: ico
|
|
|
|
|
|
|
|
|
2017-02-14 14:32:00 +11:00
|
|
|
\ Internal request handling
|
2017-02-16 15:13:46 +11:00
|
|
|
: HTTP/1.1 s" HTTP/1.1 " ;
|
|
|
|
|
|
|
|
: response-status ( u -- addr u )
|
|
|
|
dup case \ get status code info
|
|
|
|
200 of s" OK" endof
|
|
|
|
404 of s" Not Found" endof
|
|
|
|
endcase
|
|
|
|
s\" \n" s+
|
|
|
|
rot s>d <# #s #> +s \ convert status code to string and prepend to info
|
|
|
|
HTTP/1.1 +s ; \ prepend HTTP/1.1
|
|
|
|
|
|
|
|
: content-type ( addr u -- caddr cu )
|
|
|
|
s" Content-Type: " +s \ Prepend to the provided content type
|
|
|
|
s\" \n\n" s+ ; \ Append 2 new lines
|
|
|
|
|
|
|
|
: set-header ( u addr u -- raddr ru ) \ Accepts status code and content type string
|
|
|
|
rot response-status \ Set response status
|
|
|
|
2swap content-type \ Set content-type
|
|
|
|
s+ ; \ Join
|
|
|
|
|
|
|
|
: read-request ( socket -- addr u )
|
|
|
|
pad 4096 read-socket ;
|
2017-02-12 14:16:49 +11:00
|
|
|
|
|
|
|
: send-response ( addr u socket -- )
|
2017-02-15 13:36:17 +11:00
|
|
|
dup >r write-socket r> close-socket ;
|
|
|
|
|
2017-02-18 01:10:06 +11:00
|
|
|
: store-query-string ( addr u -- raddr ru )
|
|
|
|
2dup s" ?" search if
|
|
|
|
2dup set-query-string \ store query string
|
|
|
|
swap drop -
|
|
|
|
else
|
|
|
|
s" " set-query-string \ store empty query string (reset)
|
|
|
|
2drop
|
|
|
|
then ;
|
|
|
|
|
|
|
|
: requested-route ( addr u -- raddr ru )
|
|
|
|
bl scan 1- swap 1+ swap
|
|
|
|
2dup bl scan swap drop - \ get the space-separated route
|
|
|
|
store-query-string ; \ strip and store the query, leave route
|
2017-02-15 13:36:17 +11:00
|
|
|
|
2017-02-16 04:22:38 +11:00
|
|
|
: file-exists? ( addr u -- addr u bool )
|
|
|
|
2dup file-status nip 0= ;
|
|
|
|
|
2017-02-16 15:13:46 +11:00
|
|
|
: .extension ( addr u -- addr u )
|
|
|
|
2dup reverse \ reverse the file name
|
|
|
|
2dup s" ." search \ search for the first occurance of "."
|
|
|
|
if
|
|
|
|
swap drop - \ remove the "." from the search results
|
|
|
|
else
|
|
|
|
s" txt"
|
|
|
|
then
|
|
|
|
2dup reverse ; \ reverse reversed extension
|
|
|
|
|
|
|
|
: serve-file-type ( addr u -- )
|
2017-02-17 15:03:37 +11:00
|
|
|
.extension get-filetype set-content-type ;
|
2017-02-16 15:13:46 +11:00
|
|
|
|
2017-02-16 04:22:38 +11:00
|
|
|
: serve-file ( addr u -- addr u )
|
|
|
|
slurp-file ;
|
|
|
|
|
2017-02-17 15:03:37 +11:00
|
|
|
: 404content-type txt ;
|
2017-02-16 15:13:46 +11:00
|
|
|
: 404html s" 404";
|
|
|
|
|
2017-02-15 13:36:17 +11:00
|
|
|
: either-resolve ( addr u -- resolveaddr resolveu )
|
|
|
|
s" GET" search if
|
2017-02-17 15:03:37 +11:00
|
|
|
s" html" get-filetype set-content-type \ reset the request's content-type
|
2017-02-15 13:36:17 +11:00
|
|
|
requested-route
|
2017-02-16 04:22:38 +11:00
|
|
|
2dup find-route dup if
|
2017-02-16 15:13:46 +11:00
|
|
|
>r 2drop r> \ keep xt, drop the route string
|
|
|
|
execute \ execute the user's route handler
|
2017-02-15 13:36:17 +11:00
|
|
|
else
|
2017-02-16 15:13:46 +11:00
|
|
|
drop \ drop the null xt
|
|
|
|
get-public-path +s \ see if route exists in public dir
|
2017-02-16 04:22:38 +11:00
|
|
|
file-exists? if
|
2017-02-16 15:13:46 +11:00
|
|
|
2dup serve-file \ collect file contents
|
|
|
|
2swap serve-file-type \ set the file type
|
2017-02-16 04:22:38 +11:00
|
|
|
else
|
2017-02-16 15:13:46 +11:00
|
|
|
exit \ continue to 404
|
2017-02-16 04:22:38 +11:00
|
|
|
then
|
2017-02-15 13:36:17 +11:00
|
|
|
then
|
2017-02-16 15:13:46 +11:00
|
|
|
200 get-content-type set-header +s
|
|
|
|
rdrop exit then ;
|
2017-02-15 13:36:17 +11:00
|
|
|
|
2017-02-16 04:22:38 +11:00
|
|
|
: or-404 ( addr u -- 404addr 404u )
|
|
|
|
2drop
|
2017-02-16 15:13:46 +11:00
|
|
|
404 404content-type set-header 404html s+ ;
|
2017-02-16 04:22:38 +11:00
|
|
|
|
2017-02-15 13:36:17 +11:00
|
|
|
: prepare-response ( addr u -- returnaddr returnu)
|
|
|
|
either-resolve or-404 ;
|
2017-02-12 14:16:49 +11:00
|
|
|
|
|
|
|
: start-server { server client }
|
|
|
|
begin
|
|
|
|
server 255 listen
|
|
|
|
server accept-socket to client
|
|
|
|
|
2017-02-15 13:36:17 +11:00
|
|
|
client read-request prepare-response client send-response
|
2017-02-12 14:16:49 +11:00
|
|
|
again ;
|
|
|
|
|
2017-02-14 14:32:00 +11:00
|
|
|
|
|
|
|
\ Userland
|
|
|
|
: 1991: ( port -- )
|
|
|
|
create-server 0 start-server ;
|
2017-02-15 13:36:17 +11:00
|
|
|
: /1991 ( "<path> <word>" -- )
|
2017-02-14 14:32:00 +11:00
|
|
|
bl word ' swap count register-route ;
|