Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- httpObserver.HeaderInfoVisitor = function(oHttp) {
- this.oHttp = oHttp;
- this.headers = new Array();
- this.httpHeaders = new Array();
- }
- httpObserver.HeaderInfoVisitor.prototype = {
- oHttp : null,
- headers : null,
- getHttpResponseVersion: function () {
- var version = "1.z"; // Default value
- // Check if this is Mozilla v1.5a and more
- try {
- var maj = new Object();
- var min = new Object();
- this.oHttp.QueryInterface(Components.interfaces.nsIHttpChannelInternal);
- this.oHttp.getResponseVersion(maj,min);
- version = "" + maj.value + "."+ min.value;
- } catch (ex) {
- }
- return version;
- },
- getHttpRequestVersion: function (httpProxy) {
- var version = "1.0"; // Default value for direct HTTP and proxy HTTP
- try {
- // This code is based on netwerk/protocol/http/src/nsHttpHandler.cpp (PrefsChanged)
- var pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
- pref = pref.getBranch("");
- // Now, get the value of the HTTP version fields
- if (httpProxy) {
- var tmp = pref.getCharPref("network.http.proxy.version");
- if (tmp == "1.1")
- version = tmp;
- } else {
- var tmp = pref.getCharPref("network.http.version");
- if (tmp == "1.1" || tmp == "0.9")
- version = tmp;
- }
- } catch (ex) {
- }
- return version;
- },
- useHttpProxy : function (uri) {
- // This code is based on netwerk/base/src/nsProtocolProxyService.cpp (ExamineForProxy)
- try {
- var pps = Components.classes["@mozilla.org/network/protocol-proxy-service;1"].getService().QueryInterface(Components.interfaces.nsIProtocolProxyService);
- // If a proxy is used for this url, we need to keep the host part
- if (typeof(pps.proxyEnabled) != "undefined") {
- // Mozilla up to 1.7
- if (pps.proxyEnabled && (pps.examineForProxy(uri)!=null)) {
- // Proxies are enabled. Now, check if it is an HTTP proxy.
- return this.isHttpProxy();
- }
- } else {
- // Firefox and Mozilla 1.8+
- if (pps.resolve(uri, pps.RESOLVE_NON_BLOCKING)!=null) {
- // Proxies are enabled. Now, check if it is an HTTP proxy.
- return this.isHttpProxy();
- }
- }
- return false; // No proxy or not HTTP Proxy
- } catch (ex) {
- return null; // Error
- }
- },
- isHttpProxy : function() {
- // Check if an HTTP proxy is configured.
- var pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
- pref = pref.getBranch("");
- // Now, get the value of the HTTP proxy fields
- var http_host = pref.getCharPref("network.proxy.http");
- var http_port = pref.getIntPref("network.proxy.http_port");
- // network.proxy.http_port network.proxy.http
- if (http_host && http_port>0) {
- return true; // HTTP Proxy
- }
- return false;
- },
- getPostData : function(oHttp) {
- function postData(stream) {
- // Scriptable Stream Constants
- const JS_SCRIPTABLEINPUTSTREAM_CID = "@mozilla.org/scriptableinputstream;1";
- const JS_SCRIPTABLEINPUTSTREAM = "nsIScriptableInputStream";
- const JS_ScriptableInputStream = new Components.Constructor
- ( JS_SCRIPTABLEINPUTSTREAM_CID, JS_SCRIPTABLEINPUTSTREAM );
- // Create a scriptable stream
- this.seekablestream = stream;
- this.stream = new JS_ScriptableInputStream();
- this.stream.init(this.seekablestream);
- this.mode = this.FAST;
- // Check if the stream has headers
- try {
- this.seekablestream.QueryInterface(Components.interfaces.nsIMIMEInputStream);
- this.hasheaders = true;
- this.body = -1; // Must read header to find body
- } catch (ex) {
- this.hasheaders = false;
- this.body = 0; // Body at the start of the stream
- }
- }
- postData.prototype = {
- NONE: 0,
- FAST: 1,
- SLOW: 2,
- rewind: function() {
- this.seekablestream.seek(0,0);
- },
- tell: function() {
- return this.seekablestream.tell();
- },
- readLine: function() {
- var line = "";
- var size = 0;
- try {
- size = this.stream.available();
- } catch (ex) {
- size = 0;
- }
- for (var i=0; i<size; i++) {
- var c = this.stream.read(1);
- if (c == '\r') {
- } else if (c == '\n') {
- break;
- } else {
- line += c;
- }
- }
- return line;
- },
- setMode: function(mode) {
- if (mode < this.NONE && mode > this.SLOW) {
- throw "postData: unsupported mode: " + this.mode;
- }
- this.mode = mode;
- },
- visitPostHeaders: function(visitor) {
- this.rewind();
- if (!this.hasheaders) {
- return;
- }
- var line = this.readLine();
- while(line) {
- if (visitor) {
- var tmp = line.split(/:\s?/);
- visitor.visitHeader(tmp[0],tmp[1]);
- }
- line = this.readLine();
- }
- body = this.tell();
- },
- getPostBody: function(max) {
- // Position the stream to the start of the body
- if (this.body < 0 || this.seekablestream.tell() != this.body) {
- this.visitPostHeaders(null);
- }
- var size = 0;
- try {
- size = this.stream.available();
- } catch(ex) {
- size = 0;
- }
- if (max && max >= 0 && max<size)
- size = max;
- var postString = "";
- try {
- switch (this.mode) {
- case this.NONE:
- //Don't get any content
- break;
- case this.FAST:
- //Get the content in one shot
- if (size>0) {
- postString = this.stream.read(size);
- }
- break;
- case this.SLOW:
- //Must read octet by octet because of a bug in nsIMultiplexStream.cpp
- //This is to avoid 'NS_BASE_STREAM_CLOSED' exception that may occurs
- //See bug #188328.
- for (var i=0; i<size; i++) {
- var c=this.stream.read(1);
- c ? postString+=c : postString+='\0';
- }
- break;
- }
- } catch (ex) {
- //dump("Exception while getting POST CONTENT with mode "+this.mode+": "+ex+"\n");
- return ""+ex;
- } finally {
- // Need to close the stream after use.
- //this.seekablestream.close();
- //this.stream.close();
- //this.seekablestream.seek(2,0);
- this.rewind();
- //this.seekablestream.close();
- //try { this.stream.read(this.stream.available()); } catch (ex) {}
- }
- return postString;
- }
- }
- // Get the postData stream from the Http Object
- try {
- // Must change HttpChannel to UploadChannel to be able to access post data
- oHttp.QueryInterface(Components.interfaces.nsIUploadChannel);
- // Get the post data stream
- if (oHttp.uploadStream) {
- // Must change to SeekableStream to be able to rewind
- oHttp.uploadStream.QueryInterface(Components.interfaces.nsISeekableStream);
- // And return a postData object
- return new postData(oHttp.uploadStream);
- }
- } catch (e) {
- //dump("POSTDATAEXCEPTION:"+e+"\n");
- }
- return null;
- },
- visitHeader : function (name, value) {
- this.headers[name] = value;
- this.httpHeaders[name] = value;
- },
- visitRequest : function () {
- this.headers = new Array();
- var uri, note, ver;
- try {
- // Get the URL and get parts
- // Should I use this.oHttp.URI.prePath and this.oHttp.URI.path to make
- // the URL ? I still need to remove the '#' sign if present in 'path'
- var url = String(this.oHttp.URI.asciiSpec);
- // If an http proxy is used for this url, we need to keep the host part
- if (this.useHttpProxy(this.oHttp.URI)==true) {
- uri = url.match(/^(.*?\/\/[^\/]+\/[^#]*)/)[1];
- ver = this.getHttpRequestVersion(true);
- } else {
- uri = url.match(/^.*?\/\/[^\/]+(\/[^#]*)/)[1];
- ver = this.getHttpRequestVersion(false);
- }
- } catch (ex) {
- //dump("PPS: cas5: " + ex + "\n");
- uri = String(this.oHttp.URI.asciiSpec);
- note = "Unsure about the precedent REQUEST uri";
- }
- this.headers["REQUEST"] = this.oHttp.requestMethod + " "
- + uri + " HTTP/" + ver;
- if (note)
- this.headers["NOTE"] = note;
- this.oHttp.visitRequestHeaders(this);
- // There may be post data in the request
- var postData = this.getPostData(this.oHttp);
- if (postData) {
- postData.visitPostHeaders(this);
- this.visitHeader("POSTDATA",postData);
- } else {
- this.visitHeader("POSTDATA",null);
- }
- return this.headers;
- },
- visitResponse : function () {
- var ver = this.getHttpResponseVersion();
- this.headers = new Array();
- this.headers["RESPONSE"] = "HTTP/" + ver + " " + this.oHttp.responseStatus
- + " " + this.oHttp.responseStatusText;
- //this.headers["loadGroup"] = this.oHttp.loadGroup
- //this.headers["owner"] = this.oHttp.owner
- //this.headers["notificationCallbacks"] = this.oHttp.notificationCallbacks
- //if (this.oHttp.loadGroup) this.headers["loadGroup.ncb"] = this.oHttp.loadGroup.notificationCallbacks
- this.oHttp.visitResponseHeaders(this);
- return this.headers;
- }
- }//end HeaderInfoVisitor
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement