Search in sources :

Example 16 with HttpVersion

use of io.netty.handler.codec.http.HttpVersion in project xipki by xipki.

the class HttpRestServlet method service.

@Override
public FullHttpResponse service(FullHttpRequest request, ServletURI servletUri, SSLSession sslSession, SslReverseProxyMode sslReverseProxyMode) {
    HttpVersion version = request.protocolVersion();
    HttpMethod method = request.method();
    if (method != HttpMethod.POST && method != HttpMethod.GET) {
        return createErrorResponse(version, HttpResponseStatus.METHOD_NOT_ALLOWED);
    }
    AuditEvent event = new AuditEvent(new Date());
    try {
        Rest rest = responderManager.getRest();
        HttpRequestMetadataRetriever httpRetriever = new HttpRequestMetadataRetrieverImpl(request, servletUri, sslSession, sslReverseProxyMode);
        byte[] requestBytes = readContent(request);
        RestResponse response = rest.service(servletUri.getPath(), event, requestBytes, httpRetriever);
        HttpResponseStatus status = HttpResponseStatus.valueOf(response.getStatusCode());
        FullHttpResponse resp = createResponse(version, status, response.getContentType(), response.getBody());
        for (String headerName : response.getHeaders().keySet()) {
            resp.headers().add(headerName, response.getHeaders().get(headerName));
        }
        return resp;
    } finally {
        event.finish();
        auditServiceRegister.getAuditService().logEvent(event);
    }
}
Also used : HttpRequestMetadataRetriever(org.xipki.ca.server.api.HttpRequestMetadataRetriever) Rest(org.xipki.ca.server.api.Rest) RestResponse(org.xipki.ca.server.api.RestResponse) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) AuditEvent(org.xipki.audit.AuditEvent) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) HttpVersion(io.netty.handler.codec.http.HttpVersion) HttpMethod(io.netty.handler.codec.http.HttpMethod) Date(java.util.Date)

Example 17 with HttpVersion

use of io.netty.handler.codec.http.HttpVersion in project moco by dreamhead.

the class AbstractProxyResponseHandler method setupNormalResponse.

private HttpResponse setupNormalResponse(final org.apache.http.HttpResponse remoteResponse) throws IOException {
    HttpVersion httpVersion = HttpVersion.valueOf(remoteResponse.getProtocolVersion().toString());
    HttpResponseStatus status = HttpResponseStatus.valueOf(remoteResponse.getStatusLine().getStatusCode());
    FullHttpResponse response = new DefaultFullHttpResponse(httpVersion, status);
    response.setStatus(status);
    Arrays.stream(remoteResponse.getAllHeaders()).filter(this::isResponseHeader).forEach(header -> response.headers().set(header.getName(), header.getValue()));
    HttpEntity entity = remoteResponse.getEntity();
    if (entity != null) {
        byte[] content = toByteArray(entity);
        if (content.length > 0) {
            ByteBuf buffer = Unpooled.copiedBuffer(content);
            response.content().writeBytes(buffer);
        }
    }
    return newResponse(response);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpEntity(org.apache.http.HttpEntity) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) ByteBuf(io.netty.buffer.ByteBuf) HttpVersion(io.netty.handler.codec.http.HttpVersion)

Example 18 with HttpVersion

use of io.netty.handler.codec.http.HttpVersion in project riposte by Nike-Inc.

the class RequestInfoImplTest method netty_helper_constructor_populates_request_info_appropriately.

