Example usage for io.netty.handler.codec.http HttpMethod HEAD

List of usage examples for io.netty.handler.codec.http HttpMethod HEAD

Introduction

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

Prototype

HttpMethod HEAD

To view the source code for io.netty.handler.codec.http HttpMethod HEAD.

Click Source Link

Document

The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response.

Usage

From source file:com.strategicgains.restexpress.plugin.cache.DateHeaderPostprocessorTest.java

License:Apache License

@Test
public void shouldAddDateHeaderOnHead() throws ParseException {
    FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.HEAD,
            "/foo?param1=bar&param2=blah&yada");
    httpRequest.headers().add("Host", "testing-host");
    Response response = new Response();
    processor.process(new Request(httpRequest, null), response);
    assertTrue(response.hasHeaders());//from   w  w w  .  j  a va  2s.  c  o  m
    String dateHeader = response.getHeader(HttpHeaders.Names.DATE);
    assertNotNull(dateHeader);
    Date dateValue = new HttpHeaderTimestampAdapter().parse(dateHeader);
    assertNotNull(dateValue);
}

From source file:com.strategicgains.restexpress.plugin.cache.EtagHeaderPostprocessor.java

License:Apache License

@Override
public void process(Request request, Response response) {
    if (!request.isMethodGet() && !HttpMethod.HEAD.equals(request.getHttpMethod()))
        return;//from ww  w .  j ava2 s.c o m
    if (!response.hasBody())
        return;

    Object body = response.getBody();

    if (!response.hasHeader(ETAG)) {
        String format = request.getFormat() == null ? request.getResolvedRoute().getDefaultFormat()
                : request.getFormat();
        response.addHeader(ETAG, String.format("\"%d%d\"", body.hashCode(), format.hashCode()));
    }
}

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 = "/";
    }/*from   w w  w . j a v  a2s .  com*/

    // 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));
}

From source file:io.advantageous.conekt.http.impl.HttpClientImpl.java

License:Open Source License

@Override
public HttpClientRequest head(int port, String host, String requestURI) {
    return request(io.advantageous.conekt.http.HttpMethod.HEAD, port, host, requestURI);
}

From source file:io.advantageous.conekt.http.impl.HttpClientImpl.java

License:Open Source License

@Override
public HttpClientRequest head(int port, String host, String requestURI,
        Handler<HttpClientResponse> responseHandler) {
    return request(io.advantageous.conekt.http.HttpMethod.HEAD, port, host, requestURI, responseHandler);
}

From source file:io.advantageous.conekt.http.impl.HttpClientImpl.java

License:Open Source License

@Override
public HttpClientRequest head(String requestURI) {
    return request(io.advantageous.conekt.http.HttpMethod.HEAD, requestURI);
}

From source file:io.advantageous.conekt.http.impl.HttpClientImpl.java

License:Open Source License

@Override
public HttpClientRequest head(String requestURI, Handler<HttpClientResponse> responseHandler) {
    return request(io.advantageous.conekt.http.HttpMethod.HEAD, requestURI, responseHandler);
}

From source file:io.advantageous.conekt.http.impl.HttpClientImpl.java

License:Open Source License

@Override
public HttpClientRequest headAbs(String absoluteURI) {
    return requestAbs(io.advantageous.conekt.http.HttpMethod.HEAD, absoluteURI);
}

From source file:io.advantageous.conekt.http.impl.HttpClientImpl.java

License:Open Source License

@Override
public HttpClientRequest headAbs(String absoluteURI, Handler<HttpClientResponse> responseHandler) {
    return requestAbs(io.advantageous.conekt.http.HttpMethod.HEAD, absoluteURI, responseHandler);
}

From source file:io.advantageous.conekt.http.impl.HttpClientRequestImpl.java

License:Open Source License

private HttpMethod toNettyHttpMethod(io.advantageous.conekt.http.HttpMethod method) {
    switch (method) {
    case CONNECT: {
        return HttpMethod.CONNECT;
    }//  w  w  w . j av  a  2  s .co m
    case GET: {
        return HttpMethod.GET;
    }
    case PUT: {
        return HttpMethod.PUT;
    }
    case POST: {
        return HttpMethod.POST;
    }
    case DELETE: {
        return HttpMethod.DELETE;
    }
    case HEAD: {
        return HttpMethod.HEAD;
    }
    case OPTIONS: {
        return HttpMethod.OPTIONS;
    }
    case TRACE: {
        return HttpMethod.TRACE;
    }
    case PATCH: {
        return HttpMethod.PATCH;
    }
    default:
        throw new IllegalArgumentException();
    }
}