CC: Tweaked

CC: Tweaked

42M Downloads

HTTP API corrupts request payload

viluon opened this issue ยท 1 comments

commented

The HTTP API implementation reinterprets POST payloads as text regardless of the binary setting. I ran into it when trying to POST binary data to a server and getting garbage on the other side.

Also, the default headers are "Content-Type": "application/x-www-form-urlencoded; charset=utf-8" and "Accept-Charset": "UTF-8" irrespective of the binary flag.

this.address = address;
postBuffer = postText != null
? Unpooled.wrappedBuffer(postText.getBytes(StandardCharsets.UTF_8))
: Unpooled.buffer(0);
this.headers = headers;

Test with

local h = http.post {
  url = "https://httpbin.org/anything",
  body = string.rep("\254", 7),
  binary = true,
}

local response = h.readAll()
h.close()

-- expected: 7
-- actual: 14
print(textutils.unserialiseJSON(response).headers["Content-Length"])

Or simply pastebin run uSfZyzi8.

Originally posted by @viluon in #1526

commented

This issue concerns versions since at least v1.100.5 up to and including the latest v1.108.0, but the problematic code appeared with the http rework as early as v1.81.0. It's possible that binary uploads never worked correctly in the first place.

Affected versions and flavours

Verified on CC:Tweaked 1.100.5 via copy-cat:
computer-2023-08-28_16-35

Verified by @tizu69 on Minecraft 1.18.2 / CC:Tweaked 1.101.3:

Verified on CC:Tweaked 1.103.1 via copy-cat:
computer-2023-08-28_16-37

Verified on CC:Tweaked 1.106.1 via CCEmuX 1.1.0:
image

Verified on CC:Tweaked 1.108.0 via eval.tweaked.cc: