breezewiki/src/page-proxy.rkt
Cadence Ember a2ec8bb923
Fix image loading not working after a while
http-easy connections are pooled, and streaming responses does not
automatically close the response (they are closed automatically in
other situations). After 128 (the default) opened connections to fetch
images, the pool ran out of connections, and future attempts would
just time out waiting for a connection.

Fixed by manually closing the connection at the correct time.
2022-09-03 19:55:17 +12:00

30 lines
920 B
Racket

#lang racket/base
(require racket/dict
racket/match
racket/port
; libs
(prefix-in easy: net/http-easy)
; web server libs
net/url
web-server/http
(only-in web-server/dispatchers/dispatch next-dispatcher)
"url-utils.rkt"
"xexpr-utils.rkt")
(provide
page-proxy)
(define (page-proxy req)
(match (dict-ref (url-query (request-uri req)) 'dest #f)
[(? string? dest)
(if (is-fandom-url? dest)
(response-handler
(let ([dest-r (easy:get dest #:stream? #t)])
(response/output
#:code (easy:response-status-code dest-r)
#:mime-type (easy:response-headers-ref dest-r 'content-type)
(λ (out)
(copy-port (easy:response-output dest-r) out)
(easy:response-close! dest-r)))))
(next-dispatcher))]
[#f (next-dispatcher)]))