1 # ***** BEGIN LICENSE BLOCK *****
2 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 # The contents of this file are subject to the Mozilla Public License Version
5 # 1.1 (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
7 # http://www.mozilla.org/MPL/
9 # Software distributed under the License is distributed on an "AS IS" basis,
10 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 # for the specific language governing rights and limitations under the
14 # The Original Code is Google Safe Browsing.
16 # The Initial Developer of the Original Code is Google Inc.
17 # Portions created by the Initial Developer are Copyright (C) 2006
18 # the Initial Developer. All Rights Reserved.
21 # Fritz Schneider <fritz@google.com> (original author)
23 # Alternatively, the contents of this file may be used under the terms of
24 # either the GNU General Public License Version 2 or later (the "GPL"), or
25 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 # in which case the provisions of the GPL or the LGPL are applicable instead
27 # of those above. If you wish to allow use of your version of this file only
28 # under the terms of either the GPL or the LGPL, and not to allow others to
29 # use your version of this file under the terms of the MPL, indicate your
30 # decision by deleting the provisions above and replace them with the notice
31 # and other provisions required by the GPL or the LGPL. If you do not delete
32 # the provisions above, a recipient may use your version of this file under
33 # the terms of any one of the MPL, the GPL or the LGPL.
35 # ***** END LICENSE BLOCK *****
37 // A simple class that encapsulates a request. You'll notice the
38 // style here is different from the rest of the extension; that's
39 // because this was re-used from really old code we had. At some
40 // point it might be nice to replace this with something better
41 // (e.g., something that has explicit onerror handler, ability
42 // to set headers, and so on).
44 // The only interesting thing here is its ability to strip cookies
48 * Because we might be in a component, we can't just assume that
49 * XMLHttpRequest exists. So we use this tiny factory function to wrap the
52 * @return XMLHttpRequest object
54 function PROT_NewXMLHttpRequest() {
55 var Cc = Components.classes;
56 var Ci = Components.interfaces;
57 var request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
58 .createInstance(Ci.nsIXMLHttpRequest);
59 // Need the following so we get onerror/load/progresschange
60 request.QueryInterface(Ci.nsIJSXMLHttpRequest);
65 * A helper class that does HTTP GETs and calls back a function with
66 * the content it receives. Asynchronous, so uses a closure for the
69 * @param opt_stripCookies Boolean indicating whether we should strip
70 * cookies from this request
74 function PROT_XMLFetcher(opt_stripCookies) {
75 this.debugZone = "xmlfetcher";
76 this._request = PROT_NewXMLHttpRequest();
77 this._stripCookies = !!opt_stripCookies;
80 PROT_XMLFetcher.prototype = {
82 * Function that will be called back upon fetch completion.
88 * Fetches some content.
90 * @param page URL to fetch
91 * @param callback Function to call back when complete.
93 get: function(page, callback) {
94 this._request.abort(); // abort() is asynchronous, so
95 this._request = PROT_NewXMLHttpRequest();
96 this._callback = callback;
97 var asynchronous = true;
98 this._request.open("GET", page, asynchronous);
99 this._request.channel.notificationCallbacks = this;
101 if (this._stripCookies)
102 new PROT_CookieStripper(this._request.channel);
106 this._request.onreadystatechange = function() {
107 self.readyStateChange(self);
110 this._request.send(null);
114 * Called periodically by the request to indicate some state change. 4
115 * means content has been received.
117 readyStateChange: function(fetcher) {
118 if (fetcher._request.readyState != 4)
121 // If the request fails, on trunk we get status set to
122 // NS_ERROR_NOT_AVAILABLE. On 1.8.1 branch we get an exception
123 // forwarded from nsIHttpChannel::GetResponseStatus. To be consistent
124 // between branch and trunk, we send back NS_ERROR_NOT_AVAILABLE for
126 var responseText = null;
127 var status = Components.results.NS_ERROR_NOT_AVAILABLE;
129 G_Debug(this, "xml fetch status code: \"" +
130 fetcher._request.status + "\"");
131 status = fetcher._request.status;
132 responseText = fetcher._request.responseText;
134 G_Debug(this, "Caught exception trying to read xmlhttprequest " +
138 if (fetcher._callback)
139 fetcher._callback(responseText, status);
142 // Suppress any certificate errors
143 notifyCertProblem: function(socketInfo, status, targetSite) {
147 // Suppress any ssl errors
148 notifySSLError: function(socketInfo, error, targetSite) {
152 // nsIInterfaceRequestor
153 getInterface: function(iid) {
154 return this.QueryInterface(iid);
157 QueryInterface: function(iid) {
158 if (!iid.equals(Components.interfaces.nsIBadCertListener2) &&
159 !iid.equals(Components.interfaces.nsISSLErrorListener) &&
160 !iid.equals(Components.interfaces.nsIInterfaceRequestor) &&
161 !iid.equals(Components.interfaces.nsISupports))
162 throw Components.results.NS_ERROR_NO_INTERFACE;
169 * This class knows how to strip cookies from an HTTP request. It
170 * listens for http-on-modify-request, and modifies the request
171 * accordingly. We can't do this using xmlhttprequest.setHeader() or
172 * nsIChannel.setRequestHeader() before send()ing because the cookie
173 * service is called after send().
175 * @param channel nsIChannel in which the request is happening
178 function PROT_CookieStripper(channel) {
179 this.debugZone = "cookiestripper";
180 this.topic_ = "http-on-modify-request";
181 this.channel_ = channel;
183 var Cc = Components.classes;
184 var Ci = Components.interfaces;
185 this.observerService_ = Cc["@mozilla.org/observer-service;1"]
186 .getService(Ci.nsIObserverService);
187 this.observerService_.addObserver(this, this.topic_, false);
189 // If the request doesn't issue, don't hang around forever
190 var twentySeconds = 20 * 1000;
191 this.alarm_ = new G_Alarm(BindToObject(this.stopObserving, this),
196 * Invoked by the observerservice. See nsIObserve.
198 PROT_CookieStripper.prototype.observe = function(subject, topic, data) {
199 if (topic != this.topic_ || subject != this.channel_)
202 G_Debug(this, "Stripping cookies for channel.");
204 this.channel_.QueryInterface(Components.interfaces.nsIHttpChannel);
205 this.channel_.setRequestHeader("Cookie", "", false /* replace, not add */);
206 this.alarm_.cancel();
207 this.stopObserving();
211 * Remove us from the observerservice
213 PROT_CookieStripper.prototype.stopObserving = function() {
214 G_Debug(this, "Removing observer");
215 this.observerService_.removeObserver(this, this.topic_);
216 this.channel_ = this.alarm_ = this.observerService_ = null;
222 PROT_CookieStripper.prototype.QueryInterface = function(iid) {
223 var Ci = Components.interfaces;
224 if (iid.equals(Ci.nsISupports) || iid.equals(Ci.nsIObserve))
226 throw Components.results.NS_ERROR_NO_INTERFACE;