diff --git a/OsmAnd/assets/server/go.html b/OsmAnd/assets/server/go.html
new file mode 100644
index 0000000000..d797f1083f
--- /dev/null
+++ b/OsmAnd/assets/server/go.html
@@ -0,0 +1,65 @@
+
+
+
+
+
+
+
+
+
+
+ OsmAnd - Offline Mobile Maps and Navigation
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/OsmAnd/src/net/osmand/plus/activities/ServerActivity.java b/OsmAnd/src/net/osmand/plus/activities/ServerActivity.java
index c5081fa10c..1313c13f2c 100644
--- a/OsmAnd/src/net/osmand/plus/activities/ServerActivity.java
+++ b/OsmAnd/src/net/osmand/plus/activities/ServerActivity.java
@@ -11,6 +11,7 @@ import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
+import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.server.OsmAndHttpServer;
@@ -70,6 +71,7 @@ public class ServerActivity extends AppCompatActivity {
TrafficStats.setThreadStatsTag(THREAD_ID);
try {
server = new OsmAndHttpServer();
+ server.setAndroidContext((OsmandApplication)this.getApplication());
initialized = true;
updateTextView("Server started at: http://" + OsmAndHttpServer.HOSTNAME
+ ":" + OsmAndHttpServer.PORT);
diff --git a/OsmAnd/src/net/osmand/plus/server/ApiRouter.java b/OsmAnd/src/net/osmand/plus/server/ApiRouter.java
new file mode 100644
index 0000000000..bfd6e4581d
--- /dev/null
+++ b/OsmAnd/src/net/osmand/plus/server/ApiRouter.java
@@ -0,0 +1,91 @@
+package net.osmand.plus.server;
+
+import android.util.Log;
+
+import net.osmand.plus.OsmandApplication;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.nio.charset.Charset;
+
+import fi.iki.elonen.NanoHTTPD;
+
+import static fi.iki.elonen.NanoHTTPD.newFixedLengthResponse;
+
+public class ApiRouter {
+ private OsmandApplication androidContext;
+
+ public OsmandApplication getAndroidContext() {
+ return androidContext;
+ }
+
+ public void setAndroidContext(OsmandApplication androidContext) {
+ this.androidContext = androidContext;
+ }
+
+ public NanoHTTPD.Response route(NanoHTTPD.IHTTPSession session) {
+ Log.d("SERVER", "URI: " + session.getUri());
+ if (session.getUri().contains("/scripts/") ||
+ session.getUri().contains("/images/") ||
+ session.getUri().contains("/css/") ||
+ session.getUri().contains("/favicon.ico")
+ ) {
+ return getStatic(session.getUri());
+ } else {
+ return getGoHtml();
+ }
+ }
+
+ public NanoHTTPD.Response getStatic(String uri) {
+ InputStream is = null;
+ if (androidContext != null) {
+ try {
+ is = androidContext.getAssets().open("server" + uri);
+ if (is.available() == 0){
+ return ErrorResponses.response404;
+ }
+ return newFixedLengthResponse(
+ NanoHTTPD.Response.Status.OK,
+ "text/plain",
+ is,
+ is.available());
+ } catch (IOException e) {
+ return ErrorResponses.response500;
+ }
+ }
+ return ErrorResponses.response500;
+ }
+
+ public NanoHTTPD.Response getGoHtml() {
+ String responseText = "";
+ if (androidContext != null) {
+ try {
+ InputStream is = androidContext.getAssets().open("server/go.html");
+ StringBuilder sb = new StringBuilder();
+ BufferedReader br = new BufferedReader(new InputStreamReader(is,
+ Charset.forName("UTF-8")));
+ String str;
+ while ((str = br.readLine()) != null) {
+ sb.append(str);
+ }
+ br.close();
+ responseText = sb.toString();
+ } catch (IOException e) {
+ return ErrorResponses.response500;
+ }
+ }
+ return newFixedLengthResponse(responseText);
+ }
+
+ static class ErrorResponses {
+ static NanoHTTPD.Response response404 =
+ newFixedLengthResponse(NanoHTTPD.Response.Status.NOT_FOUND,
+ NanoHTTPD.MIME_PLAINTEXT, "404 Not Found");
+
+ static NanoHTTPD.Response response500 =
+ newFixedLengthResponse(NanoHTTPD.Response.Status.INTERNAL_ERROR,
+ NanoHTTPD.MIME_PLAINTEXT, "500 Internal Server Error");
+ }
+}
\ No newline at end of file
diff --git a/OsmAnd/src/net/osmand/plus/server/OsmAndHttpServer.java b/OsmAnd/src/net/osmand/plus/server/OsmAndHttpServer.java
index 7ba670eac8..2c13732547 100644
--- a/OsmAnd/src/net/osmand/plus/server/OsmAndHttpServer.java
+++ b/OsmAnd/src/net/osmand/plus/server/OsmAndHttpServer.java
@@ -1,5 +1,7 @@
package net.osmand.plus.server;
+import net.osmand.plus.OsmandApplication;
+
import java.io.IOException;
import java.util.Map;
@@ -9,21 +11,25 @@ public class OsmAndHttpServer extends NanoHTTPD {
public static int PORT = 24990;
public static String HOSTNAME = "0.0.0.0";
+ private ServerSessionHandler sessionHandler = new ServerSessionHandler();
+ private OsmandApplication androidContext;
+
+ public OsmandApplication getAndroidContext() {
+ return androidContext;
+ }
+
+ public void setAndroidContext(OsmandApplication androidContext) {
+ this.androidContext = androidContext;
+ sessionHandler.setAndroidContext(androidContext);
+ }
+
public OsmAndHttpServer() throws IOException {
super(HOSTNAME,PORT);
start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
- System.out.println("\nRunning! Point your browsers to http://localhost:8080/ \n");
}
@Override
public Response serve(IHTTPSession session) {
- String msg = "Hello server
\n";
- Map parms = session.getParms();
- if (parms.get("username") == null) {
- msg += "\n";
- } else {
- msg += "Hello, " + parms.get("username") + "!
";
- }
- return newFixedLengthResponse(msg + "\n");
+ return sessionHandler.handle(session);
}
}
diff --git a/OsmAnd/src/net/osmand/plus/server/ServerSessionHandler.java b/OsmAnd/src/net/osmand/plus/server/ServerSessionHandler.java
new file mode 100644
index 0000000000..a4d2a5f253
--- /dev/null
+++ b/OsmAnd/src/net/osmand/plus/server/ServerSessionHandler.java
@@ -0,0 +1,24 @@
+package net.osmand.plus.server;
+
+import net.osmand.plus.OsmandApplication;
+
+import fi.iki.elonen.NanoHTTPD;
+
+public class ServerSessionHandler {
+ private OsmandApplication androidContext;
+
+ private ApiRouter router = new ApiRouter();
+
+ public OsmandApplication getAndroidContext() {
+ return androidContext;
+ }
+
+ public void setAndroidContext(OsmandApplication androidContext) {
+ this.androidContext = androidContext;
+ router.setAndroidContext(androidContext);
+ }
+
+ public NanoHTTPD.Response handle(NanoHTTPD.IHTTPSession session) {
+ return router.route(session);
+ }
+}