Search in sources :

Example 11 with Response

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();
}
Also used : Response(org.nanohttpd.protocols.http.response.Response) File(java.io.File)

Example 12 with Response

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");
}
Also used : Response(org.nanohttpd.protocols.http.response.Response) Test(org.junit.Test)

Example 13 with Response

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));
}
Also used : Response(org.nanohttpd.protocols.http.response.Response) Test(org.junit.Test)

Example 14 with Response

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());
}
Also used : Response(org.nanohttpd.protocols.http.response.Response) Test(org.junit.Test)

Example 15 with Response

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);
}
Also used : Response(org.nanohttpd.protocols.http.response.Response) Test(org.junit.Test)

Aggregations

Response (org.nanohttpd.protocols.http.response.Response)17 Test (org.junit.Test)9 ByteArrayInputStream (java.io.ByteArrayInputStream)4 IOException (java.io.IOException)4 FileInputStream (java.io.FileInputStream)3 CookieHandler (org.nanohttpd.protocols.http.content.CookieHandler)3 BufferedReader (java.io.BufferedReader)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 InputStreamReader (java.io.InputStreamReader)2 HashMap (java.util.HashMap)2 HTTPSession (org.nanohttpd.protocols.http.HTTPSession)2 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1