Fix wide-character decoding in URLs.

This commit is contained in:
Sam Tobin-Hochstadt 2013-07-05 11:00:41 -04:00
parent 0295d44316
commit a4e529a816

View File

@ -167,19 +167,21 @@ See more in PR8831.
;; vector string -> string
(define (decode table str)
(define max-ascii (integer->char ascii-size))
;; internal-decode : list -> listof[byte]
(define (internal-decode l)
(if (null? l) '()
(let* ([c (car l)] [l (cdr l)]
[hex (and (equal? #\% c) (pair? l) (pair? (cdr l))
(string->number (string (car l) (cadr l)) 16))])
(if hex (cons hex (internal-decode (cddr l)))
(cons (if (char<? c max-ascii)
(vector-ref table (char->integer c))
;; This should probably error, but strings to be decoded
;; might come from misbehaving sources; maybe it's better
;; to add some parameter for a permissive mode
(bytes->list (string->bytes/utf-8 (string c))))
(internal-decode l))))))
(if hex
(cons hex (internal-decode (cddr l)))
(append (if (char<? c max-ascii)
(list (vector-ref table (char->integer c)))
;; This should probably error, but strings to be decoded
;; might come from misbehaving sources; maybe it's better
;; to add some parameter for a permissive mode
(bytes->list (string->bytes/utf-8 (string c))))
(internal-decode l))))))
(bytes->string/utf-8 (apply bytes (internal-decode (string->list str)))))
;; Utility for defining codecs