本文整理匯總了Java中io.netty.handler.codec.http.HttpHeaderValues類的典型用法代碼示例。如果您正苦於以下問題:Java HttpHeaderValues類的具體用法?Java HttpHeaderValues怎麽用?Java HttpHeaderValues使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
HttpHeaderValues類屬於io.netty.handler.codec.http包,在下文中一共展示了HttpHeaderValues類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: channelRead
import io.netty.handler.codec.http.HttpHeaderValues; //導入依賴的package包/類
@Override
public void channelRead(ChannelHandlerContext ctx, Object e) throws Exception {
if (e instanceof ServletResponse) {
logger.info("Handler async task...");
HttpServletResponse response = (HttpServletResponse) e;
Runnable task = ThreadLocalAsyncExecutor.pollTask(response);
task.run();
// write response...
ChannelFuture future = ctx.channel().writeAndFlush(response);
String keepAlive = response.getHeader(CONNECTION.toString());
if (null != keepAlive && HttpHeaderValues.KEEP_ALIVE.toString().equalsIgnoreCase(keepAlive)) {
future.addListener(ChannelFutureListener.CLOSE);
}
} else {
ctx.fireChannelRead(e);
}
}
示例2: writeResponseHead
import io.netty.handler.codec.http.HttpHeaderValues; //導入依賴的package包/類
@Override
public void writeResponseHead(Response restletResponse) throws IOException {
setNettyResponse(new DefaultHttpResponse(HTTP_1_1, new HttpResponseStatus(getStatusCode(), getReasonPhrase())));
HttpHeaders headers = getNettyResponse().headers();
// this.response.clear();
for (Header header : getResponseHeaders()) {
headers.add(header.getName(), header.getValue());
}
// Decide whether to close the connection or not.
if (isKeepAlive()) {
headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
getNettyChannel().write(getNettyResponse());
} else {
getNettyChannel().writeAndFlush(getNettyResponse()).addListener(ChannelFutureListener.CLOSE);
}
}
示例3: setDefaultHeaders
import io.netty.handler.codec.http.HttpHeaderValues; //導入依賴的package包/類
protected static void setDefaultHeaders(HttpRequest httpRequest) {
if (!httpRequest.headers().contains(HttpHeaderNames.HOST)) {
httpRequest.headers().set(HttpHeaderNames.HOST, httpRequest.uriObject().getHost());
}
if (!httpRequest.headers().contains(HttpHeaderNames.CONNECTION)) {
httpRequest.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
}
if (!httpRequest.headers().contains(HttpHeaderNames.ACCEPT_ENCODING)) {
httpRequest.headers().set(HttpHeaderNames.ACCEPT_ENCODING,
HttpHeaderValues.GZIP + ", " + HttpHeaderValues.DEFLATE);
}
if (!httpRequest.headers().contains(HttpHeaderNames.ACCEPT_CHARSET)) {
httpRequest.headers().set(HttpHeaderNames.ACCEPT_CHARSET, "utf-8");
}
if (!httpRequest.headers().contains(HttpHeaderNames.CONTENT_TYPE)) {
httpRequest.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED);
}
}
示例4: setDefaultHeaders
import io.netty.handler.codec.http.HttpHeaderValues; //導入依賴的package包/類
protected static void setDefaultHeaders(FullHttpRequest request, HttpResponse response) {
response.headers().add(HttpHeaderNames.SERVER,
"lannister " + net.anyflow.lannister.Settings.INSTANCE.version());
boolean keepAlive = HttpHeaderValues.KEEP_ALIVE.toString()
.equals(request.headers().get(HttpHeaderNames.CONNECTION));
if (keepAlive) {
response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
}
if (Settings.INSTANCE.getProperty("webserver.allowCrossDomain", "false").equalsIgnoreCase("true")) {
response.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
response.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_METHODS, "POST, GET, PUT, DELETE");
response.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, "X-PINGARUNER");
response.headers().add(HttpHeaderNames.ACCESS_CONTROL_MAX_AGE, "1728000");
}
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
}
示例5: parameters
import io.netty.handler.codec.http.HttpHeaderValues; //導入依賴的package包/類
public Map<String, List<String>> parameters() {
if (parameters != null) { return parameters; }
Map<String, List<String>> ret = Maps.newHashMap();
if (HttpMethod.GET.equals(method()) || HttpMethod.DELETE.equals(method())) {
ret.putAll(new QueryStringDecoder(uri()).parameters());
return ret;
}
else if (headers().contains(HttpHeaderNames.CONTENT_TYPE)
&& headers().get(HttpHeaderNames.CONTENT_TYPE)
.startsWith(HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED.toString())
&& (HttpMethod.POST.equals(method()) || HttpMethod.PUT.equals(method()))) {
ret.putAll(new QueryStringDecoder("/dummy?" + content().toString(CharsetUtil.UTF_8)).parameters());
}
return ret;
}
示例6: normalizeParameters
import io.netty.handler.codec.http.HttpHeaderValues; //導入依賴的package包/類
private void normalizeParameters() {
String address = new StringBuilder().append(uriObject().getScheme()).append("://")
.append(uriObject().getAuthority()).append(uriObject().getPath()).toString();
if (HttpMethod.GET.equals(method()) || HttpMethod.DELETE.equals(method())) {
String parameters = convertParametersToString();
address += Strings.isNullOrEmpty(parameters) ? "" : "?" + parameters;
}
else if ((HttpMethod.POST.equals(method()) || HttpMethod.PUT.equals(method()))
&& (!headers().contains(HttpHeaderNames.CONTENT_TYPE) || headers().get(HttpHeaderNames.CONTENT_TYPE)
.startsWith(HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED.toString()))) {
ByteBuf content = Unpooled.copiedBuffer(convertParametersToString(), CharsetUtil.UTF_8);
headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
content().clear();
content().writeBytes(content);
}
setUri(address);
}
示例7: handler
import io.netty.handler.codec.http.HttpHeaderValues; //導入依賴的package包/類
static Function<? super HttpClientRequest, ? extends Publisher<Void>> handler(Function<? super HttpClientRequest, ? extends Publisher<Void>> h,
HttpClientOptions opts) {
if (opts.acceptGzip()) {
if (h != null) {
return req -> h.apply(req.header(HttpHeaderNames.ACCEPT_ENCODING,
HttpHeaderValues.GZIP));
}
else {
return req -> req.header(HttpHeaderNames.ACCEPT_ENCODING,
HttpHeaderValues.GZIP);
}
}
else {
return h;
}
}
示例8: ws
import io.netty.handler.codec.http.HttpHeaderValues; //導入依賴的package包/類
/**
* Listen for WebSocket on the passed path to be used as a routing condition. Incoming
* connections will query the internal registry to invoke the matching handlers. <p>
* Additional regex matching is available e.g. "/test/{param}".
* Params are resolved using {@link HttpServerRequest#param(CharSequence)}
* They are not accessible in the handler provided as parameter.
*
* @param path The websocket path used by clients
* @param handler an handler to invoke for the given condition
* @param protocols sub-protocol to use in WS handshake signature
*
* @return a new handler
*/
@SuppressWarnings("unchecked")
default HttpServerRoutes ws(String path,
BiFunction<? super WebsocketInbound, ? super WebsocketOutbound, ? extends
Publisher<Void>> handler,
String protocols) {
Predicate<HttpServerRequest> condition = HttpPredicate.get(path);
return route(condition, (req, resp) -> {
if (req.requestHeaders()
.contains(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE, true)) {
HttpServerOperations ops = (HttpServerOperations) req;
return ops.withWebsocketSupport(req.uri(), protocols,
handler);
}
return resp.sendNotFound();
});
}
示例9: onOutboundError
import io.netty.handler.codec.http.HttpHeaderValues; //導入依賴的package包/類
@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);
}
示例10: writeResponse
import io.netty.handler.codec.http.HttpHeaderValues; //導入依賴的package包/類
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);
}
示例11: channelRead0
import io.netty.handler.codec.http.HttpHeaderValues; //導入依賴的package包/類
@Override
public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
if (HttpUtil.is100ContinueExpected(req)) {
ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
}
boolean keepAlive = HttpUtil.isKeepAlive(req);
ByteBuf content = ctx.alloc().buffer();
content.writeBytes(HelloWorldHttp2Handler.RESPONSE_BYTES.duplicate());
ByteBufUtil.writeAscii(content, " - via " + req.protocolVersion() + " (" + establishApproach + ")");
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content);
response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());
if (!keepAlive) {
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
} else {
response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
ctx.writeAndFlush(response);
}
}
示例12: sendResponse
import io.netty.handler.codec.http.HttpHeaderValues; //導入依賴的package包/類
@Override
protected void sendResponse(final ChannelHandlerContext ctx, String streamId, int latency,
final FullHttpResponse response, final FullHttpRequest request) {
HttpUtil.setContentLength(response, response.content().readableBytes());
ctx.executor().schedule(new Runnable() {
@Override
public void run() {
if (isKeepAlive(request)) {
response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
ctx.writeAndFlush(response);
} else {
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
}
}, latency, TimeUnit.MILLISECONDS);
}
示例13: channelRead0
import io.netty.handler.codec.http.HttpHeaderValues; //導入依賴的package包/類
@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof HttpRequest) {
HttpRequest req = (HttpRequest) msg;
if (is100ContinueExpected(req)) {
ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
}
boolean keepAlive = isKeepAlive(req);
ByteBuf content = Unpooled.copiedBuffer("Hello World " + new Date(), CharsetUtil.UTF_8);
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content);
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
response.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
if (!keepAlive) {
ctx.write(response).addListener(ChannelFutureListener.CLOSE);
} else {
response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
ctx.write(response);
}
}
}
示例14: messageReceived
import io.netty.handler.codec.http.HttpHeaderValues; //導入依賴的package包/類
@Override
public void messageReceived(ChannelHandlerContext ctx, HttpRequest req) throws Exception {
if (HttpHeaderUtil.is100ContinueExpected(req)) {
ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
}
boolean keepAlive = HttpHeaderUtil.isKeepAlive(req);
ByteBuf content = ctx.alloc().buffer();
content.writeBytes(HelloWorldHttp2Handler.RESPONSE_BYTES.duplicate());
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content);
response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());
if (!keepAlive) {
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
} else {
response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
ctx.writeAndFlush(response);
}
}
示例15: fail
import io.netty.handler.codec.http.HttpHeaderValues; //導入依賴的package包/類
private void fail(ChannelHandlerContext ctx, HttpResponseStatus status) {
discarding = true;
req = null;
final ChannelFuture future;
if (receivedRequests <= sentResponses) {
// Just close the connection if sending an error response will make the number of the sent
// responses exceed the number of the received requests, which doesn't make sense.
future = ctx.writeAndFlush(Unpooled.EMPTY_BUFFER);
} else {
final ByteBuf content = Unpooled.copiedBuffer(status.toString(), StandardCharsets.UTF_8);
final FullHttpResponse res = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1, status, content);
final HttpHeaders headers = res.headers();
headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
headers.set(HttpHeaderNames.CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8);
headers.setInt(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
future = ctx.writeAndFlush(res);
}
future.addListener(ChannelFutureListener.CLOSE);
}