private void detachWriter0(final HttpSession oldWriter) { if (oldWriter.getIoThread() == getIoThread()) { if (!oldWriter.isClosing()) { oldWriter.write(WsCommandMessage.RECONNECT); } if (logger.isDebugEnabled()) { logger.debug(String.format("detachWriter on WsebSession wseb#%d, oldWriter=%s", this.getId(), oldWriter)); } oldWriter.close(false); } else { final Executor ioExecutor = getIoExecutor(); ioExecutor.execute(new Runnable() { @Override public void run() { detachWriter0(oldWriter); } }); } }
@Override protected void doFilterWrite(NextFilter nextFilter, HttpSession session, WriteRequest writeRequest) throws Exception { // GL.debug("http", getClass().getSimpleName() + " filter write."); // note: this assumes the session is still not committed // but commitFuture is on HttpAcceptSession only, // so need to add to HttpConnectSession interface too // and then promote to HttpSession.getCommitFuture() String contentLengthHeader = session.getWriteHeader(HEADER_CONTENT_LENGTH); int contentLength = (contentLengthHeader != null) ? parseInt(contentLengthHeader) : 0; Object message = writeRequest.getMessage(); IoBuffer buf = (IoBuffer) message; int remaining = buf.remaining(); int newContentLength = contentLength + remaining; session.setWriteHeader(HEADER_CONTENT_LENGTH, valueOf(newContentLength)); // GL.debug("http", String.format(getClass().getSimpleName() + " reset content length header from %d to %d.", // contentLength, newContentLength)); // auto-remove after a single HttpRequestMessage or HttpResponseMessage IoFilterChain filterChain = session.getFilterChain(); filterChain.remove(this); super.doFilterWrite(nextFilter, session, writeRequest); }
private void processResponseHeaders(HttpSession connectSession, HttpSession acceptSession) { boolean upgrade = connectSession.getReadHeader(HEADER_UPGRADE) != null; if (upgrade) { hopByHopHeaders.remove(HEADER_UPGRADE); for (Map.Entry<String, List<String>> e : connectSession.getReadHeaders().entrySet()) { String name = e.getKey(); value = processCookiePath(value, cookiePathMap); acceptSession.addWriteHeader(name, value); } else if (name.equalsIgnoreCase(HEADER_LOCATION)) { if (rewriteLocation) { value = processLocationHeader(value, locationMap); acceptSession.addWriteHeader(name, value); } else { acceptSession.addWriteHeader(name ,value); acceptSession.setWriteHeader(HEADER_CONNECTION, HEADER_UPGRADE);
public static boolean canStream(HttpSession session) { if ("p".equals(session.getParameter(".ki"))) { return false; } String responseMode = session.getReadHeader(PROXY_BUFFERING_HEADER); if (responseMode != null) { assert responseMode.equals("on") || responseMode.equals("off"); return responseMode.equals("off"); } return true; }
private static boolean processHopByHopHeaders(HttpSession src, HttpSession dest) { Set<String> hopByHopHeaders = getHopByHopHeaders(src); boolean upgrade = src.getReadHeader(HEADER_UPGRADE) != null; if (upgrade) { hopByHopHeaders.remove(HEADER_UPGRADE); } // Add source session headers to destination session for (Map.Entry<String, List<String>> e : src.getReadHeaders().entrySet()) { String name = e.getKey(); for (String value : e.getValue()) { if (!hopByHopHeaders.contains(name)) { dest.addWriteHeader(name, value); } } } return upgrade; }
private void receiveAndExtractHttpResponse(NextFilter nextFilter, HttpSession session, HttpResponseMessage httpResponse) throws Exception { if (session.getVersion() != httpResponse.getVersion()) { throw new ProtocolDecoderException("HTTP version mismatch"); switch (session.getStatus()) { case SUCCESS_OK: break; for (String readHeaderName : session.getReadHeaderNames()) { if (HEADER_CONTENT_LENGTH.equalsIgnoreCase(readHeaderName)) { if (!httpResponse.hasHeader(readHeaderName)) { long contentLength = parseLong(session.getReadHeader(readHeaderName)); long newContentLength = contentLength - session.getReadBytes(); httpResponse.setHeader(readHeaderName, valueOf(newContentLength)); String contentType = session.getReadHeader("Content-Type"); String innerContentType = httpResponse.getHeader("Content-Type"); if (innerContentType != null) { String readHeaderValue = session.getReadHeader(readHeaderName); httpResponse.setHeader(readHeaderName, readHeaderValue); for (HttpCookie readCookie : session.getReadCookies()) { httpResponse.addCookie(readCookie);
switch (session.getStatus()) { case REDIRECT_MOVED_PERMANENTLY: String location = session.getReadHeader("Location"); if (location == null) { sseSession.reset(new Exception("Redirect attempted without Location header").fillInStackTrace());
private void fireContentReceived(HttpSession session, HttpContentMessage content) { IoBufferEx buffer = content.asBuffer(); if (buffer != null && buffer.hasRemaining()) { IoFilterChain filterChain = session.getFilterChain(); filterChain.fireMessageReceived(buffer); } // deliver the session close event when we receive the last chunk if (content.isComplete()) { session.close(false); } } };
firstWriter = false; if (Long.valueOf(0L).equals(newWriter.getAttribute(WsebAcceptor.CLIENT_BUFFER_KEY))) { newWriter.suspendWrite(); if (newWriter != null) { if (!isCloseSent()) { newWriter.write(WsCommandMessage.CLOSE); newWriter.write(WsCommandMessage.RECONNECT); newWriter.close(false);
IoBufferEx buf = CREATE_RESPONSE_KEY.remove(createSession); if (buf == null || createSession.getStatus() != HttpStatus.SUCCESS_CREATED) { ConnectRequest<?> connectRequest = CONNECT_REQUEST_KEY.get(createSession); ConnectFuture connectFuture = connectRequest.connectFuture; ResourceAddress remoteAddress = createSession.getRemoteAddress(); ResourceAddress connectAddress = remoteAddress.getTransport(); BridgeConnector connector = bridgeServiceFactory.newBridgeConnector(connectAddress);
private static void addExpiresHeader(HttpSession session, long time) { session.setWriteHeader("Expires", RFC822_FORMAT_PATTERN.format(time)); }
@Override protected void doSessionClosed(HttpSession session) throws Exception { if (session.getStatus() != HttpStatus.SUCCESS_OK || wsebSession.getCloseException() != null) { wsebSession.reset( new IOException(LoggingUtils.NETWORK_CONNECTIVITY_ERROR_MESSAGE, wsebSession.getAndClearCloseException()).fillInStackTrace()); } } }
private void finishWrite(final WsebSession session, final HttpSession writer) { // ensure upstream is serialized to avoid out-of-order delivery session.suspendWrite(); // writer.write(IoBuffer.allocate(0)) throws exception // but we need to signal to HTTP layer that write is complete WriteFutureEx writeFuture = new DefaultWriteFutureEx(writer); WriteRequestEx writeRequest = new DefaultWriteRequestEx(HttpConnectProcessor.WRITE_COMPLETE, writeFuture); IoFilterChain filterChain = writer.getFilterChain(); filterChain.fireFilterWrite(writeRequest); // wait for writer to close before detaching the writer and flushing writer.getCloseFuture().addListener(new IoFutureListener<CloseFuture>() { @Override public void operationComplete(CloseFuture future) { session.detachWriter(writer); //TODO: remove suspendWrite/resumeWrite completely (replace with a WSEB-specific "send queue") session.resumeWrite(); // We are always aligned now. if (session.isIoAligned()) {; session.getProcessor().flush(session); } }); }
@Override protected void doExceptionCaught(HttpSession session, Throwable cause) throws Exception { wsebSession.setCloseException(cause); session.close(true); }
public static boolean hasBeenModified(HttpSession session, String etag, File requestFile) { String ifNoneMatch = session.getReadHeader("If-None-Match"); String ifModifiedSince = session.getReadHeader("If-Modified-Since"); return hasBeenModified(requestFile, etag, ifNoneMatch, ifModifiedSince); }
@Override protected void doSessionOpened(HttpSession session) throws Exception { // TODO session.get[Ready]Future().addListener(...) to check // response status / headers IoFilterChain filterChain = session.getFilterChain(); addBridgeFilters(filterChain); SseSession sseSession = SSE_SESSION_KEY.get(session); if (sseSession == null) { Callable<SseSession> sessionFactory = SSE_SESSION_FACTORY_KEY.remove(session); SseSession newSseSession = sessionFactory.call(); SSE_SESSION_KEY.set(session, newSseSession); } }
if (session instanceof HttpAcceptSession && child != null && child.isCommitting() && !child.isClosing()) { ((HttpAcceptSession)session).commit().addListener( new IoFutureListener<IoFuture>() {
final IoBufferAllocatorEx<SseBuffer> allocator = new SseBufferAllocator(httpSession.getBufferAllocator());
private boolean cannotWrite(WsebSession session) { // get parent and check if null (no attached http session) boolean isAcceptor = session.getService().getClass() == WsebAcceptor.class; // TODO: make this neater final HttpSession writer = session.getWriter(); if (isAcceptor) { if (writer == null || writer.isClosing()) { return true; } } else if (session.getWriteAddress() == null) { // WsebConnector create handshake failed return true; } return false; } }
@Override public void initializeSession(final IoSession parent, ConnectFuture future) { // initializer for bridge session to specify bridge handler, // and call user-defined bridge session initializer if present final IoSessionInitializer<T> bridgeSessionInitializer = new IoSessionInitializer<T>() { @Override public void initializeSession(IoSession bridgeSession, T future) { bridgeSession.setAttribute( BridgeConnectHandler.DELEGATE_KEY, handler); if (initializer != null) { initializer .initializeSession(bridgeSession, future); } } }; parent.setAttribute(CONNECT_REQUEST_KEY, new ConnectRequest<>(connectAddress, bridgeConnectFuture, bridgeSessionInitializer)); // WebSocket-Version required by WsrAcceptor HttpSession httpParent = (HttpSession)parent; httpParent.addWriteHeader("X-WebSocket-Version", "wsr-1.0"); } };