mitre / HTTP-Proxy-Servlet

Smiley's HTTP Proxy implemented as a Java servlet
Apache License 2.0
1.45k stars 555 forks source link

Not suitable for dynamic urls calculated for each request #57

Open szarza opened 9 years ago

szarza commented 9 years ago

Hello: First, thank you very much for sharing your excellent work.

In my case, I need a proxy for which I get the full (complete) url through a service, like this: http://user:password@10.0.2.40/112/2015_04/file88d78d.mp3

For this case, your servlet does not work unless some methods are overridden, because you code assumes that targetUri and targetHost are fixed and initialized once.

Then my solution working:

    @Override
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        log.trace("<== {}?{}",request.getRequestURL(),request.getQueryString());
        if (cat.verify(request.getQueryString())) {
            try {
                targetUriObj = new URI(sercomResources.getRecord(request.getParameter("id")).getUrl());
                targetHost = URIUtils.extractHost(targetUriObj);
                log.trace("==> {}",targetUriObj);
                super.service(request, response);
            } catch (Exception e) {
                log.warn("Error obteniendo destino",e);
                throw new ServletException("Trying to process targetUri init parameter: " + e, e);
            }
        } else {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, CAT.CESTEL_AUTH_TOKEN
                    + " missing.");
            log.warn("Intento de acceso prohibido: {}", request.getRequestURI());
        }
    }

    @Override
    protected void initTarget() throws ServletException {
        /**
         * evitar super.initTarget() porque, al no estabecer parámetros en la
         * inicialización, esta llamada genera NPE
         */
        // super.initTarget();
    }

    @Override
    protected String rewriteUrlFromRequest(HttpServletRequest servletRequest) {
        /**
         * super.rewriteUrlFromRequest(servletRequest) genera Url errónea
         */
        return targetUriObj.toString();
    }

Thanks again. Regards.

dsmiley commented 9 years ago

Hi, I think you can use https://github.com/mitre/HTTP-Proxy-Servlet/blob/master/src/main/java/org/mitre/dsmiley/httpproxy/URITemplateProxyServlet.java

szarza commented 9 years ago

In my case, for a request "A" without any parameters, I get a complete URL "B" by an external service passing "A" as a parameter.

The "calculated" URL's are not known at init() time. I don't know any "template" for final URL either, and doesn't have to be homogeneous (final url can be http://a/b/c/d/e/h.mp3 or http://a/j.mp3 or any other form).

So the parameters for the template can't come from client request. The service (EJB) sercomResources.getRecord(request.getParameter("id")).getUrl() gives me the complete (full) final URL. The client knows nothing about final destination proxified.

For example, for client request http://play?id=345, the final URL would be http://192.168.2.3/882/2015/04/23/83676dgdteye.mp3 and for client request http://play?id=346, the final URL would be http://192.168.56.98/adada/2014/04/23/ueyhdbd.mp3 (different servers, paths, etc...)

Thanks.

dsmiley commented 9 years ago

Okay. If you can recommend API extensibility improvements that will help you, then send a pull request. Otherwise, it seems the proxy is working for you. It's expected you'd need to subclass to meet a requirement like this.

tecgie commented 8 years ago

Just wonder if your proxy can handle winsocket. I have a requirement to proxy the winsocket in addition to http requests. I am new to the winsocket, so I am not sure.

For example, ws://localhost -> ws://localhost:59595

Moreover, my proxy mapping looks something like the following. The {port} is dynamically generated. So it doesn't seem I can use your code as is, I may need to modify the code a bit? http://localhost:8080/app -> http://localhost:{port}

Thanks, Denny

dsmiley commented 8 years ago

Denny, next time please create a new issue instead of commenting on an unrelated issue.

RE winsocket: I have no idea honestly. I've never heard of it.I looked at https://en.wikipedia.org/wiki/Winsock extremely briefly and I at least didn't see any URLs like that; it looks like an API, not a URL scheme. I dunno.

tecgie commented 8 years ago

I meant websocket.

ws://host:port/ws

I have minimal knowledge with it, but we have a requirement to have reverse proxy for it.

Do you know if your proxy will work for websocket?

Thanks Denny

On Feb 2, 2016, at 10:32 PM, David Smiley notifications@github.com wrote:

Denny, next time please create a new issue instead of commenting on an unrelated issue.

RE winsocket: I have no idea honestly. I've never heard of it.I looked at https://en.wikipedia.org/wiki/Winsock extremely briefly and I at least didn't see any URLs like that; it looks like an API, not a URL scheme. I dunno.

— Reply to this email directly or view it on GitHub.

dsmiley commented 8 years ago

It appears not, as this proxy uses Apache HttpClient which has this open issue to support it (back from 2010) https://issues.apache.org/jira/browse/HTTPCLIENT-973