|
8 | 8 | import com.google.gson.JsonObject; |
9 | 9 | import com.google.gson.JsonParser; |
10 | 10 | import fi.iki.elonen.NanoHTTPD; // Note: org.nanohttpd:nanohttpd:2.3.1 still uses fi.iki.elonen package |
| 11 | +import java.io.DataInputStream; |
11 | 12 | import java.io.IOException; |
| 13 | +import java.nio.charset.StandardCharsets; |
12 | 14 | import java.security.SecureRandom; |
13 | 15 | import java.util.Map; |
14 | 16 | import java.util.concurrent.ExecutionException; |
@@ -105,11 +107,11 @@ private Response routeRequest(IHTTPSession session, DatabaseConnection db) throw |
105 | 107 | } else if (method == Method.POST && uri.equals("/batch")) { |
106 | 108 | return addCorsHeaders(handleBatch(session, db)); |
107 | 109 | } else if (method == Method.POST && uri.equals("/transaction/begin")) { |
108 | | - return addCorsHeaders(handleBeginTransaction(db)); |
| 110 | + return addCorsHeaders(handleBeginTransaction(session, db)); |
109 | 111 | } else if (method == Method.POST && uri.equals("/transaction/commit")) { |
110 | | - return addCorsHeaders(handleCommitTransaction(db)); |
| 112 | + return addCorsHeaders(handleCommitTransaction(session, db)); |
111 | 113 | } else if (method == Method.POST && uri.equals("/transaction/rollback")) { |
112 | | - return addCorsHeaders(handleRollbackTransaction(db)); |
| 114 | + return addCorsHeaders(handleRollbackTransaction(session, db)); |
113 | 115 | } else { |
114 | 116 | return addCorsHeaders(newFixedLengthResponse(Response.Status.NOT_FOUND, "text/plain", "Endpoint not found")); |
115 | 117 | } |
@@ -204,26 +206,29 @@ private Response handleBatch(IHTTPSession session, DatabaseConnection db) throws |
204 | 206 | return newFixedLengthResponse(Response.Status.OK, "application/json", results.toString()); |
205 | 207 | } |
206 | 208 |
|
207 | | - private Response handleBeginTransaction(DatabaseConnection db) throws Exception { |
| 209 | + private Response handleBeginTransaction(IHTTPSession session, DatabaseConnection db) throws Exception { |
| 210 | + readRequestBody(session); // drain any body to keep the connection clean for keep-alive |
208 | 211 | db.beginTransaction(); |
209 | 212 | return newFixedLengthResponse(Response.Status.OK, "application/json", "{}"); |
210 | 213 | } |
211 | 214 |
|
212 | | - private Response handleCommitTransaction(DatabaseConnection db) throws Exception { |
| 215 | + private Response handleCommitTransaction(IHTTPSession session, DatabaseConnection db) throws Exception { |
| 216 | + readRequestBody(session); // drain any body to keep the connection clean for keep-alive |
213 | 217 | db.commitTransaction(); |
214 | 218 | return newFixedLengthResponse(Response.Status.OK, "application/json", "{}"); |
215 | 219 | } |
216 | 220 |
|
217 | | - private Response handleRollbackTransaction(DatabaseConnection db) throws Exception { |
| 221 | + private Response handleRollbackTransaction(IHTTPSession session, DatabaseConnection db) throws Exception { |
| 222 | + readRequestBody(session); // drain any body to keep the connection clean for keep-alive |
218 | 223 | db.rollbackTransaction(); |
219 | 224 | return newFixedLengthResponse(Response.Status.OK, "application/json", "{}"); |
220 | 225 | } |
221 | 226 |
|
222 | 227 | private String readRequestBody(IHTTPSession session) throws IOException { |
223 | 228 | int contentLength = Integer.parseInt(session.getHeaders().get("content-length")); |
224 | 229 | byte[] buffer = new byte[contentLength]; |
225 | | - session.getInputStream().read(buffer, 0, contentLength); |
226 | | - return new String(buffer); |
| 230 | + new DataInputStream(session.getInputStream()).readFully(buffer); |
| 231 | + return new String(buffer, StandardCharsets.UTF_8); |
227 | 232 | } |
228 | 233 |
|
229 | 234 | private static String generateToken() { |
|
0 commit comments