Search in sources :

Example 16 with Http2Header

use of com.webpieces.http2.api.dto.lowlevel.lib.Http2Header in project webpieces by deanhiller.

the class Http11ToHttp2 method requestToHeaders.

private static Http2Request requestToHeaders(HttpRequest request, boolean fromSslChannel) {
    HttpRequestLine requestLine = request.getRequestLine();
    List<Header> requestHeaders = request.getHeaders();
    LinkedList<Http2Header> headerList = new LinkedList<>();
    // add special headers
    headerList.add(new Http2Header(":method", requestLine.getMethod().getMethodAsString()));
    UrlInfo urlInfo = requestLine.getUri().getUriBreakdown();
    headerList.add(new Http2Header(":path", requestLine.getUri().getUri()));
    // Figure out scheme
    if (urlInfo.getPrefix() != null) {
        headerList.add(new Http2Header(":scheme", urlInfo.getPrefix()));
    } else if (fromSslChannel) {
        headerList.add(new Http2Header(":scheme", "https"));
    } else {
        headerList.add(new Http2Header(":scheme", "http"));
    }
    // Figure out authority
    Header hostHeader = request.getHeaderLookupStruct().getHeader(KnownHeaderName.HOST);
    if (hostHeader == null)
        throw new IllegalArgumentException("Host header is required in http1.1");
    // Add regular headers
    for (Header header : requestHeaders) {
        if (header.getKnownName() == KnownHeaderName.HOST) {
            // keeps headers in order of http1 headers
            String h = hostHeader.getValue();
            headerList.add(new Http2Header(":authority", h));
            continue;
        }
        headerList.add(new Http2Header(header.getName().toLowerCase(), header.getValue()));
    }
    Http2Request headers = new Http2Request(headerList);
    Header contentLen = request.getHeaderLookupStruct().getHeader(KnownHeaderName.CONTENT_LENGTH);
    if (request.isHasChunkedTransferHeader()) {
        headers.setEndOfStream(false);
    } else if (contentLenGreaterThanZero(contentLen)) {
        headers.setEndOfStream(false);
    } else
        headers.setEndOfStream(true);
    return headers;
}
Also used : UrlInfo(org.webpieces.httpparser.api.dto.UrlInfo) HttpRequestLine(org.webpieces.httpparser.api.dto.HttpRequestLine) Header(org.webpieces.httpparser.api.common.Header) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) LinkedList(java.util.LinkedList)

Example 17 with Http2Header

use of com.webpieces.http2.api.dto.lowlevel.lib.Http2Header in project webpieces by deanhiller.

the class Http11ToHttp2 method responseToHeaders.

public static Http2Response responseToHeaders(HttpResponse response) {
    List<Http2Header> headers = new ArrayList<>();
    headers.add(new Http2Header(Http2HeaderName.STATUS, response.getStatusLine().getStatus().getCode().toString()));
    for (Header header : response.getHeaders()) {
        if (header.getKnownName() == KnownHeaderName.TRANSFER_ENCODING) {
            if ("chunked".equals(header.getValue())) {
                // skip as http2 does not allow this header
                continue;
            }
        }
        headers.add(new Http2Header(header.getName(), header.getValue()));
    }
    Http2Response resp = new Http2Response(headers);
    Header header = response.getHeaderLookupStruct().getHeader(KnownHeaderName.CONTENT_LENGTH);
    if (header != null) {
        int len = Integer.parseInt(header.getValue());
        if (len == 0) {
            resp.setEndOfStream(true);
        } else {
            resp.setEndOfStream(false);
        }
    } else if (response.isHasChunkedTransferHeader()) {
        resp.setEndOfStream(false);
    } else
        resp.setEndOfStream(true);
    return resp;
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) Header(org.webpieces.httpparser.api.common.Header) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) ArrayList(java.util.ArrayList)

Example 18 with Http2Header

use of com.webpieces.http2.api.dto.lowlevel.lib.Http2Header in project webpieces by deanhiller.

the class HeaderEncoding method translateToFrames.

