ConnectionHandler implements Runnable { void preProcessRequest(HttpRequest request) throws InvalidHttpRequestException { List<HttpHeader> hs = request.getHeader(HttpRequestHeader.HOST); if (hs.isEmpty()) { throw new InvalidHttpRequestException( "client sent HTTP/1.1 request without hostname (see RFC2616 section 14.23)"); } hs = request.getHeader(HttpGeneralHeader.CONNECTION); if (!hs.isEmpty()) { HttpHeader h = hs.get(0); String s = h.getFieldBody(); if ("close".equalsIgnoreCase(s)) { log.debug("turning connection persistence off for " + connection); connection.setPersistent(false); } } hs = request.getHeader(HttpRequestHeader.USER_AGENT); if (!hs.isEmpty()) { HttpHeader h = hs.get(0); String s = h.getFieldBody(); connection.setUserAgent(s); } } ConnectionHandler(HttpServer server, Connection connection); void run(); void handleRequests(); boolean isActive(); @Override String toString();  }
@Test public void preProcessRequest() throws Exception { MockHttpServer ms = new MockHttpServer(); MockConnection mc = new MockConnection(); ConnectionHandler ch = new ConnectionHandler(ms, mc); assertTrue(mc.isPersistent()); assertNull(mc.getUserAgent()); HttpRequest r = new HttpRequest(HttpMethod.GET, "/test"); r.addHeader(HttpRequestHeader.HOST, "test"); r.addHeader(HttpRequestHeader.USER_AGENT, "test user agent"); ch.preProcessRequest(r); assertTrue(mc.isPersistent()); assertEquals("test user agent", mc.getUserAgent()); }
@Test public void preProcessRequest_ConnectionClose() throws Exception { MockHttpServer ms = new MockHttpServer(); MockConnection mc = new MockConnection(); ConnectionHandler ch = new ConnectionHandler(ms, mc); assertTrue(mc.isPersistent()); HttpRequest r = new HttpRequest(HttpMethod.GET, "/test"); r.addHeader(HttpRequestHeader.HOST, "test"); r.addHeader(HttpGeneralHeader.CONNECTION, "close"); ch.preProcessRequest(r); assertFalse(mc.isPersistent()); }
@Test public void preProcessRequest_RFC2616_14_23_400IfNoHost() throws Exception { MockHttpServer ms = new MockHttpServer(); MockConnection mc = new MockConnection(); ConnectionHandler ch = new ConnectionHandler(ms, mc); HttpRequest r = new HttpRequest(HttpMethod.GET, "/test"); try { ch.preProcessRequest(r); fail("should throw exception"); } catch(InvalidHttpRequestException e) { String msg = e.getMessage(); log.info(msg); assertEquals("client sent HTTP/1.1 request without hostname (see RFC2616 section 14.23)", msg); } }