NanoHttpd / nanohttpd

Tiny, easily embeddable HTTP server in Java.
http://nanohttpd.org
BSD 3-Clause "New" or "Revised" License
6.9k stars 1.69k forks source link

No Content Response send Content length header #577

Open TillJohanndeiter opened 4 years ago

TillJohanndeiter commented 4 years ago

If server return a newFixedLengthResponse with status code 204 then response contains a content length header. This provoke IOException on client side. I used HttpClient from .net package.

Example:

Server:


//Server
public class Server extends NanoHTTPD {
    public Server(final int port) {
        super(port);
    }

    @Override
    public Response serve(IHTTPSession session) {
        return newFixedLengthResponse(Response.Status.NO_CONTENT, null, null);
    }
} 

//Client

import java.net.http.HttpClient;

public class MainClass {

    public static void main (String...args) throws IOException {
        Server server = new Server(8080);
        server.start();
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder().uri(URI.create("http://localhost:8080/")).build();
        try {
            int statusCode = client.send(request, HttpResponse.BodyHandlers.ofString()).statusCode();
            System.out.println(statusCode);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
joelcho commented 4 years ago

Response headers

Date => Tue, 28 Apr 2020 03:19:37 GMT 
Connection => keep-alive 
Content-Length => 0 

I tested this issue. the response Content-Length is 0. no exceptions thrown in Java. client: org.apache.http.impl.client.CloseableHttpClient.

TillJohanndeiter commented 4 years ago

@joelcho Sry i used java.net.http.HttpClient;