return contentLength;
}
}
public void handle(HttpExchange exchange) throws IOException {
WebApplication _application = application;
/**
* This is a URI that contains the path, query and
* fragment components.
*/
URI exchangeUri = exchange.getRequestURI();
/**
* The base path specified by the HTTP context of the HTTP handler.
* It is in decoded form.
*/
String decodedBasePath = exchange.getHttpContext().getPath();
// Ensure that the base path ends with a '/'
if (!decodedBasePath.endsWith("/")) {
if (decodedBasePath.equals(exchangeUri.getPath())) {
/**
* This is an edge case where the request path
* does not end in a '/' and is equal to the context
* path of the HTTP handler.
* Both the request path and base path need to end in a '/'
* Currently the request path is modified.
* TODO support redirection in accordance with resource
* configuration feature.
*/
exchangeUri = UriBuilder.fromUri(exchangeUri).
path("/").build();
}
decodedBasePath += "/";
}
/*
* The following is madness, there is no easy way to get
* the complete URI of the HTTP request!!
*
* TODO this is missing the user information component, how
* can this be obtained?
*/
String scheme = (exchange instanceof HttpsExchange) ? "https" : "http";
URI baseUri = null;
try {
List<String> hostHeader = exchange.getRequestHeaders().get("Host");
if (hostHeader != null) {
StringBuilder sb = new StringBuilder(scheme);
sb.append("://").append(hostHeader.get(0)).append(decodedBasePath);
baseUri = new URI(sb.toString());
} else {
InetSocketAddress addr = exchange.getLocalAddress();
baseUri = new URI(scheme, null, addr.getHostName(), addr.getPort(),
decodedBasePath, null, null);
}
} catch (URISyntaxException ex) {
throw new IllegalArgumentException(ex);
}
final URI requestUri = baseUri.resolve(exchangeUri);
final ContainerRequest cRequest = new ContainerRequest(
_application,
exchange.getRequestMethod(),
baseUri,
requestUri,
getHeaders(exchange),
exchange.getRequestBody()
);
try {
_application.handleRequest(cRequest, new Writer(exchange));
} catch (RuntimeException e) {
e.printStackTrace();
exchange.getResponseHeaders().clear();
exchange.sendResponseHeaders(500, -1);
} catch (IOException ex) {