http server: return specific http codes for errors
This commit is contained in:
parent
6fd1c6151d
commit
179525712f
1 changed files with 33 additions and 17 deletions
|
@ -36,6 +36,11 @@ import btools.util.StackSampler;
|
||||||
public class RouteServer extends Thread
|
public class RouteServer extends Thread
|
||||||
{
|
{
|
||||||
public static final String PROFILE_UPLOAD_URL = "/brouter/profile";
|
public static final String PROFILE_UPLOAD_URL = "/brouter/profile";
|
||||||
|
static final String HTTP_STATUS_OK = "200 OK";
|
||||||
|
static final String HTTP_STATUS_BAD_REQUEST = "400 Bad Request";
|
||||||
|
static final String HTTP_STATUS_FORBIDDEN = "403 Forbidden";
|
||||||
|
static final String HTTP_STATUS_NOT_FOUND = "404 Not Found";
|
||||||
|
static final String HTTP_STATUS_INTERNAL_SERVER_ERROR = "500 Internal Server Error";
|
||||||
|
|
||||||
public ServiceContext serviceContext;
|
public ServiceContext serviceContext;
|
||||||
|
|
||||||
|
@ -80,6 +85,8 @@ public class RouteServer extends Thread
|
||||||
String line = br.readLine();
|
String line = br.readLine();
|
||||||
if ( line == null )
|
if ( line == null )
|
||||||
{
|
{
|
||||||
|
writeHttpHeader(bw, HTTP_STATUS_BAD_REQUEST);
|
||||||
|
bw.flush();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ( line.length() == 0 )
|
if ( line.length() == 0 )
|
||||||
|
@ -108,7 +115,7 @@ public class RouteServer extends Thread
|
||||||
{
|
{
|
||||||
if ( agent.indexOf( tk.nextToken() ) >= 0 )
|
if ( agent.indexOf( tk.nextToken() ) >= 0 )
|
||||||
{
|
{
|
||||||
writeHttpHeader( bw );
|
writeHttpHeader( bw, HTTP_STATUS_FORBIDDEN );
|
||||||
bw.write( "Bad agent: " + agent );
|
bw.write( "Bad agent: " + agent );
|
||||||
bw.flush();
|
bw.flush();
|
||||||
return;
|
return;
|
||||||
|
@ -118,11 +125,13 @@ public class RouteServer extends Thread
|
||||||
|
|
||||||
if ( getline.startsWith("GET /favicon.ico") )
|
if ( getline.startsWith("GET /favicon.ico") )
|
||||||
{
|
{
|
||||||
return;
|
writeHttpHeader( bw, HTTP_STATUS_NOT_FOUND );
|
||||||
|
bw.flush();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
if ( getline.startsWith("GET /robots.txt") )
|
if ( getline.startsWith("GET /robots.txt") )
|
||||||
{
|
{
|
||||||
writeHttpHeader( bw );
|
writeHttpHeader( bw, HTTP_STATUS_OK );
|
||||||
bw.write( "User-agent: *\n" );
|
bw.write( "User-agent: *\n" );
|
||||||
bw.write( "Disallow: /\n" );
|
bw.write( "Disallow: /\n" );
|
||||||
bw.flush();
|
bw.flush();
|
||||||
|
@ -149,13 +158,13 @@ public class RouteServer extends Thread
|
||||||
// handle CORS preflight request (Safari)
|
// handle CORS preflight request (Safari)
|
||||||
String corsHeaders = "Access-Control-Allow-Methods: GET, POST\n"
|
String corsHeaders = "Access-Control-Allow-Methods: GET, POST\n"
|
||||||
+ "Access-Control-Allow-Headers: Content-Type\n";
|
+ "Access-Control-Allow-Headers: Content-Type\n";
|
||||||
writeHttpHeader( bw, "text/plain", null, corsHeaders );
|
writeHttpHeader( bw, "text/plain", null, corsHeaders, HTTP_STATUS_OK );
|
||||||
bw.flush();
|
bw.flush();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
writeHttpHeader(bw, "application/json");
|
writeHttpHeader(bw, "application/json", HTTP_STATUS_OK);
|
||||||
|
|
||||||
String profileId = null;
|
String profileId = null;
|
||||||
if ( url.length() > PROFILE_UPLOAD_URL.length() + 1 )
|
if ( url.length() > PROFILE_UPLOAD_URL.length() + 1 )
|
||||||
|
@ -173,13 +182,15 @@ public class RouteServer extends Thread
|
||||||
}
|
}
|
||||||
else if ( url.startsWith( "/brouter/suspects" ) )
|
else if ( url.startsWith( "/brouter/suspects" ) )
|
||||||
{
|
{
|
||||||
writeHttpHeader(bw, url.endsWith( ".json" ) ? "application/json" : "text/html");
|
writeHttpHeader(bw, url.endsWith( ".json" ) ? "application/json" : "text/html", HTTP_STATUS_OK);
|
||||||
SuspectManager.process( url, bw );
|
SuspectManager.process( url, bw );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
throw new IllegalArgumentException( "unknown request syntax: " + getline );
|
writeHttpHeader( bw, HTTP_STATUS_NOT_FOUND );
|
||||||
|
bw.flush();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
RoutingContext rc = handler.readRoutingContext();
|
RoutingContext rc = handler.readRoutingContext();
|
||||||
List<OsmNodeNamed> wplist = handler.readWayPointList();
|
List<OsmNodeNamed> wplist = handler.readWayPointList();
|
||||||
|
@ -214,7 +225,7 @@ public class RouteServer extends Thread
|
||||||
|
|
||||||
if ( cr.getErrorMessage() != null )
|
if ( cr.getErrorMessage() != null )
|
||||||
{
|
{
|
||||||
writeHttpHeader(bw);
|
writeHttpHeader(bw, HTTP_STATUS_INTERNAL_SERVER_ERROR);
|
||||||
bw.write( cr.getErrorMessage() );
|
bw.write( cr.getErrorMessage() );
|
||||||
bw.write( "\n" );
|
bw.write( "\n" );
|
||||||
}
|
}
|
||||||
|
@ -223,7 +234,7 @@ public class RouteServer extends Thread
|
||||||
OsmTrack track = cr.getFoundTrack();
|
OsmTrack track = cr.getFoundTrack();
|
||||||
|
|
||||||
String headers = encodings == null || encodings.indexOf( "gzip" ) < 0 ? null : "Content-Encoding: gzip\n";
|
String headers = encodings == null || encodings.indexOf( "gzip" ) < 0 ? null : "Content-Encoding: gzip\n";
|
||||||
writeHttpHeader(bw, handler.getMimeType(), handler.getFileName(), headers );
|
writeHttpHeader(bw, handler.getMimeType(), handler.getFileName(), headers, HTTP_STATUS_OK );
|
||||||
if ( track != null )
|
if ( track != null )
|
||||||
{
|
{
|
||||||
if ( headers != null ) // compressed
|
if ( headers != null ) // compressed
|
||||||
|
@ -245,6 +256,11 @@ public class RouteServer extends Thread
|
||||||
}
|
}
|
||||||
catch (Throwable e)
|
catch (Throwable e)
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
|
writeHttpHeader(bw, HTTP_STATUS_INTERNAL_SERVER_ERROR);
|
||||||
|
bw.flush();
|
||||||
|
}
|
||||||
|
catch (IOException _ignore){}
|
||||||
System.out.println("RouteServer got exception (will continue): "+e);
|
System.out.println("RouteServer got exception (will continue): "+e);
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -369,25 +385,25 @@ public class RouteServer extends Thread
|
||||||
return maxRunningTime;
|
return maxRunningTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void writeHttpHeader( BufferedWriter bw ) throws IOException
|
private static void writeHttpHeader( BufferedWriter bw, String status ) throws IOException
|
||||||
{
|
{
|
||||||
writeHttpHeader( bw, "text/plain" );
|
writeHttpHeader( bw, "text/plain", status );
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void writeHttpHeader( BufferedWriter bw, String mimeType ) throws IOException
|
private static void writeHttpHeader( BufferedWriter bw, String mimeType, String status ) throws IOException
|
||||||
{
|
{
|
||||||
writeHttpHeader( bw, mimeType, null );
|
writeHttpHeader( bw, mimeType, null, status );
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void writeHttpHeader( BufferedWriter bw, String mimeType, String fileName ) throws IOException
|
private static void writeHttpHeader( BufferedWriter bw, String mimeType, String fileName, String status ) throws IOException
|
||||||
{
|
{
|
||||||
writeHttpHeader( bw, mimeType, fileName, null);
|
writeHttpHeader( bw, mimeType, fileName, null, status);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void writeHttpHeader( BufferedWriter bw, String mimeType, String fileName, String headers ) throws IOException
|
private static void writeHttpHeader( BufferedWriter bw, String mimeType, String fileName, String headers, String status ) throws IOException
|
||||||
{
|
{
|
||||||
// http-header
|
// http-header
|
||||||
bw.write( "HTTP/1.1 200 OK\n" );
|
bw.write( String.format("HTTP/1.1 %s\n", status) );
|
||||||
bw.write( "Connection: close\n" );
|
bw.write( "Connection: close\n" );
|
||||||
bw.write( "Content-Type: " + mimeType + "; charset=utf-8\n" );
|
bw.write( "Content-Type: " + mimeType + "; charset=utf-8\n" );
|
||||||
if ( fileName != null )
|
if ( fileName != null )
|
||||||
|
|
Loading…
Reference in a new issue