Search in sources :

Example 6 with Http2Header

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

the class Http2Requests method createRequest.

public static Http2Request createRequest(int streamId, boolean eos) {
    List<Http2Header> headers = new ArrayList<>();
    headers.add(new Http2Header(Http2HeaderName.METHOD, "GET"));
    headers.add(new Http2Header(Http2HeaderName.AUTHORITY, "somehost.com"));
    headers.add(new Http2Header(Http2HeaderName.PATH, "/"));
    headers.add(new Http2Header(Http2HeaderName.SCHEME, "http"));
    headers.add(new Http2Header(Http2HeaderName.ACCEPT, "*/*"));
    headers.add(new Http2Header(Http2HeaderName.ACCEPT_ENCODING, "gzip, deflate"));
    headers.add(new Http2Header(Http2HeaderName.USER_AGENT, "webpieces/1.15.0"));
    Http2Request request = new Http2Request(headers);
    request.setStreamId(streamId);
    request.setEndOfStream(eos);
    return request;
}
Also used : Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) ArrayList(java.util.ArrayList)

Example 7 with Http2Header

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

the class TestS4FrameSizeAndHeaders method fillHeaders.

private void fillHeaders(Http2Response response1) {
    String value = "heaheaheaheaheaheahahoz.zhxheh,h,he,he,heaheaeaheaheahoahoahozzoqorqzro.zo.zrszaroatroathoathoathoathoatoh";
    for (int i = 0; i < 10; i++) {
        value = value + value;
        response1.addHeader(new Http2Header("eaheahaheaheaeha" + i, value));
    }
}
Also used : Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)

Example 8 with Http2Header

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

the class TestS4FrameSizeAndHeaders method createInterleavedFrames.

private List<Http2Frame> createInterleavedFrames() {
    Http2Response response1 = new Http2Response();
    response1.setStreamId(1);
    response1.setEndOfStream(true);
    fillHeaders(response1);
    HeaderEncoding encoding = new HeaderEncoding();
    List<Http2Frame> frames1 = encoding.translateToFrames(localSettings.getMaxFrameSize(), new Encoder(localSettings.getHeaderTableSize()), response1);
    Http2Response response2 = new Http2Response();
    response2.setStreamId(3);
    response1.setEndOfStream(true);
    response2.addHeader(new Http2Header(Http2HeaderName.ACCEPT, "value"));
    List<Http2Frame> frames2 = encoding.translateToFrames(localSettings.getMaxFrameSize(), new Encoder(localSettings.getHeaderTableSize()), response2);
    List<Http2Frame> frames = new ArrayList<>();
    frames.addAll(frames1);
    frames.add(1, frames2.get(0));
    return frames;
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) HeaderEncoding(com.webpieces.hpack.impl.HeaderEncoding) Encoder(com.twitter.hpack.Encoder) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) ArrayList(java.util.ArrayList) Http2Frame(com.webpieces.http2.api.dto.lowlevel.lib.Http2Frame)

Example 9 with Http2Header

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

the class DefaultCorsProcessor method processOptionsCors.

