Skip to content

Commit 6f4333c

Browse files
committed
ANDROID: file manager webui (wip)
1 parent 6a9e190 commit 6f4333c

File tree

1 file changed

+19
-19
lines changed
  • src/platform/android/app/src/main/java/net/sourceforge/smallbasic

1 file changed

+19
-19
lines changed

src/platform/android/app/src/main/java/net/sourceforge/smallbasic/WebServer.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
import java.util.zip.ZipOutputStream;
2424

2525
/**
26-
* Minimal AppServer with adequate performance for a single user
26+
* Minimal AppServer with adequate performance for a single user.
27+
* Uses some older java constructs to support SDK-16
2728
*
2829
* @author chrisws
2930
*/
@@ -83,8 +84,7 @@ private void runServer(final int socketNum, final String token) throws IOExcepti
8384
try {
8485
Socket socket = serverSocket.accept();
8586
log("Accepted connection from " + socket.getRemoteSocketAddress().toString());
86-
Request request = new Request(socket, token);
87-
THREAD_POOL.submit(request);
87+
THREAD_POOL.submit(new Request(socket, token));
8888
} catch (Exception e) {
8989
log("Server failed", e);
9090
}
@@ -98,18 +98,18 @@ public abstract static class AbstractRequest {
9898
final Socket socket;
9999
final String method;
100100
final String url;
101-
final String token;
101+
final String requestToken;
102+
final String tokenKey;
102103
final List<String> headers;
103104
final InputStream inputStream;
104-
final String tokenKey;
105105

106106
public AbstractRequest(Socket socket, String tokenKey) throws IOException {
107107
this.socket = socket;
108108
this.tokenKey = tokenKey;
109109
this.inputStream = socket.getInputStream();
110110
this.headers = getHeaders();
111-
this.token = getToken(headers);
112-
String first = headers.get(0);
111+
this.requestToken = getToken(headers);
112+
String first = headers.size() > 0 ? headers.get(0) : null;
113113
String[] fields;
114114
if (first != null) {
115115
fields = first.split("\\s");
@@ -175,7 +175,7 @@ private List<String> getHeaders() throws IOException {
175175
ByteArrayOutputStream line = new ByteArrayOutputStream(LINE_SIZE);
176176
final char[] endHeader = {'\r', '\n', '\r', '\n'};
177177
int index = 0;
178-
int firstChar = 0;
178+
int firstChar;
179179
for (int b = firstChar = inputStream.read(); b != -1; b = inputStream.read()) {
180180
if (b == endHeader[index]) {
181181
index++;
@@ -311,11 +311,11 @@ public void run() {
311311
if (!headers.isEmpty()) {
312312
log("URL: " + url);
313313
if ("GET".equals(method)) {
314-
handleGet(getParameters(url), tokenKey);
314+
handleGet(getParameters(url));
315315
} else if ("POST".equals(method)) {
316-
handlePost(getPostData(inputStream), tokenKey);
316+
handlePost(getPostData(inputStream));
317317
} else if ("RUN".equals(method)) {
318-
handleRun(inputStream, tokenKey);
318+
handleRun(inputStream);
319319
} else {
320320
log("Invalid request");
321321
}
@@ -324,11 +324,11 @@ public void run() {
324324
log("request failed", e);
325325
}
326326
finally {
327-
socketCleanup();
327+
afterRun();
328328
}
329329
}
330330

331-
private void socketCleanup() {
331+
private void afterRun() {
332332
log("socket cleanup");
333333
try {
334334
if (socket != null) {
@@ -412,9 +412,9 @@ private Response handleFileList() throws IOException {
412412
/**
413413
* Handler for GET requests
414414
*/
415-
private void handleGet(Map<String, Collection<String>> parameters, String tokenKey) throws IOException {
415+
private void handleGet(Map<String, Collection<String>> parameters) throws IOException {
416416
if (url.startsWith("/api/download?")) {
417-
if (tokenKey.equals(token)) {
417+
if (tokenKey.equals(requestToken)) {
418418
handleDownload(parameters).send(socket, null);
419419
} else {
420420
log("Invalid token");
@@ -428,10 +428,10 @@ private void handleGet(Map<String, Collection<String>> parameters, String tokenK
428428
/**
429429
* Handler for POST requests
430430
*/
431-
private void handlePost(Map<String, String> data, String tokenKey) throws IOException {
431+
private void handlePost(Map<String, String> data) throws IOException {
432432
String userToken = data.get(TOKEN);
433433
if (userToken == null) {
434-
userToken = token;
434+
userToken = requestToken;
435435
}
436436
log("userToken=" + userToken);
437437
if (tokenKey.equals(userToken)) {
@@ -473,8 +473,8 @@ private Response handleRename(Map<String, String> data) throws IOException {
473473
return result;
474474
}
475475

476-
private void handleRun(InputStream inputStream, String tokenKey) throws IOException {
477-
if (tokenKey.equals(token)) {
476+
private void handleRun(InputStream inputStream) throws IOException {
477+
if (tokenKey.equals(requestToken)) {
478478
execStream(inputStream);
479479
} else {
480480
log("Invalid token");

0 commit comments

Comments
 (0)