@Test
public void netty_helper_constructor_populates_request_info_appropriately() {
    // given
    String uri = "/some/uri/path/%24foobar%26?foo=bar&secondparam=secondvalue";
    Map<String, List<String>> expectedQueryParamMap = new HashMap<>();
    expectedQueryParamMap.put("foo", Arrays.asList("bar"));
    expectedQueryParamMap.put("secondparam", Arrays.asList("secondvalue"));
    HttpMethod method = HttpMethod.PATCH;
    String cookieName = UUID.randomUUID().toString();
    String cookieValue = UUID.randomUUID().toString();
    String content = UUID.randomUUID().toString();
    byte[] contentBytes = content.getBytes();
    Charset contentCharset = CharsetUtil.UTF_8;
    ByteBuf contentByteBuf = Unpooled.copiedBuffer(contentBytes);
    HttpHeaders headers = new DefaultHttpHeaders().add("header1", "val1").add(HttpHeaders.Names.CONTENT_TYPE, contentCharset).add(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE).add(HttpHeaders.Names.COOKIE, ClientCookieEncoder.LAX.encode(cookieName, cookieValue));
    HttpHeaders trailingHeaders = new DefaultHttpHeaders().add("trailingHeader1", "trailingVal1");
    HttpVersion protocolVersion = HttpVersion.HTTP_1_1;
    FullHttpRequest nettyRequestMock = mock(FullHttpRequest.class);
    doReturn(uri).when(nettyRequestMock).uri();
    doReturn(method).when(nettyRequestMock).method();
    doReturn(headers).when(nettyRequestMock).headers();
    doReturn(trailingHeaders).when(nettyRequestMock).trailingHeaders();
    doReturn(contentByteBuf).when(nettyRequestMock).content();
    doReturn(protocolVersion).when(nettyRequestMock).protocolVersion();
    // when
    RequestInfoImpl<?> requestInfo = new RequestInfoImpl<>(nettyRequestMock);
    // then
    assertThat("getUri was not the same value sent in", requestInfo.getUri(), is(uri));
    assertThat("getPath did not decode as expected", requestInfo.getPath(), is("/some/uri/path/$foobar&"));
    assertThat(requestInfo.getMethod(), is(method));
    assertThat(requestInfo.getHeaders(), is(headers));
    assertThat(requestInfo.getTrailingHeaders(), is(trailingHeaders));
    assertThat(requestInfo.getQueryParams(), notNullValue());
    assertThat(requestInfo.getQueryParams().parameters(), is(expectedQueryParamMap));
    assertThat(requestInfo.getCookies(), is(Sets.newHashSet(new DefaultCookie(cookieName, cookieValue))));
    assertThat(requestInfo.pathTemplate, nullValue());
    assertThat(requestInfo.pathParams.isEmpty(), is(true));
    assertThat(requestInfo.getRawContentBytes(), is(contentBytes));
    assertThat(requestInfo.getRawContent(), is(content));
    assertThat(requestInfo.content, nullValue());
    assertThat(requestInfo.getContentCharset(), is(contentCharset));
    assertThat(requestInfo.getProtocolVersion(), is(protocolVersion));
    assertThat(requestInfo.isKeepAliveRequested(), is(true));
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HashMap(java.util.HashMap) Charset(java.nio.charset.Charset) ByteBuf(io.netty.buffer.ByteBuf) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) List(java.util.List) HttpVersion(io.netty.handler.codec.http.HttpVersion) HttpMethod(io.netty.handler.codec.http.HttpMethod) Test(org.junit.Test)

Example 19 with HttpVersion

use of io.netty.handler.codec.http.HttpVersion in project riposte by Nike-Inc.

the class RequestInfoImplTest method uber_constructor_works_for_valid_values.

