     redirects inc
     redirects get max-redirects get < [
         ! XXX: detect cross-protocol redirects
+        ! XXX: detect redirect to same link
         drop nip gemini-request
     ] [ too-many-redirects ] if ;
 

                { [ "```" ?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 ;
