use of io.netty.handler.codec.http.DefaultFullHttpRequest in project ratpack by ratpack.
the class WebSocketEngine method connect.
@SuppressWarnings("deprecation")
public static <T> void connect(final Context context, String path, int maxLength, final WebSocketHandler<T> handler) {
PublicAddress publicAddress = context.get(PublicAddress.class);
URI address = publicAddress.get(context);
URI httpPath = address.resolve(path);
URI wsPath;
try {
wsPath = new URI("ws", httpPath.getUserInfo(), httpPath.getHost(), httpPath.getPort(), httpPath.getPath(), httpPath.getQuery(), httpPath.getFragment());
} catch (URISyntaxException e) {
throw uncheck(e);
}
WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(wsPath.toString(), null, false, maxLength);
Request request = context.getRequest();
HttpMethod method = valueOf(request.getMethod().getName());
FullHttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, request.getUri());
nettyRequest.headers().add(SEC_WEBSOCKET_VERSION, request.getHeaders().get(SEC_WEBSOCKET_VERSION));
nettyRequest.headers().add(SEC_WEBSOCKET_KEY, request.getHeaders().get(SEC_WEBSOCKET_KEY));
final WebSocketServerHandshaker handshaker = factory.newHandshaker(nettyRequest);
final DirectChannelAccess directChannelAccess = context.getDirectChannelAccess();
final Channel channel = directChannelAccess.getChannel();
if (!channel.config().isAutoRead()) {
channel.config().setAutoRead(true);
}
handshaker.handshake(channel, nettyRequest).addListener(new HandshakeFutureListener<>(context, handshaker, handler));
}
use of io.netty.handler.codec.http.DefaultFullHttpRequest in project ambry by linkedin.
the class FrontendIntegrationTest method buildRequest.
// helpers
// general
/**
* Method to easily create a request.
* @param httpMethod the {@link HttpMethod} desired.
* @param uri string representation of the desired URI.
* @param headers any associated headers as a {@link HttpHeaders} object. Can be null.
* @param content the content that accompanies the request. Can be null.
* @return A {@link FullHttpRequest} object that defines the request required by the input.
*/
private FullHttpRequest buildRequest(HttpMethod httpMethod, String uri, HttpHeaders headers, ByteBuffer content) {
ByteBuf contentBuf;
if (content != null) {
contentBuf = Unpooled.wrappedBuffer(content);
} else {
contentBuf = Unpooled.buffer(0);
}
FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, httpMethod, uri, contentBuf);
if (headers != null) {
httpRequest.headers().set(headers);
}
if (HttpMethod.POST.equals(httpMethod) && !HttpUtil.isContentLengthSet(httpRequest)) {
HttpUtil.setTransferEncodingChunked(httpRequest, true);
}
return httpRequest;
}
use of io.netty.handler.codec.http.DefaultFullHttpRequest in project xipki by xipki.
the class OcspBenchRequestor method ask.
public void ask(BigInteger[] serialNumbers) throws OcspRequestorException {
byte[] ocspReq = buildRequest(serialNumbers);
int size = ocspReq.length;
FullHttpRequest request;
if (size <= MAX_LEN_GET && requestOptions.isUseHttpGetForRequest()) {
String b64Request = Base64.encodeToString(ocspReq);
String urlEncodedReq;
try {
urlEncodedReq = URLEncoder.encode(b64Request, "UTF-8");
} catch (UnsupportedEncodingException ex) {
throw new OcspRequestorException(ex.getMessage());
}
String newRawpath = StringUtil.concat(responderRawPathGet, urlEncodedReq);
request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, newRawpath);
} else {
ByteBuf content = Unpooled.wrappedBuffer(ocspReq);
request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, responderRawPathPost, content);
request.headers().addInt("Content-Length", content.readableBytes());
}
request.headers().add("Content-Type", "application/ocsp-request");
httpClient.send(request);
}
use of io.netty.handler.codec.http.DefaultFullHttpRequest in project jocean-http by isdom.
the class DefaultHttpClientTestCase method testInitiatorInteractionSuccessAsHttps10ConnectionClose.
@Test(timeout = 5000)
public void testInitiatorInteractionSuccessAsHttps10ConnectionClose() throws Exception {
// 配置 池化分配器 为 取消缓存,使用 Heap
configDefaultAllocator();
final PooledByteBufAllocator allocator = defaultAllocator();
final BlockingQueue<HttpTrade> trades = new ArrayBlockingQueue<>(1);
final String addr = UUID.randomUUID().toString();
final Subscription server = TestHttpUtil.createTestServerWith(addr, trades, enableSSL4ServerWithSelfSigned(), Feature.ENABLE_LOGGING_OVER_SSL);
final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), enableSSL4Client(), Feature.ENABLE_LOGGING_OVER_SSL);
try {
final HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "/");
request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.just(request), new Interaction() {
@Override
public void interact(final HttpInitiator initiator, final Observable<DisposableWrapper<FullHttpResponse>> getresp) throws Exception {
final Observable<DisposableWrapper<FullHttpResponse>> cached = getresp.cache();
cached.subscribe();
// server side recv req
final HttpTrade trade = trades.take();
// recv all request
trade.inbound().toCompletable().await();
final ByteBuf svrRespContent = allocator.buffer(CONTENT.length).writeBytes(CONTENT);
// for HTTP 1.0 Connection: Close response behavior
final FullHttpResponse fullrespfromsvr = new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.OK, svrRespContent);
fullrespfromsvr.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
// missing Content-Length
// response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
fullrespfromsvr.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
trade.outbound(Observable.just(fullrespfromsvr));
// wait for recv all resp at client side
cached.toCompletable().await();
svrRespContent.release();
assertTrue(Arrays.equals(dumpResponseContentAsBytes(cached), CONTENT));
}
});
} finally {
assertEquals(0, allActiveAllocationsCount(allocator));
client.close();
server.unsubscribe();
}
}
use of io.netty.handler.codec.http.DefaultFullHttpRequest in project jocean-http by isdom.
the class DefaultHttpTradeTestCase method testTradeForCallAbortBeforeRequestPublish.
// TODO: fix
@Test
public final void testTradeForCallAbortBeforeRequestPublish() {
final DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", Nettys4Test.buildByteBuf("testcontent"));
final EmbeddedChannel channel = new EmbeddedChannel();
final HttpTrade trade = new DefaultHttpTrade(channel);
final TestSubscriber<DisposableWrapper<HttpObject>> reqSubscriber = new TestSubscriber<>();
trade.inbound().subscribe(reqSubscriber);
trade.close();
assertTrue(!trade.isActive());
reqSubscriber.assertTerminalEvent();
reqSubscriber.assertError(Exception.class);
writeToInboundAndFlush(channel, request);
reqSubscriber.assertValueCount(0);
}
Aggregations