 ! Copyright (C) 2021 John Benediktsson
 ! See http://factorcode.org/license.txt for BSD license
 
-USING: accessors ascii colors.constants combinators
-continuations images.loader images.loader.private images.viewer
-io io.encodings.binary io.encodings.latin1 io.encodings.string
+USING: accessors ascii assocs colors.constants combinators
+combinators.short-circuit continuations images.loader
+images.loader.private images.viewer io io.encodings.ascii
+io.encodings.binary io.encodings.latin1 io.encodings.string
 io.encodings.utf8 io.pathnames io.sockets io.sockets.secure
 io.styles kernel make math namespaces present sequences
 sequences.extras splitting urls wrap.strings ;

                { [ "```" ?head ] [ drop "pre" on ] }
                { [ "=>" ?head ] [ base-url gemini-link. ] }
                { [ "> " ?head ] [ gemini-quoted. ] }
                [ "pre" get [ 80 wrap-string ] unless print ]
            } cond
        ] each
    ] with-variable ;

: read-body ( -- body )
    [ 1024 read ] loop>array concat ;

ERROR: invalid-status value ;

: check-status ( status -- status )
    dup length 1 > [ invalid-status ] unless ;

: ?read-body ( status -- body/f )
    check-status ?first CHAR: 2 = [ read-body ] [ f ] if ;

: read-response ( -- status meta body/f )
    readln utf8 decode "\r" ?tail drop [ blank? ] split1-when over ?read-body ;
