use of io.netty.handler.codec.http.DefaultHttpResponse in project netty-socketio by mrniko.
the class WrongUrlHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof FullHttpRequest) {
FullHttpRequest req = (FullHttpRequest) msg;
Channel channel = ctx.channel();
QueryStringDecoder queryDecoder = new QueryStringDecoder(req.uri());
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.BAD_REQUEST);
ChannelFuture f = channel.writeAndFlush(res);
f.addListener(ChannelFutureListener.CLOSE);
req.release();
log.warn("Blocked wrong socket.io-context request! url: {}, params: {}, ip: {}", queryDecoder.path(), queryDecoder.parameters(), channel.remoteAddress());
return;
}
super.channelRead(ctx, msg);
}
use of io.netty.handler.codec.http.DefaultHttpResponse in project netty by netty.
the class StompWebSocketClientPageHandler method sendResource.
private static boolean sendResource(FullHttpRequest request, ChannelHandlerContext ctx) {
if (request.uri().isEmpty() || !request.uri().startsWith("/")) {
return false;
}
String requestResource = request.uri().substring(1);
if (requestResource.isEmpty()) {
requestResource = "index.html";
}
URL resourceUrl = INSTANCE.getClass().getResource(requestResource);
if (resourceUrl == null) {
return false;
}
RandomAccessFile raf = null;
long fileLength = -1L;
try {
raf = new RandomAccessFile(resourceUrl.getFile(), "r");
fileLength = raf.length();
} catch (FileNotFoundException fne) {
System.out.println("File not found " + fne.getMessage());
return false;
} catch (IOException io) {
System.out.println("Cannot read file length " + io.getMessage());
return false;
} finally {
if (fileLength < 0 && raf != null) {
try {
raf.close();
} catch (IOException io) {
// Nothing to do
}
}
}
HttpResponse response = new DefaultHttpResponse(request.protocolVersion(), OK);
HttpUtil.setContentLength(response, fileLength);
String contentType = "application/octet-stream";
if (requestResource.endsWith("html")) {
contentType = "text/html; charset=UTF-8";
} else if (requestResource.endsWith("css")) {
contentType = "text/css; charset=UTF-8";
} else if (requestResource.endsWith("js")) {
contentType = "application/javascript";
}
response.headers().set(CONTENT_TYPE, contentType);
sendResponse(response, ctx, false);
ctx.write(new DefaultFileRegion(raf.getChannel(), 0, fileLength));
ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
return true;
}
use of io.netty.handler.codec.http.DefaultHttpResponse in project netty by netty.
the class HttpConversionUtil method toHttpResponse.
/**
* Create a new object to contain the response data.
*
* @param streamId The stream associated with the response
* @param http2Headers The initial set of HTTP/2 headers to create the response with
* @param validateHttpHeaders <ul>
* <li>{@code true} to validate HTTP headers in the http-codec</li>
* <li>{@code false} not to validate HTTP headers in the http-codec</li>
* </ul>
* @return A new response object which represents headers for a chunked response
* @throws Http2Exception see {@link #addHttp2ToHttpHeaders(int, Http2Headers,
* HttpHeaders, HttpVersion, boolean, boolean)}
*/
public static HttpResponse toHttpResponse(final int streamId, final Http2Headers http2Headers, final boolean validateHttpHeaders) throws Http2Exception {
final HttpResponseStatus status = parseStatus(http2Headers.status());
// HTTP/2 does not define a way to carry the version or reason phrase that is included in an
// HTTP/1.1 status line.
final HttpResponse msg = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status, validateHttpHeaders);
try {
addHttp2ToHttpHeaders(streamId, http2Headers, msg.headers(), msg.protocolVersion(), false, false);
} catch (final Http2Exception e) {
throw e;
} catch (final Throwable t) {
throw streamError(streamId, PROTOCOL_ERROR, t, "HTTP/2 to HTTP/1.x headers conversion error");
}
return msg;
}
use of io.netty.handler.codec.http.DefaultHttpResponse in project netty by netty.
the class Http2StreamFrameToHttpObjectCodecTest method encodeNonFullHttpResponse100ContinueIsRejected.
@Test
public void encodeNonFullHttpResponse100ContinueIsRejected() throws Exception {
final EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(true));
assertThrows(EncoderException.class, new Executable() {
@Override
public void execute() {
ch.writeOutbound(new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE));
}
});
ch.finishAndReleaseAll();
}
use of io.netty.handler.codec.http.DefaultHttpResponse in project netty by netty.
the class WebSocketExtensionTestUtil method newUpgradeResponse.
public static HttpResponse newUpgradeResponse(String ext) {
HttpResponse res = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.SWITCHING_PROTOCOLS);
res.headers().set(HttpHeaderNames.HOST, "server.example.com");
res.headers().set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET.toString().toLowerCase());
res.headers().set(HttpHeaderNames.CONNECTION, "Upgrade");
res.headers().set(HttpHeaderNames.ORIGIN, "http://example.com");
if (ext != null) {
res.headers().set(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS, ext);
}
return res;
}
Aggregations