webmetrics / browsermob-proxy

NOTICE: this project has been forked and is being maintained at https://github.com/lightbody/browsermob-proxy
https://github.com/lightbody/browsermob-proxy
Apache License 2.0
234 stars 773 forks source link

Starting proxy servers on custom ports + Refactor of custom headers #23

Closed davbo closed 12 years ago

davbo commented 12 years ago

Added support for creating proxies which run on a custom port. Also supported and documented in the REST API. This way you can programmatically bind to a port and see if it's free before attempting to open a Proxy on that port.

Perhaps default behaviour should be to attempt to ask the OS for a free port rather than current behaviour of incrementing a counter?

-- See comment below for details on the new implementation of the custom headers

davbo commented 12 years ago

I've updated this with a couple of new commits which change how adding custom headers to requests works. Now you send an individual PUT request to /port/addHeader with the paramater "header" and value should be a "name:value" pair delimited by a colon. Kinda similar to how the white/blacklisting currently works.

It's implemented in this fashion because if you attempt to PUT a parameter with a hyphen ('-') it raises a BindingException (iirc) from sitebricks. A little annoying but this works just fine. This implementation attaches headers at request execution before we store them in a HAR rather than the previous implementation which used a HttpRequestInterceptor which attached the new headers after we had placed them in a HAR. Meaning HAR output was incorrect.

I think this should probably be a separate pull request but I'm new to GitHub :-)

davbo commented 12 years ago

Also, I changed the REST API for custom headers but since there hasn't been a release between you accepting the previous pull request and now I see no need to support that previous API.

lightbody commented 12 years ago

Dave, The patch looks good, but I'm not sure about the changes you made to the custom headers. I don't think the URI should have a verb in it ("addHeaders"). Instead that can be accomplished with the verbs PUT or POST to make it truly restful.

As for the Sitebricks problem, maybe we can just solve that by instead of passing the header name/value pairs in via HTTP parameters, we can come up with a JSON structure for each of the name/value pairs. For example:

POST /put/headers
{
  'X-Header-A': 'foo',
  'X-Header-B': 'bar'
}

Finally, as for letting the OS pick the next port - I agree. We can leave it as is, but if you're up for it you can solve that by using the techniques discussed here: http://stackoverflow.com/questions/2675362/how-to-find-an-available-port

davbo commented 12 years ago

Hey Patrick, thanks for the feedback.

Definitely agree with you, I think this will mean adding a library for doing the JSON -> Java deserialization right? Unless I can use sitebricks for this?

I'll look into it more but if anyone could point me in the right direction I'd appreciate that.

lightbody commented 12 years ago

Sitebricks can do it using request.read(...).as(Json.class), but it's only good at mapping it when the field names are fixed and not dynamic. In this case, where the name/value pairs can be any arbitrary request header name/value, it may not be as simple. You can try:

    @Post
    @At("/:port/headers")
    public Reply<?> updateHeaders(Request request) {
        Map<String, String> headers = request.read(Map.class).as(Json.class);
        // stuff here

        return Reply.saying().ok();
    }

But if it can't pull it out as a Map, then you may need to read the JSON out directly with a more low-level JSON library like http://www.json.org/java/index.html, which you can import in to Maven using:

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20090211</version>
</dependency>

On Dec 5, 2011, at 11:53 AM, davbo wrote:

Hey Patrick, thanks for the feedback.

Definitely agree with you, I think this will mean adding a library for doing the JSON -> Java deserialization right? Unless I can use sitebricks for this?

I'll look into it more but if anyone could point me in the right direction I'd appreciate that.


Reply to this email directly or view it on GitHub: https://github.com/webmetrics/browsermob-proxy/pull/23#issuecomment-3022204

davbo commented 12 years ago

Blah, I realise I've left this 2 weeks. I will get around to this over the next couple of weeks. Apparently there is some silly holiday which demands my attention and consumption of Brussel Sprouts.

davbo commented 12 years ago

Hey Patrick,

Thanks for posting that code earlier this month, took me less than 15 minutes to get this working thanks to that.

Now you just POST to /port/headers with your custom headers json encoded (as you suggested). Works much better.