本文整理汇总了Java中io.netty.handler.codec.http2.Http2Headers类的典型用法代码示例。如果您正苦于以下问题:Java Http2Headers类的具体用法?Java Http2Headers怎么用?Java Http2Headers使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Http2Headers类属于io.netty.handler.codec.http2包,在下文中一共展示了Http2Headers类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onHeadersRead
import io.netty.handler.codec.http2.Http2Headers; //导入依赖的package包/类
@Override
void onHeadersRead(ChannelHandlerContext ctx, int streamId, boolean endOfStream,
Http2Headers headers) {
String[] splitPath = headers.path().toString().split("\\?");
if (splitPath.length <= 1) {
sendResponseString(ctx, streamId, "Header name not found.");
return;
}
String headerName = splitPath[1].toLowerCase(Locale.US);
if (headers.get(headerName) == null) {
sendResponseString(ctx, streamId, "Header not found:" + headerName);
return;
}
sendResponseString(ctx, streamId, headers.get(headerName).toString());
}
示例2: toArmeria
import io.netty.handler.codec.http2.Http2Headers; //导入依赖的package包/类
/**
* Converts the specified Netty HTTP/2 into Armeria HTTP/2 headers.
*/
public static HttpHeaders toArmeria(Http2Headers headers) {
final HttpHeaders converted = new DefaultHttpHeaders(false, headers.size());
StringJoiner cookieJoiner = null;
for (Entry<CharSequence, CharSequence> e : headers) {
final AsciiString name = AsciiString.of(e.getKey());
final CharSequence value = e.getValue();
// Cookies must be concatenated into a single octet string.
// https://tools.ietf.org/html/rfc7540#section-8.1.2.5
if (name.equals(HttpHeaderNames.COOKIE)) {
if (cookieJoiner == null) {
cookieJoiner = new StringJoiner(COOKIE_SEPARATOR);
}
COOKIE_SPLITTER.split(value).forEach(cookieJoiner::add);
} else {
converted.add(name, value.toString());
}
}
if (cookieJoiner != null && cookieJoiner.length() != 0) {
converted.add(HttpHeaderNames.COOKIE, cookieJoiner.toString());
}
return converted;
}
示例3: toNettyHttp2
import io.netty.handler.codec.http2.Http2Headers; //导入依赖的package包/类
/**
* Converts the specified Armeria HTTP/2 headers into Netty HTTP/2 headers.
*/
public static Http2Headers toNettyHttp2(HttpHeaders in) {
final Http2Headers out = new DefaultHttp2Headers(false, in.size());
out.set(in);
out.remove(HttpHeaderNames.CONNECTION);
out.remove(HttpHeaderNames.TRANSFER_ENCODING);
out.remove(HttpHeaderNames.TRAILER);
if (!out.contains(HttpHeaderNames.COOKIE)) {
return out;
}
// Split up cookies to allow for better compression.
// https://tools.ietf.org/html/rfc7540#section-8.1.2.5
final List<CharSequence> cookies = out.getAllAndRemove(HttpHeaderNames.COOKIE);
for (CharSequence c : cookies) {
out.add(HttpHeaderNames.COOKIE, COOKIE_SPLITTER.split(c));
}
return out;
}
示例4: onHeadersRead
import io.netty.handler.codec.http2.Http2Headers; //导入依赖的package包/类
@Override
public void onHeadersRead(
ChannelHandlerContext ctx,
int streamId,
Http2Headers headers,
int streamDependency,
short weight,
boolean exclusive,
int padding,
boolean endStream)
throws Http2Exception {
if (isServer) {
ctx.fireChannelRead(Http2Request.build(streamId, headers, endStream));
} else {
ctx.fireChannelRead(Http2Response.build(streamId, headers, endStream));
}
}
示例5: writeContent
import io.netty.handler.codec.http2.Http2Headers; //导入依赖的package包/类
void writeContent(ChannelHandlerContext ctx, StreamingData data, ChannelPromise promise) {
Request request = getChannelRequest(ctx);
int streamId = request.streamId();
if (data.endOfStream()) {
setChannelRequest(ctx, null);
}
boolean dataEos = data.endOfStream() && data.trailingHeaders().size() == 0;
Http2Response response =
Http2Response.build(streamId, new DefaultHttp2DataFrame(data.content(), dataEos), dataEos);
if (data.trailingHeaders().size() != 0) {
Http2Headers headers = data.trailingHeaders().http2Headers();
Http2Response last = Http2Response.build(streamId, headers, true);
PromiseCombiner combiner = new PromiseCombiner();
combiner.add(ctx.write(response, ctx.newPromise()));
combiner.add(ctx.write(last, ctx.newPromise()));
combiner.finish(promise);
} else {
ctx.write(response, promise);
}
}
示例6: keepAliveEnforcer_sendingDataResetsCounters
import io.netty.handler.codec.http2.Http2Headers; //导入依赖的package包/类
@Test
public void keepAliveEnforcer_sendingDataResetsCounters() throws Exception {
permitKeepAliveWithoutCalls = false;
permitKeepAliveTimeInNanos = TimeUnit.HOURS.toNanos(1);
manualSetUp();
createStream();
Http2Headers headers = Utils.convertServerHeaders(new Metadata());
ChannelFuture future = enqueue(
SendResponseHeadersCommand.createHeaders(stream.transportState(), headers));
future.get();
ByteBuf payload = handler().ctx().alloc().buffer(8);
payload.writeLong(1);
for (int i = 0; i < 10; i++) {
future = enqueue(
new SendGrpcFrameCommand(stream.transportState(), content().retainedSlice(), false));
future.get();
channel().releaseOutbound();
channelRead(pingFrame(false /* isAck */, payload.slice()));
}
payload.release();
verifyWrite(never()).writeGoAway(eq(ctx()), eq(STREAM_ID),
eq(Http2Error.ENHANCE_YOUR_CALM.code()), any(ByteBuf.class), any(ChannelPromise.class));
}
示例7: testFullRequest
import io.netty.handler.codec.http2.Http2Headers; //导入依赖的package包/类
@Test
public void testFullRequest() throws Exception {
outputReceived = new CountDownLatch(1);
Http2Headers headers = new DefaultHttp2Headers().method("GET").path("/");
Http2Request requestIn = Http2Request.build(1, headers, true);
channel.writeInbound(requestIn);
channel.runPendingTasks(); // blocks
Uninterruptibles.awaitUninterruptibly(outputReceived);
Request requestOut = requests.remove(0);
assertTrue(requestOut != null);
assertTrue(requestOut instanceof FullRequest);
assertEquals("h2", requestOut.version());
assertEquals(HttpMethod.GET, requestOut.method());
assertEquals("/", requestOut.path());
assertFalse(requestOut.hasBody());
assertFalse(requestOut.body() == null);
assertEquals(0, requestOut.body().readableBytes());
assertEquals(1, requestOut.streamId());
}
示例8: testFullResponse
import io.netty.handler.codec.http2.Http2Headers; //导入依赖的package包/类
@Test
public void testFullResponse() throws Exception {
outputReceived = new CountDownLatch(2);
Http2Headers headersIn = new DefaultHttp2Headers().method("GET").path("/");
Http2Request requestIn = Http2Request.build(1, headersIn, true);
FullResponse responseIn = ResponseBuilders.newOk().body(Unpooled.EMPTY_BUFFER).build();
channel.writeInbound(requestIn);
channel.runPendingTasks(); // blocks
channel.writeOutbound(responseIn);
channel.runPendingTasks(); // blocks
Uninterruptibles.awaitUninterruptibly(outputReceived);
Http2Response responseOut = (Http2Response) responses.remove(0);
assertTrue(responseOut != null);
assertTrue(responseOut.payload instanceof Http2Headers);
assertEquals("200", ((Http2Headers) responseOut.payload).status().toString());
assertTrue(responseOut.eos);
assertEquals(1, responseOut.streamId);
}
示例9: testFullResponse
import io.netty.handler.codec.http2.Http2Headers; //导入依赖的package包/类
@Test
public void testFullResponse() throws Exception {
outputReceived = new CountDownLatch(1);
Http2Headers headers = new DefaultHttp2Headers().status("200");
Http2Response responseIn = Http2Response.build(1, headers, true);
channel.writeInbound(responseIn);
channel.runPendingTasks(); // blocks
Uninterruptibles.awaitUninterruptibly(outputReceived);
Response responseOut = responses.remove(0);
assertTrue(responseOut != null);
assertTrue(responseOut instanceof FullResponse);
assertEquals("h2", responseOut.version());
assertEquals(OK, responseOut.status());
assertFalse(responseOut.hasBody());
assertEquals(1, responseOut.streamId());
}
示例10: push
import io.netty.handler.codec.http2.Http2Headers; //导入依赖的package包/类
@Override
public void push(final String method, final String path, final Map<String, Object> headers) {
ctx.channel().eventLoop().execute(() -> {
AsciiString streamIdHeader = HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text();
Http2Connection connection = encoder.connection();
int nextStreamId = connection.local().incrementAndGetNextStreamId();
Http2Headers h2headers = new DefaultHttp2Headers()
.path(path)
.method(method)
.authority(authority)
.scheme(scheme);
headers.forEach((n, v) -> h2headers.add(n, v.toString()));
encoder.writePushPromise(ctx, streamId, nextStreamId, h2headers, 0, ctx.newPromise());
// TODO: Is there another way of handling a push promise?
DefaultFullHttpRequest pushRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
HttpMethod.valueOf(method.toUpperCase()), path, Unpooled.EMPTY_BUFFER,
new DefaultHttpHeaders(false).set(streamIdHeader, nextStreamId),
EmptyHttpHeaders.INSTANCE);
ctx.pipeline().fireChannelRead(pushRequest);
ctx.pipeline().fireChannelReadComplete();
});
}
示例11: headersWithInvalidMethodShouldFail
import io.netty.handler.codec.http2.Http2Headers; //导入依赖的package包/类
@Test
public void headersWithInvalidMethodShouldFail() throws Exception {
manualSetUp();
Http2Headers headers = new DefaultHttp2Headers()
.method(HTTP_FAKE_METHOD)
.set(CONTENT_TYPE_HEADER, CONTENT_TYPE_GRPC)
.path(new AsciiString("/foo/bar"));
ByteBuf headersFrame = headersFrame(STREAM_ID, headers);
channelRead(headersFrame);
Http2Headers responseHeaders = new DefaultHttp2Headers()
.set(InternalStatus.CODE_KEY.name(), String.valueOf(Code.INTERNAL.value()))
.set(InternalStatus.MESSAGE_KEY.name(), "Method 'FAKE' is not supported")
.status("" + 405)
.set(CONTENT_TYPE_HEADER, "text/plain; encoding=utf-8");
verifyWrite().writeHeaders(eq(ctx()), eq(STREAM_ID), eq(responseHeaders), eq(0),
eq(DEFAULT_PRIORITY_WEIGHT), eq(false), eq(0), eq(false), any(ChannelPromise.class));
}
示例12: convertClientHeaders
import io.netty.handler.codec.http2.Http2Headers; //导入依赖的package包/类
public static Http2Headers convertClientHeaders(Metadata headers,
AsciiString scheme,
AsciiString defaultPath,
AsciiString authority,
AsciiString method,
AsciiString userAgent) {
Preconditions.checkNotNull(defaultPath, "defaultPath");
Preconditions.checkNotNull(authority, "authority");
Preconditions.checkNotNull(method, "method");
// Discard any application supplied duplicates of the reserved headers
headers.discardAll(CONTENT_TYPE_KEY);
headers.discardAll(GrpcUtil.TE_HEADER);
headers.discardAll(GrpcUtil.USER_AGENT_KEY);
return GrpcHttp2OutboundHeaders.clientRequestHeaders(
toHttp2Headers(headers),
authority,
defaultPath,
method,
scheme,
userAgent);
}
示例13: createStream
import io.netty.handler.codec.http2.Http2Headers; //导入依赖的package包/类
private void createStream() throws Exception {
Http2Headers headers = new DefaultHttp2Headers()
.method(HTTP_METHOD)
.set(CONTENT_TYPE_HEADER, CONTENT_TYPE_GRPC)
.set(TE_HEADER, TE_TRAILERS)
.path(new AsciiString("/foo/bar"));
ByteBuf headersFrame = headersFrame(STREAM_ID, headers);
channelRead(headersFrame);
ArgumentCaptor<NettyServerStream> streamCaptor =
ArgumentCaptor.forClass(NettyServerStream.class);
ArgumentCaptor<String> methodCaptor = ArgumentCaptor.forClass(String.class);
verify(transportListener).streamCreated(streamCaptor.capture(), methodCaptor.capture(),
any(Metadata.class));
stream = streamCaptor.getValue();
}
示例14: decode_responseHeaders
import io.netty.handler.codec.http2.Http2Headers; //导入依赖的package包/类
@Test
public void decode_responseHeaders() throws Http2Exception {
Http2HeadersDecoder decoder = new GrpcHttp2ClientHeadersDecoder(DEFAULT_MAX_HEADER_LIST_SIZE);
Http2HeadersEncoder encoder =
new DefaultHttp2HeadersEncoder(NEVER_SENSITIVE);
Http2Headers headers = new DefaultHttp2Headers(false);
headers.add(of(":status"), of("200")).add(of("custom"), of("header"));
encodedHeaders = Unpooled.buffer();
encoder.encodeHeaders(1 /* randomly chosen */, headers, encodedHeaders);
Http2Headers decodedHeaders = decoder.decodeHeaders(3 /* randomly chosen */, encodedHeaders);
assertEquals(headers.get(of(":status")), decodedHeaders.get(of(":status")));
assertEquals(headers.get(of("custom")), decodedHeaders.get(of("custom")));
assertEquals(headers.size(), decodedHeaders.size());
String toString = decodedHeaders.toString();
assertContainsKeyAndValue(toString, ":status", decodedHeaders.get(of(":status")));
assertContainsKeyAndValue(toString, "custom", decodedHeaders.get(of("custom")));
}
示例15: basicCorrectness
import io.netty.handler.codec.http2.Http2Headers; //导入依赖的package包/类
@Test
public void basicCorrectness() {
Http2Headers headers = new GrpcHttp2RequestHeaders(1);
headers.add(of(":method"), of("POST"));
headers.add(of("content-type"), of("application/grpc+proto"));
headers.add(of(":path"), of("/google.pubsub.v2.PublisherService/CreateTopic"));
headers.add(of(":scheme"), of("https"));
headers.add(of("te"), of("trailers"));
headers.add(of(":authority"), of("pubsub.googleapis.com"));
headers.add(of("foo"), of("bar"));
assertEquals(7, headers.size());
// Number of headers without the pseudo headers and 'te' header.
assertEquals(2, ((GrpcHttp2InboundHeaders)headers).numHeaders());
assertEquals(of("application/grpc+proto"), headers.get(of("content-type")));
assertEquals(of("/google.pubsub.v2.PublisherService/CreateTopic"), headers.path());
assertEquals(of("https"), headers.scheme());
assertEquals(of("POST"), headers.method());
assertEquals(of("pubsub.googleapis.com"), headers.authority());
assertEquals(of("trailers"), headers.get(of("te")));
assertEquals(of("bar"), headers.get(of("foo")));
}