NanoHTTPD.Response process() { if (isPassed()) { return NanoHTTPD.newFixedLengthResponse(Status.OK, "text/plain; charset=utf-8", "OK!"); } else { return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE, "text/plain; charset=utf-8", "Bad!"); } }
public Response get(UriResource uriResource, Map<String, String> urlParams, IHTTPSession session) { String baseUri = uriResource.getUri(); String realUri = normalizeUri(session.getUri()); for (int index = 0; index < Math.min(baseUri.length(), realUri.length()); index++) { if (baseUri.charAt(index) != realUri.charAt(index)) { realUri = normalizeUri(realUri.substring(index)); break; } } File fileOrdirectory = uriResource.initParameter(File.class); for (String pathPart : getPathArray(realUri)) { fileOrdirectory = new File(fileOrdirectory, pathPart); } if (fileOrdirectory.isDirectory()) { fileOrdirectory = new File(fileOrdirectory, "index.html"); if (!fileOrdirectory.exists()) { fileOrdirectory = new File(fileOrdirectory.getParentFile(), "index.htm"); } } if (!fileOrdirectory.exists() || !fileOrdirectory.isFile()) { return new Error404UriHandler().get(uriResource, urlParams, session); } else { try { return NanoHTTPD.newChunkedResponse(getStatus(), getMimeTypeForFile(fileOrdirectory.getName()), fileToInputStream(fileOrdirectory)); } catch (IOException ioe) { return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.REQUEST_TIMEOUT, "text/plain", null); } } }
r = serve(this); this.cookies.unloadQueue(r); r.setRequestMethod(this.method); r.setGzipEncoding(useGzipWhenAccepted(r) && acceptEncoding != null && acceptEncoding.contains("gzip")); r.setKeepAlive(keepAlive); r.send(this.outputStream); Response resp = newFixedLengthResponse(Response.Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, "SSL PROTOCOL FAILURE: " + ssle.getMessage()); resp.send(this.outputStream); safeClose(this.outputStream); } catch (IOException ioe) { Response resp = newFixedLengthResponse(Response.Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage()); resp.send(this.outputStream); safeClose(this.outputStream); } catch (ResponseException re) { Response resp = newFixedLengthResponse(re.getStatus(), NanoHTTPD.MIME_PLAINTEXT, re.getMessage()); resp.send(this.outputStream); safeClose(this.outputStream);
public NanoHTTPD.Response getResponse(String targetPath) { try { OrchidResource faviconResource = this.resources.getResourceEntry("favicon.ico"); if(faviconResource != null) { InputStream is = faviconResource.getContentStream(); if(is != null) { return NanoHTTPD.newChunkedResponse(NanoHTTPD.Response.Status.OK, "image/x-icon", is); } } } catch (Exception e) { e.printStackTrace(); } return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.NOT_FOUND, "image/x-icon", ""); } }
/** * Override this to customize the server. * <p/> * <p/> * (By default, this returns a 404 "Not Found" plain text error response.) * * @param session * The HTTP session * @return HTTP response, see class Response for details */ public Response serve(IHTTPSession session) { Map<String, String> files = new HashMap<String, String>(); Method method = session.getMethod(); if (Method.PUT.equals(method) || Method.POST.equals(method)) { try { session.parseBody(files); } catch (IOException ioe) { return newFixedLengthResponse(Response.Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage()); } catch (ResponseException re) { return newFixedLengthResponse(re.getStatus(), NanoHTTPD.MIME_PLAINTEXT, re.getMessage()); } } Map<String, String> parms = session.getParms(); parms.put(NanoHTTPD.QUERY_STRING_PARAMETER, session.getQueryParameterString()); return serve(session.getUri(), method, session.getHeaders(), parms, files); }
public static void executeInstance(NanoHTTPD server) { try { server.start(NanoHTTPD.SOCKET_READ_TIMEOUT, false); } catch (IOException ioe) { System.err.println("Couldn't start server:\n" + ioe); System.exit(-1); } System.out.println("Server started, Hit Enter to stop.\n"); try { System.in.read(); } catch (Throwable ignored) { } server.stop(); System.out.println("Server stopped.\n"); }
/** * Constructs an HTTP server on given hostname and port. */ public NanoHTTPD(String hostname, int port) { this.hostname = hostname; this.myPort = port; setTempFileManagerFactory(new DefaultTempFileManagerFactory()); setAsyncRunner(new DefaultAsyncRunner()); }
public void start() throws IOException { if (super.isAlive()) { //kill the server if its already running try { super.stop(); } catch (Exception e) { //ignore the exception } } try { super.start(NanoHTTPD.SOCKET_READ_TIMEOUT, false); } catch (SocketException e) { throw new IllegalStateException("The application under test must require android.permission.INTERNET " + "permission in its manifest", e); } logger.info(String.format("\nRunning Appium Espresso Server at port %d \n", DEFAULT_PORT)); router = new Router(); }
@Override protected void finalize() throws Throwable { for (NanoHTTPD server : servers.values()) { if (server != null && server.wasStarted()) { server.stop(); } } }
/** * Decodes parameters in percent-encoded URI-format ( e.g. "value=Jack%20Daniels&pass=Single%20Malt" ) and * adds them to given Map. NOTE: this doesn't support multiple identical keys due to the simplicity of Map. */ private void decodeParms(String parms, Map<String, String> p) { if (parms == null) { queryParameterString = ""; return; } queryParameterString = parms; StringTokenizer st = new StringTokenizer(parms, "&"); while (st.hasMoreTokens()) { String e = st.nextToken(); int sep = e.indexOf('='); if (sep >= 0) { p.put(decodePercent(e.substring(0, sep)).trim(), decodePercent(e.substring(sep + 1))); } else { p.put(decodePercent(e).trim(), ""); } } }
protected Response serveHttp(final IHTTPSession session) { return super.serve(session); }
/** * Forcibly closes all connections that are open. */ public synchronized void closeAllConnections() { stop(); }
public final boolean isAlive() { return wasStarted() && !myServerSocket.isClosed() && myThread.isAlive(); } }
/** * Forcibly closes all connections that are open. */ public synchronized void closeAllConnections() { for (Socket socket : openConnections) { safeClose(socket); } }
/** * Start the server. * * @throws IOException * if the socket is in use. */ public void start() throws IOException { start(NanoHTTPD.SOCKET_READ_TIMEOUT); }
/** * Stop the server. */ public void stop() { try { safeClose(myServerSocket); closeAllConnections(); if (myThread != null) { myThread.join(); } } catch (Exception e) { e.printStackTrace(); } }
/** * Decode parameters from a URL, handing the case where a single parameter name might have been * supplied several times, by return lists of values. In general these lists will contain a single * element. * * @param parms original <b>NanoHttpd</b> parameters values, as passed to the <code>serve()</code> method. * @return a map of <code>String</code> (parameter name) to <code>List<String></code> (a list of the values supplied). */ protected Map<String, List<String>> decodeParameters(Map<String, String> parms) { return this.decodeParameters(parms.get(QUERY_STRING_PARAMETER)); }
public NanoHTTPD.Response build() { if (file == null) { throw new IllegalArgumentException("File cannot be null!"); } if (fileName == null) { fileName = createDefaultFileName(); } NanoHTTPD.Response response = NanoHTTPD.newChunkedResponse(NanoHTTPD.Response.Status.OK, MimeType.APK.getValue(), createFileInputStream()); return FileHeaderDecorator.decorate(response, file, fileName); }
public NanoHTTPD.Response getResponse(String targetPath) { try { OrchidResource faviconResource = context.getResourceEntry("favicon.ico"); if(faviconResource != null) { InputStream is = faviconResource.getContentStream(); if(is != null) { return NanoHTTPD.newChunkedResponse(NanoHTTPD.Response.Status.OK, "image/x-icon", is); } } } catch (Exception e) { e.printStackTrace(); } return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.NOT_FOUND, "image/x-icon", ""); } }
public static void executeInstance(NanoHTTPD server) { try { server.start(); } catch (IOException ioe) { System.err.println("Couldn't start server:\n" + ioe); System.exit(-1); } System.out.println("Server started, Hit Enter to stop.\n"); try { System.in.read(); } catch (Throwable ignored) { } server.stop(); System.out.println("Server stopped.\n"); } }