Java Code Examples for io.netty.handler.codec.http.HttpRequest#setUri()

The following examples show how to use io.netty.handler.codec.http.HttpRequest#setUri() . 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: HapporWebserver.java    From happor with MIT License 6 votes vote down vote up
public HapporContext getContext(HttpRequest request) {
	contextLock.readLock().lock();
	HapporContext retContext = context;
	if (pathContexts != null) {
		String uri = request.getUri();
		for (Map.Entry<String, HapporContext> entry : pathContexts.entrySet()) {
			String path = entry.getKey();
			HapporContext ctx = entry.getValue();
			if (uri.startsWith("/" + path + "/")) {
				logger.info("enter path: " + path);
				request.setUri(uri.substring(1 + path.length()));
				retContext = ctx;
				break;
			}
		}
	}
	contextLock.readLock().unlock();
	return retContext;
}
 
Example 2
Source File: ClientToProxyConnection.java    From g4proxy with Apache License 2.0 5 votes vote down vote up
/**
 * If and only if our proxy is not running in transparent mode, modify the
 * request headers to reflect that it was proxied.
 * 
 * @param httpRequest
 */
private void modifyRequestHeadersToReflectProxying(HttpRequest httpRequest) {
    if (!currentServerConnection.hasUpstreamChainedProxy()) {
        /*
         * We are making the request to the origin server, so must modify
         * the 'absolute-URI' into the 'origin-form' as per RFC 7230
         * section 5.3.1.
         *
         * This must happen even for 'transparent' mode, otherwise the origin
         * server could infer that the request came via a proxy server.
         */
        LOG.debug("Modifying request for proxy chaining");
        // Strip host from uri
        String uri = httpRequest.getUri();
        String adjustedUri = ProxyUtils.stripHost(uri);
        LOG.debug("Stripped host from uri: {}    yielding: {}", uri,
                adjustedUri);
        httpRequest.setUri(adjustedUri);
    }
    if (!proxyServer.isTransparent()) {
        LOG.debug("Modifying request headers for proxying");

        HttpHeaders headers = httpRequest.headers();

        // Remove sdch from encodings we accept since we can't decode it.
        ProxyUtils.removeSdchEncoding(headers);
        switchProxyConnectionHeader(headers);
        stripConnectionTokens(headers);
        stripHopByHopHeaders(headers);
        ProxyUtils.addVia(httpRequest, proxyServer.getProxyAlias());
    }
}
 
Example 3
Source File: NettyUtils.java    From karate with MIT License 4 votes vote down vote up
public static void fixHeadersForProxy(HttpRequest request) {
    String adjustedUri = ProxyContext.removeHostColonPort(request.uri());
    request.setUri(adjustedUri);
    request.headers().remove(HttpHeaderNames.CONNECTION);
    // addViaHeader(request, PROXY_ALIAS);
}
 
Example 4
Source File: RequestToPageLoadingEventConverter.java    From bromium with MIT License 4 votes vote down vote up
@Override
public Optional<Map<String, String>> convert(HttpRequest httpRequest) throws MalformedURLException, UnsupportedEncodingException {
    String requestUri = httpRequest.getUri();
    Path requestUriPath = Paths.get(requestUri);

    if (!((requestUri.startsWith(baseUrl)) || requestUri.startsWith("/"))) {
        throw new IllegalArgumentException("I am not responsible for this request URL: " + requestUri);
    }

    if (requestUri.endsWith(LINK_INTERCEPTOR_MARKER)) {
        httpRequest.setUri(requestUri.split(LINK_INTERCEPTOR_MARKER)[0]);
        return Optional.empty();
    }

    for (ApplicationActionConfiguration applicationActionConfiguration: applicationActionConfigurations) {
        ParameterConfiguration urlParameterConfiguration = applicationActionConfiguration
                .getWebDriverAction()
                .getParametersConfiguration()
                .get(Constants.PAGE);

        // first the easy case; if the value is hardcoded in the configuration
        // and the current url matches, then we have found the action
        if (!urlParameterConfiguration.isExposed()) {
            Path expectedPath = Paths.get(baseUrl, urlParameterConfiguration.getValue());
            Path relativeExpectedPath = Paths.get(urlParameterConfiguration.getValue());
            if (expectedPath.equals(requestUriPath) || relativeExpectedPath.equals(requestUriPath)) {
                return Optional.of(ImmutableMap.of(EVENT, applicationActionConfiguration.getName()));
            }
        } else {
            String[] urlParts = requestUri.split(baseUrl);
            if (urlParts.length < 2) {
                return Optional.empty();
            }
            String urlContinuation = urlParts[1];
            return Optional.of(ImmutableMap.of(
                    EVENT, applicationActionConfiguration.getName(),
                    urlParameterConfiguration.getAlias(), urlContinuation
            ));
        }
    }

    return Optional.empty();
}
 
Example 5
Source File: SimpleProxyHandler.java    From xio with Apache License 2.0 4 votes vote down vote up
@Override
public RequestUpdateHandler handle(HttpRequest payload, ChannelHandlerContext ctx) {
  ReferenceCountUtil.retain(payload);
  buildAndAttach(ctx);
  if (HttpUtil.is100ContinueExpected(payload)) {
    ctx.writeAndFlush(
        new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE));
  }

  Optional<String> path =
      route
          .groups(payload.uri())
          .entrySet()
          .stream()
          .filter(e -> e.getKey().equals("path"))
          .map(e -> e.getValue())
          .findFirst();

  if (!config.pathPassthru) {
    payload.setUri(path.map(config.urlPath::concat).orElse(config.urlPath));
  }

  payload.headers().set("Host", config.hostHeader);

  XioRequest request =
      HttpTracingState.hasSpan(ctx)
          ? new XioRequest(payload, HttpTracingState.getSpan(ctx).context())
          : new XioRequest(payload, null);

  log.info("Requesting {}", payload);
  ctx.channel().attr(key).get().write(request);

  return new RequestUpdateHandler() {
    @Override
    public void update(HttpContent content) {
      ReferenceCountUtil.retain(content);
      client.write(content);
    }

    @Override
    public void update(LastHttpContent last) {
      ReferenceCountUtil.retain(last);
      client.write(last);
    }
  };
}