}
private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
// Handle a bad request.
if (!req.getDecoderResult().isSuccess()) {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST));
return;
}
// Allow only GET methods.
if (req.getMethod() != GET) {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN));
return;
}
if ("/".equals(req.getUri()) || "/favicon.ico".equals(req.getUri())) {
FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND);
sendHttpResponse(ctx, req, res);
return;
}
if (req.getUri().startsWith("/livereload.js")) {
InputStream is = getClass().getResourceAsStream("/livereload.js");
byte[] data = IOUtils.toByteArray(is);
ByteBuf content = Unpooled.wrappedBuffer(data);
FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, OK, content);
res.headers().set(CONTENT_TYPE, "application/javascript");
setContentLength(res, content.readableBytes());
sendHttpResponse(ctx, req, res);
return;
}