@Override public HttpHeaders method(HttpMethod method) { requireNonNull(method, "method"); this.method = method; set(HttpHeaderNames.METHOD, method.name()); return this; }
@Nullable @Override public HttpMethod method() { final HttpMethod method = this.method; if (method != null) { return method; } final String methodStr = get(HttpHeaderNames.METHOD); if (methodStr == null) { return null; } return this.method = HttpMethod.isSupported(methodStr) ? HttpMethod.valueOf(methodStr) : HttpMethod.UNKNOWN; }
ArmeriaClientHttpRequest(HttpClient client, HttpMethod httpMethod, String pathAndQuery, URI uri, DataBufferFactoryWrapper<?> factoryWrapper) { this.client = requireNonNull(client, "client"); this.httpMethod = requireNonNull(httpMethod, "httpMethod"); this.uri = requireNonNull(uri, "uri"); this.factoryWrapper = requireNonNull(factoryWrapper, "factoryWrapper"); headers = HttpHeaders.of(com.linecorp.armeria.common.HttpMethod.valueOf(httpMethod.name()), requireNonNull(pathAndQuery, "pathAndQuery")); }
return; if (!HttpMethod.isSupported(method.toString())) { writeErrorResponse(ctx, streamId, HttpResponseStatus.METHOD_NOT_ALLOWED); return;
/** * Converts the headers of the given Netty HTTP/1.x request into Armeria HTTP/2 headers. * The following headers are only used if they can not be found in the {@code HOST} header or the * {@code Request-Line} as defined by <a href="https://tools.ietf.org/html/rfc7230">rfc7230</a> * <ul> * <li>{@link ExtensionHeaderNames#SCHEME}</li> * </ul> * {@link ExtensionHeaderNames#PATH} is ignored and instead extracted from the {@code Request-Line}. */ public static HttpHeaders toArmeria(HttpRequest in) throws URISyntaxException { final URI requestTargetUri = toUri(in); final io.netty.handler.codec.http.HttpHeaders inHeaders = in.headers(); final HttpHeaders out = new DefaultHttpHeaders(true, inHeaders.size()); out.path(toHttp2Path(requestTargetUri)); out.method(HttpMethod.valueOf(in.method().name())); setHttp2Scheme(inHeaders, requestTargetUri, out); if (!isOriginForm(requestTargetUri) && !isAsteriskForm(requestTargetUri)) { // Attempt to take from HOST header before taking from the request-line final String host = inHeaders.getAsString(HttpHeaderNames.HOST); setHttp2Authority(host == null || host.isEmpty() ? requestTargetUri.getAuthority() : host, out); } // Add the HTTP headers which have not been consumed above toArmeria(inHeaders, out); return out; }
if (!HttpMethod.isSupported(nettyReq.method().name())) { fail(id, HttpResponseStatus.METHOD_NOT_ALLOWED); return;
@Override public String getMethodValue() { return req.method().name(); }
private static void exportMethod(Map<String, String> out, RequestContext ctx) { out.put(REQ_METHOD.mdcKey, ctx.method().name()); }
final HttpRequest req = new DefaultHttpRequest( HttpVersion.HTTP_1_1, io.netty.handler.codec.http.HttpMethod.valueOf(method.name()), path, false);
private List<Tag> buildTags(RequestLog log) { final RequestContext ctx = log.context(); final Object requestContent = log.requestContent(); String methodName = null; if (requestContent instanceof RpcRequest) { methodName = ((RpcRequest) requestContent).method(); } if (methodName == null) { final HttpHeaders requestHeaders = log.requestHeaders(); final HttpMethod httpMethod = requestHeaders.method(); if (httpMethod != null) { methodName = httpMethod.name(); } } if (methodName == null) { methodName = MoreObjects.firstNonNull(log.method().name(), "__UNKNOWN_METHOD__"); } final List<Tag> tags = new ArrayList<>(4); // method, hostNamePattern, pathMapping, status tags.add(Tag.of("method", methodName)); if (ctx instanceof ServiceRequestContext) { final ServiceRequestContext sCtx = (ServiceRequestContext) ctx; tags.add(Tag.of("hostnamePattern", sCtx.virtualHost().hostnamePattern())); tags.add(Tag.of("pathMapping", sCtx.pathMapping().meterTag())); } return tags; } };
private static MetaData.Request toRequestMetadata(ServiceRequestContext ctx, AggregatedHttpMessage aReq) { // Construct the HttpURI final StringBuilder uriBuf = new StringBuilder(); final HttpHeaders aHeaders = aReq.headers(); uriBuf.append(ctx.sessionProtocol().isTls() ? "https" : "http"); uriBuf.append("://"); uriBuf.append(aHeaders.authority()); uriBuf.append(aHeaders.path()); final HttpURI uri = new HttpURI(uriBuf.toString()); uri.setPath(ctx.mappedPath()); // Convert HttpHeaders to HttpFields final HttpFields jHeaders = new HttpFields(aHeaders.size()); aHeaders.forEach(e -> { final AsciiString key = e.getKey(); if (!key.isEmpty() && key.byteAt(0) != ':') { jHeaders.add(key.toString(), e.getValue()); } }); return new MetaData.Request( aHeaders.method().name(), uri, HttpVersion.HTTP_1_1, jHeaders, aReq.content().length()); }
uriBuilder.append('?').append(log.query()); span.tag("http.method", log.method().name()) .tag("http.path", log.path()) .tag("http.url", uriBuilder.toString())
coyoteReq.method().setString(method.name());
@Test public void readBodyStream() throws Exception { final HttpRequest httpRequest = HttpRequest.of(HttpHeaders.of(HttpMethod.POST, "/"), Flux.just("a", "b", "c", "d", "e") .map(HttpData::ofUtf8)); final ServiceRequestContext ctx = newRequestContext(httpRequest); final ArmeriaServerHttpRequest req = request(ctx); assertThat(req.getMethodValue()).isEqualTo(HttpMethod.POST.name()); assertThat(req.<Object>getNativeRequest()).isInstanceOf(HttpRequest.class).isEqualTo(httpRequest); assertThat(httpRequest.completionFuture().isDone()).isFalse(); final Flux<String> body = req.getBody().map(TestUtil::bufferToString); StepVerifier.create(body, 1) .expectNext("a").thenRequest(1) .expectNext("b").thenRequest(1) .expectNext("c").thenRequest(1) .expectNext("d").thenRequest(1) .expectNext("e").thenRequest(1) .expectComplete() .verify(); await().until(() -> httpRequest.completionFuture().isDone()); }
@Override public HttpResponse execute(ClientRequestContext ctx, HttpRequest req) throws Exception { final Span span = tracer.nextSpan(); injector.inject(span.context(), req.headers()); // For no-op spans, we only need to inject into headers and don't set any other attributes. if (span.isNoop()) { return delegate().execute(ctx, req); } final String method = ctx.method().name(); span.kind(Kind.CLIENT).name(method); ctx.log().addListener(log -> SpanContextUtil.startSpan(span, log), RequestLogAvailability.REQUEST_START); // Ensure the trace context propagates to children ctx.onChild(RequestContextCurrentTraceContext::copy); ctx.log().addListener(log -> { SpanTags.logWireSend(span, log.requestFirstBytesTransferredTimeNanos(), log); SpanTags.logWireReceive(span, log.responseFirstBytesTransferredTimeNanos(), log); finishSpan(span, log); }, RequestLogAvailability.COMPLETE); try (SpanInScope ignored = tracer.withSpanInScope(span)) { return delegate().execute(ctx, req); } }
@Override public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception { final TraceContextOrSamplingFlags contextOrFlags = extractor.extract(req.headers()); final Span span = contextOrFlags.context() != null ? tracer.joinSpan(contextOrFlags.context()) : tracer.nextSpan(contextOrFlags); // For no-op spans, nothing special to do. if (span.isNoop()) { return delegate().serve(ctx, req); } final String method = ctx.method().name(); span.kind(Kind.SERVER).name(method); ctx.log().addListener(log -> SpanContextUtil.startSpan(span, log), RequestLogAvailability.REQUEST_START); // Ensure the trace context propagates to children ctx.onChild(RequestContextCurrentTraceContext::copy); ctx.log().addListener(log -> { SpanTags.logWireReceive(span, log.requestFirstBytesTransferredTimeNanos(), log); SpanTags.logWireSend(span, log.responseFirstBytesTransferredTimeNanos(), log); SpanContextUtil.closeSpan(span, log); }, RequestLogAvailability.COMPLETE); try (SpanInScope ignored = tracer.withSpanInScope(span)) { return delegate().serve(ctx, req); } } }
private static void exportMethod(Map<String, String> out, RequestContext ctx) { out.put(REQ_METHOD.mdcKey, ctx.method().name()); }
private static void exportMethod(Map<String, String> out, RequestContext ctx) { out.put(REQ_METHOD.mdcKey, ctx.method().name()); }