Search in sources :

Example 56 with DefaultHttpResponse

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);
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) ChannelFuture(io.netty.channel.ChannelFuture) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) Channel(io.netty.channel.Channel) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse)

Example 57 with DefaultHttpResponse

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;
}
Also used : RandomAccessFile(java.io.RandomAccessFile) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) FileNotFoundException(java.io.FileNotFoundException) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) IOException(java.io.IOException) DefaultFileRegion(io.netty.channel.DefaultFileRegion) URL(java.net.URL)

Example 58 with DefaultHttpResponse

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;
}
Also used : HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse)

Example 59 with DefaultHttpResponse

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();
}
Also used : DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Executable(org.junit.jupiter.api.function.Executable) Test(org.junit.jupiter.api.Test)

Example 60 with DefaultHttpResponse

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;
}
Also used : DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse)

Aggregations

DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)64 HttpResponse (io.netty.handler.codec.http.HttpResponse)34 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)23 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)18 HttpContent (io.netty.handler.codec.http.HttpContent)14 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)11 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)11 ChannelFuture (io.netty.channel.ChannelFuture)10 HttpRequest (io.netty.handler.codec.http.HttpRequest)10 ByteBuf (io.netty.buffer.ByteBuf)9 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)9 HttpObject (io.netty.handler.codec.http.HttpObject)9 RandomAccessFile (java.io.RandomAccessFile)9 Test (org.junit.Test)9 FileNotFoundException (java.io.FileNotFoundException)7 IOException (java.io.IOException)7 Channel (io.netty.channel.Channel)6 File (java.io.File)6 HttpChunkedInput (io.netty.handler.codec.http.HttpChunkedInput)5 URL (java.net.URL)5