Search in sources :

Example 1 with HeaderListener

use of com.twitter.hpack.HeaderListener in project service-proxy by membrane.

the class Http2Logic method handleFrame.

private void handleFrame(HeadersFrame headers) throws IOException {
    int streamId1 = headers.getFrame().getStreamId();
    if (streamId1 == 0)
        throw new FatalConnectionException(ERROR_PROTOCOL_ERROR);
    StreamInfo streamInfo = streams.get(streamId1);
    if (streamInfo == null) {
        streamInfo = new StreamInfo(streamId1, sender, peerSettings, ourSettings);
        streams.put(streamId1, streamInfo);
    }
    streamInfo.receivedHeaders();
    if (headers.isPriority())
        priorityTree.reprioritize(streamInfo, headers.getWeight(), streams.get(headers.getStreamDependency()), headers.isExclusive());
    else
        priorityTree.reprioritize(streamInfo, 16, null, false);
    List<HeaderBlockFragment> headerFrames = new ArrayList<>();
    headerFrames.add(headers);
    HeaderBlockFragment last = headers;
    while (!last.isEndHeaders()) {
        Frame frame = new Frame(ourSettings);
        frame.read(srcIn);
        if (frame.getType() != TYPE_CONTINUATION)
            throw new FatalConnectionException(ERROR_PROTOCOL_ERROR);
        last = frame.asContinuation();
        int streamId = frame.getStreamId();
        if (streamId != streamId1)
            throw new FatalConnectionException(ERROR_PROTOCOL_ERROR);
        headerFrames.add(last);
    }
    Message request = messageHandler.createMessage();
    StringBuilder sb = log.isDebugEnabled() ? new StringBuilder() : null;
    if (sb != null) {
        sb.append("Headers on stream ");
        sb.append(streamId1);
        sb.append(":\n");
    }
    decoder.decode(getPackedHeaderStream(headerFrames), new HeaderListener() {

        @Override
        public void addHeader(byte[] name, byte[] value, boolean sensitive) {
            String key = new String(name);
            String val = new String(value);
            if (sb != null) {
                sb.append(key);
                sb.append(": ");
                sb.append(val);
                sb.append("\n");
            }
            if (":method".equals(key) && request instanceof Request)
                ((Request) request).setMethod(val);
            else if (":scheme".equals(key))
                // ignore
                ;
            else if (":authority".equals(key))
                request.getHeader().setHost(val);
            else if (":path".equals(key) && request instanceof Request) {
                ((Request) request).setUri(val);
                log.info("streamId=" + streamId1 + " uri=" + val);
            } else if (":status".equals(key) && request instanceof Response) {
                ((Response) request).setStatusCode(Integer.parseInt(val));
                log.debug("streamId=" + streamId1 + " status=" + val);
            } else
                request.getHeader().add(key, val);
        }
    });
    decoder.endHeaderBlock();
    if (sb != null)
        log.debug(sb.toString());
    if (!headers.isEndStream())
        request.setBody(streamInfo.createBody());
    messageHandler.handleExchange(streamInfo, request, showSSLExceptions, remoteAddr);
    if (headers.isEndStream())
        streamInfo.receivedEndStream(false);
}
Also used : HeaderListener(com.twitter.hpack.HeaderListener) Frame(com.predic8.membrane.core.transport.http2.frame.Frame) SettingsFrame(com.predic8.membrane.core.transport.http2.frame.SettingsFrame) Message(com.predic8.membrane.core.http.Message) ArrayList(java.util.ArrayList) Request(com.predic8.membrane.core.http.Request) Response(com.predic8.membrane.core.http.Response)

Aggregations

Message (com.predic8.membrane.core.http.Message)1 Request (com.predic8.membrane.core.http.Request)1 Response (com.predic8.membrane.core.http.Response)1 Frame (com.predic8.membrane.core.transport.http2.frame.Frame)1 SettingsFrame (com.predic8.membrane.core.transport.http2.frame.SettingsFrame)1 HeaderListener (com.twitter.hpack.HeaderListener)1 ArrayList (java.util.ArrayList)1