Home / Function/ writeResponse() — netty Function Reference

writeResponse() — netty Function Reference

Architecture documentation for the writeResponse() function in HttpSnoopServerHandler.java from the netty codebase.

Function java Buffer Search called by 1

Entity Profile

Dependency Diagram

graph TD
  47526183_e8b0_7ed5_2265_4a4123e1c6fb["writeResponse()"]
  0dc0edc6_a74a_0a99_9be5_a85731fba901["HttpSnoopServerHandler"]
  47526183_e8b0_7ed5_2265_4a4123e1c6fb -->|defined in| 0dc0edc6_a74a_0a99_9be5_a85731fba901
  38a6ac21_703f_7d5d_2772_e7831e8a40e0["channelRead0()"]
  38a6ac21_703f_7d5d_2772_e7831e8a40e0 -->|calls| 47526183_e8b0_7ed5_2265_4a4123e1c6fb
  style 47526183_e8b0_7ed5_2265_4a4123e1c6fb fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

example/src/main/java/io/netty/example/http/snoop/HttpSnoopServerHandler.java lines 147–185

    private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) {
        // Decide whether to close the connection or not.
        boolean keepAlive = HttpUtil.isKeepAlive(request);
        // Build the response object.
        FullHttpResponse response = new DefaultFullHttpResponse(
                HTTP_1_1, currentObj.decoderResult().isSuccess()? OK : BAD_REQUEST,
                Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8));

        response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");

        if (keepAlive) {
            // Add 'Content-Length' header only for a keep-alive connection.
            response.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
            // Add keep alive header as per:
            // - https://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection
            response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
        }

        // Encode the cookie.
        String cookieString = request.headers().get(HttpHeaderNames.COOKIE);
        if (cookieString != null) {
            Set<Cookie> cookies = ServerCookieDecoder.STRICT.decode(cookieString);
            if (!cookies.isEmpty()) {
                // Reset the cookies if necessary.
                for (Cookie cookie: cookies) {
                    response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.STRICT.encode(cookie));
                }
            }
        } else {
            // Browser sent no cookie.  Add some.
            response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.STRICT.encode("key1", "value1"));
            response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.STRICT.encode("key2", "value2"));
        }

        // Write the response.
        ctx.write(response);

        return keepAlive;
    }

Domain

Subdomains

Called By

Frequently Asked Questions

What does writeResponse() do?
writeResponse() is a function in the netty codebase, defined in example/src/main/java/io/netty/example/http/snoop/HttpSnoopServerHandler.java.
Where is writeResponse() defined?
writeResponse() is defined in example/src/main/java/io/netty/example/http/snoop/HttpSnoopServerHandler.java at line 147.
What calls writeResponse()?
writeResponse() is called by 1 function(s): channelRead0.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free