You can set encodings to a Socket object with Socket#set_encoding.
But Socket#recv is an binary API like IO#read(n)
You can use textual API IO#read and get ISO-8859-1 string.
To summarize:
File IO encoding works correctly in that it respects the default external encoding specified in the -E option to ruby. But Socket encoding does not.
I've attached a simple test case to illustrate the problem. When I run it with ruby -E ISO-8859-1 socket_vs_file.rb, I expect the following output:
You can set encodings to a Socket object with Socket#set_encoding.
I understand, but if I don't call Socket#set_encoding, shouldn't the encoding fall back to the default encoding specified by the -E option to ruby?
Socket doesn't respect default_external because default_external is set from the locale of the client system,
but the encoding of the input string from sockets is depend on the server software.
Moreover data from socket is usually binary.
But Socket#recv is an binary API like IO#read(n)
You can use textual API IO#read and get ISO-8859-1 string.
Is IO#read the same as Socket#read? Because changing recv to read in client.rb doesn't change anything about the encoding.
I know File#read respects the default encoding. It would be nice if Socket#read did the same thing, especially since Net::HTTP uses Socket.
File and Socket are different.
Note that Net::HTTP's policy is independent from Socket.