@Override
public void processOptionsCors(Http2Request request, List<HttpMethod> methods, ResponseStreamHandle responseStream) {
    Http2Header originHeader = request.getHeaderLookupStruct().getHeader(Http2HeaderName.ORIGIN);
    if (originHeader == null)
        throw new IllegalStateException("Should only use this for CORS which requires an Origin header");
    else if (!allowedDomains.contains("*") && !allowedDomains.contains(originHeader.getValue())) {
        send403Response(responseStream, request);
        return;
    }
    Http2Response response = new Http2Response();
    Http2Header methodHeader = request.getHeaderLookupStruct().getHeader(Http2HeaderName.ACCESS_CONTROL_REQUEST_METHOD);
    HttpMethod lookup = HttpMethod.lookup(methodHeader.getValue());
    Http2Header headersRequested = request.getHeaderLookupStruct().getHeader(Http2HeaderName.ACCESS_CONTROL_REQUEST_HEADERS);
    if (!methods.contains(lookup)) {
        response.addHeader(new Http2Header(Http2HeaderName.STATUS, "403"));
    } else if (hasInvalidHeader(allowedHeaders, headersRequested)) {
        response.addHeader(new Http2Header(Http2HeaderName.STATUS, "403"));
    } else {
        response.addHeader(new Http2Header(Http2HeaderName.STATUS, "204"));
    }
    response.addHeader(new Http2Header(Http2HeaderName.ACCESS_CONTROL_ALLOW_ORIGIN, originHeader.getValue()));
    if (allowedDomains.contains("*")) {
        // since all domains, we must tell caches that Origin header in response will vary
        // since it responds with the domain that requested it
        response.addHeader(new Http2Header(Http2HeaderName.VARY, "Origin"));
    }
    if (isAllowCredsCookies) {
        response.addHeader(new Http2Header(Http2HeaderName.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true"));
    }
    String allowedMethodsStr = methods.stream().map(e -> e.getCode()).collect(Collectors.joining(", "));
    String allowedHeadersStr = String.join(", ", allowedHeaders);
    response.addHeader(new Http2Header(Http2HeaderName.ACCESS_CONTROL_ALLOW_METHODS, allowedMethodsStr));
    response.addHeader(new Http2Header(Http2HeaderName.ACCESS_CONTROL_ALLOW_HEADERS, allowedHeadersStr));
    if (exposeTheseResponseHeadersToBrowserStr != null) {
        response.addHeader(new Http2Header(Http2HeaderName.ACCESS_CONTROL_EXPOSE_HEADERS, exposeTheseResponseHeadersToBrowserStr));
    }
    response.addHeader(new Http2Header(Http2HeaderName.ACCESS_CONTROL_MAX_AGE, maxAgeSeconds + ""));
    response.addHeader(new Http2Header(Http2HeaderName.CONTENT_LENGTH, "0"));
    sendResponse(responseStream, response);
}
Also used : SneakyThrow(org.webpieces.util.exceptions.SneakyThrow) Set(java.util.Set) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Collectors(java.util.stream.Collectors) ResponseStreamHandle(com.webpieces.http2.api.streaming.ResponseStreamHandle) HeaderType(com.webpieces.http2.api.dto.lowlevel.lib.HeaderType) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) HttpMethod(org.webpieces.ctx.api.HttpMethod) XFuture(org.webpieces.util.futures.XFuture) OverwritePlatformResponse(org.webpieces.ctx.api.OverwritePlatformResponse) RequestContext(org.webpieces.ctx.api.RequestContext) Locale(java.util.Locale) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) Http2HeaderName(com.webpieces.http2.api.dto.lowlevel.lib.Http2HeaderName) Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) HttpMethod(org.webpieces.ctx.api.HttpMethod)

Example 10 with Http2Header

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

the class DefaultCorsProcessor method isAccessAllowed.

@Override
public AccessResult isAccessAllowed(RequestContext ctx) {
    Http2Request request = ctx.getRequest().originalRequest;
    Http2Header originHeader = request.getHeaderLookupStruct().getHeader(Http2HeaderName.ORIGIN);
    if (originHeader == null) {
        throw new IllegalStateException("Should only use this for CORS which requires an Origin header");
    } else if (!allowedDomains.contains("*") && !allowedDomains.contains(originHeader.getValue())) {
        return new AccessResult("Domain not allowed");
    }
    // method is allowed since we are here OR else CORSProcessor is not called
    List<Http2Header> headers = request.getHeaders();
    for (Http2Header header : headers) {
        if (DEFAULTS.contains(header.getName())) {
            continue;
        } else if (!isAllowCredsCookies && header.getKnownName() == Http2HeaderName.COOKIE) {
            return new AccessResult("Credentials / Cookies not supported on this CORS request");
        } else if (!allowedHeaders.contains("*") && !allowedHeaders.contains(header.getName().toLowerCase())) {
            return new AccessResult("Header '" + header.getName() + "' not supported on this CORS request");
        }
    }
    ctx.addModifyResponse(new OverwriteForCorsResponse(originHeader));
    return new AccessResult();
}
Also used : Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)

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