Skip to content

Commit d5152b2

Browse files
fix(android): Fix transaction handling by reading request body to prevent leftover data in TCP connection for keep-alive connections (#27)
* fix(android): Fix transaction handling by reading request body to prevent leftover data in TCP connection for keep-alive connections * fix: Adjust formatting in SQLHTTPServer.java * fix: fix formatting --------- Co-authored-by: Dariusz Hatala <dariusz.hatala@quantum-software.com>
1 parent 6fa16f5 commit d5152b2

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

android/src/main/java/app/capgo/capacitor/fastsql/SQLHTTPServer.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import com.google.gson.JsonObject;
99
import com.google.gson.JsonParser;
1010
import fi.iki.elonen.NanoHTTPD; // Note: org.nanohttpd:nanohttpd:2.3.1 still uses fi.iki.elonen package
11+
import java.io.DataInputStream;
1112
import java.io.IOException;
13+
import java.nio.charset.StandardCharsets;
1214
import java.security.SecureRandom;
1315
import java.util.Map;
1416
import java.util.concurrent.ExecutionException;
@@ -105,11 +107,11 @@ private Response routeRequest(IHTTPSession session, DatabaseConnection db) throw
105107
} else if (method == Method.POST && uri.equals("/batch")) {
106108
return addCorsHeaders(handleBatch(session, db));
107109
} else if (method == Method.POST && uri.equals("/transaction/begin")) {
108-
return addCorsHeaders(handleBeginTransaction(db));
110+
return addCorsHeaders(handleBeginTransaction(session, db));
109111
} else if (method == Method.POST && uri.equals("/transaction/commit")) {
110-
return addCorsHeaders(handleCommitTransaction(db));
112+
return addCorsHeaders(handleCommitTransaction(session, db));
111113
} else if (method == Method.POST && uri.equals("/transaction/rollback")) {
112-
return addCorsHeaders(handleRollbackTransaction(db));
114+
return addCorsHeaders(handleRollbackTransaction(session, db));
113115
} else {
114116
return addCorsHeaders(newFixedLengthResponse(Response.Status.NOT_FOUND, "text/plain", "Endpoint not found"));
115117
}
@@ -204,26 +206,29 @@ private Response handleBatch(IHTTPSession session, DatabaseConnection db) throws
204206
return newFixedLengthResponse(Response.Status.OK, "application/json", results.toString());
205207
}
206208

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
208211
db.beginTransaction();
209212
return newFixedLengthResponse(Response.Status.OK, "application/json", "{}");
210213
}
211214

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
213217
db.commitTransaction();
214218
return newFixedLengthResponse(Response.Status.OK, "application/json", "{}");
215219
}
216220

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
218223
db.rollbackTransaction();
219224
return newFixedLengthResponse(Response.Status.OK, "application/json", "{}");
220225
}
221226

222227
private String readRequestBody(IHTTPSession session) throws IOException {
223228
int contentLength = Integer.parseInt(session.getHeaders().get("content-length"));
224229
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);
227232
}
228233

229234
private static String generateToken() {

0 commit comments

Comments
 (0)