@Override public void handle( final HttpRequest request, final HttpResponse response, final HttpContext context) throws HttpException, IOException { final HttpInetConnection conn = (HttpInetConnection) context.getAttribute(HttpCoreContext.HTTP_CONNECTION); String localhost = conn.getLocalAddress().getHostName(); if (localhost.equals("127.0.0.1")) { localhost = "localhost"; } final int port = conn.getLocalPort(); final String uri = request.getRequestLine().getUri(); if (uri.equals("/oldlocation/")) { response.setStatusCode(this.statuscode); response.addHeader(new BasicHeader("Location", "http://" + localhost + ":" + port + "/newlocation/")); response.addHeader(new BasicHeader("Connection", "close")); } else if (uri.equals("/newlocation/")) { response.setStatusCode(HttpStatus.SC_OK); final StringEntity entity = new StringEntity("Successful redirect"); response.setEntity(entity); } else { response.setStatusCode(HttpStatus.SC_NOT_FOUND); } }
@Override public void handle( final HttpRequest httpRequest, final HttpResponse httpResponse, final HttpContext httpContext ) throws IOException { log.trace( "Handling [{}]", httpRequest ); final HttpInetConnection connection = ( HttpInetConnection ) httpContext.getAttribute( HTTP_CONNECTION ); final InetAddress remoteAddress = connection.getRemoteAddress(); final String httpContextProtocol = String.valueOf( httpContext.getAttribute( "protocol" ) ); final Request request = new Request( httpRequest, new Context( location, remoteAddress, httpContextProtocol ) ); RequestCors cors = corsPolicy.getCors( request ); final Response response = new Response( httpResponse, cors ); if( Protocol.LOCAL.equals( this.protocol ) && !Inet.isLocalAddress( remoteAddress ) ) { response.respond( HTTP_FORBIDDEN ); } else if( cors.autoOptions && request.httpMethod == Request.HttpMethod.OPTIONS ) { response.respond( NO_CONTENT ); } else { handler.handle( request, response ); } }
@Override public void handle( final HttpRequest httpRequest, final HttpAsyncExchange httpAsyncExchange, final HttpContext httpContext ) throws HttpException, IOException { LOGGER.trace( "handling [{}]", httpRequest ); final HttpInetConnection connection = ( HttpInetConnection ) httpContext .getAttribute( HttpCoreContext.HTTP_CONNECTION ); final InetAddress remoteAddress = connection.getRemoteAddress(); final HttpResponse response = httpAsyncExchange.getResponse(); final String httpContextProtocol = String.valueOf( httpContext.getAttribute( "protocol" ) ); if( Protocol.LOCAL.equals( this.protocol ) && !Inet.isLocalAddress( remoteAddress ) ) { response.setStatusCode( HTTP_FORBIDDEN ); } else { Request request = new Request( httpRequest, new Context( location, remoteAddress, httpContextProtocol ) ); handler.handle( request, new Response( response, corsPolicy.getCors( request ) ) ); } httpAsyncExchange.submitResponse(); }
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { Args.notNull(request, "HTTP request"); final HttpCoreContext corecontext = HttpCoreContext.adapt(context); final ProtocolVersion ver = request.getRequestLine().getProtocolVersion(); final String method = request.getRequestLine().getMethod(); if (method.equalsIgnoreCase("CONNECT") && ver.lessEquals(HttpVersion.HTTP_1_0)) { return; } if (!request.containsHeader(HTTP.TARGET_HOST)) { HttpHost targethost = corecontext.getTargetHost(); if (targethost == null) { final HttpConnection conn = corecontext.getConnection(); if (conn instanceof HttpInetConnection) { // Populate the context with a default HTTP host based on the // inet address of the target host final InetAddress address = ((HttpInetConnection) conn).getRemoteAddress(); final int port = ((HttpInetConnection) conn).getRemotePort(); if (address != null) { targethost = new HttpHost(address.getHostName(), port); } } if (targethost == null) { if (ver.lessEquals(HttpVersion.HTTP_1_0)) { return; } else { throw new ProtocolException("Target host missing"); } } } request.addHeader(HTTP.TARGET_HOST, targethost.toHostString()); } }
@Override public HttpResponse execute(HttpRequest request, HttpClientConnection conn, HttpContext context) throws IOException, HttpException { HttpInetConnection connection = (HttpInetConnection) conn; context.setAttribute(HOST_ADDRESS, connection.getRemoteAddress().getHostAddress()); return super.execute(request, conn, context); }
public static String getRemoteAddressFromContext(HttpContext context) { String remoteAddress = (String)context.getAttribute(Constants.SOCKET_REMOTE_ADDRESS); if (remoteAddress == null) { if (HttpClientContext.class.isInstance(context)) { HttpClientContext cc = (HttpClientContext)context; if (cc.getConnection() instanceof HttpInetConnection) { HttpInetConnection inetConnection = (HttpInetConnection)cc.getConnection(); remoteAddress = inetConnection.getRemoteAddress().getHostAddress(); } } } return remoteAddress; }
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { if (request == null) { throw new IllegalArgumentException("HTTP request may not be null"); } if (context == null) { throw new IllegalArgumentException("HTTP context may not be null"); } ProtocolVersion ver = request.getRequestLine().getProtocolVersion(); String method = request.getRequestLine().getMethod(); if (method.equalsIgnoreCase("CONNECT") && ver.lessEquals(HttpVersion.HTTP_1_0)) { return; } if (!request.containsHeader(HTTP.TARGET_HOST)) { HttpHost targethost = (HttpHost) context .getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (targethost == null) { HttpConnection conn = (HttpConnection) context .getAttribute(ExecutionContext.HTTP_CONNECTION); if (conn instanceof HttpInetConnection) { // Populate the context with a default HTTP host based on the // inet address of the target host InetAddress address = ((HttpInetConnection) conn).getRemoteAddress(); int port = ((HttpInetConnection) conn).getRemotePort(); if (address != null) { targethost = new HttpHost(address.getHostName(), port); } } if (targethost == null) { if (ver.lessEquals(HttpVersion.HTTP_1_0)) { return; } else { throw new ProtocolException("Target host missing"); } } } request.addHeader(HTTP.TARGET_HOST, targethost.toHostString()); } }