public static NanoHTTPD.IHTTPSession createHttpSession(NanoHTTPD.Method method, @Nullable Map<String, String> params) { NanoHTTPD.IHTTPSession res = mock(NanoHTTPD.IHTTPSession.class); when(res.getMethod()).thenReturn(method); if (params != null) { when(res.getParms()).thenReturn(params); } return res; } }
Log.e(TAG,"unable to parse body of request",re); String inPassword = session.getParms().get("p"); String inSid = session.getCookies().read("sid");
@Override public NanoHTTPD.Response serve(NanoHTTPD.IHTTPSession session) { if (session.getMethod() != NanoHTTPD.Method.POST) { return newFixedLengthResponse(METHOD_NOT_ALLOWED, MIME_PLAINTEXT, null); } String levelStr = session.getParms().get(PARAM_LEVEL); if (levelStr == null || levelStr.isEmpty()) { return newFixedLengthResponse(BAD_REQUEST, MIME_PLAINTEXT, format("Parameter '%s' is missing", PARAM_LEVEL)); } try { LoggerLevel level = LoggerLevel.valueOf(levelStr); logging.changeLevel(level); return newFixedLengthResponse(OK, MIME_PLAINTEXT, null); } catch (IllegalArgumentException e) { Loggers.get(ChangeLogLevelHttpAction.class).debug("Value '{}' for parameter '" + PARAM_LEVEL + "' is invalid: {}", levelStr, e); return newFixedLengthResponse(BAD_REQUEST, MIME_PLAINTEXT, format("Value '%s' for parameter '%s' is invalid", levelStr, PARAM_LEVEL)); } } }
public Response serve(IHTTPSession session) { try { Map<String, String> params = session.getParms(); Map<String, String> body = new HashMap<String, String>(); session.parseBody(body);
/** * 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 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 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); }
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 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); }
public Response get(UriResource uriResource, Map<String, String> urlParams, IHTTPSession session) { StringBuilder text = new StringBuilder("<html><body>"); text.append("<h1>Url: "); text.append(session.getUri()); text.append("</h1><br>"); Map<String, String> queryParams = session.getParms(); if (queryParams.size() > 0) { for (Map.Entry<String, String> entry : queryParams.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); text.append("<p>Param '"); text.append(key); text.append("' = "); text.append(value); text.append("</p>"); } } else { text.append("<p>no params in url</p><br>"); } return NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), text.toString()); } }
@Override public NanoHTTPD.Response serve(NanoHTTPD.IHTTPSession session) { if (session.getMethod() != NanoHTTPD.Method.POST) { return newFixedLengthResponse(METHOD_NOT_ALLOWED, MIME_PLAINTEXT, null); } String levelStr = session.getParms().get(PARAM_LEVEL); if (levelStr == null || levelStr.isEmpty()) { return newFixedLengthResponse(BAD_REQUEST, MIME_PLAINTEXT, format("Parameter '%s' is missing", PARAM_LEVEL)); } try { LoggerLevel level = LoggerLevel.valueOf(levelStr); logging.changeLevel(level); return newFixedLengthResponse(OK, MIME_PLAINTEXT, null); } catch (IllegalArgumentException e) { Loggers.get(ChangeLogLevelHttpAction.class).debug("Value '{}' for parameter '" + PARAM_LEVEL + "' is invalid: {}", levelStr, e); return newFixedLengthResponse(BAD_REQUEST, MIME_PLAINTEXT, format("Value '%s' for parameter '%s' is invalid", levelStr, PARAM_LEVEL)); } } }
@Override public Response serve(IHTTPSession session) { try { session.parseBody(new HashMap<String, String>()); Map<String, String> params = session.getParms(); String urlRequested = session.getUri(); String action = params.get("action"); log.debug(session.getMethod().name()+": "+urlRequested+" : "+action); if (action.equals("getInfo")) { return getInfo(); } else if (action.equals("addUser")) { return addUser(params.get("userName"), params.get("blob"), params.get("clientKey")); } return new Response(Response.Status.NOT_FOUND, "", ""); }catch(Exception ex) { return new Response(Response.Status.INTERNAL_ERROR, "text/html", ex.getMessage()); } }
Map<String, String> parms = session.getParms(); String type = parms.get("type"); String html = new String();