Why does Glassfish Jersey Sends Malformed Request Body For Large Post Bodies?

The below issue occurs in the latest version '3.0.2'. These are my dependencies :

dependencies {
    compile "org.glassfish.jersey.core:jersey-client:3.0.2"
    implementation 'org.glassfish.jersey.inject:jersey-hk2:3.0.2'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
}

When the below post request is executed the request body received at the server is malformed for any entity with length greater than or equal to 65840.

import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.core.Response;

public class MyClass {
    public static void main(String[] args) {
        Client client = ClientBuilder.newClient();
//        int count = 65839; // Works
        int count = 65840; // Doesn't work
        String body = new String(new char[count]).replace("\0", "A");
        assert (body.length() == count);
        Response response = null;
        try {
            response = client.target("http://localhost:8888/")
                    .request()
                    .post(Entity.text(body));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

To reproduce the issue run a server, nc -l 8888 > output.txt and then run the java code. The output looks like the following:

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPOST / HTTP/1.1
Content-Type: text/plain
User-Agent: Jersey/3.0.2 (HttpUrlConnection 1.8.0_212)
Host: localhost:8888
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Length: 65840

AAAAAAAAAAAAAAAAA... (Rest Of The Body)

While for any body with length less than 65840, the output looks like:

POST / HTTP/1.1
Content-Type: text/plain
User-Agent: Jersey/3.0.2 (HttpUrlConnection 1.8.0_212)
Host: localhost:8888
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Length: 65839

AAAAAAAAAAAAAAAAA... (Rest Of The Body)


Read more here: https://stackoverflow.com/questions/67553318/why-does-glassfish-jersey-sends-malformed-request-body-for-large-post-bodies

Content Attribution

This content was originally published by Deepak Kar at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: