本文整理汇总了Java中io.netty.handler.codec.http.HttpVersion.HTTP_1_1属性的典型用法代码示例。如果您正苦于以下问题:Java HttpVersion.HTTP_1_1属性的具体用法?Java HttpVersion.HTTP_1_1怎么用?Java HttpVersion.HTTP_1_1使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类io.netty.handler.codec.http.HttpVersion
的用法示例。
在下文中一共展示了HttpVersion.HTTP_1_1属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testBasicAuthenticationFailure
@Test
public void testBasicAuthenticationFailure() throws Exception {
Configuration config = TestConfiguration.createMinimalConfigurationForTest();
BasicAuthLogin auth = new BasicAuthLogin();
auth.setUsername("test");
auth.setPassword("test2");
DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/login");
request.content().writeBytes(JsonSerializer.getObjectMapper().writeValueAsBytes(auth));
TestHttpQueryDecoder decoder = new TestHttpQueryDecoder(config);
decoder.decode(null, request, results);
Assert.assertEquals(1, results.size());
Object result = results.iterator().next();
Assert.assertEquals(BasicAuthLoginRequest.class, result.getClass());
BasicAuthLoginRequestHandler handler = new BasicAuthLoginRequestHandler(config);
CaptureChannelHandlerContext ctx = new CaptureChannelHandlerContext();
handler.channelRead(ctx, result);
Assert.assertNotNull(ctx.msg);
Assert.assertTrue(ctx.msg instanceof DefaultFullHttpResponse);
DefaultFullHttpResponse response = (DefaultFullHttpResponse) ctx.msg;
Assert.assertEquals(HttpResponseStatus.UNAUTHORIZED, response.getStatus());
Assert.assertTrue(response.headers().contains(Names.CONTENT_TYPE));
Assert.assertEquals(Constants.JSON_TYPE, response.headers().get(Names.CONTENT_TYPE));
}
示例2: newResponse
/**
* Returns a full HTTP response with the specified status, content type, and custom headers.
*
* <p>Headers should be specified as a map of strings. For example, to allow CORS, add the
* following key and value: "access-control-allow-origin", "http://foo.example"
*
* <p>If content type or content length are passed in as custom headers, they will be ignored.
* Instead, content type will be as specified by the parameter contentType and content length will
* be the length of the parameter contentLength.
*/
public static FullHttpResponse newResponse(
HttpResponseStatus status,
ByteBuf payload,
ContentType contentType,
Map<String, String> customHeaders) {
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, payload);
if (customHeaders != null) {
for (Map.Entry<String, String> entry : customHeaders.entrySet()) {
response.headers().set(entry.getKey(), entry.getValue());
}
}
response.headers().set(CONTENT_TYPE, contentType.value);
response.headers().setInt(CONTENT_LENGTH, payload.readableBytes());
return response;
}
示例3: shouldHandlerRequestAndResponse
@Test
public void shouldHandlerRequestAndResponse() {
inboundChannel.pipeline().addLast(handler);
DefaultFullHttpRequest req = new DefaultFullHttpRequest(
HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
inboundChannel.write(req);
assertEquals(1, inboundChannel.outboundMessages().size());
Object outboundReq = inboundChannel.outboundMessages().poll();
assertTrue(outboundReq instanceof ByteBuf);
assertEquals("GET / HTTP/1.1\r\n\r\n", new String(readBytes((ByteBuf) outboundReq)));
DefaultFullHttpResponse resp = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
assertFalse(inboundChannel.writeInbound(resp));
assertEquals(1, outboundChannel.outboundMessages().size());
assertEquals(resp, outboundChannel.outboundMessages().poll());
resp.release();
}
示例4: shouldPendingRequests
@Test
public void shouldPendingRequests() {
inboundChannel.pipeline().addLast(handler);
DefaultFullHttpRequest req = new DefaultFullHttpRequest(
HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
// First request
inboundChannel.write(req.retain());
assertEquals(1, inboundChannel.outboundMessages().size());
assertTrue(inboundChannel.outboundMessages().poll() instanceof ByteBuf);
// Second request
inboundChannel.write(req);
// Should pending second request
assertTrue(inboundChannel.outboundMessages().isEmpty());
}
示例5: onOutboundError
@Override
protected void onOutboundError(Throwable err) {
if (!channel().isActive()) {
super.onOutboundError(err);
return;
}
discreteRemoteClose(err);
if (markSentHeaders()) {
log.error("Error starting response. Replying error status", err);
HttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
HttpResponseStatus.INTERNAL_SERVER_ERROR);
response.headers()
.setInt(HttpHeaderNames.CONTENT_LENGTH, 0)
.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
channel().writeAndFlush(response)
.addListener(ChannelFutureListener.CLOSE);
return;
}
markSentBody();
channel().writeAndFlush(EMPTY_BUFFER)
.addListener(ChannelFutureListener.CLOSE);
}
示例6: testIsProxyRequest
@Test
public void testIsProxyRequest() throws IOException{
AppConfiguration appConfig = new AppConfiguration(new ConfigLoader(),
null);
appConfig.init();
PolicyManager policyManager = mock(PolicyManager.class);
NettyRequestProxyFilter filter = new NettyRequestProxyFilter(
policyManager, appConfig);
DefaultFullHttpRequest req = new DefaultFullHttpRequest(
HttpVersion.HTTP_1_1, HttpMethod.GET, "/version");
assertTrue(filter.isProxyRequest(req));
assertRequest(filter, req, false, "localhost:32876");
assertRequest(filter, req, false, "127.0.0.1:32876");
assertRequest(filter, req, true, "localhost:32877");
assertRequest(filter, req, true, "localhost");
assertRequest(filter, req, true, "127.0.0.1");
assertRequest(filter, req, true, "trace.vip.ebay.com");
assertRequest(filter, req, true, "");
}
示例7: writeResponse
private void writeResponse(final Channel channel, final HttpResponseStatus statusCode) {
// Convert the response content to a ChannelBuffer.
final ByteBuf buf = copiedBuffer(responseContent.toString(), CharsetUtil.UTF_8);
responseContent.setLength(0);
// Decide whether to close the connection or not.
final boolean close = HttpHeaders.Values.CLOSE.equalsIgnoreCase(request.headers().get(CONNECTION)) ||
request.getProtocolVersion().equals(HttpVersion.HTTP_1_0) && !HttpHeaders.Values.KEEP_ALIVE.equalsIgnoreCase(request.headers().get(CONNECTION));
// Build the response object.
final FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, statusCode, buf);
response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
if (!close) {
// There's no need to add 'Content-Length' header if this is the last response.
response.headers().set(CONTENT_LENGTH, buf.readableBytes());
}
// Write the response.
final ChannelFuture future = channel.writeAndFlush(response);
// Close the connection after the write operation is done if necessary.
if (close) {
future.addListener(ChannelFutureListener.CLOSE);
}
}
示例8: startProxyServerWithFilterAnsweringStatusCode
private void startProxyServerWithFilterAnsweringStatusCode(int statusCode) {
final HttpResponseStatus status = HttpResponseStatus.valueOf(statusCode);
HttpFiltersSource filtersSource = new HttpFiltersSourceAdapter() {
@Override
public HttpFilters filterRequest(HttpRequest originalRequest) {
return new HttpFiltersAdapter(originalRequest) {
@Override
public HttpResponse clientToProxyRequest(HttpObject httpObject) {
return new DefaultHttpResponse(HttpVersion.HTTP_1_1, status);
}
};
}
};
proxyServer = DefaultHttpProxyServer.bootstrap()
.withPort(0)
.withFiltersSource(filtersSource)
.start();
}
示例9: writeResponse
private void writeResponse(Channel channel, Response response, HttpRequest httpRequest) {
ByteBuf buf = Unpooled.copiedBuffer(JsonCodec.encodeResponse(response), CharsetUtil.UTF_8);
FullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf);
String contentType = "text/html; charset=UTF-8";
httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, contentType);
boolean close = httpRequest.headers().contains(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE, true)
|| httpRequest.protocolVersion().equals(HttpVersion.HTTP_1_0)
|| !httpRequest.headers().contains(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE, true);
if (!close) {
httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, buf.readableBytes());
}
ChannelFuture future = channel.writeAndFlush(response);
future.addListener(ChannelFutureListener.CLOSE);
}
示例10: testSuggestPostWithValidTypeAndQuery
@Test
public void testSuggestPostWithValidTypeAndQuery() throws Exception {
// @formatter:off
String content =
"{\n" +
" \"type\": \"metrics\",\n" +
" \"q\": \"sys.cpu.user\"\n" +
"}";
// @formatter:on
decoder = new TestHttpQueryDecoder(config);
DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
"/api/suggest");
request.content().writeBytes(content.getBytes());
addCookie(request);
decoder.decode(null, request, results);
Assert.assertEquals(1, results.size());
Assert.assertEquals(SuggestRequest.class, results.iterator().next().getClass());
SuggestRequest suggest = (SuggestRequest) results.iterator().next();
Assert.assertEquals("metrics", suggest.getType());
Assert.assertEquals("sys.cpu.user", suggest.getQuery().get());
Assert.assertEquals(25, suggest.getMax());
suggest.validate();
}
示例11: sendRequest
private void sendRequest(HttpUrl url) {
start = System.nanoTime();
total = 0;
HttpRequest request = new DefaultFullHttpRequest(
HttpVersion.HTTP_1_1, HttpMethod.GET, url.encodedPath());
request.headers().set(HttpHeaders.Names.HOST, url.host());
request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
channel.writeAndFlush(request);
}
示例12: sendError
private void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
ByteBuf content = Unpooled.copiedBuffer("Failure: " + status + "\r\n",
CharsetUtil.UTF_8);
FullHttpResponse response = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1, status, content);
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
// Close the connection as soon as the error message is sent.
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
示例13: sendRedirect
public void sendRedirect(ChannelHandlerContext ctx, String newUri) {
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.FOUND);
response.headers().set(HttpHeaders.Names.LOCATION, newUri);
// Close the connection as soon as the error message is sent.
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
示例14: createHttpCarbonMessage
public HTTPCarbonMessage createHttpCarbonMessage(String method) {
HTTPCarbonMessage httpCarbonMessage = null;
switch (method) {
case "GET": {
httpCarbonMessage = new HTTPCarbonMessage(
new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, ""));
break;
}
case "PUT": {
httpCarbonMessage = new HTTPCarbonMessage(
new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.PUT, ""));
break;
}
case "PATCH": {
httpCarbonMessage = new HTTPCarbonMessage(
new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.PATCH, ""));
break;
}
case "DELETE": {
httpCarbonMessage = new HTTPCarbonMessage(
new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.DELETE, ""));
break;
}
case "POST": {
httpCarbonMessage = new HTTPCarbonMessage(
new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, ""));
break;
}
default: {
log.error("Invalid request type.");
break;
}
}
return httpCarbonMessage;
}
示例15: newFullRequest
public static FullHttpRequest newFullRequest(
HttpMethod method, String urlPath, ByteBuf payload, ContentType contentType) {
FullHttpRequest request =
new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, urlPath, payload);
request.headers().set(CONTENT_TYPE, contentType.value);
request.headers().setInt(CONTENT_LENGTH, payload.readableBytes());
return request;
}