@Test public void testWriteSharedMessageCompleted() { Mockery context = new Mockery(); context.setThreadingPolicy(new Synchroniser()); final IoServiceEx httpService = context.mock(IoServiceEx.class); final IoHandler httpHandler = context.mock(IoHandler.class, "httpHandler"); final IoSessionEx session = context.mock(IoSessionEx.class); final IoFilterChain filterChain = context.mock(IoFilterChain.class); final IoHandler handler = context.mock(IoHandler.class); final WriteRequestQueue writeRequestQueue = context.mock(WriteRequestQueue.class); final WriteRequest writeRequest = context.mock(WriteRequest.class); final WriteFuture writeFuture = context.mock(WriteFuture.class); final HttpBuffer buf = httpAllocator.wrap(ByteBuffer.wrap(new byte[] { 0x00 })); final HttpContentMessage message = new HttpContentMessage(buf, false); buf.mark(); buf.putMessage("KEY", message); final Executor executor = new Executor() { @Override public void execute(Runnable command) { command.run(); } }; context.checking(new Expectations() { { try { allowing(httpService).getTransportMetadata(); will(returnValue(new DefaultTransportMetadata(HttpProtocol.NAME))); allowing(httpService).getHandler(); will(returnValue(httpHandler)); allowing(httpService).getSessionConfig(); will(returnValue(new DefaultIoSessionConfigEx())); allowing(httpService).getThreadLocalWriteRequest(with(any(int.class))); will(returnValue(new ShareableWriteRequest())); allowing(session).getIoLayer(); will(returnValue(0)); allowing(session).getIoThread(); will(returnValue(currentThread())); allowing(session).getIoExecutor(); will(returnValue(IMMEDIATE_EXECUTOR)); allowing(session).getConfig(); will(returnValue(new DefaultIoSessionConfigEx())); allowing(session).isClosing(); will(returnValue(false)); allowing(session).getFilterChain(); will(returnValue(filterChain)); allowing(session).getIoThread(); will(returnValue(Thread.currentThread())); allowing(session).getIoExecutor(); will(returnValue(executor)); allowing(writeRequest).getMessage(); will(returnValue(buf)); allowing(writeRequest).getFuture(); will(returnValue(writeFuture)); allowing(session).getWrittenBytes(); will(returnValue(0L)); allowing(session).resumeRead(); oneOfSslUtilsIsSecure(this,session); allowing(writeRequestQueue).isEmpty(with(aNonNull(IoSession.class))); will(returnValue(false)); oneOf(writeRequestQueue).poll(with(aNonNull(IoSession.class))); will(returnValue(writeRequest)); allowing(writeRequestQueue).poll(with(aNonNull(IoSession.class))); will(returnValue(null)); oneOf(session).write(with(aNonNull(HttpResponseMessage.class))); will(returnValue(DefaultWriteFuture.newWrittenFuture(session))); oneOf(session).write(with(aNonNull(HttpContentMessage.class))); will(returnValue(DefaultWriteFuture.newWrittenFuture(session))); oneOf(writeFuture).setWritten(); never(handler).messageSent(with(aNonNull(IoSession.class)), with(aNonNull(IoBuffer.class))); } catch (Exception e) { throw new RuntimeException(e); } } }); HttpAcceptProcessor processor = new HttpAcceptProcessor(); ResourceAddressFactory addressFactory = ResourceAddressFactory.newResourceAddressFactory(); ResourceOptions options = ResourceOptions.FACTORY.newResourceOptions(); options.setOption(TRANSPORT_URI, "pipe: ResourceAddress address = addressFactory.newResourceAddress("http: ResourceAddress remoteAddress = addressFactory.newResourceAddress("http: DefaultHttpSession httpSession = new DefaultHttpSession(httpService, processor, address, remoteAddress, session, httpAllocator, new Properties()); httpSession.setHandler(handler); httpSession.setWriteRequestQueue(writeRequestQueue); httpSession.commit(); httpSession.getProcessor().flush(httpSession); context.assertIsSatisfied(); }
public void commit(final DefaultHttpSession session) { final IoSession parent = session.getParent(); if (parent == null || parent.isClosing()) { return; } final CommitFuture commitFuture = session.getCommitFuture(); if (commitFuture.isCommitted()) { return; } HttpResponseMessage httpResponse = new HttpResponseMessage(); httpResponse.setStatus(session.getStatus()); httpResponse.setReason(session.getReason()); httpResponse.setVersion(session.getVersion()); httpResponse.setHeaders(session.getWriteHeaders()); httpResponse.setInjectableHeaders(session.getLocalAddress().getOption(INJECTABLE_HEADERS)); httpResponse.setCookies(session.getWriteCookies()); httpResponse.setBlockPadding(session.getParameter(".kbp") != null); boolean complete = (session.getCurrentWriteRequest() == null) && session.getWriteRequestQueue().isEmpty(session) && session.isClosing(); IoBufferAllocatorEx<? extends HttpBuffer> allocator = session.getBufferAllocator(); HttpBuffer unsharedEmpty = allocator.wrap(allocator.allocate(0)); httpResponse.setContent(new HttpContentMessage(unsharedEmpty, complete)); if (session.getMethod() == HttpMethod.HEAD) { httpResponse.setContentExcluded(true); } switch (session.getVersion()) { case HTTP_1_1: boolean isConnectionClose = "close".equals(session.getWriteHeader("Connection")); if (isConnectionClose) { writeNonPersistentResponse(session, parent, commitFuture, httpResponse); } else { if (!httpResponse.isComplete()) { String contentLength = session.getWriteHeader(HEADER_CONTENT_LENGTH); if (contentLength == null) { if (session.isClosing() || session.isWriteShutdown()) { long scheduledWriteBytes = session.getScheduledWriteBytes(); httpResponse.setHeader(HEADER_CONTENT_LENGTH, valueOf(scheduledWriteBytes)); } else if (!httpResponse.isContentLengthImplicit()) { if ( session.isChunkingNecessary() ) { httpResponse.setHeader("Transfer-Encoding", "chunked"); session.setChunked(true); } } } } writePersistentResponse(parent, commitFuture, httpResponse); } break; case HTTP_1_0: writeNonPersistentResponse(session, parent, commitFuture, httpResponse); break; default: throw new IllegalStateException("Unexpected HTTP version: " + session.getVersion()); } }
HttpAcceptProcessor extends BridgeAcceptProcessor<DefaultHttpSession> { public void commit(final DefaultHttpSession session) { final IoSession parent = session.getParent(); if (parent == null || parent.isClosing()) { return; } final CommitFuture commitFuture = session.getCommitFuture(); if (commitFuture.isCommitted()) { return; } HttpResponseMessage httpResponse = new HttpResponseMessage(); httpResponse.setStatus(session.getStatus()); httpResponse.setReason(session.getReason()); httpResponse.setVersion(session.getVersion()); httpResponse.setHeaders(session.getWriteHeaders()); httpResponse.setInjectableHeaders(session.getLocalAddress().getOption(INJECTABLE_HEADERS)); httpResponse.setCookies(session.getWriteCookies()); httpResponse.setBlockPadding(session.getParameter(".kbp") != null); boolean complete = (session.getCurrentWriteRequest() == null) && session.getWriteRequestQueue().isEmpty(session) && session.isClosing(); IoBufferAllocatorEx<? extends HttpBuffer> allocator = session.getBufferAllocator(); HttpBuffer unsharedEmpty = allocator.wrap(allocator.allocate(0)); httpResponse.setContent(new HttpContentMessage(unsharedEmpty, complete)); if (session.getMethod() == HttpMethod.HEAD) { httpResponse.setContentExcluded(true); } switch (session.getVersion()) { case HTTP_1_1: boolean isConnectionClose = "close".equals(session.getWriteHeader("Connection")); if (isConnectionClose) { writeNonPersistentResponse(session, parent, commitFuture, httpResponse); } else { if (!httpResponse.isComplete()) { String contentLength = session.getWriteHeader(HEADER_CONTENT_LENGTH); if (contentLength == null) { if (session.isClosing() || session.isWriteShutdown()) { long scheduledWriteBytes = session.getScheduledWriteBytes(); httpResponse.setHeader(HEADER_CONTENT_LENGTH, valueOf(scheduledWriteBytes)); } else if (!httpResponse.isContentLengthImplicit()) { if ( session.isChunkingNecessary() ) { httpResponse.setHeader("Transfer-Encoding", "chunked"); session.setChunked(true); } } } } writePersistentResponse(parent, commitFuture, httpResponse); } break; case HTTP_1_0: writeNonPersistentResponse(session, parent, commitFuture, httpResponse); break; default: throw new IllegalStateException("Unexpected HTTP version: " + session.getVersion()); } } }
HttpAcceptProcessor extends BridgeAcceptProcessor<DefaultHttpSession> { public void commit(final DefaultHttpSession session) { final IoSession parent = session.getParent(); if (parent == null || parent.isClosing()) { return; } final CommitFuture commitFuture = session.getCommitFuture(); if (commitFuture.isCommitted()) { return; } HttpResponseMessage httpResponse = new HttpResponseMessage(); httpResponse.setStatus(session.getStatus()); httpResponse.setReason(session.getReason()); httpResponse.setVersion(session.getVersion()); httpResponse.setHeaders(session.getWriteHeaders()); httpResponse.setInjectableHeaders(session.getLocalAddress().getOption(INJECTABLE_HEADERS)); httpResponse.setCookies(session.getWriteCookies()); httpResponse.setBlockPadding(session.getParameter(".kbp") != null); boolean complete = (session.getCurrentWriteRequest() == null) && session.getWriteRequestQueue().isEmpty(session) && session.isClosing(); IoBufferAllocatorEx<? extends HttpBuffer> allocator = session.getBufferAllocator(); HttpBuffer unsharedEmpty = allocator.wrap(allocator.allocate(0)); httpResponse.setContent(new HttpContentMessage(unsharedEmpty, complete)); if (session.getMethod() == HttpMethod.HEAD) { httpResponse.setContentExcluded(true); } switch (session.getVersion()) { case HTTP_1_1: boolean isConnectionClose = "close".equals(session.getWriteHeader("Connection")); if (isConnectionClose) { writeNonPersistentResponse(session, parent, commitFuture, httpResponse); } else { if (!httpResponse.isComplete()) { String contentLength = session.getWriteHeader(HEADER_CONTENT_LENGTH); if (contentLength == null) { if (session.isClosing() || session.isWriteShutdown()) { long scheduledWriteBytes = session.getScheduledWriteBytes(); httpResponse.setHeader(HEADER_CONTENT_LENGTH, valueOf(scheduledWriteBytes)); } else if (!httpResponse.isContentLengthImplicit()) { if ( session.isChunkingNecessary() ) { httpResponse.setHeader("Transfer-Encoding", "chunked"); session.setChunked(true); } } } } writePersistentResponse(parent, commitFuture, httpResponse); } break; case HTTP_1_0: writeNonPersistentResponse(session, parent, commitFuture, httpResponse); break; default: throw new IllegalStateException("Unexpected HTTP version: " + session.getVersion()); } }  }
HttpAcceptProcessor extends BridgeAcceptProcessor<DefaultHttpSession> { public void commit(final DefaultHttpSession session) { final IoSession parent = session.getParent(); if (parent == null || parent.isClosing()) { return; } final CommitFuture commitFuture = session.getCommitFuture(); if (commitFuture.isCommitted()) { return; } HttpResponseMessage httpResponse = new HttpResponseMessage(); httpResponse.setStatus(session.getStatus()); httpResponse.setReason(session.getReason()); httpResponse.setVersion(session.getVersion()); httpResponse.setHeaders(session.getWriteHeaders()); httpResponse.setInjectableHeaders(session.getLocalAddress().getOption(INJECTABLE_HEADERS)); httpResponse.setCookies(session.getWriteCookies()); httpResponse.setBlockPadding(session.getParameter(".kbp") != null); boolean complete = (session.getCurrentWriteRequest() == null) && session.getWriteRequestQueue().isEmpty(session) && session.isClosing(); IoBufferAllocatorEx<? extends HttpBuffer> allocator = session.getBufferAllocator(); HttpBuffer unsharedEmpty = allocator.wrap(allocator.allocate(0)); httpResponse.setContent(new HttpContentMessage(unsharedEmpty, complete)); if (session.getMethod() == HttpMethod.HEAD) { httpResponse.setContentExcluded(true); } switch (session.getVersion()) { case HTTP_1_1: boolean isConnectionClose = "close".equals(session.getWriteHeader("Connection")); if (isConnectionClose) { writeNonPersistentResponse(session, parent, commitFuture, httpResponse); } else { if (!httpResponse.isComplete()) { String contentLength = session.getWriteHeader(HEADER_CONTENT_LENGTH); if (contentLength == null) { if (session.isClosing() || session.isWriteShutdown()) { long scheduledWriteBytes = session.getScheduledWriteBytes(); httpResponse.setHeader(HEADER_CONTENT_LENGTH, valueOf(scheduledWriteBytes)); } else if (!httpResponse.isContentLengthImplicit()) { if ( session.isChunkingNecessary() ) { httpResponse.setHeader("Transfer-Encoding", "chunked"); session.setChunked(true); } } } } writePersistentResponse(parent, commitFuture, httpResponse); } break; case HTTP_1_0: writeNonPersistentResponse(session, parent, commitFuture, httpResponse); break; default: throw new IllegalStateException("Unexpected HTTP version: " + session.getVersion()); } }  void commit(final DefaultHttpSession session); static void setServerHeader(IoSession session, HttpResponseMessage response); static void setDateHeader(IoSession session, HttpResponseMessage response); static void setServerHeader(DefaultHttpSession httpSession, HttpResponseMessage httpResponse); }
HttpAcceptProcessor extends BridgeAcceptProcessor<DefaultHttpSession> { public void commit(final DefaultHttpSession session) { final IoSession parent = session.getParent(); if (parent == null || parent.isClosing()) { return; } final CommitFuture commitFuture = session.getCommitFuture(); if (commitFuture.isCommitted()) { return; } HttpResponseMessage httpResponse = new HttpResponseMessage(); httpResponse.setStatus(session.getStatus()); httpResponse.setReason(session.getReason()); httpResponse.setVersion(session.getVersion()); httpResponse.setHeaders(session.getWriteHeaders()); httpResponse.setInjectableHeaders(session.getLocalAddress().getOption(INJECTABLE_HEADERS)); httpResponse.setCookies(session.getWriteCookies()); httpResponse.setBlockPadding(session.getParameter(".kbp") != null); boolean complete = (session.getCurrentWriteRequest() == null) && session.getWriteRequestQueue().isEmpty(session) && session.isClosing(); IoBufferAllocatorEx<? extends HttpBuffer> allocator = session.getBufferAllocator(); HttpBuffer unsharedEmpty = allocator.wrap(allocator.allocate(0)); httpResponse.setContent(new HttpContentMessage(unsharedEmpty, complete)); if (session.getMethod() == HttpMethod.HEAD) { httpResponse.setContentExcluded(true); } switch (session.getVersion()) { case HTTP_1_1: boolean isConnectionClose = "close".equals(session.getWriteHeader("Connection")); if (isConnectionClose) { writeNonPersistentResponse(session, parent, commitFuture, httpResponse); } else { if (!httpResponse.isComplete()) { String contentLength = session.getWriteHeader(HEADER_CONTENT_LENGTH); if (contentLength == null) { if (session.isClosing() || session.isWriteShutdown()) { long scheduledWriteBytes = session.getScheduledWriteBytes(); httpResponse.setHeader(HEADER_CONTENT_LENGTH, valueOf(scheduledWriteBytes)); } else if (!httpResponse.isContentLengthImplicit()) { if ( session.isChunkingNecessary() ) { httpResponse.setHeader("Transfer-Encoding", "chunked"); session.setChunked(true); } } } } writePersistentResponse(parent, commitFuture, httpResponse); } break; case HTTP_1_0: writeNonPersistentResponse(session, parent, commitFuture, httpResponse); break; default: throw new IllegalStateException("Unexpected HTTP version: " + session.getVersion()); } }  void commit(final DefaultHttpSession session); static void setServerHeader(IoSession session, HttpResponseMessage response); static void setDateHeader(IoSession session, HttpResponseMessage response); static void setServerHeader(DefaultHttpSession httpSession, HttpResponseMessage httpResponse);  }