Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2012 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty5.handler.codec.http;
import io.netty5.buffer.Buffer;
import io.netty5.buffer.BufferAllocator;
import io.netty5.channel.ChannelHandlerContext;
import io.netty5.channel.embedded.EmbeddedChannel;
import io.netty5.handler.codec.MessageToMessageCodec;
import io.netty5.handler.codec.compression.Compressor;
import io.netty5.handler.codec.http.headers.HttpHeaders;
import io.netty5.util.Resource;
import java.util.ArrayDeque;
import java.util.Iterator;
import java.util.List;
import java.util.Queue;
import java.util.StringJoiner;
import static io.netty5.handler.codec.http.HttpHeaderNames.ACCEPT_ENCODING;
import static java.util.Objects.requireNonNull;
/**
* Encodes the content of the outbound {@link HttpResponse} and {@link HttpContent}.
* The original content is replaced with the new content encoded by the
* {@link EmbeddedChannel}, which is created by {@link #beginEncode(HttpResponse, String)}.
* Once encoding is finished, the value of the 'Content-Encoding' header
* is set to the target content encoding, as returned by
* {@link #beginEncode(HttpResponse, String)}.
* Also, the 'Content-Length' header is updated to the length of the
* encoded content. If there is no supported or allowed encoding in the
* corresponding {@link HttpRequest}'s {@code "Accept-Encoding"} header,
* {@link #beginEncode(HttpResponse, String)} should return {@code null} so that
* no encoding occurs (i.e. pass-through).
*
* Please note that this is an abstract class. You have to extend this class
* and implement {@link #beginEncode(HttpResponse, String)} properly to make
* this class functional. For example, refer to the source code of
* {@link HttpContentCompressor}.
*
* This handler must be placed after {@link HttpObjectEncoder} in the pipeline
* so that this handler can intercept HTTP responses before {@link HttpObjectEncoder}
* converts them into {@link Buffer}s.
*/
public abstract class HttpContentEncoder extends MessageToMessageCodec {
private enum State {
PASS_THROUGH,
AWAIT_HEADERS,
AWAIT_CONTENT
}
private static final CharSequence ZERO_LENGTH_HEAD = "HEAD";
private static final CharSequence ZERO_LENGTH_CONNECT = "CONNECT";
private static final int CONTINUE_CODE = HttpResponseStatus.CONTINUE.code();
private final Queue acceptEncodingQueue = new ArrayDeque<>();
private Compressor compressor;
private State state = State.AWAIT_HEADERS;
@Override
public boolean acceptOutboundMessage(Object msg) throws Exception {
return msg instanceof HttpContent || msg instanceof HttpResponse;
}
@Override
protected void decode(ChannelHandlerContext ctx, HttpRequest msg) throws Exception {
throw new UnsupportedOperationException("HttpContentEncoder use decodeAndClose().");
}
@Override
protected void decodeAndClose(ChannelHandlerContext ctx, HttpRequest msg) throws Exception {
CharSequence acceptEncoding;
Iterator acceptEncodingHeaders = msg.headers().valuesIterator(ACCEPT_ENCODING);
if (!acceptEncodingHeaders.hasNext()) {
acceptEncoding = HttpContentDecoder.IDENTITY;
} else {
acceptEncoding = acceptEncodingHeaders.next();
if (acceptEncodingHeaders.hasNext()) {
// Multiple message-header fields https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
StringJoiner joiner = new StringJoiner(",");
joiner.add(acceptEncoding);
do {
joiner.add(acceptEncodingHeaders.next());
} while (acceptEncodingHeaders.hasNext());
acceptEncoding = joiner.toString();
}
}
HttpMethod method = msg.method();
if (HttpMethod.HEAD.equals(method)) {
acceptEncoding = ZERO_LENGTH_HEAD;
} else if (HttpMethod.CONNECT.equals(method)) {
acceptEncoding = ZERO_LENGTH_CONNECT;
}
acceptEncodingQueue.add(acceptEncoding);
ctx.fireChannelRead(msg);
}
@Override
protected void encodeAndClose(ChannelHandlerContext ctx, HttpObject msg, List