Method method = session.getMethod(); if (Method.PUT.equals(method) || Method.POST.equals(method)) try { session.parseBody(content); } catch (IOException ioe) { Log.e(TAG,"unable to parse body of request",ioe); Log.e(TAG,"unable to parse body of request",re); String inPassword = session.getParms().get("p"); String inSid = session.getCookies().read("sid"); session.getCookies().set(cookie); Uri uri = Uri.parse(session.getUri()); List<String> pathSegs = uri.getPathSegments(); session.getCookies().unloadQueue(response); return response; session.getCookies().unloadQueue(response); return response;
private Response serveFile(IHTTPSession session, MediaFile file) { LOGGER.debug("Serving: " + file.getFileAsPath()); Response res; Map<String, String> header = session.getHeaders(); LOGGER.debug("Headers: " + header); try { if (session.getMethod() == Method.HEAD) { res = newFixedLengthResponse(Response.Status.OK, mime, null, fileLen);
@Override public Response serve(IHTTPSession session) { if (session.getMethod().equals(Method.OPTIONS)) { Response result = NanoHTTPD.newFixedLengthResponse(Response.Status.NO_CONTENT, "text/plain; charset=utf-8", ""); result.addHeader("Access-Control-Allow-Origin", "*"); return result; if (session.getUri().startsWith(staticController.getRequestMappingURL())) { return staticController.doGet(session); return result; HttpContoller controller = controllers.get(session.getUri()); ModelAndView model = null; if (controller == null) { } else { try { switch (session.getMethod()) { case GET: model = controller.doGet(session);
String msg = "<html><body><h1>Hello server</h1>\n"; String msgUnauthorized = "<html><body><h1>Unauthorized</h1>\n"; Method method = session.getMethod(); String uri = session.getUri(); if ((session.getHeaders().containsKey("authorization") && session .getHeaders().get("authorization") .equalsIgnoreCase(AUTH_KEY))) { if (session.getHeaders().containsKey("sample")) { logger.info("Sample Header value {}: " + session.getHeaders().get("sample")); msg = session.getHeaders().get("sample"); logger.info("SERVER_RESPONSE_200: " + msg); return new fi.iki.elonen.NanoHTTPD.Response(Status.OK,
@Override public Response serve(IHTTPSession session) { String uri = session.getUri(); LOGGER.info("Incoming: " + session.getRemoteIpAddress() + " " + session.getMethod() + " " + uri);
if (object instanceof UriResponder) { UriResponder responder = (UriResponder) object; switch (session.getMethod()) { case GET: return responder.get(this, urlParams, session); return responder.delete(this, urlParams, session); default: return responder.other(session.getMethod().toString(), this, urlParams, session);
String urlRequested = session.getUri();
OrchidRoute matchingRoute = null; String route = "/" + OrchidUtils.normalizePath(session.getUri()); switch (session.getMethod()) { case GET: matchingRoute = findRoute(route, getRoutes); break; case POST: session.parseBody(files); matchingRoute = findRoute(route, postRoutes); break; case PUT: session.parseBody(files); matchingRoute = findRoute(route, putRoutes); break;
@Override public Response serve(IHTTPSession session) { try { session.parseBody(new HashMap<String, String>()); } catch (IOException e) { e.printStackTrace(); return respond(session, makeResponse("500", "IOException", null)); } catch (ResponseException e) { e.printStackTrace(); return respond(session, makeResponse("501", "ResponseException", null)); } String uri = session.getUri(); Map<String, List<String>> parameters = session.getParameters(); Method method = session.getMethod(); Log.d("CoreHttpServer", "uri:"+uri); Log.d("CoreHttpServer", "parameters:"+ JSONObject.toJSONString(parameters)); Log.d("CoreHttpServer", "method:"+ method.toString()); if(!routeMap.containsKey(uri)){ return respond(session, makeResponse("404", "not found", null)); }else{ try{ Object result = routeMap.get(uri).server(session, method, uri, parameters); return respond(session, makeResponse("0", "ok", result)); }catch (CommonException e){ return respond(session, makeResponse(e.getCode(), e.getMessage(), null)); } } }
@Override public Response serve(final IHTTPSession session) { Map<String, String> headers = session.getHeaders(); if (isWebsocketRequested(session)) { if (!NanoWSD.HEADER_WEBSOCKET_VERSION_VALUE.equalsIgnoreCase(headers.get(NanoWSD.HEADER_WEBSOCKET_VERSION))) { return newFixedLengthResponse(Response.Status.BAD_REQUEST, NanoHTTPD.MIME_PLAINTEXT, "Invalid Websocket-Version " + headers.get(NanoWSD.HEADER_WEBSOCKET_VERSION)); } if (!headers.containsKey(NanoWSD.HEADER_WEBSOCKET_KEY)) { return newFixedLengthResponse(Response.Status.BAD_REQUEST, NanoHTTPD.MIME_PLAINTEXT, "Missing Websocket-Key"); } WebSocket webSocket = openWebSocket(session); Response handshakeResponse = webSocket.getHandshakeResponse(); try { handshakeResponse.addHeader(NanoWSD.HEADER_WEBSOCKET_ACCEPT, makeAcceptKey(headers.get(NanoWSD.HEADER_WEBSOCKET_KEY))); } catch (NoSuchAlgorithmException e) { return newFixedLengthResponse(Response.Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, "The SHA-1 Algorithm required for websockets is not available on the server."); } if (headers.containsKey(NanoWSD.HEADER_WEBSOCKET_PROTOCOL)) { handshakeResponse.addHeader(NanoWSD.HEADER_WEBSOCKET_PROTOCOL, headers.get(NanoWSD.HEADER_WEBSOCKET_PROTOCOL).split(",")[0]); } return handshakeResponse; } else { return serveHttp(session); } }
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); } } }
public Response serve(IHTTPSession session) { Map<String, String> header = session.getHeaders(); Map<String, String> parms = session.getParms(); String uri = session.getUri(); if (!quiet) { System.out.println(session.getMethod() + " '" + uri + "' "); Iterator<String> e = header.keySet().iterator(); while (e.hasNext()) { String value = e.next(); System.out.println(" HDR: '" + value + "' = '" + header.get(value) + "'"); } e = parms.keySet().iterator(); while (e.hasNext()) { String value = e.next(); System.out.println(" PRM: '" + value + "' = '" + parms.get(value) + "'"); } } for (File homeDir : getRootDirs()) { // Make sure we won't die of an exception later if (!homeDir.isDirectory()) { return getInternalErrorResponse("given path is not a directory (" + homeDir + ")."); } } return respond(Collections.unmodifiableMap(header), session, uri); }
/** * 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); }
@Override public Response serve(IHTTPSession session) { BaseResponse response; final Map<String, String> files = new LinkedHashMap<>(); try { session.parseBody(files); response = router.route(session.getUri(), session.getMethod(), files); } catch (RuntimeException | IOException | ResponseException e) { response = new AppiumResponse<>(AppiumStatus.UNKNOWN_ERROR, Log.getStackTraceString(e)); } if (response instanceof AppiumResponse) { AppiumResponse appiumResponse = (AppiumResponse) response; if (appiumResponse.getStatus() == AppiumStatus.SUCCESS) { logger.info(String.format("Responding to server with value: %s", abbreviate(String.valueOf(appiumResponse.getValue()), 300))); } else { logger.info(String.format("Responding to server with error: %s", appiumResponse.getValue())); } } try { return buildFixedLengthResponse(response); } catch (RuntimeException e) { return buildFixedLengthResponse( new AppiumResponse<>(AppiumStatus.UNKNOWN_ERROR, Log.getStackTraceString(e))); } }
/** * Override this to customize the server. * <p/> * <p/> * (By default, this delegates to serveFile() and allows directory listing.) * * @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 new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage()); } catch (ResponseException re) { return new Response(re.getStatus(), MIME_PLAINTEXT, re.getMessage()); } } Map<String, String> parms = session.getParms(); parms.put(QUERY_STRING_PARAMETER, session.getQueryParameterString()); return serve(session.getUri(), method, session.getHeaders(), parms, files); }
/** * Search in the mappings if the given url matches some of the rules If * there are more than one marches returns the rule with less parameters * e.g. mapping 1 = /user/:id mapping 2 = /user/help if the incoming uri * is www.example.com/user/help - mapping 2 is returned if the incoming * uri is www.example.com/user/3232 - mapping 1 is returned * * @param url * @return */ public Response process(IHTTPSession session) { String work = normalizeUri(session.getUri()); Map<String, String> params = null; UriResource uriResource = error404Url; for (UriResource u : routePrioritizer.getPrioritizedRoutes()) { params = u.match(work); if (params != null) { uriResource = u; break; } } return uriResource.process(params, session); }
/** * Override this to customize the server. * <p/> * <p/> * (By default, this delegates to serveFile() and allows directory listing.) * * @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 new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage()); } catch (ResponseException re) { return new Response(re.getStatus(), MIME_PLAINTEXT, re.getMessage()); } } Map<String, String> parms = session.getParms(); parms.put(QUERY_STRING_PARAMETER, session.getQueryParameterString()); return serve(session.getUri(), method, session.getHeaders(), parms, files); }
@Override public Response serve(IHTTPSession session) { Map<String, String> header = session.getHeaders(); Map<String, String> parms = session.getParms(); String uri = session.getUri(); if (!this.quiet) { System.out.println(session.getMethod() + " '" + uri + "' "); Iterator<String> e = header.keySet().iterator(); while (e.hasNext()) { String value = e.next(); System.out.println(" HDR: '" + value + "' = '" + header.get(value) + "'"); } e = parms.keySet().iterator(); while (e.hasNext()) { String value = e.next(); System.out.println(" PRM: '" + value + "' = '" + parms.get(value) + "'"); } } for (File homeDir : this.rootDirs) { // Make sure we won't die of an exception later if (!homeDir.isDirectory()) { return getInternalErrorResponse("given path is not a directory (" + homeDir + ")."); } } return respond(Collections.unmodifiableMap(header), session, uri); }