public String GetDomainFromRequest() {
    String toReturn = "";
    if (request != null
        && request.GetParams() != null
        && !request.GetParams().get("domain").isEmpty()) {
      toReturn = request.GetParams().get("domain").replace("%3a", ":").replace("%2f", "/");
    }

    return toReturn;
  }
예제 #2
0
  /**
   * Tests RequestParser.parse() with multipart request.
   *
   * @throws IOException I/O Exception
   * @throws QueryException query exception
   */
  @Test
  public void parseMultipartReq() throws IOException, QueryException {
    final String multiReq =
        "<http:request "
            + "xmlns:http='http://expath.org/ns/http-client' "
            + "method='POST' href='"
            + REST_ROOT
            + "'>"
            + "<http:header name='hdr1' value='hdr1val'/>"
            + "<http:header name='hdr2' value='hdr2val'/>"
            + "<http:multipart media-type='multipart/mixed' boundary='xxxx'>"
            + "<http:header name='p1hdr1' value='p1hdr1val'/>"
            + "<http:header name='p1hdr2' value='p1hdr2val'/>"
            + "<http:body media-type='text/plain'>"
            + "Part1"
            + "</http:body>"
            + "<http:header name='p2hdr1' value='p2hdr1val'/>"
            + "<http:body media-type='text/plain'>"
            + "Part2"
            + "</http:body>"
            + "<http:body media-type='text/plain'>"
            + "Part3"
            + "</http:body>"
            + "</http:multipart>"
            + "</http:request>";

    final DBNode dbNode1 = new DBNode(new IOContent(multiReq));
    final HttpRequestParser rp = new HttpRequestParser(null);
    final HttpRequest r = rp.parse(dbNode1.children().next(), null);

    assertEquals(2, r.attributes.size());
    assertEquals(2, r.headers.size());
    assertTrue(r.isMultipart);
    assertEquals(3, r.parts.size());

    // check parts
    final Iterator<Part> i = r.parts.iterator();
    Part part = i.next();
    assertEquals(2, part.headers.size());
    assertEquals(1, part.bodyContent.size());
    assertEquals(1, part.bodyAttrs.size());

    part = i.next();
    assertEquals(1, part.headers.size());
    assertEquals(1, part.bodyContent.size());
    assertEquals(1, part.bodyAttrs.size());

    part = i.next();
    assertEquals(0, part.headers.size());
    assertEquals(1, part.bodyContent.size());
    assertEquals(1, part.bodyAttrs.size());
  }
  private void processRequest() throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    DataOutputStream writer = new DataOutputStream(socket.getOutputStream());

    try {
      request.parseRequest(reader);
    } catch (HttpRequestException e) {
      System.out.println(e.getMessage());
      ResponseCode = 400;
      ResponseReason = "Bad Request";
    }

    if (isCrawlingRequest()) {
      CrawlerHandler crawler = new CrawlerHandler(GetDomainFromRequest());
      crawler.StartCrawling();
    }

    checkTypeOfResponse();

    writer.write(createHTTPResponseHeaders().getBytes());
    writer.write(createHTTPResponseBody(writer));
    writer.write(CRLF.getBytes());

    writer.flush();
    writer.close();
    reader.close();
  }
 private void checkTypeOfResponse() {
   responseBuilder = new ResponseFactory(request, root);
   if (ResponseCode != 400) {
     try {
       File temp = new File(root + request.GetRequestPath());
       System.out.println(request.GetMessageBody());
       System.out.println(request.GetRequestPath());
       if (temp.isFile() || request.GetRequestPath().equals(PARAMS_PAGE)) {
         switch (request.GetRequestType()) {
           case GET:
             responseBuilder = new GETResponse(request, root);
             ResponseCode = 200;
             ResponseReason = "OK";
             break;
           case POST:
             responseBuilder = new GETResponse(request, root);
             ResponseCode = 200;
             ResponseReason = "OK";
             break;
           case TRACE:
             responseBuilder = new TRACEResponse(request, root);
             ResponseCode = 200;
             ResponseReason = "OK";
             break;
           case HEAD:
             responseBuilder = new HEADRespones(request, root);
             ResponseCode = 200;
             ResponseReason = "OK";
             break;
           default:
             ResponseCode = 501;
             ResponseReason = "Unimplemented Method";
             break;
         }
       } else {
         ResponseCode = 404;
         ResponseReason = "File Not Found";
       }
     } catch (Exception e) {
       ResponseCode = 500;
       ResponseReason = "Internal Server Error";
     }
   }
 }
