List of usage examples for io.netty.handler.codec.http HttpUtil getCharset
public static Charset getCharset(CharSequence contentTypeValue)
From source file:com.google.devtools.build.lib.remote.blobstore.http.HttpDownloadHandler.java
License:Open Source License
@Override protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception { if (!msg.decoderResult().isSuccess()) { failAndClose(new IOException("Failed to parse the HTTP response."), ctx); return;/*from www .j a va 2s .c o m*/ } if (!(msg instanceof HttpResponse) && !(msg instanceof HttpContent)) { failAndClose( new IllegalArgumentException("Unsupported message type: " + StringUtil.simpleClassName(msg)), ctx); return; } checkState(userPromise != null, "response before request"); if (msg instanceof HttpResponse) { response = (HttpResponse) msg; if (!response.protocolVersion().equals(HttpVersion.HTTP_1_1)) { HttpException error = new HttpException(response, "HTTP version 1.1 is required, was: " + response.protocolVersion(), null); failAndClose(error, ctx); return; } if (!HttpUtil.isContentLengthSet(response) && !HttpUtil.isTransferEncodingChunked(response)) { HttpException error = new HttpException(response, "Missing 'Content-Length' or 'Transfer-Encoding: chunked' header", null); failAndClose(error, ctx); return; } downloadSucceeded = response.status().equals(HttpResponseStatus.OK); if (!downloadSucceeded) { out = new ByteArrayOutputStream(); } keepAlive = HttpUtil.isKeepAlive((HttpResponse) msg); } if (msg instanceof HttpContent) { checkState(response != null, "content before headers"); ByteBuf content = ((HttpContent) msg).content(); content.readBytes(out, content.readableBytes()); if (msg instanceof LastHttpContent) { if (downloadSucceeded) { succeedAndReset(ctx); } else { String errorMsg = response.status() + "\n"; errorMsg += new String(((ByteArrayOutputStream) out).toByteArray(), HttpUtil.getCharset(response)); out.close(); HttpException error = new HttpException(response, errorMsg, null); failAndReset(error, ctx); } } } }
From source file:com.google.devtools.build.lib.remote.blobstore.http.HttpUploadHandler.java
License:Open Source License
@SuppressWarnings("FutureReturnValueIgnored") @Override//from ww w. j ava 2 s. c om protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse response) { if (!response.decoderResult().isSuccess()) { failAndClose(new IOException("Failed to parse the HTTP response."), ctx); return; } try { checkState(userPromise != null, "response before request"); if (!response.status().equals(HttpResponseStatus.OK) && !response.status().equals(HttpResponseStatus.ACCEPTED) && !response.status().equals(HttpResponseStatus.CREATED) && !response.status().equals(HttpResponseStatus.NO_CONTENT)) { // Supporting more than OK status to be compatible with nginx webdav. String errorMsg = response.status().toString(); if (response.content().readableBytes() > 0) { byte[] data = new byte[response.content().readableBytes()]; response.content().readBytes(data); errorMsg += "\n" + new String(data, HttpUtil.getCharset(response)); } failAndResetUserPromise(new HttpException(response, errorMsg, null)); } else { succeedAndResetUserPromise(); } } finally { if (!HttpUtil.isKeepAlive(response)) { ctx.close(); } } }