Subject: bug#24117: 25.1; url-http-create-request: Multibyte text in HTTP request

From: Dmitry Gutov <[email-masked]>
Date: Tue, 2 Aug 2016 03:52:25 +0300

On 08/01/2016 04:17 PM, Eli Zaretskii wrote:

> To summarize, I still don't understand how come the error happened.
> Could you perhaps step with Edebug into url-http-create-request, and
> see what is going on there?  Or come up with a reproducible recipe of
> calling url-http-create-request that I could examine on my machine?

Here's the essence of the problem:

(length (concat (encode-coding-string "фыва" 'utf-8) 
(string-as-multibyte "abc")))

=> 11

(string-bytes (concat (encode-coding-string "фыва" 'utf-8) 
(string-as-multibyte "abc")))

=> 19

And

(multibyte-string-p (url-host (url-generic-parse-url "http://127.0.0.1")))

=> t

Apparently, url-generic-parse-url creates a multibyte string for the 
host name because it performs its parsing in a buffer. And 
url-http-create-request uses the return value of (url-host 
url-http-target-url) to set the Location header. And all of that gets 
concatenated in the request.

Some possible solutions:

- Perform the "string-bytes = length" verification only for 
url-http-data, not the the whole request string. This strikes me as 
ugly, but apparently we've been living with using a multibyte string 
here for a while.

- Call url-encode-url on the return value of (url-host 
url-http-target-url), and hope that no similar problem pops up with any 
of the related variables. This does solve the immediate problem with 
anaconda-mode, I've checked.

- Something else?