'OpenSSL BIO_read is wrong

This is the code how I am reading the response from my HTTPS request:

int len = 0;
f = fopen("response.txt", "wb");
do
{
    char buff[1534];
    len = BIO_read(bio, buff, sizeof(buff));

    if (len > 0) {
        fwrite(buff, sizeof(char), len, f);
    }

} while (len > 0 || BIO_should_retry(bio));
fclose(f);

The response should be a "minimized" JSON (no new lines), but for whatever reason, I am seeing some "randomly" inserted \r\n if I look at the response with a HEX editor:

Image

33 35 2C 2D 31 31 35 0D 0A 31 30 30 30 0D 0A

This is an excerpt from a JSON array, so instead of \r\n there should be just a ,.

Also the HTTP response body contains some weird characters before the actual JSON:

Image

HTTP/1.1 201 Created
Server: nginx/1.21.3
Date: Wed, 04 May 2022 11:22:22 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Location: http://myserver.de/my/endpoint
api-supported-versions: 1.0

1f1b

Inbetween a JSON array (with just numbers) there are three characters which shouldn't be there:

Image

ead

And after the JSON there is an additional 0 which shouldn't be there:

Image

0.

The only reason I can think about is some memory constraint violation.

The returned JSON is correct besides the mentioned errors and the backend doesn't have any issues with the request so I assume that it is correct.

Any ideas how I can fix this?



Solution 1:[1]

This happend because my request had HTTP/1.1

POST /my/endpoint HTTP/1.1
Host: localhost
Content-Type: multipart/form-data; boundary=123456
Content-Length: 83031
Connection: close

if I change it to HTTP/1.0, the artifacts disappear.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bopeh74638