Better fix for Fix NanoHTTP 2.3.1 bug "BAD REQUEST"...
authorChristian Weiske <cweiske@cweiske.de>
Fri, 3 Jun 2022 20:08:31 +0000 (22:08 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Fri, 3 Jun 2022 20:08:31 +0000 (22:08 +0200)
The previous one did not work for "POST /api/v1/events" that
the OUYA regularly makes.

Solution by @marianolc in
https://github.com/NanoHttpd/nanohttpd/issues/356#issuecomment-380158910

src/main/java/de/cweiske/ouya/louyapi/HttpServer.java
src/main/java/fi/iki/elonen/NanoHTTPD.java

index 024a1b841ca074e91f4e245bb2144c13879d383b..568280519167011cff731185fa1fe1c7163a7385 100644 (file)
@@ -7,6 +7,8 @@ import android.util.Log;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
 
 import fi.iki.elonen.NanoHTTPD;
 
@@ -34,6 +36,17 @@ public class HttpServer extends NanoHTTPD {
             path = path.substring(1);
         }
 
+        if (session.getMethod() == Method.POST || session.getMethod() == Method.PUT) {
+            Map<String, String> parameters = new HashMap<String, String>();
+            try {
+                session.parseBody(parameters);
+            } catch (Exception e) {
+                //we do not care about the content
+                //we only parse the body to prevent errors, see
+                // https://github.com/NanoHttpd/nanohttpd/issues/356
+            }
+        }
+
         InputStream content;
 
         if (path.equals("/api/v1/status") || path.equals("/generate_204")) {
index 1810a5a393c955753fb28b8ee29ca63eb41186ec..75174f33fdb8855d8884bea92403ff646159c92f 100644 (file)
@@ -981,7 +981,6 @@ public abstract class NanoHTTPD {
                 safeClose(this.outputStream);
             } finally {
                 safeClose(r);
-                safeClose(this.inputStream);
                 this.tempFileManager.clear();
             }
         }