예제 #1
0
  @Test
  public void testStabilityAfterWrongMagic() throws Exception {
    HttpHandler a =
        new HttpHandler() {

          @Override
          public void service(Request request, Response response) throws Exception {
            response.setStatus(200, "FINE");
          }
        };

    startHttpServer(a);

    // Send junk data
    final Socket s = new Socket("localhost", PORT);
    try {
      s.getOutputStream().write("junkjunkjunk".getBytes());
      s.getOutputStream().flush();
      while (s.getInputStream().read() != -1) {}
    } finally {
      s.close();
    }

    AjpForwardRequestPacket forward = new AjpForwardRequestPacket("GET", "/bob", PORT, 0);

    for (int i = 0; i < 1024; i++) {
      send(forward.toByteArray());
      AjpResponse ajpResponse = Utils.parseResponse(readAjpMessage());

      Assert.assertEquals(200, ajpResponse.getResponseCode());
      Assert.assertEquals("FINE", ajpResponse.getResponseMessage());
      closeClient();
    }
  }
예제 #2
0
  @Test
  public void testPingPong() throws Exception {
    startHttpServer(
        new HttpHandler() {

          @Override
          public void service(Request request, Response response) throws Exception {
            response.setStatus(200, "FINE");
          }
        },
        "/");

    final MemoryManager mm =
        httpServer.getListener(LISTENER_NAME).getTransport().getMemoryManager();
    final Buffer request = mm.allocate(512);
    request.put((byte) 0x12);
    request.put((byte) 0x34);
    request.putShort((short) 1);
    request.put(AjpConstants.JK_AJP13_CPING_REQUEST);
    request.flip();

    final Future<Buffer> responseFuture = send("localhost", PORT, request);
    Buffer response = responseFuture.get(10, TimeUnit.SECONDS);

    assertEquals('A', response.get());
    assertEquals('B', response.get());
    assertEquals((short) 1, response.getShort());
    assertEquals(AjpConstants.JK_AJP13_CPONG_REPLY, response.get());

    final AjpForwardRequestPacket headersPacket =
        new AjpForwardRequestPacket("GET", "/TestServlet/normal", 80, PORT);
    headersPacket.addHeader("Host", "localhost:80");
    send(headersPacket.toByteArray());

    AjpResponse ajpResponse = Utils.parseResponse(readAjpMessage());
    Assert.assertEquals("FINE", ajpResponse.getResponseMessage());
  }
예제 #3
0
  @Test
  public void test100ContinuePost() throws IOException, InstantiationException, Exception {
    HttpHandler httpHanlder =
        new HttpHandler() {

          @Override
          public void service(Request request, Response response) throws Exception {
            if (request.getHeader("Expect") != null) {
              response.sendAcknowledgement();

              final int length = request.getContentLength();
              final InputStream is = request.getInputStream();

              for (int i = 0; i < length; i++) {
                final int c = is.read();
                final int expected = (i % 'Z' - 'A') + 'A';
                if (c != expected) {
                  response.sendError(
                      400,
                      "Unexpected char["
                          + i
                          + "]. Expected: "
                          + ((char) expected)
                          + " but was: "
                          + ((char) c)
                          + "("
                          + c
                          + ")");
                  return;
                }
              }

              response.setStatus(200, "FINE");
            } else {
              response.sendError(500, "100-continue header has been lost?");
            }
          }
        };

    final int size = 1024;

    startHttpServer(httpHanlder);

    final AjpForwardRequestPacket headersPacket =
        new AjpForwardRequestPacket("POST", "/myresource", 80, PORT);
    headersPacket.addHeader("Content-Length", String.valueOf(size));
    headersPacket.addHeader("Host", "localhost:80");
    headersPacket.addHeader("Expect", "100-continue");

    send(headersPacket.toByteArray());

    byte[] postBody = new byte[size];
    for (int i = 0; i < postBody.length; i++) {
      postBody[i] = (byte) ((i % 'Z' - 'A') + 'A');
    }

    final AjpDataPacket dataPacket = new AjpDataPacket(postBody);
    send(dataPacket.toByteArray());

    AjpResponse ajpResponse = Utils.parseResponse(readAjpMessage());
    Assert.assertEquals(ajpResponse.getResponseMessage(), 200, ajpResponse.getResponseCode());
    Assert.assertEquals("FINE", ajpResponse.getResponseMessage());
  }