@Override public String getRequestMethod() { return request.getMethod().getName(); }
/** * Specifies the request method. * * @param method the method * @return {@code this} */ default RequestSpec method(String method) { return method(HttpMethod.of(method)); }
public void handle(Context context) { ratpack.http.HttpMethod requestMethod = context.getRequest().getMethod(); if (requestMethod == method || requestMethod.name(method.getName())) { context.next(); } else if (requestMethod.isOptions()) { Response response = context.getResponse(); response.getHeaders().add(HttpHeaderConstants.ALLOW, method); response.status(200).send(); } else { context.clientError(405); } } }
@Override public void handle(Context context) throws Exception { HttpMethod method = context.getRequest().getMethod(); if (method.isOptions() && !handlers.containsKey(HttpMethod.OPTIONS)) { List<String> parts = new ArrayList<>(Collections2.transform(handlers.keySet(), HttpMethod::getName)); Collections.sort(parts); String methods = JOINER.join(parts); context.getResponse().getHeaders().add(HttpHeaderConstants.ALLOW, methods); context.getResponse().status(200).send(); } else { Handler handler = handlers.get(method); if (handler != null) { context.insert(handler); return; } if (method.isHead()) { Handler getHandler = handlers.get(HttpMethod.GET); if (getHandler != null) { context.insert(getHandler); return; } } NO_METHOD_HANDLER.handle(context); } }
private void send(Downstream<? super T> downstream, Channel channel) throws Exception { channel.config().setAutoRead(true); FullHttpRequest request = new DefaultFullHttpRequest( HttpVersion.HTTP_1_1, requestConfig.method.getNettyMethod(), getFullPath(requestConfig.uri), requestConfig.body.touch(), requestConfig.headers.getNettyHeaders(), EmptyHttpHeaders.INSTANCE ); addCommonResponseHandlers(channel.pipeline(), downstream); Future<?> channelFuture; if (channelKey.ssl) { channelFuture = channel.pipeline().get(SslHandler.class).handshakeFuture(); } else { channelFuture = channel.newSucceededFuture(); } channelFuture.addListener(firstFuture -> { if (firstFuture.isSuccess()) { channel.writeAndFlush(request).addListener(writeFuture -> { if (!writeFuture.isSuccess()) { error(downstream, writeFuture.cause()); } }); } else { error(downstream, firstFuture.cause()); } }); }
private void servePath(final Context context, final Path file) throws Exception { readAttributes(file, cacheMetadata, attributes -> { if (attributes == null) { context.next(); } else if (attributes.isRegularFile()) { if (context.getRequest().getMethod().isGet()) { sendFile(context, file, attributes); } else { context.clientError(405); } } else if (attributes.isDirectory()) { if (context.getRequest().getMethod().isGet()) { maybeSendFile(context, file, 0); } else { context.clientError(405); } } else { context.next(); } }); }
String ncsaLogFormat(HostAndPort client, String rfc1413Ident, Optional<CharSequence> userId, Instant timestamp, HttpMethod method, String uri, String httpProtocol, Status status, String responseSize) { return String.format("%s %s %s [%s] \"%s %s %s\" %d %s", client.getHost(), rfc1413Ident, userId.orElse("-"), FORMATTER.format(timestamp), method.getName(), uri, httpProtocol, status.getCode(), responseSize); } }
@Override public ByMethodSpec named(String methodName, Handler handler) { return add(HttpMethod.of(methodName), handler); }
@SuppressWarnings("deprecation") public static <T> void connect(final Context context, String path, int maxLength, final WebSocketHandler<T> handler) { PublicAddress publicAddress = context.get(PublicAddress.class); URI address = publicAddress.get(context); URI httpPath = address.resolve(path); URI wsPath; try { wsPath = new URI("ws", httpPath.getUserInfo(), httpPath.getHost(), httpPath.getPort(), httpPath.getPath(), httpPath.getQuery(), httpPath.getFragment()); } catch (URISyntaxException e) { throw uncheck(e); } WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(wsPath.toString(), null, false, maxLength); Request request = context.getRequest(); HttpMethod method = valueOf(request.getMethod().getName()); FullHttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, request.getUri()); nettyRequest.headers().add(SEC_WEBSOCKET_VERSION, request.getHeaders().get(SEC_WEBSOCKET_VERSION)); nettyRequest.headers().add(SEC_WEBSOCKET_KEY, request.getHeaders().get(SEC_WEBSOCKET_KEY)); final WebSocketServerHandshaker handshaker = factory.newHandshaker(nettyRequest); final DirectChannelAccess directChannelAccess = context.getDirectChannelAccess(); final Channel channel = directChannelAccess.getChannel(); if (!channel.config().isAutoRead()) { channel.config().setAutoRead(true); } handshaker.handshake(channel, nettyRequest).addListener(new HandshakeFutureListener<>(context, handshaker, handler)); }
description .append("No response sent for ") .append(request.getMethod().getName()) .append(" request to ") .append(request.getUri());
@SuppressWarnings("deprecation") public static Form parseForm(Context context, TypedData body, MultiValueMap<String, String> base) throws RuntimeException { Request request = context.getRequest(); HttpMethod method = io.netty.handler.codec.http.HttpMethod.valueOf(request.getMethod().getName()); HttpRequest nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, method, request.getUri()); nettyRequest.headers().add(HttpHeaderNames.CONTENT_TYPE, body.getContentType().toString());