Skip to content

Instantly share code, notes, and snippets.

@michael-simons
Forked from nigjo/Server.java
Created October 12, 2020 09:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save michael-simons/4c71479870cadc94ce934b7f1c34a80d to your computer and use it in GitHub Desktop.
Save michael-simons/4c71479870cadc94ce934b7f1c34a80d to your computer and use it in GitHub Desktop.
A simple, insecure One-File-Java-Server to serve static pages. Main purpose is to have a simple server to locally test some github pages.
/*
* Copyright 2020 Jens "Nigjo" Hofschröer.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Runs a simple Debug Server in a local folder.
* This file can be directly started with Java 11+.
*/
class Server
{
static final java.util.Map<String, String> types =
new java.util.HashMap<>(java.util.Map.of(
"html", "text/html",
"js", "text/javascript",
"css", "text/css",
"jpg", "image/jpeg",
"png", "image/png",
"ico", "image/x-icon",
"json", "application/json"
));
private static int port = 8080;
private static String prefix = "/";
public static void args(String[] a)
{
for(String arg : a)
{
try
{
port = Integer.parseInt(arg);
}
catch(NumberFormatException ex)
{
if(arg.indexOf('=') > 0)
{
String pair[] = arg.split("=", 2);
types.putIfAbsent(pair[0], pair[1]);
}
else
{
prefix = arg;
if(prefix.charAt(0) != '/')
{
prefix = '/' + prefix;
}
if(prefix.charAt(prefix.length() - 1) != '/')
{
prefix += '/';
}
}
}
}
}
public static void main(String[] a) throws java.io.IOException
{
args(a);
java.net.InetSocketAddress host =
new java.net.InetSocketAddress("localhost", port);
com.sun.net.httpserver.HttpServer server =
com.sun.net.httpserver.HttpServer.create(host, 0
);
server.createContext("/", Server::handleRequest);
server.start();
System.out.println("Server is running at http://"
+ host.getHostName() + ":" + host.getPort() + prefix);
}
private static void handleRequest(com.sun.net.httpserver.HttpExchange t)
throws java.io.IOException
{
java.net.URI uri = t.getRequestURI();
if(uri.toString().endsWith("/"))
{
uri = uri.resolve("index.html");
}
String path = uri.getPath();
java.io.File local = null;
if(path.startsWith(prefix))
{
local = new java.io.File(".", path.substring(prefix.length()));
}
System.out.print(new java.util.Date().toString());
System.out.print(" GET " + uri);
if(local != null && local.exists())
{
//String response = "This is the response of "+local.getAbsolutePath();
String filename = local.getName();
String ext = filename.substring(filename.lastIndexOf('.') + 1);
if(types.containsKey(ext))
{
System.out.print(" " + types.get(ext));
t.getResponseHeaders()
.add("Content-Type", types.get(ext));
}
System.out.print(" 200 " + local.length());
t.sendResponseHeaders(200, local.length());
try(java.io.OutputStream out = t.getResponseBody())
{
java.nio.file.Files.copy(local.toPath(), out);
}
}
else
{
System.out.print(" 404");
String response = "File not found " + uri.toString();
t.sendResponseHeaders(404, response.length());
try(java.io.OutputStream os = t.getResponseBody())
{
os.write(response.getBytes());
}
}
System.out.println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment