use of org.nanohttpd.protocols.http.response.Response in project nanohttpd by NanoHttpd.
the class SimpleWebServer method defaultRespond.
private Response defaultRespond(Map<String, String> headers, IHTTPSession session, String uri) {
// Remove URL arguments
uri = uri.trim().replace(File.separatorChar, '/');
if (uri.indexOf('?') >= 0) {
uri = uri.substring(0, uri.indexOf('?'));
}
// Prohibit getting out of current directory
if (uri.contains("../")) {
return getForbiddenResponse("Won't serve ../ for security reasons.");
}
boolean canServeUri = false;
File homeDir = null;
for (int i = 0; !canServeUri && i < this.rootDirs.size(); i++) {
homeDir = this.rootDirs.get(i);
canServeUri = canServeUri(uri, homeDir);
}
if (!canServeUri) {
return getNotFoundResponse();
}
// Browsers get confused without '/' after the directory, send a
// redirect.
File f = new File(homeDir, uri);
if (f.isDirectory() && !uri.endsWith("/")) {
uri += "/";
Response res = newFixedLengthResponse(Status.REDIRECT, NanoHTTPD.MIME_HTML, "<html><body>Redirected: <a href=\"" + uri + "\">" + uri + "</a></body></html>");
res.addHeader("Location", uri);
return res;
}
if (f.isDirectory()) {
// First look for index files (index.html, index.htm, etc) and if
// none found, list the directory if readable.
String indexFile = findIndexFileInDirectory(f);
if (indexFile == null) {
if (f.canRead()) {
// No index file, list the directory if it is readable
return newFixedLengthResponse(Status.OK, NanoHTTPD.MIME_HTML, listDirectory(uri, f));
} else {
return getForbiddenResponse("No directory listing.");
}
} else {
return respond(headers, session, uri + indexFile);
}
}
String mimeTypeForFile = getMimeTypeForFile(uri);
WebServerPlugin plugin = SimpleWebServer.mimeTypeHandlers.get(mimeTypeForFile);
Response response = null;
if (plugin != null && plugin.canServeUri(uri, homeDir)) {
response = plugin.serveFile(uri, headers, session, f, mimeTypeForFile);
if (response != null && response instanceof InternalRewrite) {
InternalRewrite rewrite = (InternalRewrite) response;
return respond(rewrite.getHeaders(), session, rewrite.getUri());
}
} else {
response = serveFile(uri, headers, f, mimeTypeForFile);
}
return response != null ? response : getNotFoundResponse();
}
use of org.nanohttpd.protocols.http.response.Response in project nanohttpd by NanoHttpd.
the class WebSocketResponseHandlerTest method testHandshakeReturnsResponseWithExpectedHeaders.
@Test
public void testHandshakeReturnsResponseWithExpectedHeaders() {
Response handshakeResponse = this.nanoWebSocketServer.handle(this.session);
assertNotNull(handshakeResponse);
assertEquals(handshakeResponse.getHeader(NanoWSD.HEADER_WEBSOCKET_ACCEPT), "HSmrc0sMlYUkAGmm5OPpG2HaGWk=");
assertEquals(handshakeResponse.getHeader(NanoWSD.HEADER_WEBSOCKET_PROTOCOL), "chat");
}
use of org.nanohttpd.protocols.http.response.Response in project nanohttpd by NanoHttpd.
the class WebSocketResponseHandlerTest method testWrongUpgradeHeaderReturnsNullResponse.
@Test
public void testWrongUpgradeHeaderReturnsNullResponse() {
this.headers.put("upgrade", "not a websocket");
Response handshakeResponse = this.nanoWebSocketServer.handle(this.session);
assertNull(handshakeResponse.getHeader(NanoWSD.HEADER_UPGRADE));
}
use of org.nanohttpd.protocols.http.response.Response in project nanohttpd by NanoHttpd.
the class WebSocketResponseHandlerTest method testWrongWebsocketVersionReturnsErrorResponse.
@Test
public void testWrongWebsocketVersionReturnsErrorResponse() {
this.headers.put("sec-websocket-version", "12");
Response handshakeResponse = this.nanoWebSocketServer.handle(this.session);
assertNotNull(handshakeResponse);
assertEquals(Status.BAD_REQUEST, handshakeResponse.getStatus());
}
use of org.nanohttpd.protocols.http.response.Response in project nanohttpd by NanoHttpd.
the class WebSocketResponseHandlerTest method testConnectionHeaderHandlesKeepAlive_FixingFirefoxConnectIssue.
@Test
public void testConnectionHeaderHandlesKeepAlive_FixingFirefoxConnectIssue() {
this.headers.put("connection", "keep-alive, Upgrade");
Response handshakeResponse = this.nanoWebSocketServer.handle(this.session);
assertNotNull(handshakeResponse);
}
Aggregations