NanoHttpd / nanohttpd

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

windows java: Could not send response to the client #511

Closed KnIfER closed 5 years ago

KnIfER commented 5 years ago

server looks like:

NanoHTTPD server = new NanoHTTPD(8080) {
        @Override
        public Response serve(IHTTPSession session) {
                      Map<String, String> headerTags = session.getHeaders();
                       if(headerTags.get("user-agent").contains("Java/1.8.0_172")){
                    try {
                        return newChunkedResponse(Status.OK,"*/*", new FileInputStream("F:\\123.mp3"));
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
                return null;

}
}

and the issue happens here:

Media hit = new Media("http://127.0.0.1:8080//123.mp3");
                    MediaPlayer mediaPlayer = new MediaPlayer(hit);
                    mediaPlayer.play();

where request header is:

Key = remote-addr, Value = 127.0.0.1
Key = http-client-ip, Value = 127.0.0.1
Key = host, Value = 127.0.0.1:8080
Key = connection, Value = keep-alive
Key = user-agent, Value = Java/1.8.0_172
Key = accept, Value = text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
getUri()==//123.mp3

the log is always the same:

java.net.SocketException: Software caused connection abort: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(Unknown Source)
    at java.net.SocketOutputStream.write(Unknown Source)
    at fi.iki.elonen.NanoHTTPD$Response$ChunkedOutputStream.write(NanoHTTPD.java:1442)
    at fi.iki.elonen.NanoHTTPD$Response.sendBody(NanoHTTPD.java:1695)
    at fi.iki.elonen.NanoHTTPD$Response.sendBodyWithCorrectEncoding(NanoHTTPD.java:1667)
    at fi.iki.elonen.NanoHTTPD$Response.sendBodyWithCorrectTransferAndEncoding(NanoHTTPD.java:1654)
    at fi.iki.elonen.NanoHTTPD$Response.send(NanoHTTPD.java:1624)
    at fi.iki.elonen.NanoHTTPD$HTTPSession.execute(NanoHTTPD.java:957)
    at fi.iki.elonen.NanoHTTPD$ClientHandler.run(NanoHTTPD.java:192)
    at java.lang.Thread.run(Unknown Source)

Also occurs when:

webengine.executeScript("new Audio('\123.mp3').play();");

it seems that nanohttpd cannot respond to javafx requests.

KnIfER commented 5 years ago

this version of nanahttpd have the same problem too, but it wont throw any exception .
And I solved this by adding:

pw.print("Content-Length: " + data.available() + "\r\n");

near line 684.

this

text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2

pattern seems to be a sign of breakpoint-resumable downloading.