Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

intercepting http and opening sockets?

41 views
Skip to first unread message

Venqin

unread,
Oct 6, 2006, 11:06:14 AM10/6/06
to
Hi.

I'm a (complete) newbie to Firefox extension development, and there are
a number of items I would like to accomplish. I've been digging through
some of the docs, and I haven't found any clear description that would
tell me how to do some of this stuff. Here's what I'd like to be able
to do:

- Whenever an HTTP request is made, I'd like to intercept this and
based on a condition I check, either:
- let the request proceed as is
- redirect the request to another ip address/port.

- If I redirected the request, I would like to intercept the data
that's returned (i.e., the HTTP response) do some minor processing on
it, and then let the data be fed back to Firefox in such a way that it
appears that this is the return data coming from the original request.

- I would also like to be able to open a server socket. Is this going
to require a native (C++) component, or is it doable from within
javascript with XPCOM/XPConnect?

- Finally, I would like to be able to look up content in the cache and
be able to serve this to anyone who connects to my server socket and
makes an appropriate request.

- Related to this, when does content become available in the
cache? Is it after a request has completed, or can "partial" data be
read from the cache while the request is still in progress? (For
example: If I request http://mycompany.com/large_data_set, will the
data become accessible in the cache as the data streams back, or only
once the entire response is finished?)

How difficult is this stuff to do with Firefox extensions? Can anyone
point me to any good tutorials that may help me get started?

Thanks.

Jesse D

unread,
Jun 15, 2008, 4:05:59 AM6/15/08
to Venqin
Has anyone found any way of dealing with the first item in the original
message below? I'm trying to do something similar - I need to intercept
an HTTP request before it leaves the browser and possibly change the URL
depending on a condition, but have the response be returned as the
response to the original requesting page/XHR. I'd prefer to be able to
do it in Javascript, if possible.

One solution I've found is to programmatically create a new handler
whose methods are all just copies of the originals, then register that
handler with the HTTP contract ID using the nsIComponentRegistrar
interface's registerFactory method (see
http://blues.ath.cx/firekeeper/resources/http_tracer.html for an
example). The problem with this technique, as noted on
http://blues.ath.cx/firekeeper/archives/2006-07.html#e2006-07-02T21_55_39.txt,
is that "there is an addition[al] security check when component code is
written in JavaScript. It checks out if JavaScript version of HTTP
handler can be accessed from the context of a web page that performed
XMLHttpRequest." This causes all sorts of problems, including complete
incapacitation of the XHR object and problems with manipulating the
location object. Does anyone know of any way around this problem?

Thanks,
Jesse

Jesse D

unread,
Jun 15, 2008, 4:06:09 AM6/15/08
to Venqin
Has anyone found any way of dealing with the first item in the original
message below? I'm trying to do something similar - I need to intercept
an HTTP request before it leaves the browser and possibly change the URL
depending on a condition, but have the response be returned as the
response to the original requesting page/XHR. I'd prefer to be able to
do it in Javascript, if possible.

One solution I've found is to programmatically create a new handler
whose methods are all just copies of the originals, then register that
handler with the HTTP contract ID using the nsIComponentRegistrar
interface's registerFactory method (see
http://blues.ath.cx/firekeeper/resources/http_tracer.html for an
example). The problem with this technique, as noted on
http://blues.ath.cx/firekeeper/archives/2006-07.html#e2006-07-02T21_55_39.txt,
is that "there is an addition[al] security check when component code is
written in JavaScript. It checks out if JavaScript version of HTTP
handler can be accessed from the context of a web page that performed
XMLHttpRequest." This causes all sorts of problems, including complete
incapacitation of the XHR object and problems with manipulating the
location object. Does anyone know of any way around this problem?

Thanks,
Jesse

Jesse D

unread,
Jun 15, 2008, 4:41:38 AM6/15/08
to
I apologize for the spam; I just wanted to make sure this topic got
spotted as being effectively a new question.

John J. Barton

unread,
Jun 15, 2008, 11:46:53 AM6/15/08
to
Jesse D wrote:
> Has anyone found any way of dealing with the first item in the original
> message below? I'm trying to do something similar - I need to intercept
> an HTTP request before it leaves the browser and possibly change the URL
> depending on a condition, but have the response be returned as the
> response to the original requesting page/XHR. I'd prefer to be able to
> do it in Javascript, if possible.
>
...