public List<Http2Frame> translateToFrames(long maxFrameSize, Encoder encoder, Http2Headers headers) {
    HeadersFrame frame = new HeadersFrame();
    frame.setStreamId(headers.getStreamId());
    frame.setEndOfStream(headers.isEndOfStream());
    frame.setPriorityDetails(headers.getPriorityDetails());
    List<Http2Header> headerList = headers.getHeaders();
    List<Http2Frame> headerFrames = toHeaderFrames(maxFrameSize, encoder, frame, headerList);
    return headerFrames;
}
Also used : Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) HeadersFrame(com.webpieces.http2.api.dto.lowlevel.HeadersFrame) Http2Frame(com.webpieces.http2.api.dto.lowlevel.lib.Http2Frame)

Example 19 with Http2Header

use of com.webpieces.http2.api.dto.lowlevel.lib.Http2Header in project webpieces by deanhiller.

the class HeaderEncoding method translateToFrames.

public List<Http2Frame> translateToFrames(long maxFrameSize, Encoder encoder, Http2Push p) {
    PushPromiseFrame frame = new PushPromiseFrame();
    frame.setStreamId(p.getStreamId());
    frame.setPromisedStreamId(p.getPromisedStreamId());
    List<Http2Header> headerList = p.getHeaders();
    List<Http2Frame> headerFrames = toHeaderFrames(maxFrameSize, encoder, frame, headerList);
    return headerFrames;
}
Also used : Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) PushPromiseFrame(com.webpieces.http2.api.dto.lowlevel.PushPromiseFrame) Http2Frame(com.webpieces.http2.api.dto.lowlevel.lib.Http2Frame)

Example 20 with Http2Header

use of com.webpieces.http2.api.dto.lowlevel.lib.Http2Header in project webpieces by deanhiller.

the class Http2ToHttp11 method translateResponse.

public static HttpResponse translateResponse(Http2Response headers) {
    HttpResponseStatus status = new HttpResponseStatus();
    HttpResponseStatusLine statusLine = new HttpResponseStatusLine();
    statusLine.setStatus(status);
    HttpResponse response = new HttpResponse();
    response.setStatusLine(statusLine);
    for (Http2Header header : headers.getHeaders()) {
        if (header.getKnownName() == Http2HeaderName.STATUS) {
            fillStatus(header, status);
        } else if ("reason".equals(header.getName())) {
            fillReason(header, status);
        } else if (header.getKnownName() == Http2HeaderName.SCHEME) {
        // do nothing and drop it
        } else {
            Header http1Header = convertHeader(header);
            response.addHeader(http1Header);
        }
    }
    if (headers.getHeaderLookupStruct().getHeader(Http2HeaderName.CONTENT_LENGTH) == null) {
        if (headers.isEndOfStream()) {
            // firefox really needs content length!!! if end of stream
            response.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
        } else {
            response.addHeader(new Header(KnownHeaderName.TRANSFER_ENCODING, "chunked"));
        }
    }
    if (status.getCode() == null)
        throw new IllegalArgumentException("The header :status is required to send the response");
    // Do a queue instead with last few frames that can be enabled
    log.debug("response=" + response);
    return response;
}
Also used : Header(org.webpieces.httpparser.api.common.Header) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) HttpResponseStatus(org.webpieces.httpparser.api.dto.HttpResponseStatus) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) HttpResponseStatusLine(org.webpieces.httpparser.api.dto.HttpResponseStatusLine)

Aggregations

Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)72 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)26 Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)23 ArrayList (java.util.ArrayList)23 DataWrapper (org.webpieces.data.api.DataWrapper)9 Http2HeaderName (com.webpieces.http2.api.dto.lowlevel.lib.Http2HeaderName)8 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)8 Http2Push (com.webpieces.http2.api.dto.highlevel.Http2Push)7 XFuture (org.webpieces.util.futures.XFuture)7 InetSocketAddress (java.net.InetSocketAddress)6 Header (org.webpieces.httpparser.api.common.Header)6 CancelReason (com.webpieces.http2.api.dto.lowlevel.CancelReason)5 Http2Frame (com.webpieces.http2.api.dto.lowlevel.lib.Http2Frame)5 StreamRef (com.webpieces.http2.api.streaming.StreamRef)5 List (java.util.List)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 ResponseStreamHandle (com.webpieces.http2.api.streaming.ResponseStreamHandle)4 Executor (java.util.concurrent.Executor)4 Executors (java.util.concurrent.Executors)4