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.
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
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
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.
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
Not that it's important, but I think I meant FoxyProxy, not Firebug.
--Jesse
----- 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
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
----- 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.
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