@Override public boolean isEmpty() { return this.headers.isEmpty(); }
@Override public boolean isEmpty() { return headers.isEmpty(); }
@Override public boolean hasResponseHeaders() { return headers != null && !headers.isEmpty(); }
@Override public boolean isEmpty() { return this.headers.isEmpty(); }
private boolean abortAfterHandlingHeaders(AsyncHandler<?> handler, HttpHeaders responseHeaders) throws Exception { return !responseHeaders.isEmpty() && handler.onHeadersReceived(responseHeaders) == State.ABORT; }
/** * Retains all current headers but calls {@link #set(String, Object)} for each entry in {@code headers} * * @param headers The headers used to {@link #set(String, Object)} values in this instance * @return {@code this} */ public HttpHeaders setAll(HttpHeaders headers) { checkNotNull(headers, "headers"); if (headers.isEmpty()) { return this; } for (Entry<String, String> entry : headers) { set(entry.getKey(), entry.getValue()); } return this; }
/** * Cleans the current header entries and copies all header entries of the specified {@code headers}. * * @return {@code this} */ public HttpHeaders set(HttpHeaders headers) { checkNotNull(headers, "headers"); clear(); if (headers.isEmpty()) { return this; } for (Entry<String, String> entry : headers) { add(entry.getKey(), entry.getValue()); } return this; }
@Override public String toString() { StringBuilder sb = new StringBuilder(getUrl()); sb.append("\t"); sb.append(method); sb.append("\theaders:"); if (!headers.isEmpty()) { for (Map.Entry<String, String> header : headers) { sb.append("\t"); sb.append(header.getKey()); sb.append(":"); sb.append(header.getValue()); } } if (isNonEmpty(formParams)) { sb.append("\tformParams:"); for (Param param : formParams) { sb.append("\t"); sb.append(param.getName()); sb.append(":"); sb.append(param.getValue()); } } return sb.toString(); } }
/** * Checks if the given HTTP message should be considered as a last SPDY frame. * * @param httpMessage check this HTTP message * @return whether the given HTTP message should generate a <em>last</em> SPDY frame. */ private static boolean isLast(HttpMessage httpMessage) { if (httpMessage instanceof FullHttpMessage) { FullHttpMessage fullMessage = (FullHttpMessage) httpMessage; if (fullMessage.trailingHeaders().isEmpty() && !fullMessage.content().isReadable()) { return true; } } return false; } }
private void encodeLastContent(LastHttpContent last, List<Object> out) { boolean needFiller = !(last instanceof FullHttpMessage) && last.trailingHeaders().isEmpty(); if (last.content().isReadable() || needFiller) { out.add(new DefaultHttp2DataFrame(last.content().retain(), last.trailingHeaders().isEmpty())); } if (!last.trailingHeaders().isEmpty()) { Http2Headers headers = HttpConversionUtil.toHttp2Headers(last.trailingHeaders(), validateHeaders); out.add(new DefaultHttp2HeadersFrame(headers, true)); } }
public static Http2Headers toHttp2Headers(HttpHeaders inHeaders, boolean validateHeaders) { if (inHeaders.isEmpty()) { return EmptyHttp2Headers.INSTANCE; } final Http2Headers out = new DefaultHttp2Headers(validateHeaders, inHeaders.size()); toHttp2Headers(inHeaders, out); return out; }
/** * Converts the specified Netty HTTP/1 headers into Armeria HTTP/2 headers. */ public static HttpHeaders toArmeria(io.netty.handler.codec.http.HttpHeaders inHeaders) { if (inHeaders.isEmpty()) { return HttpHeaders.EMPTY_HEADERS; } final HttpHeaders out = new DefaultHttpHeaders(true, inHeaders.size()); toArmeria(inHeaders, out); return out; }
@Test public void testSetHeaders() { RequestBuilder requestBuilder = new RequestBuilder(); assertTrue(requestBuilder.headers.isEmpty(), "Headers should be empty by default."); Map<CharSequence, Collection<?>> headers = new HashMap<>(); headers.put("Content-Type", Collections.singleton("application/json")); requestBuilder.setHeaders(headers); assertTrue(requestBuilder.headers.contains("Content-Type"), "headers set by setHeaders have not been set"); assertEquals(requestBuilder.headers.get("Content-Type"), "application/json", "header value incorrect"); }
private void decodeContent(HttpContent c, List<Object> out) { ByteBuf content = c.content(); decode(content, out); if (c instanceof LastHttpContent) { finishDecode(out); LastHttpContent last = (LastHttpContent) c; // Generate an additional chunk if the decoder produced // the last product on closure, HttpHeaders headers = last.trailingHeaders(); if (headers.isEmpty()) { out.add(LastHttpContent.EMPTY_LAST_CONTENT); } else { out.add(new ComposedLastHttpContent(headers)); } } }
private void encodeChunkedContent(ChannelHandlerContext ctx, Object msg, long contentLength, List<Object> out) { if (contentLength > 0) { String lengthHex = Long.toHexString(contentLength); ByteBuf buf = ctx.alloc().buffer(lengthHex.length() + 2); buf.writeCharSequence(lengthHex, CharsetUtil.US_ASCII); ByteBufUtil.writeShortBE(buf, CRLF_SHORT); out.add(buf); out.add(encodeAndRetain(msg)); out.add(CRLF_BUF.duplicate()); } if (msg instanceof LastHttpContent) { HttpHeaders headers = ((LastHttpContent) msg).trailingHeaders(); if (headers.isEmpty()) { out.add(ZERO_CRLF_CRLF_BUF.duplicate()); } else { ByteBuf buf = ctx.alloc().buffer((int) trailersEncodedSizeAccumulator); ByteBufUtil.writeMediumBE(buf, ZERO_CRLF_MEDIUM); encodeHeaders(headers, buf); ByteBufUtil.writeShortBE(buf, CRLF_SHORT); trailersEncodedSizeAccumulator = TRAILERS_WEIGHT_NEW * padSizeForAccumulation(buf.readableBytes()) + TRAILERS_WEIGHT_HISTORICAL * trailersEncodedSizeAccumulator; out.add(buf); } } else if (contentLength == 0) { // Need to produce some output otherwise an // IllegalStateException will be thrown out.add(encodeAndRetain(msg)); } }
private boolean encodeContent(HttpContent c, List<Object> out) { ByteBuf content = c.content(); encode(content, out); if (c instanceof LastHttpContent) { finishEncode(out); LastHttpContent last = (LastHttpContent) c; // Generate an additional chunk if the decoder produced // the last product on closure, HttpHeaders headers = last.trailingHeaders(); if (headers.isEmpty()) { out.add(LastHttpContent.EMPTY_LAST_CONTENT); } else { out.add(new ComposedLastHttpContent(headers)); } return true; } return false; }
private void handleReadHttpContent(ChannelHandlerContext ctx, HttpContent content) { if (!ignoreBodyRead) { if (content instanceof LastHttpContent) { if (content.content().readableBytes() > 0 || !((LastHttpContent) content).trailingHeaders().isEmpty()) { // It has data or trailing headers, send them ctx.fireChannelRead(content); } else { ReferenceCountUtil.release(content); } removeHandlerIfActive(ctx, ctx.name() + "-body-publisher"); currentlyStreamedMessage = null; consumedInMessage(ctx); } else { ctx.fireChannelRead(content); } } else { ReferenceCountUtil.release(content); if (content instanceof LastHttpContent) { ignoreBodyRead = false; if (currentlyStreamedMessage != null) { removeHandlerIfActive(ctx, ctx.name() + "-body-publisher"); } currentlyStreamedMessage = null; } } }
private void handleChunk(HttpContent chunk, final Channel channel, final NettyResponseFuture<?> future, AsyncHandler<?> handler) throws Exception { boolean abort = false; boolean last = chunk instanceof LastHttpContent; // Netty 4: the last chunk is not empty if (last) { LastHttpContent lastChunk = (LastHttpContent) chunk; HttpHeaders trailingHeaders = lastChunk.trailingHeaders(); if (!trailingHeaders.isEmpty()) { abort = handler.onTrailingHeadersReceived(trailingHeaders) == State.ABORT; } } ByteBuf buf = chunk.content(); if (!abort && !(handler instanceof StreamedAsyncHandler) && (buf.isReadable() || last)) { HttpResponseBodyPart bodyPart = config.getResponseBodyPartFactory().newResponseBodyPart(buf, last); abort = handler.onBodyPartReceived(bodyPart) == State.ABORT; } if (abort || last) { boolean close = abort || !future.isKeepAlive(); finishUpdate(future, channel, close); } }
if (obj instanceof FullHttpMessage) { FullHttpMessage full = (FullHttpMessage) obj; noMoreFrames = !full.content().isReadable() && full.trailingHeaders().isEmpty();
static void handle(ChannelHandlerContext ctx, Http2Connection connection, Http2FrameListener listener, FullHttpMessage message) throws Http2Exception { try { int streamId = getStreamId(connection, message.headers()); Http2Stream stream = connection.stream(streamId); if (stream == null) { stream = connection.remote().createStream(streamId, false); } message.headers().set(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), HttpScheme.HTTP.name()); Http2Headers messageHeaders = HttpConversionUtil.toHttp2Headers(message, true); boolean hasContent = message.content().isReadable(); boolean hasTrailers = !message.trailingHeaders().isEmpty(); listener.onHeadersRead( ctx, streamId, messageHeaders, 0, !(hasContent || hasTrailers)); if (hasContent) { listener.onDataRead(ctx, streamId, message.content(), 0, !hasTrailers); } if (hasTrailers) { Http2Headers headers = HttpConversionUtil.toHttp2Headers(message.trailingHeaders(), true); listener.onHeadersRead(ctx, streamId, headers, 0, true); } stream.closeRemoteSide(); } finally { message.release(); } } }