Java Code Examples for com.linecorp.armeria.common.HttpMethod#HEAD

The following examples show how to use com.linecorp.armeria.common.HttpMethod#HEAD . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JettyService.java    From armeria with Apache License 2.0 6 votes vote down vote up
private static ResponseHeaders toResponseHeaders(ArmeriaHttpTransport transport) {
    final MetaData.Response info = transport.info;
    if (info == null) {
        throw new IllegalStateException("response metadata unavailable");
    }

    final ResponseHeadersBuilder headers = ResponseHeaders.builder();
    headers.status(info.getStatus());
    info.getFields().forEach(e -> headers.add(HttpHeaderNames.of(e.getName()), e.getValue()));

    if (transport.method != HttpMethod.HEAD) {
        headers.setLong(HttpHeaderNames.CONTENT_LENGTH, transport.contentLength);
    }

    return headers.build();
}
 
Example 2
Source File: ArmeriaSdkHttpClient.java    From curiostack with MIT License 5 votes vote down vote up
private static HttpMethod convert(SdkHttpMethod method) {
  switch (method) {
    case GET:
      return HttpMethod.GET;
    case POST:
      return HttpMethod.POST;
    case PUT:
      return HttpMethod.PUT;
    case DELETE:
      return HttpMethod.DELETE;
    case HEAD:
      return HttpMethod.HEAD;
    case PATCH:
      return HttpMethod.PATCH;
    case OPTIONS:
      return HttpMethod.OPTIONS;
    default:
      try {
        return HttpMethod.valueOf(method.name());
      } catch (IllegalArgumentException unused) {
        throw new IllegalArgumentException(
            "Unknown SdkHttpMethod: "
                + method
                + ". Cannot convert to an Armeria request. This could only practically happen if "
                + "the HTTP standard has new methods added and is very unlikely.");
      }
  }
}
 
Example 3
Source File: AbstractThriftOverHttpTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static RequestLog takeLog() throws InterruptedException {
    for (;;) {
        final RequestLog log = requestLogs.take();
        if (log.requestHeaders().method() == HttpMethod.HEAD) {
            // Skip the upgrade request.
            continue;
        }

        return log;
    }
}
 
Example 4
Source File: HealthCheckService.java    From armeria with Apache License 2.0 5 votes vote down vote up
private HttpResponse newResponse(HttpMethod method, boolean isHealthy) {
    final AggregatedHttpResponse aRes = getResponse(isHealthy);

    if (method == HttpMethod.HEAD) {
        return HttpResponse.of(aRes.headers());
    } else {
        return aRes.toHttpResponse();
    }
}
 
Example 5
Source File: HttpServerHandler.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the 'content-length' header to the response.
 */
private static void setContentLength(HttpRequest req, ResponseHeadersBuilder headers,
                                     int contentLength) {
    // https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.4
    // prohibits to send message body for below cases.
    // and in those cases, content should be empty.
    if (req.method() == HttpMethod.HEAD || headers.status().isContentAlwaysEmpty()) {
        return;
    }
    headers.setInt(HttpHeaderNames.CONTENT_LENGTH, contentLength);
}
 
Example 6
Source File: AbstractPooledHttpServiceTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@EnumSource(value = HttpMethod.class, mode = Mode.EXCLUDE, names = {"CONNECT", "UNKNOWN"})
void implemented(HttpMethod method) {
    final AggregatedHttpResponse response = client.execute(HttpRequest.of(method, "/implemented"))
                                                  .aggregate().join();
    assertThat(response.status()).isEqualTo(HttpStatus.OK);
    // HEAD responses content stripped out by framework.
    if (method != HttpMethod.HEAD) {
        assertThat(response.contentUtf8()).isEqualTo(method.name().toLowerCase());
    }
}
 
Example 7
Source File: AbstractHttpFile.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
public HttpService asService() {
    return (ctx, req) -> {
        final HttpMethod method = ctx.method();
        if (method != HttpMethod.GET && method != HttpMethod.HEAD) {
            return HttpResponse.of(HttpStatus.METHOD_NOT_ALLOWED);
        }

        return HttpResponse.from(readAttributes(ctx.blockingTaskExecutor()).thenApply(attrs -> {
            if (attrs == null) {
                return HttpResponse.of(HttpStatus.NOT_FOUND);
            }

            // See https://tools.ietf.org/html/rfc7232#section-6 for more information
            // about how conditional requests are handled.

            // Handle 'if-none-match' header.
            final RequestHeaders reqHeaders = req.headers();
            final String etag = generateEntityTag(attrs);
            final String ifNoneMatch = reqHeaders.get(HttpHeaderNames.IF_NONE_MATCH);
            if (etag != null && ifNoneMatch != null) {
                if ("*".equals(ifNoneMatch) || entityTagMatches(etag, ifNoneMatch)) {
                    return newNotModified(attrs, etag);
                }
            }

            // Handle 'if-modified-since' header, only if 'if-none-match' does not exist.
            if (ifNoneMatch == null) {
                try {
                    final Long ifModifiedSince =
                            reqHeaders.getTimeMillis(HttpHeaderNames.IF_MODIFIED_SINCE);
                    if (ifModifiedSince != null) {
                        // HTTP-date does not have subsecond-precision; add 999ms to it.
                        final long ifModifiedSinceMillis = LongMath.saturatedAdd(ifModifiedSince, 999);
                        if (attrs.lastModifiedMillis() <= ifModifiedSinceMillis) {
                            return newNotModified(attrs, etag);
                        }
                    }
                } catch (Exception ignore) {
                    // Malformed date.
                }
            }

            // Precondition did not match. Handle as usual.
            switch (ctx.method()) {
                case HEAD:
                    final ResponseHeaders resHeaders = readHeaders(attrs);
                    if (resHeaders != null) {
                        return HttpResponse.of(resHeaders);
                    }
                    break;
                case GET:
                    final HttpResponse res = read(ctx.blockingTaskExecutor(), ctx.alloc(), attrs);
                    if (res != null) {
                        return res;
                    }
                    break;
                default:
                    throw new Error(); // Never reaches here.
            }

            // readHeaders() or read() returned null above.
            return HttpResponse.of(HttpStatus.NOT_FOUND);
        }));
    };
}