static GrizzlyChannel getOrAddChannel(Connection<?> connection, URL url, ChannelHandler handler) { if (connection == null) { return null; } GrizzlyChannel ret = ATTRIBUTE.get(connection); if (ret == null) { ret = new GrizzlyChannel(connection, url, handler); if (connection.isOpen()) { ATTRIBUTE.set(connection, ret); } } return ret; }
if (ctx.getConnection().isOpen()) { final HttpContext context = HttpContext.get(ctx); final HttpRequestPacket httpRequest = context.getRequest(); keepAliveContextAttr.get(context); httpRequest.getProcessingState().isKeepAlive(); return ctx.getStopAction(); return ctx.getInvokeAction();
private void sendMoreDataRequestIfNeeded(final FilterChainContext ctx) throws IOException { final Connection connection = ctx.getConnection(); final HttpContext context = HttpContext.get(ctx); // Check if message is still in process if (httpRequestInProcessAttr.isSet(context)) { final MemoryManager mm = connection.getTransport().getMemoryManager(); final Buffer buffer = mm.allocate(7); buffer.put((byte) 'A'); buffer.put((byte) 'B'); buffer.putShort((short) 3); buffer.put(AjpConstants.JK_AJP13_GET_BODY_CHUNK); buffer.putShort(AjpConstants.SUGGESTED_MAX_PAYLOAD_SIZE); buffer.flip(); buffer.allowBufferDispose(true); ctx.write(buffer); } }
private NextAction processData(final FilterChainContext ctx, final Buffer messageContent) { final AjpHttpRequest httpRequestPacket = httpRequestInProcessAttr.get( ctx.getConnection()); httpRequestPacket.getProcessingState().getHttpContext().attach(ctx); if (messageContent.hasRemaining()) { messageContent.position(messageContent.position() + 2); final HttpContent content = HttpContent.builder(httpRequestPacket) .content(messageContent) .last(!httpRequestPacket.isExpectContent()) .build(); ctx.setMessage(content); return ctx.getInvokeAction(httpRequestPacket.isExpectContent() ? NEED_MORE_DATA_MESSAGE : null);
@Override public NextAction handleRead(FilterChainContext ctx) throws IOException { // Get the parsed HttpContent (we assume prev. filter was HTTP) HttpContent message = ctx.getMessage(); Socket tunnelSocket = tunnelSockets.get(ctx.getConnection()); if (tunnelSocket == null) { // handle connection procedure return GrizzlyModProxy.this.handleConnect(ctx, message); } if (message.getContent().hasRemaining()) { // relay the content to the tunnel connection Buffer buffer = message.getContent(); message.recycle(); tunnelSocket.getOutputStream().write(buffer.array(), buffer.arrayOffset(), buffer.remaining()); } return ctx.getStopAction(); } }
@Override public NextAction handleEvent(final FilterChainContext ctx, final FilterChainEvent event) throws IOException { if (event.type() == HttpEvents.ChangePacketInProgressEvent.TYPE) { final HttpResponsePacket responsePacket = (HttpResponsePacket) ((HttpEvents.ChangePacketInProgressEvent) event).getPacket(); httpResponseInProcessAttr.set( responsePacket.getProcessingState().getHttpContext(), responsePacket); return ctx.getStopAction(); } else { return super.handleEvent(ctx, event); } }
private NextAction sendRequest(FilterChainContext ctx, UpgradeRequest upgradeRequest) { HttpRequestPacket.Builder builder = HttpRequestPacket.builder(); if (proxy && !PROXY_CONNECTED.get(ctx.getConnection())) { UPGRADE_REQUEST.set(ctx.getConnection(), upgradeRequest); URI requestURI = upgradeRequest.getRequestURI(); final int requestPort = Utils.getWsPort(requestURI); builder = builder.uri(String.format("%s:%d", requestURI.getHost(), requestPort)); builder = builder.protocol(Protocol.HTTP_1_1); builder = builder.method(Method.CONNECT); if (proxyHeaders != null && proxyHeaders.size() > 0) { for (Map.Entry<String, String> entry : proxyHeaders.entrySet()) { builder.header(entry.getKey(), entry.getValue()); } } builder = builder.header(Header.Host, requestURI.getHost()); builder = builder.header(Header.ProxyConnection, "keep-alive"); builder = builder.header(Header.Connection, "keep-alive"); ctx.write(HttpContent.builder(builder.build()).build()); ctx.flush(null); } else { ctx.write(getHttpContent(upgradeRequest)); } // call the next filter in the chain return ctx.getInvokeAction(); }
private int extractType(final FilterChainContext ctx, final Buffer buffer) { return !httpRequestInProcessAttr.isSet(ctx.getConnection()) ? // if request is no in process - it should be a new Ajp message buffer.get() & 0xFF : // Ajp Data Packet AjpConstants.JK_AJP13_DATA; } }
private boolean sendAsGrizzlyRequest(final HttpTransactionContext httpTxCtx, final FilterChainContext ctx) throws IOException { final Connection connection = ctx.getConnection(); final boolean isUsedConnection = Boolean.TRUE.equals(USED_CONNECTION.get(connection)); if (!isUsedConnection) { USED_CONNECTION.set(connection, Boolean.TRUE); final HttpRequestPacket.Builder builder = HttpRequestPacket.builder() .protocol(Protocol.HTTP_1_1) .method(method); httpTxCtx.protocolHandler = Version.RFC6455.createHandler(true); httpTxCtx.handshake = httpTxCtx.protocolHandler.createClientHandShake(wsURI); requestPacket = (HttpRequestPacket) httpTxCtx.handshake.composeHeaders().getHttpHeader(); } catch (URISyntaxException e) { throw new IllegalArgumentException("Invalid WS URI: " + httpTxCtx.wsRequestURI); requestPacket.setSecure(secure); setupKeepAlive(requestPacket, connection); ctx.notifyDownstream(new SSLSwitchingEvent(connection, secure, uri.getHost(), uri.getPort()));
@Override public NextAction handleEvent(final FilterChainContext ctx, final FilterChainEvent event) throws IOException { if (event.type() == SSLSwitchingEvent.class) { final SSLSwitchingEvent se = (SSLSwitchingEvent) event; final boolean isSecure = se.isSecure(); CONNECTION_IS_SECURE.set(se.getConnection(), isSecure); // if enabling security - create SSLEngine here, because default // Grizzly SSLFilter will use host/port info from the Connection, rather // than request URL. Specifically this doesn't work with CONNECT tunnels. if (isSecure && SSLUtils.getSSLEngine(ctx.getConnection()) == null) { // if SSLEngine is not yet set for the connection - initialize it final SSLEngine sslEngine = getClientSSLEngineConfigurator() .createSSLEngine(se.getHost(), se.getPort() == -1 ? 443 : se.getPort() ); sslEngine.beginHandshake(); SSLUtils.setSSLEngine(ctx.getConnection(), sslEngine); } return ctx.getStopAction(); } return ctx.getInvokeAction(); }
@Override protected void notifyHandshakeComplete(final Connection<?> connection, final SSLEngine sslEngine) { final SSLHandshakeContext handshakeContext = handshakeContextAttr.get(connection); if (handshakeContext != null) { connection.removeCloseListener(closeListener); handshakeContext.completed(sslEngine); handshakeContextAttr.remove(connection); } super.notifyHandshakeComplete(connection, sslEngine); }
static void bind(final HttpContext httpCtx, final HttpTransactionContext httpTxContext) { httpCtx.getCloseable().addCloseListener(httpTxContext.listener); REQUEST_STATE_ATTR.set(httpCtx, httpTxContext); }
@Override public void onCopy(final FilterChainContext srcContext, final FilterChainContext copiedContext) { final FilterChainContext suspendedContextCopy = suspendedContextAttribute.get(srcContext).copy(); suspendedContextAttribute.set(copiedContext, suspendedContextCopy); copiedContext.addCopyListener(this); }
private void cleanup(final HttpContext httpCtx) { httpCtx.getCloseable().removeCloseListener(listener); REQUEST_STATE_ATTR.remove(httpCtx); }
@Override public Object getAttribute(String key) { return Grizzly.DEFAULT_ATTRIBUTE_BUILDER.createAttribute(key).get(connection); }
@Override public void setAttribute(String key, Object value) { Grizzly.DEFAULT_ATTRIBUTE_BUILDER.createAttribute(key).set(connection, value); }
@Test public void testAttributeGetWithoutInitializerOnEmptyHolder() { AttributeBuilder builder = new DefaultAttributeBuilder(); AttributeHolder holder = isSafe ? builder.createSafeAttributeHolder() : builder.createUnsafeAttributeHolder(); final Attribute<String> attr = builder.createAttribute("attribute"); assertNull(attr.peek(holder)); assertEquals(null, attr.get(holder)); assertFalse(attr.isSet(holder)); } }
@Override protected void notifyHandshakeFailed(Connection connection, Throwable t) { final SSLHandshakeContext handshakeContext = handshakeContextAttr.get(connection); if (handshakeContext != null) { connection.removeCloseListener(closeListener); handshakeContext.failed(t); } super.notifyHandshakeFailed(connection, t); }