use of io.netty.handler.codec.http.DefaultFullHttpResponse in project flink by apache.
the class StaticFileServerHandler method sendError.
// ------------------------------------------------------------------------
// Utilities to encode headers and responses
// ------------------------------------------------------------------------
/**
* Writes a simple error response message.
*
* @param ctx The channel context to write the response to.
* @param status The response status.
*/
private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8));
response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
// close the connection as soon as the error message is sent.
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
use of io.netty.handler.codec.http.DefaultFullHttpResponse in project flink by apache.
the class AbstractJsonRequestHandler method handleRequest.
@Override
public FullHttpResponse handleRequest(Map<String, String> pathParams, Map<String, String> queryParams, ActorGateway jobManager) throws Exception {
String result = handleJsonRequest(pathParams, queryParams, jobManager);
byte[] bytes = result.getBytes(ENCODING);
DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(bytes));
response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json");
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
return response;
}
use of io.netty.handler.codec.http.DefaultFullHttpResponse in project flink by apache.
the class ConstantTextHandler method channelRead0.
@Override
protected void channelRead0(ChannelHandlerContext ctx, Routed routed) throws Exception {
HttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(encodedText));
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, encodedText.length);
response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
KeepAliveWrite.flush(ctx, routed.request(), response);
}
use of io.netty.handler.codec.http.DefaultFullHttpResponse in project moco by dreamhead.
the class AbstractProxyResponseHandler method setupNormalResponse.
private HttpResponse setupNormalResponse(final org.apache.http.HttpResponse remoteResponse) throws IOException {
HttpVersion httpVersion = HttpVersion.valueOf(remoteResponse.getProtocolVersion().toString());
HttpResponseStatus status = HttpResponseStatus.valueOf(remoteResponse.getStatusLine().getStatusCode());
FullHttpResponse response = new DefaultFullHttpResponse(httpVersion, status);
response.setStatus(status);
Header[] allHeaders = remoteResponse.getAllHeaders();
for (Header header : allHeaders) {
if (isResponseHeader(header)) {
response.headers().set(header.getName(), header.getValue());
}
}
HttpEntity entity = remoteResponse.getEntity();
if (entity != null) {
byte[] content = toByteArray(entity);
if (content.length > 0) {
ByteBuf buffer = Unpooled.copiedBuffer(content);
response.content().writeBytes(buffer);
}
}
return newResponse(response);
}
use of io.netty.handler.codec.http.DefaultFullHttpResponse in project netty by netty.
the class WebSocketServerHandshaker07 method newHandshakeResponse.
/**
* <p>
* Handle the web socket handshake for the web socket specification <a href=
* "http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-07">HyBi version 7</a>.
* </p>
*
* <p>
* Browser request to the server:
* </p>
*
* <pre>
* GET /chat HTTP/1.1
* Host: server.example.com
* Upgrade: websocket
* Connection: Upgrade
* Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
* Sec-WebSocket-Origin: http://example.com
* Sec-WebSocket-Protocol: chat, superchat
* Sec-WebSocket-Version: 7
* </pre>
*
* <p>
* Server response:
* </p>
*
* <pre>
* HTTP/1.1 101 Switching Protocols
* Upgrade: websocket
* Connection: Upgrade
* Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
* Sec-WebSocket-Protocol: chat
* </pre>
*/
@Override
protected FullHttpResponse newHandshakeResponse(FullHttpRequest req, HttpHeaders headers) {
FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.SWITCHING_PROTOCOLS);
if (headers != null) {
res.headers().add(headers);
}
CharSequence key = req.headers().get(HttpHeaderNames.SEC_WEBSOCKET_KEY);
if (key == null) {
throw new WebSocketHandshakeException("not a WebSocket request: missing key");
}
String acceptSeed = key + WEBSOCKET_07_ACCEPT_GUID;
byte[] sha1 = WebSocketUtil.sha1(acceptSeed.getBytes(CharsetUtil.US_ASCII));
String accept = WebSocketUtil.base64(sha1);
if (logger.isDebugEnabled()) {
logger.debug("WebSocket version 07 server handshake key: {}, response: {}.", key, accept);
}
res.headers().add(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET);
res.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE);
res.headers().add(HttpHeaderNames.SEC_WEBSOCKET_ACCEPT, accept);
String subprotocols = req.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL);
if (subprotocols != null) {
String selectedSubprotocol = selectSubprotocol(subprotocols);
if (selectedSubprotocol == null) {
if (logger.isDebugEnabled()) {
logger.debug("Requested subprotocol(s) not supported: {}", subprotocols);
}
} else {
res.headers().add(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol);
}
}
return res;
}
Aggregations