@Test
public void uber_constructor_works_for_valid_values() {
    // given
    String uri = "/some/uri/path/%24foobar%26?notused=blah";
    HttpMethod method = HttpMethod.PATCH;
    Charset contentCharset = CharsetUtil.US_ASCII;
    HttpHeaders headers = new DefaultHttpHeaders().add("header1", "val1").add(HttpHeaders.Names.CONTENT_TYPE, "text/text charset=" + contentCharset.displayName());
    QueryStringDecoder queryParams = new QueryStringDecoder(uri + "?foo=bar&secondparam=secondvalue");
    Set<Cookie> cookies = new HashSet<>(Arrays.asList(new DefaultCookie("cookie1", "val1"), new DefaultCookie("cookie2", "val2")));
    Map<String, String> pathParams = Arrays.stream(new String[][] { { "pathParam1", "val1" }, { "pathParam2", "val2" } }).collect(Collectors.toMap(pair -> pair[0], pair -> pair[1]));
    String content = UUID.randomUUID().toString();
    byte[] contentBytes = content.getBytes();
    LastHttpContent chunk = new DefaultLastHttpContent(Unpooled.copiedBuffer(contentBytes));
    chunk.trailingHeaders().add("trailingHeader1", "trailingVal1");
    List<HttpContent> contentChunks = Collections.singletonList(chunk);
    HttpVersion protocolVersion = HttpVersion.HTTP_1_1;
    boolean keepAlive = true;
    boolean fullRequest = true;
    boolean isMultipart = false;
    // when
    RequestInfoImpl<?> requestInfo = new RequestInfoImpl<>(uri, method, headers, chunk.trailingHeaders(), queryParams, cookies, pathParams, contentChunks, protocolVersion, keepAlive, fullRequest, isMultipart);
    // then
    assertThat("getUri should return passed in value", requestInfo.getUri(), is(uri));
    assertThat("getPath did not decode as expected", requestInfo.getPath(), is("/some/uri/path/$foobar&"));
    assertThat(requestInfo.getMethod(), is(method));
    assertThat(requestInfo.getHeaders(), is(headers));
    assertThat(requestInfo.getTrailingHeaders(), is(chunk.trailingHeaders()));
    assertThat(requestInfo.getQueryParams(), is(queryParams));
    assertThat(requestInfo.getCookies(), is(cookies));
    assertThat(requestInfo.pathTemplate, nullValue());
    assertThat(requestInfo.pathParams, is(pathParams));
    assertThat(requestInfo.getRawContentBytes(), is(contentBytes));
    assertThat(requestInfo.getRawContent(), is(content));
    assertThat(requestInfo.content, nullValue());
    assertThat(requestInfo.getContentCharset(), is(contentCharset));
    assertThat(requestInfo.getProtocolVersion(), is(protocolVersion));
    assertThat(requestInfo.isKeepAliveRequested(), is(keepAlive));
    assertThat(requestInfo.isCompleteRequestWithAllChunks, is(fullRequest));
    assertThat(requestInfo.isMultipart, is(isMultipart));
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Arrays(java.util.Arrays) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) Unpooled(io.netty.buffer.Unpooled) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Mockito.doThrow(org.mockito.Mockito.doThrow) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Map(java.util.Map) CharsetUtil(io.netty.util.CharsetUtil) Assertions(org.assertj.core.api.Assertions) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Mockito.doReturn(org.mockito.Mockito.doReturn) RequestContentDeserializationException(com.nike.riposte.server.error.exception.RequestContentDeserializationException) Set(java.util.Set) UUID(java.util.UUID) Cookie(io.netty.handler.codec.http.cookie.Cookie) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) Collectors(java.util.stream.Collectors) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Sets(com.google.common.collect.Sets) List(java.util.List) Type(java.lang.reflect.Type) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) HttpPostMultipartRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostMultipartRequestDecoder) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) RequestInfo(com.nike.riposte.server.http.RequestInfo) HttpVersion(io.netty.handler.codec.http.HttpVersion) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) Mockito.spy(org.mockito.Mockito.spy) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HashSet(java.util.HashSet) Charset(java.nio.charset.Charset) ByteBuf(io.netty.buffer.ByteBuf) ClientCookieEncoder(io.netty.handler.codec.http.cookie.ClientCookieEncoder) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) PathParameterMatchingException(com.nike.riposte.server.error.exception.PathParameterMatchingException) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Whitebox(com.nike.riposte.testutils.Whitebox) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) CoreMatchers.sameInstance(org.hamcrest.CoreMatchers.sameInstance) HttpContent(io.netty.handler.codec.http.HttpContent) FileUpload(io.netty.handler.codec.http.multipart.FileUpload) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TestCase.fail(junit.framework.TestCase.fail) HttpMethod(io.netty.handler.codec.http.HttpMethod) Test(org.junit.Test) IOException(java.io.IOException) Mockito.verify(org.mockito.Mockito.verify) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) Mockito.never(org.mockito.Mockito.never) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) Pair(com.nike.internal.util.Pair) Collections(java.util.Collections) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Charset(java.nio.charset.Charset) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpVersion(io.netty.handler.codec.http.HttpVersion) HttpMethod(io.netty.handler.codec.http.HttpMethod) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 20 with HttpVersion

use of io.netty.handler.codec.http.HttpVersion in project riposte by Nike-Inc.

the class HttpServletRequestWrapperForRequestInfoTest method getProtocol_delegates_to_requestInfo.

@DataProvider(value = { "true", "false" }, splitBy = "\\|")
@Test
public void getProtocol_delegates_to_requestInfo(boolean useNull) {
    // given
    HttpVersion protocolVersion = (useNull) ? null : HttpVersion.HTTP_1_1;
    doReturn(protocolVersion).when(requestInfoMock).getProtocolVersion();
    // when
    String result = wrapper.getProtocol();
    // then
    if (useNull)
        assertThat(result).isNull();
    else
        assertThat(result).isEqualTo(protocolVersion.text());
}
Also used : HttpVersion(io.netty.handler.codec.http.HttpVersion) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Aggregations

HttpVersion (io.netty.handler.codec.http.HttpVersion)22 HttpMethod (io.netty.handler.codec.http.HttpMethod)11 ByteBuf (io.netty.buffer.ByteBuf)8 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)8 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)7 EOFException (java.io.EOFException)7 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)6 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)6 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)5 Map (java.util.Map)4 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)3 HttpRequest (io.netty.handler.codec.http.HttpRequest)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 AuditEvent (org.xipki.audit.AuditEvent)3 ResponderAndPath (org.xipki.ocsp.api.ResponderAndPath)3 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)2 Unpooled (io.netty.buffer.Unpooled)2 Channel (io.netty.channel.Channel)2