NanoHttpd / nanohttpd

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

found a little issue when using nanohttpd with javafx webview. #566

Closed KnIfER closed 4 years ago

KnIfER commented 4 years ago

This issue only occurs if you are using this library to play a wav audio in a webview of jdk1.8 javafx via js audio API. like:

var audio = new Audio();
audio.src = "http://127.0.0.1:8080/ld45happy.mp3";
audio.play();

//and in the java server side  : 
return newFixedLengthResponse(Status.OK,"audio/x-wav", new ByteArrayInputStream(WavData), WavData.length);

In the webview of jdk1.8 javafx , no audio will be played :

(webview console log) Unhandled Promise Rejection: [object DOMError]

To solve this, I just need to add a line in Response.java,line#313:

void sendBodyWithCorrectEncoding(OutputStream outputStream, long pending) {//method former name : sendAsFixedLength
    if(this.requestMethod != Method.HEAD && this.data != null) // +++
    ……

The if-check had been there before a45eb0e96d8081f67baca79660ba44aa01978b88 . And after the introduce of gzip support , it was gone.

Although this little issue does not seem to occur in other browsers , I think it's better to and the if-check back.