gopher: add way to get result without converting to objects.

db4
John Benediktsson 2014-12-20 11:28:23 -08:00
parent 3709749ea1
commit 12050a0db9
1 changed files with 36 additions and 32 deletions

View File

@ -38,19 +38,29 @@ CONSTANT: A_PLUS_IMAGE CHAR: :
CONSTANT: A_PLUS_MOVIE CHAR: ; CONSTANT: A_PLUS_MOVIE CHAR: ;
CONSTANT: A_PLUS_SOUND CHAR: < CONSTANT: A_PLUS_SOUND CHAR: <
: get-binary ( selector -- binary ) : gopher-get ( selector -- item-type byte-array )
"/" split1 "" or
[ dup length 1 > [ string>number ] [ first ] if ]
[
"?" split1 [ "\t" glue ] when*
"\r\n" append utf8 encode write flush "\r\n" append utf8 encode write flush
input-stream get (stream-contents-by-block) ;
: get-gif ( selector -- image )
get-binary "gif" (image-class) load-image* ;
: get-text ( selector -- lines )
"?" split1 [ "\t" glue ] when* "\r\n" append
utf8 encode write flush
input-stream get (stream-contents-by-block) input-stream get (stream-contents-by-block)
utf8 decode string-lines ] bi* ;
"." over index [ head ] when* ;
PRIVATE>
ERROR: not-a-gopher-url url ;
: gopher* ( url -- item-type byte-array )
dup url? [ >url ] unless
dup protocol>> "gopher" = [ not-a-gopher-url ] unless {
[ host>> ]
[ port>> 70 or <inet> binary ]
[ path>> rest [ "1/" ] when-empty ]
[ query>> [ assoc>query url-decode "?" glue ] when* ]
} cleave '[ _ gopher-get ] with-client ;
<PRIVATE
TUPLE: gopher-link type name selector host port ; TUPLE: gopher-link type name selector host port ;
@ -70,32 +80,26 @@ M: gopher-link >url
} cleave "gopher://%s:%s/%s%s" sprintf } cleave "gopher://%s:%s/%s%s" sprintf
] if >url ; ] if >url ;
: get-menu ( selector -- lines ) : gopher-text ( object -- lines )
get-text [ <gopher-link> ] map ; utf8 decode string-lines
"." over index [ head ] when* ;
: get-selector ( selector -- stuff ) : gopher-gif ( object -- image )
"/" split1 "" or swap "gif" (image-class) load-image* ;
dup length 1 > [ string>number ] [ first ] if
{ : gopher-menu ( object -- links )
{ A_TEXT [ get-text ] } gopher-text [ <gopher-link> ] map ;
{ A_MENU [ get-menu ] }
{ A_INDEX [ get-menu ] }
{ A_GIF [ get-gif ] }
[ drop get-binary ]
} case ;
PRIVATE> PRIVATE>
ERROR: not-a-gopher-url url ;
: gopher ( url -- object ) : gopher ( url -- object )
dup url? [ >url ] unless gopher* swap {
dup protocol>> "gopher" = [ not-a-gopher-url ] unless { { A_TEXT [ gopher-text ] }
[ host>> ] { A_MENU [ gopher-menu ] }
[ port>> 70 or <inet> binary ] { A_INDEX [ gopher-menu ] }
[ path>> rest [ "1/" ] when-empty ] { A_GIF [ gopher-gif ] }
[ query>> [ assoc>query url-decode "?" glue ] when* ] [ drop ]
} cleave '[ _ get-selector ] with-client ; } case ;
: gopher. ( url -- ) : gopher. ( url -- )
gopher { gopher {