use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project protools by SeanDragon.
the class HttpClientHandler method channelRead0.
@Override
public void channelRead0(final ChannelHandlerContext ctx, final HttpObject msg) {
if (msg instanceof FullHttpResponse) {
final FullHttpResponse response = (FullHttpResponse) msg;
httpReceive.setStatusCode(response.status().code()).setStatusText(response.status().codeAsText().toString());
HttpHeaders headers = response.headers();
if (httpSend.getNeedReceiveHeaders() && !headers.isEmpty()) {
final Map<String, String> responseHeaderMap = Maps.newHashMapWithExpectedSize(headers.size());
headers.forEach(one -> {
responseHeaderMap.put(one.getKey(), one.getValue());
});
httpReceive.setResponseHeader(responseHeaderMap);
}
if (HttpUtil.isTransferEncodingChunked(response)) {
if (log.isDebugEnabled()) {
log.debug("#HTTP 内容开始{");
}
} else {
if (log.isDebugEnabled()) {
log.debug("#HTTP 内容开始{");
}
}
final String responseBody = response.content().toString(httpSend.getCharset());
httpReceive.setResponseBody(responseBody);
if (log.isDebugEnabled()) {
log.debug(responseBody);
}
if (log.isDebugEnabled()) {
log.debug("}EOF#");
}
final DecoderResult decoderResult = response.decoderResult();
if (decoderResult.isFailure()) {
Throwable cause = decoderResult.cause();
if (log.isErrorEnabled()) {
log.error(ToolFormat.toException(cause), cause);
}
httpReceive.setHaveError(true).setErrMsg(cause.getMessage()).setThrowable(cause);
} else if (response.status().code() != 200) {
httpReceive.setHaveError(true).setErrMsg("本次请求响应码不是200,是" + response.status().code());
}
httpReceive.setIsDone(true);
ctx.close();
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project xipki by xipki.
the class AbstractHttpServlet method createResponse.
protected static FullHttpResponse createResponse(HttpVersion version, HttpResponseStatus status, String contentType, byte[] content) {
FullHttpResponse resp;
ByteBuf buf = null;
int contentLen = (content == null) ? 0 : content.length;
if (contentLen != 0) {
buf = Unpooled.wrappedBuffer(content);
resp = new DefaultFullHttpResponse(version, status, buf);
} else {
resp = new DefaultFullHttpResponse(version, status);
}
resp.headers().addInt("Content-Length", contentLen);
if (contentType != null && !contentType.isEmpty()) {
resp.headers().add("Content-Type", contentType);
}
return resp;
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project xipki by xipki.
the class AbstractHttpServlet method createErrorResponse.
protected static FullHttpResponse createErrorResponse(HttpVersion version, HttpResponseStatus status) {
FullHttpResponse resp = new DefaultFullHttpResponse(version, status);
resp.headers().addInt("Content-Length", 0);
return resp;
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project xipki by xipki.
the class HttpOcspServlet method serviceGet.
// method servicePost
private FullHttpResponse serviceGet(FullHttpRequest request, ServletURI servletUri, SSLSession sslSession, SslReverseProxyMode sslReverseProxyMode) throws Exception {
HttpVersion version = request.protocolVersion();
ResponderAndPath responderAndPath = server.getResponderForPath(servletUri.getPath());
if (responderAndPath == null) {
return createErrorResponse(version, HttpResponseStatus.NOT_FOUND);
}
String path = servletUri.getPath();
String servletPath = responderAndPath.getServletPath();
Responder responder = responderAndPath.getResponder();
if (!responder.supportsHttpGet()) {
return createErrorResponse(version, HttpResponseStatus.METHOD_NOT_ALLOWED);
}
String b64OcspReq;
int offset = servletPath.length();
// GET URI contains the request and must be much longer than 10.
if (path.length() - offset > 10) {
if (path.charAt(offset) == '/') {
offset++;
}
b64OcspReq = servletUri.getPath().substring(offset);
} else {
return createErrorResponse(version, HttpResponseStatus.BAD_REQUEST);
}
try {
// POST, we support GET for longer requests anyway.
if (b64OcspReq.length() > responder.getMaxRequestSize()) {
return createErrorResponse(version, HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE);
}
OcspRespWithCacheInfo ocspRespWithCacheInfo = server.answer(responder, Base64.decode(b64OcspReq), true);
if (ocspRespWithCacheInfo == null || ocspRespWithCacheInfo.getResponse() == null) {
return createErrorResponse(version, HttpResponseStatus.INTERNAL_SERVER_ERROR);
}
byte[] encodedOcspResp = ocspRespWithCacheInfo.getResponse();
FullHttpResponse response = createOKResponse(version, CT_RESPONSE, encodedOcspResp);
OcspRespWithCacheInfo.ResponseCacheInfo cacheInfo = ocspRespWithCacheInfo.getCacheInfo();
if (cacheInfo != null) {
encodedOcspResp = ocspRespWithCacheInfo.getResponse();
HttpHeaders headers = response.headers();
// RFC 5019 6.2: Date: The date and time at which the OCSP server generated
// the HTTP response.
headers.add("Date", new Date());
// RFC 5019 6.2: Last-Modified: date and time at which the OCSP responder
// last modified the response.
headers.add("Last-Modified", new Date(cacheInfo.getThisUpdate()));
// This is overridden by max-age on HTTP/1.1 compatible components
if (cacheInfo.getNextUpdate() != null) {
headers.add("Expires", new Date(cacheInfo.getNextUpdate()));
}
// RFC 5019 6.2: This profile RECOMMENDS that the ETag value be the ASCII
// HEX representation of the SHA1 hash of the OCSPResponse structure.
headers.add("ETag", StringUtil.concat("\\", HashAlgo.SHA1.hexHash(encodedOcspResp), "\\"));
// Max age must be in seconds in the cache-control header
long maxAge;
if (responder.getCacheMaxAge() != null) {
maxAge = responder.getCacheMaxAge().longValue();
} else {
maxAge = DFLT_CACHE_MAX_AGE;
}
if (cacheInfo.getNextUpdate() != null) {
maxAge = Math.min(maxAge, (cacheInfo.getNextUpdate() - cacheInfo.getThisUpdate()) / 1000);
}
headers.add("Cache-Control", StringUtil.concat("max-age=", Long.toString(maxAge), ",public,no-transform,must-revalidate"));
}
return response;
} catch (Throwable th) {
if (th instanceof EOFException) {
LogUtil.warn(LOG, th, "Connection reset by peer");
} else {
LOG.error("Throwable thrown, this should not happen!", th);
}
return createErrorResponse(version, HttpResponseStatus.INTERNAL_SERVER_ERROR);
}
// end external try
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project xipki by xipki.
the class HealthCheckServlet method service.
@Override
public FullHttpResponse service(FullHttpRequest request, ServletURI servletUri, SSLSession sslSession, SslReverseProxyMode sslReverseProxyMode) throws Exception {
FullHttpResponse resp = service0(request, servletUri, sslSession);
resp.headers().add("Access-Control-Allow-Origin", "*");
return resp;
}
Aggregations