示例#1
0
  @Override
  public Response serve(IHTTPSession session) {
    Map<String, String> header = session.getHeaders();
    Map<String, String> parms = session.getParms();
    String uri = session.getUri();

    if (logRequests) {
      Log.i(TAG, session.getMethod() + " '" + uri + "' ");

      Iterator<String> e = header.keySet().iterator();
      while (e.hasNext()) {
        String value = e.next();
        Log.i(TAG, "  HDR: '" + value + "' = '" + header.get(value) + "'");
      }
      e = parms.keySet().iterator();
      while (e.hasNext()) {
        String value = e.next();
        Log.i(TAG, "  PRM: '" + value + "' = '" + parms.get(value) + "'");
      }
    }

    if (!webRoot.isDirectory()) {
      return createResponse(
          Response.Status.INTERNAL_ERROR,
          NanoHTTPD.MIME_PLAINTEXT,
          "INTERNAL ERRROR: given path is not a directory (" + webRoot + ").");
    }

    return respond(Collections.unmodifiableMap(header), uri);
  }
  /**
   * 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);
  }