Example usage for io.netty.handler.codec.http HttpHeaders getAsString

List of usage examples for io.netty.handler.codec.http HttpHeaders getAsString

Introduction

In this page you can find the example usage for io.netty.handler.codec.http HttpHeaders getAsString.

Prototype

public final String getAsString(CharSequence name) 

Source Link

Document

Headers#get(Object) and convert the result to a String .

Usage

From source file:gribbit.http.request.Request.java

License:Open Source License

public Request(ChannelHandlerContext ctx, HttpRequest httpReq) throws ResponseException {
    this.reqReceivedTimeEpochMillis = System.currentTimeMillis();

    this.httpRequest = httpReq;
    HttpHeaders headers = httpReq.headers();

    // Netty changes the URI of the request to "/bad-request" if the HTTP request was malformed
    this.rawURL = httpReq.uri();
    if (rawURL.equals("/bad-request")) {
        throw new BadRequestException();
    } else if (rawURL.isEmpty()) {
        rawURL = "/";
    }/*  ww w.ja v a 2s.  co  m*/

    // Decode the URL
    RequestURL requestURL = new RequestURL(rawURL);
    this.normalizedURL = requestURL.getNormalizedPath();
    this.queryParamToVals = requestURL.getQueryParams();

    // TODO: figure out how to detect HTTP/2 connections
    this.httpVersion = httpReq.protocolVersion().toString();

    // Get HTTP2 stream ID
    this.streamId = headers.getAsString(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text());

    this.isSecure = ctx.pipeline().get(SslHandler.class) != null; // TODO: is this correct for HTTP2?

    // Decode cookies
    try {
        for (CharSequence cookieHeader : headers.getAll(COOKIE)) {
            for (Cookie cookie : ServerCookieDecoder.STRICT.decode(cookieHeader.toString())) {
                // Log.fine("Cookie in request: " + nettyCookie);
                if (cookieNameToCookies == null) {
                    cookieNameToCookies = new HashMap<>();
                }
                String cookieName = cookie.name();

                // Multiple cookies may be present in the request with the same name but with different paths
                ArrayList<Cookie> cookiesWithThisName = cookieNameToCookies.get(cookieName);
                if (cookiesWithThisName == null) {
                    cookieNameToCookies.put(cookieName, cookiesWithThisName = new ArrayList<>());
                }
                cookiesWithThisName.add(cookie);
            }
        }
    } catch (IllegalArgumentException e) {
        // Malformed cookies cause ServerCookieDecoder to throw IllegalArgumentException
        // Log.info("Malformed cookie in request");
        throw new BadRequestException();
    }
    // Sort cookies into decreasing order of path length, in case client doesn't conform to RFC6295,
    // delivering the cookies in this order itself. This allows us to get the most likely single
    // cookie for a given cookie name by reading the first cookie in a list for a given name.
    if (cookieNameToCookies != null) {
        for (Entry<String, ArrayList<Cookie>> ent : cookieNameToCookies.entrySet()) {
            Collections.sort(ent.getValue(), COOKIE_COMPARATOR);
        }
    }

    this.method = httpReq.method();

    // Force the GET method if HEAD is requested
    this.isHEADRequest = this.method == HttpMethod.HEAD;
    if (this.isHEADRequest) {
        this.method = HttpMethod.GET;
    }

    this.isKeepAlive = HttpUtil.isKeepAlive(httpReq) && httpReq.protocolVersion().equals(HttpVersion.HTTP_1_0);

    CharSequence host = headers.get(HOST);
    this.host = host == null ? null : host.toString();

    this.xRequestedWith = headers.get("X-Requested-With");
    this.accept = headers.get(ACCEPT);
    this.acceptCharset = headers.get(ACCEPT_CHARSET);
    this.acceptLanguage = headers.get(ACCEPT_LANGUAGE);
    this.origin = headers.get(ORIGIN);
    this.referer = headers.get(REFERER);
    this.userAgent = headers.get(USER_AGENT);

    InetSocketAddress requestorSocketAddr = (InetSocketAddress) ctx.channel().remoteAddress();
    if (requestorSocketAddr != null) {
        InetAddress address = requestorSocketAddr.getAddress();
        if (address != null) {
            this.requestor = address.getHostAddress();
        }
    }

    CharSequence acceptEncoding = headers.get(ACCEPT_ENCODING);
    this.acceptEncodingGzip = acceptEncoding != null
            && acceptEncoding.toString().toLowerCase().contains("gzip");

    this.ifModifiedSince = headers.get(IF_MODIFIED_SINCE);
    if (this.ifModifiedSince != null && this.ifModifiedSince.length() > 0) {
        this.ifModifiedSinceEpochSecond = ZonedDateTime
                .parse(this.ifModifiedSince, DateTimeFormatter.RFC_1123_DATE_TIME).toEpochSecond();
    }

    //        // If this is a hash URL, look up original URL whose served resource was hashed to give this hash URL.
    //        // We only need to serve the resource at a hash URL once per resource per client, since resources served
    //        // from hash URLs are indefinitely cached in the browser.
    //        // TODO: Move cache-busting out of http package
    //        this.urlHashKey = CacheExtension.getHashKey(this.urlPath);
    //        this.urlPathUnhashed = this.urlHashKey != null ? CacheExtension.getOrigURL(this.urlPath) : this.urlPath;

    //        // Get flash messages from cookie, if any
    //        this.flashMessages = FlashMessage.fromCookieString(getCookieValue(Cookie.FLASH_COOKIE_NAME));
}