예제 #5
0
  /**
   * Tests RequestParser.parse() with normal (not multipart) request.
   *
   * @throws IOException I/O Exception
   * @throws QueryException query exception
   */
  @Test
  public void parseRequest() throws IOException, QueryException {
    // Simple HTTP request with no errors
    final String req =
        "<http:request "
            + "xmlns:http='http://expath.org/ns/http-client' "
            + "method='POST' href='"
            + REST_ROOT
            + "'>"
            + "<http:header name='hdr1' value='hdr1val'/>"
            + "<http:header name='hdr2' value='hdr2val'/>"
            + "<http:body media-type='text/xml'>"
            + "Test body content"
            + "</http:body>"
            + "</http:request>";
    final DBNode dbNode = new DBNode(new IOContent(req));
    final HttpRequestParser rp = new HttpRequestParser(null);
    final HttpRequest r = rp.parse(dbNode.children().next(), null);

    assertEquals(2, r.attributes.size());
    assertEquals(2, r.headers.size());
    assertTrue(r.bodyContent.size() != 0);
    assertEquals(1, r.payloadAttrs.size());
  }
예제 #6
0
  /**
   * Tests if errors are thrown when some mandatory attributes are missing in a <http:request/>,
   * <http:body/> or <http:multipart/>.
   *
   * @throws IOException I/O Exception
   */
  @Test
  public void errors() throws IOException {

    // Incorrect requests
    final List<byte[]> falseReqs = new ArrayList<>();

    // Request without method
    final byte[] falseReq1 =
        token(
            "<http:request "
                + "xmlns:http='http://expath.org/ns/http-client' "
                + "href='"
                + REST_ROOT
                + "'/>");
    falseReqs.add(falseReq1);

    // Request with send-authorization and no credentials
    final byte[] falseReq2 =
        token(
            "<http:request "
                + "xmlns:http='http://expath.org/ns/http-client' "
                + "method='GET' href='"
                + REST_ROOT
                + "' "
                + "send-authorization='true'/>");
    falseReqs.add(falseReq2);

    // Request with send-authorization and only username
    final byte[] falseReq3 =
        token(
            "<http:request "
                + "xmlns:http='http://expath.org/ns/http-client' "
                + "method='GET' href='"
                + REST_ROOT
                + "' "
                + "send-authorization='true' username='******'/>");
    falseReqs.add(falseReq3);

    // Request with body that has no media-type
    final byte[] falseReq4 =
        token(
            "<http:request "
                + "xmlns:http='http://expath.org/ns/http-client' "
                + "method='POST' href='"
                + REST_ROOT
                + "'>"
                + "<http:body>"
                + "</http:body>"
                + "</http:request>");
    falseReqs.add(falseReq4);

    // Request with multipart that has no media-type
    final byte[] falseReq5 =
        token(
            "<http:request method='POST' "
                + "xmlns:http='http://expath.org/ns/http-client' "
                + "href='"
                + REST_ROOT
                + "'>"
                + "<http:multipart boundary='xxx'>"
                + "</http:multipart>"
                + "</http:request>");
    falseReqs.add(falseReq5);

    // Request with multipart with part that has a body without media-type
    final byte[] falseReq6 =
        token(
            "<http:request method='POST' "
                + "xmlns:http='http://expath.org/ns/http-client' "
                + "href='"
                + REST_ROOT
                + "'>"
                + "<http:multipart boundary='xxx'>"
                + "<http:header name='hdr1' value-='val1'/>"
                + "<http:body media-type='text/plain'>"
                + "Part1"
                + "</http:body>"
                + "<http:header name='hdr1' value-='val1'/>"
                + "<http:body>"
                + "Part1"
                + "</http:body>"
                + "</http:multipart>"
                + "</http:request>");
    falseReqs.add(falseReq6);

    // Request with schema different from http
    final byte[] falseReq7 =
        token(
            "<http:request "
                + "xmlns:http='http://expath.org/ns/http-client' "
                + "href='ftp://basex.org'/>");
    falseReqs.add(falseReq7);

    // Request with content and method which must be empty
    final byte[] falseReq8 =
        token(
            "<http:request "
                + "xmlns:http='http://expath.org/ns/http-client' "
                + "method='DELETE' href='"
                + REST_ROOT
                + "'>"
                + "<http:body media-type='text/plain'>"
                + "</http:body>"
                + "</http:request>");
    falseReqs.add(falseReq8);

    for (final byte[] falseReq : falseReqs) {
      final DBNode dbNode = new DBNode(new IOContent(falseReq));
      try {
        final HttpRequestParser rp = new HttpRequestParser(null);
        rp.parse(dbNode.children().next(), null);
        fail("Exception not thrown");
      } catch (final QueryException ex) {
        assertTrue(ex.getMessage().contains(ErrType.HC.toString()));
      }
    }
  }
 private boolean isCrawlingRequest() {
   return (request.GetRequestType() == eRequestType.POST
       && request.GetRequestPath().contains("execResults.html"));
 }