Exemple #1
0
  @Override
  public Response serve(IHTTPSession session) {
    Method method = session.getMethod();
    if (Method.POST.equals(method) && session.getUri().startsWith("/upload")) {
      Map<String, String> files = new HashMap<String, String>();
      try {
        session.parseBody(files);
        String fileHEX = JVMClassParser.fileHEX(files.get("JvmClass"));

        return new Response(Status.OK, MIME_HTML, fileHEX);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    Response response = new Response(Status.OK, MIME_HTML, "error");
    try {
      response =
          new Response(Status.OK, MIME_HTML, new FileInputStream("target/classes/index.html"));
      if (null == session.getCookies().read("HTTPD_SESSION")) {
        response.addHeader("Set-Cookie", "HTTPD_SESSION=" + UUID.randomUUID());
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    return response;
  }
Exemple #2
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
  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 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 createResponse(
            Response.Status.INTERNAL_ERROR,
            NanoHTTPD.MIME_PLAINTEXT,
            "INTERNAL ERRROR: given path is not a directory (" + homeDir + ").");
      }
    }
    return respond(Collections.unmodifiableMap(header), uri);
  }
  private Response respond(Map<String, String> headers, IHTTPSession session, String uri) {
    // First let's handle CORS OPTION query
    Response r;
    if (cors != null && Method.OPTIONS.equals(session.getMethod())) {
      r = new NanoHTTPD.Response(Response.Status.OK, MIME_PLAINTEXT, null, 0);
    } else {
      r = defaultRespond(headers, session, uri);
    }

    if (cors != null) {
      r = addCORSHeaders(headers, r, cors);
    }
    return r;
  }
  public Response serve(IHTTPSession session) {
    Map<String, String> header = session.getHeaders();
    Map<String, String> parms = session.getParms();
    String uri = session.getUri();

    {
      if (uri.startsWith("/")) {
        int nextSlash = uri.indexOf("/", 1);
        String appName = uri.substring(1, nextSlash);

        StringBuilder b = new StringBuilder();
        b.append(session.getMethod()).append(" '").append(uri).append("' \n");

        Iterator<String> e = header.keySet().iterator();
        while (e.hasNext()) {
          String value = e.next();
          b.append("  HDR: '")
              .append(value)
              .append("' = '")
              .append(header.get(value))
              .append("'\n");
        }
        e = parms.keySet().iterator();
        while (e.hasNext()) {
          String value = e.next();
          b.append("  PRM: '")
              .append(value)
              .append("' = '")
              .append(header.get(value))
              .append("'\n");
        }
        WebLogger.getLogger(appName).i(t, b.toString());
      }
    }

    // Make sure we won't die of an exception later
    File root = new File(ODKFileUtils.getOdkFolder());
    try {
      ODKFileUtils.verifyExternalStorageAvailability();
      if (!root.exists() || !root.isDirectory()) {
        return getInternalErrorResponse(
            "given path is not a directory (" + root.getAbsolutePath() + ").");
      }
    } catch (Exception e) {
      return getInternalErrorResponse(
          "exception " + e.toString() + " accessing directory (" + root.getAbsolutePath() + ").");
    }
    return respond(Collections.unmodifiableMap(header), session, uri);
  }
Exemple #7
0
  @Override
  public Response serve(IHTTPSession session) {
    Map<String, List<String>> decodedQueryParameters =
        decodeParameters(session.getQueryParameterString());

    StringBuilder sb = new StringBuilder();
    sb.append("<html>");
    sb.append("<head><title>Debug Server</title></head>");
    sb.append("<body>");
    sb.append("<h1>Debug Server</h1>");

    sb.append("<p><blockquote><b>URI</b> = ")
        .append(String.valueOf(session.getUri()))
        .append("<br />");

    sb.append("<b>Method</b> = ")
        .append(String.valueOf(session.getMethod()))
        .append("</blockquote></p>");

    sb.append("<h3>Headers</h3><p><blockquote>")
        .append(toString(session.getHeaders()))
        .append("</blockquote></p>");

    sb.append("<h3>Parms</h3><p><blockquote>")
        .append(toString(session.getParms()))
        .append("</blockquote></p>");

    sb.append("<h3>Parms (multi values?)</h3><p><blockquote>")
        .append(toString(decodedQueryParameters))
        .append("</blockquote></p>");

    try {
      Map<String, String> files = new HashMap<String, String>();
      session.parseBody(files);
      sb.append("<h3>Files</h3><p><blockquote>")
          .append(toString(files))
          .append("</blockquote></p>");
    } catch (Exception e) {
      e.printStackTrace();
    }

    sb.append("</body>");
    sb.append("</html>");
    return newFixedLengthResponse(sb.toString());
  }
  /**
   * 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);
  }