use of io.netty.handler.codec.http.HttpMethod in project rest.li by linkedin.
the class NettyRequestAdapter method toNettyRequest.
/**
* Adapts a StreamRequest to Netty's HttpRequest
* @param request R2 stream request
* @return Adapted HttpRequest.
*/
public static HttpRequest toNettyRequest(StreamRequest request) throws Exception {
HttpMethod nettyMethod = HttpMethod.valueOf(request.getMethod());
URL url = new URL(request.getURI().toString());
String path = url.getFile();
// it MUST be given as "/" (the server root).
if (path.isEmpty()) {
path = "/";
}
HttpRequest nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, nettyMethod, path);
nettyRequest.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
// that contains a Transfer-Encoding header field.
if (entry.getKey().equalsIgnoreCase(HttpHeaderNames.CONTENT_LENGTH.toString())) {
continue;
}
nettyRequest.headers().set(entry.getKey(), entry.getValue());
}
nettyRequest.headers().set(HttpHeaderNames.HOST, url.getAuthority());
// RFC 6265
// When the user agent generates an HTTP/1.1 request, the user agent MUST
// NOT attach more than one Cookie header field.
String encodedCookieHeaderValues = CookieUtil.clientEncode(request.getCookies());
if (encodedCookieHeaderValues != null) {
nettyRequest.headers().set(HttpConstants.REQUEST_COOKIE_HEADER_NAME, encodedCookieHeaderValues);
}
return nettyRequest;
}
use of io.netty.handler.codec.http.HttpMethod in project elasticsearch by elastic.
the class Netty4HttpServerTransportTests method testCorsConfigWithDefaults.
public void testCorsConfigWithDefaults() {
final Set<String> methods = Strings.commaDelimitedListToSet(SETTING_CORS_ALLOW_METHODS.getDefault(Settings.EMPTY));
final Set<String> headers = Strings.commaDelimitedListToSet(SETTING_CORS_ALLOW_HEADERS.getDefault(Settings.EMPTY));
final long maxAge = SETTING_CORS_MAX_AGE.getDefault(Settings.EMPTY);
final Settings settings = Settings.builder().put(SETTING_CORS_ENABLED.getKey(), true).build();
final Netty4CorsConfig corsConfig = Netty4HttpServerTransport.buildCorsConfig(settings);
assertFalse(corsConfig.isAnyOriginSupported());
assertEquals(Collections.emptySet(), corsConfig.origins().get());
assertEquals(headers, corsConfig.allowedRequestHeaders());
assertEquals(methods, corsConfig.allowedRequestMethods().stream().map(HttpMethod::name).collect(Collectors.toSet()));
assertEquals(maxAge, corsConfig.maxAge());
assertFalse(corsConfig.isCredentialsAllowed());
}
use of io.netty.handler.codec.http.HttpMethod in project elasticsearch by elastic.
the class Netty4HttpServerTransportTests method testCorsConfig.
public void testCorsConfig() {
final Set<String> methods = new HashSet<>(Arrays.asList("get", "options", "post"));
final Set<String> headers = new HashSet<>(Arrays.asList("Content-Type", "Content-Length"));
// sometimes have a leading whitespace between comma delimited elements
final String prefix = randomBoolean() ? " " : "";
final Settings settings = Settings.builder().put(SETTING_CORS_ENABLED.getKey(), true).put(SETTING_CORS_ALLOW_ORIGIN.getKey(), "*").put(SETTING_CORS_ALLOW_METHODS.getKey(), collectionToDelimitedString(methods, ",", prefix, "")).put(SETTING_CORS_ALLOW_HEADERS.getKey(), collectionToDelimitedString(headers, ",", prefix, "")).put(SETTING_CORS_ALLOW_CREDENTIALS.getKey(), true).build();
final Netty4CorsConfig corsConfig = Netty4HttpServerTransport.buildCorsConfig(settings);
assertTrue(corsConfig.isAnyOriginSupported());
assertEquals(headers, corsConfig.allowedRequestHeaders());
assertEquals(methods, corsConfig.allowedRequestMethods().stream().map(HttpMethod::name).collect(Collectors.toSet()));
}
use of io.netty.handler.codec.http.HttpMethod in project rest.li by linkedin.
the class NettyRequestAdapter method toNettyRequest.
/**
* Adapts a RestRequest to Netty's HttpRequest
* @param request R2 rest request
* @return Adapted HttpRequest.
*/
static HttpRequest toNettyRequest(RestRequest request) throws Exception {
HttpMethod nettyMethod = HttpMethod.valueOf(request.getMethod());
URL url = new URL(request.getURI().toString());
String path = url.getFile();
// it MUST be given as "/" (the server root).
if (path.isEmpty()) {
path = "/";
}
ByteBuf content = Unpooled.wrappedBuffer(request.getEntity().asByteBuffer());
HttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, nettyMethod, path, content);
nettyRequest.headers().set(HttpConstants.CONTENT_LENGTH, request.getEntity().length());
for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
nettyRequest.headers().set(entry.getKey(), entry.getValue());
}
nettyRequest.headers().set(HttpHeaderNames.HOST, url.getAuthority());
nettyRequest.headers().set(HttpConstants.REQUEST_COOKIE_HEADER_NAME, request.getCookies());
return nettyRequest;
}
use of io.netty.handler.codec.http.HttpMethod in project rest.li by linkedin.
the class NettyRequestAdapter method toNettyRequest.
/**
* Adapts a StreamRequest to Netty's HttpRequest
* @param request R2 stream request
* @return Adapted HttpRequest.
*/
static HttpRequest toNettyRequest(StreamRequest request) throws Exception {
HttpMethod nettyMethod = HttpMethod.valueOf(request.getMethod());
URL url = new URL(request.getURI().toString());
String path = url.getFile();
// it MUST be given as "/" (the server root).
if (path.isEmpty()) {
path = "/";
}
HttpRequest nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, nettyMethod, path);
nettyRequest.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
// that contains a Transfer-Encoding header field.
if (entry.getKey().equalsIgnoreCase(HttpHeaderNames.CONTENT_LENGTH.toString())) {
continue;
}
nettyRequest.headers().set(entry.getKey(), entry.getValue());
}
nettyRequest.headers().set(HttpHeaderNames.HOST, url.getAuthority());
nettyRequest.headers().set(HttpConstants.REQUEST_COOKIE_HEADER_NAME, request.getCookies());
return nettyRequest;
}
Aggregations