>>
>> - Whenever an HTTP request is made, I'd like to intercept this and
>> based on a condition I check, either:
>> - let the request proceed as is
>> - redirect the request to another ip address/port.
>>

This is the work of a proxy server, that is a network component designed
to intercept HTTP traffic and do the kinds of modifications you
describe. There is a Javascript HTTP server, it may have proxy ability.

John.

Eric H. Jung

unread,
Jun 15, 2008, 5:09:25 PM6/15/08
to dev-ext...@lists.mozilla.org

I once asked Christian Biesinger a similar question in IRC. Here's his response; his idea sounds like it should work for your question. I'm <grimholtz> in this conversation, and he is <biesi>.

<biesi> what you could actually do is this:
<biesi> observe http-on-modify-request
<biesi> there, you can get the notificationCallbacks and the window
<biesi> cancel the request if you want a proxy (hopefully that works)
<biesi> then, create a new channel for the original URI and post data etc, using nsIProxiedProtocolHandler for the original scheme
<biesi> passing it the appropriate proxyinfo
<grimholtz> ok but how does the response get into the right window?
<biesi> ah right
<biesi> forgot to mention that part
<biesi> with help of nsIURILoader::openURI



Jesse D

unread,
Jun 16, 2008, 4:53:37 AM6/16/08
to
Right, I actually thought of that myself, and I also saw that
conversation snippet somewhere in a comment in the Firebug code. But
what will that do to AJAX requests? Won't cancelling the initial
request totally throw off any JS code that was expecting a response from
that request?

--Jesse

Jesse D

unread,
Jun 16, 2008, 4:56:47 AM6/16/08
to
Jesse D wrote:
> Right, I actually thought of that myself, and I also saw that
> conversation snippet somewhere in a comment in the Firebug code.


Not that it's important, but I think I meant FoxyProxy, not Firebug.

--Jesse

Eric H. Jung

unread,
Jun 16, 2008, 12:32:25 PM6/16/08
to dev-ext...@lists.mozilla.org

----- Original Message ----
> From: Jesse D <jes...@checkpoint.com>
>
> Right, I actually thought of that myself, and I also saw that

> conversation snippet somewhere in a comment in the Firebug code. But
> what will that do to AJAX requests? Won't cancelling the initial
> request totally throw off any JS code that was expecting a response from
> that request?
>

Having not tried it, I don't know for sure. My guess is that asynchronous XHR calls would be fine but that synchronous ones might be problematic. I simply don't know. Try asking Christian...or just try implementing it :)

Eric



Chris Henry

unread,
Jun 20, 2008, 4:08:47 PM6/20/08
to

Venqin wrote:
[snip]

> - I would also like to be able to open a server socket. Is this going
> to require a native (C++) component, or is it doable from within
> javascript with XPCOM/XPConnect?

I'm interested as to whether there's any solution for this one. I want
to write an extension that will open a port in localhost (not exposed
to public) for testing. For example, I can type http://localhost:2345
on Firefox address bar and the extension will pop out and I can then
specify the response to the HTTP request.

Thanks for the help!

Chris

Eric H. Jung

unread,
Jun 20, 2008, 7:35:56 PM6/20/08
to dev-ext...@lists.mozilla.org

----- Original Message ----
> From: Chris Henry <chrish...@gmail.com>

> I'm interested as to whether there's any solution for this one. I want
> to write an extension that will open a port in localhost (not exposed
> to public) for testing. For example, I can type http://localhost:2345
> on Firefox address bar and the extension will pop out and I can then
> specify the response to the HTTP request.
>
> Thanks for the help!

See http://www.xulplanet.com/tutorials/mozsdk/sockets.php for opening sockets.



Chris Henry

unread,
Jun 21, 2008, 4:47:01 AM6/21/08
to

Thanks! The information for listening to a socket is not at that page
but at the next page:
http://www.xulplanet.com/tutorials/mozsdk/serverpush.php

(: I haven't tested it yet, but it looks very promising. Thanks again!

Chris

0 new messages