+(: filter-comments (-> (Listof String) (Listof String)))
+(define (filter-comments lines)
+ (filter-not (λ (line) (string-prefix? line "#")) lines))
+
+(: str->peers (-> String (Listof Peer)))
+(define (str->peers str)
+ (filter-map str->peer (filter-comments (str->lines str))))
+
+(: file->peers (-> Path-String (Listof Peer)))
+(define (file->peers filename)
+ (str->peers (file->string filename)))
+
+(: user-agent String)
+(define user-agent
+ (let*
+ ([prog-name "tt"]
+ [prog-version (info:#%info-lookup 'version)]
+ [prog-uri "https://github.com/xandkar/tt"]
+ [user-peer-file (build-path tt-home-dir "me")]
+ [user
+ (if (file-exists? user-peer-file)
+ (match (first (file->peers user-peer-file))
+ [(Peer #f u) (format "+~a" (url->string u) )]
+ [(Peer n u) (format "+~a; @~a" (url->string u) n)])
+ (format "+~a" prog-uri))])
+ (format "~a/~a (~a)" prog-name prog-version user)))
+
+(: uri-download (-> Url Void))
+(define (uri-download u)
+ (define cache-file-path (url->cache-file-path u))
+ (log-debug "uri-download ~v into ~v" u cache-file-path)
+ (match* ((url-scheme u) (url-host u) (url-port u))
+ [(s h p)
+ #:when (and s h)
+ (define ssl? (string=? s "https"))
+ (define-values (status-line headers body-input)
+ ; TODO Timeout. Currently hangs on slow connections.
+ (http-sendrecv
+ h
+ (url->string (struct-copy url u [scheme #f] [host #f]))
+ #:ssl? ssl?
+ #:port (cond [p p] [ssl? 443] [else 80])
+ #:headers (list (format "User-Agent: ~a" user-agent))
+ ))
+ (log-debug "headers: ~v" headers)
+ (log-debug "status-line: ~v" status-line)
+ (define status
+ (string->number (second (string-split (bytes->string/utf-8 status-line)))))
+ (log-debug "status: ~v" status)
+ ; TODO Handle redirects
+ (if (= 200 status)
+ (begin
+ (make-parent-directory* cache-file-path)
+ (call-with-output-file cache-file-path
+ (curry copy-port body-input)
+ #:exists 'replace))
+ (raise status))]
+ [(_ _ _)
+ (log-error "Invalid URI: ~v" u)]))
+
+(: timeline-print (-> Out-Format (Listof Msg) Void))