From 777b82fd9b098c0966d83be9839291e6837ca3e3 Mon Sep 17 00:00:00 2001 From: "Adam D. Ruppe" Date: Sat, 7 May 2022 07:32:56 -0400 Subject: [PATCH] percent encode nonascii urls --- http2.d | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/http2.d b/http2.d index 00fed28..ca64241 100644 --- a/http2.d +++ b/http2.d @@ -578,7 +578,36 @@ struct Uri { /// Breaks down a uri string to its components this(string uri) { - reparse(uri); + size_t lastGoodIndex; + foreach(char ch; uri) { + if(ch > 127) { + break; + } + lastGoodIndex++; + } + + string replacement = uri[0 .. lastGoodIndex]; + foreach(char ch; uri[lastGoodIndex .. $]) { + if(ch > 127) { + // need to percent-encode any non-ascii in it + char[3] buffer; + buffer[0] = '%'; + + auto first = ch / 16; + auto second = ch % 16; + first += (first >= 10) ? ('A'-10) : '0'; + second += (second >= 10) ? ('A'-10) : '0'; + + buffer[1] = cast(char) first; + buffer[2] = cast(char) second; + + replacement ~= buffer[]; + } else { + replacement ~= ch; + } + } + + reparse(replacement); } /// Returns `port` if set, otherwise if scheme is https 443, otherwise always 80