Advertisement
Guest User

Untitled

a guest
Jun 17th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. httpObserver.HeaderInfoVisitor = function(oHttp) {
  2.     this.oHttp = oHttp;
  3.     this.headers = new Array();
  4.     this.httpHeaders = new Array();
  5. }
  6. httpObserver.HeaderInfoVisitor.prototype = {
  7.     oHttp : null,
  8.     headers : null,
  9.     getHttpResponseVersion: function () {
  10.         var version = "1.z"; // Default value
  11.         // Check if this is Mozilla v1.5a and more
  12.         try {
  13.             var maj = new Object();
  14.             var min = new Object();
  15.             this.oHttp.QueryInterface(Components.interfaces.nsIHttpChannelInternal);
  16.             this.oHttp.getResponseVersion(maj,min);
  17.             version = "" + maj.value + "."+ min.value;
  18.         } catch (ex) {
  19.         }
  20.         return version;
  21.     },
  22.     getHttpRequestVersion: function (httpProxy) {
  23.         var version = "1.0"; // Default value for direct HTTP and proxy HTTP
  24.         try {
  25.             // This code is based on netwerk/protocol/http/src/nsHttpHandler.cpp (PrefsChanged)
  26.             var pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
  27.             pref = pref.getBranch("");
  28.             // Now, get the value of the HTTP version fields
  29.             if (httpProxy) {
  30.                 var tmp = pref.getCharPref("network.http.proxy.version");
  31.                 if (tmp == "1.1")
  32.                     version = tmp;
  33.             } else {
  34.                 var tmp = pref.getCharPref("network.http.version");
  35.                 if (tmp == "1.1" || tmp == "0.9")
  36.                     version = tmp;
  37.             }
  38.         } catch (ex) {
  39.         }
  40.         return version;
  41.     },
  42.     useHttpProxy : function (uri) {
  43.         // This code is based on netwerk/base/src/nsProtocolProxyService.cpp (ExamineForProxy)
  44.         try {
  45.             var pps = Components.classes["@mozilla.org/network/protocol-proxy-service;1"].getService().QueryInterface(Components.interfaces.nsIProtocolProxyService);
  46.  
  47.             // If a proxy is used for this url, we need to keep the host part
  48.             if (typeof(pps.proxyEnabled) != "undefined") {
  49.                 // Mozilla up to 1.7
  50.                 if (pps.proxyEnabled && (pps.examineForProxy(uri)!=null)) {
  51.                     // Proxies are enabled.  Now, check if it is an HTTP proxy.
  52.                     return this.isHttpProxy();
  53.                 }
  54.             } else {
  55.                 // Firefox and Mozilla 1.8+
  56.                 if (pps.resolve(uri, pps.RESOLVE_NON_BLOCKING)!=null) {
  57.                     // Proxies are enabled.  Now, check if it is an HTTP proxy.
  58.                     return this.isHttpProxy();
  59.                 }
  60.             }
  61.             return false; // No proxy or not HTTP Proxy
  62.         } catch (ex) {
  63.             return null; // Error
  64.         }
  65.     },
  66.     isHttpProxy : function() {
  67.         // Check if an HTTP proxy is configured.
  68.         var pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
  69.         pref = pref.getBranch("");
  70.         // Now, get the value of the HTTP proxy fields
  71.         var http_host = pref.getCharPref("network.proxy.http");
  72.         var http_port = pref.getIntPref("network.proxy.http_port");
  73.         // network.proxy.http_port network.proxy.http
  74.         if (http_host && http_port>0) {
  75.             return true; // HTTP Proxy
  76.         }
  77.         return false;
  78.     },
  79.     getPostData : function(oHttp) {
  80.         function postData(stream) {
  81.             // Scriptable Stream Constants
  82.             const JS_SCRIPTABLEINPUTSTREAM_CID = "@mozilla.org/scriptableinputstream;1";
  83.             const JS_SCRIPTABLEINPUTSTREAM     = "nsIScriptableInputStream";
  84.             const JS_ScriptableInputStream = new Components.Constructor
  85.             ( JS_SCRIPTABLEINPUTSTREAM_CID, JS_SCRIPTABLEINPUTSTREAM );
  86.             // Create a scriptable stream
  87.             this.seekablestream = stream;
  88.             this.stream = new JS_ScriptableInputStream();
  89.             this.stream.init(this.seekablestream);
  90.             this.mode = this.FAST;
  91.  
  92.             // Check if the stream has headers
  93.             try {
  94.                 this.seekablestream.QueryInterface(Components.interfaces.nsIMIMEInputStream);
  95.                 this.hasheaders = true;
  96.                 this.body = -1; // Must read header to find body
  97.             } catch (ex) {
  98.                 this.hasheaders = false;
  99.                 this.body = 0;  // Body at the start of the stream
  100.             }
  101.         }
  102.  
  103.         postData.prototype = {
  104.             NONE: 0,
  105.             FAST: 1,
  106.             SLOW: 2,
  107.             rewind: function() {
  108.                 this.seekablestream.seek(0,0);
  109.             },
  110.             tell: function() {
  111.                 return this.seekablestream.tell();
  112.             },
  113.             readLine: function() {
  114.                 var line = "";
  115.                 var size = 0;
  116.                 try {
  117.                     size = this.stream.available();
  118.                 } catch (ex) {
  119.                     size = 0;
  120.                 }
  121.                 for (var i=0; i<size; i++) {
  122.                     var c = this.stream.read(1);
  123.                     if (c == '\r') {
  124.                     } else if (c == '\n') {
  125.                         break;
  126.                     } else {
  127.                         line += c;
  128.                     }
  129.                 }
  130.                 return line;
  131.             },
  132.             setMode: function(mode) {
  133.                 if (mode < this.NONE && mode > this.SLOW) {
  134.                     throw "postData: unsupported mode: " + this.mode;
  135.                 }
  136.                 this.mode = mode;
  137.             },
  138.             visitPostHeaders: function(visitor) {
  139.                 this.rewind();
  140.                 if (!this.hasheaders) {
  141.                     return;
  142.                 }
  143.                 var line = this.readLine();
  144.                 while(line) {
  145.                     if (visitor) {
  146.                         var tmp = line.split(/:\s?/);
  147.                         visitor.visitHeader(tmp[0],tmp[1]);
  148.                     }
  149.                     line = this.readLine();
  150.                 }
  151.                 body = this.tell();
  152.             },
  153.             getPostBody: function(max) {
  154.                 // Position the stream to the start of the body
  155.                 if (this.body < 0 || this.seekablestream.tell() != this.body) {
  156.                     this.visitPostHeaders(null);
  157.                 }
  158.  
  159.                 var size = 0;
  160.                 try {
  161.                     size = this.stream.available();
  162.                 } catch(ex) {
  163.                     size = 0;
  164.                 }
  165.                 if (max && max >= 0 && max<size)
  166.                     size = max;
  167.  
  168.                 var postString = "";
  169.                 try {
  170.                     switch (this.mode) {
  171.                         case this.NONE:
  172.                             //Don't get any content
  173.                             break;
  174.                         case this.FAST:
  175.                             //Get the content in one shot
  176.                             if (size>0) {
  177.                                 postString = this.stream.read(size);
  178.                             }
  179.                             break;
  180.                         case this.SLOW:
  181.                             //Must read octet by octet because of a bug in nsIMultiplexStream.cpp
  182.                             //This is to avoid 'NS_BASE_STREAM_CLOSED' exception that may occurs
  183.                             //See bug #188328.
  184.                             for (var i=0; i<size; i++) {
  185.                                 var c=this.stream.read(1);
  186.                                 c ? postString+=c : postString+='\0';
  187.                             }
  188.                             break;
  189.                     }
  190.                 } catch (ex) {
  191.                     //dump("Exception while getting POST CONTENT with mode "+this.mode+": "+ex+"\n");
  192.  
  193.                     return ""+ex;
  194.                 } finally {
  195.                     // Need to close the stream after use.
  196.                     //this.seekablestream.close();
  197.                     //this.stream.close();
  198.                     //this.seekablestream.seek(2,0);
  199.                     this.rewind();
  200.                     //this.seekablestream.close();
  201.                     //try { this.stream.read(this.stream.available()); } catch (ex) {}
  202.                 }
  203.                 return postString;
  204.             }
  205.         }
  206.  
  207.         // Get the postData stream from the Http Object
  208.         try {
  209.             // Must change HttpChannel to UploadChannel to be able to access post data
  210.             oHttp.QueryInterface(Components.interfaces.nsIUploadChannel);
  211.             // Get the post data stream
  212.             if (oHttp.uploadStream) {
  213.                 // Must change to SeekableStream to be able to rewind
  214.                 oHttp.uploadStream.QueryInterface(Components.interfaces.nsISeekableStream);
  215.                 // And return a postData object
  216.                 return new postData(oHttp.uploadStream);
  217.             }
  218.         } catch (e) {
  219.             //dump("POSTDATAEXCEPTION:"+e+"\n");
  220.         }
  221.         return null;
  222.     },
  223.     visitHeader : function (name, value) {
  224.         this.headers[name] = value;
  225.         this.httpHeaders[name] = value;
  226.     },
  227.     visitRequest : function () {
  228.         this.headers = new Array();
  229.         var uri, note, ver;
  230.         try {
  231.  
  232.             // Get the URL and get parts
  233.             // Should I use  this.oHttp.URI.prePath and this.oHttp.URI.path to make
  234.             // the URL ?  I still need to remove the '#' sign if present in 'path'
  235.             var url = String(this.oHttp.URI.asciiSpec);
  236.  
  237.             // If an http proxy is used for this url, we need to keep the host part
  238.             if (this.useHttpProxy(this.oHttp.URI)==true) {
  239.                 uri = url.match(/^(.*?\/\/[^\/]+\/[^#]*)/)[1];
  240.                 ver = this.getHttpRequestVersion(true);
  241.             } else {
  242.                 uri = url.match(/^.*?\/\/[^\/]+(\/[^#]*)/)[1];
  243.                 ver = this.getHttpRequestVersion(false);
  244.             }
  245.         } catch (ex) {
  246.             //dump("PPS: cas5: " + ex + "\n");
  247.             uri = String(this.oHttp.URI.asciiSpec);
  248.             note = "Unsure about the precedent REQUEST uri";
  249.         }
  250.         this.headers["REQUEST"] = this.oHttp.requestMethod + " "
  251.         + uri + " HTTP/" + ver;
  252.         if (note)
  253.             this.headers["NOTE"] = note;
  254.         this.oHttp.visitRequestHeaders(this);
  255.  
  256.         // There may be post data in the request
  257.         var postData = this.getPostData(this.oHttp);
  258.         if (postData) {
  259.             postData.visitPostHeaders(this);
  260.             this.visitHeader("POSTDATA",postData);
  261.         } else {
  262.             this.visitHeader("POSTDATA",null);
  263.         }
  264.  
  265.         return this.headers;
  266.     },
  267.     visitResponse : function () {
  268.         var ver = this.getHttpResponseVersion();
  269.         this.headers = new Array();
  270.         this.headers["RESPONSE"] = "HTTP/" + ver + " " + this.oHttp.responseStatus
  271.         + " " + this.oHttp.responseStatusText;
  272.         //this.headers["loadGroup"] = this.oHttp.loadGroup
  273.         //this.headers["owner"] = this.oHttp.owner
  274.         //this.headers["notificationCallbacks"] = this.oHttp.notificationCallbacks
  275.         //if (this.oHttp.loadGroup) this.headers["loadGroup.ncb"] = this.oHttp.loadGroup.notificationCallbacks
  276.         this.oHttp.visitResponseHeaders(this);
  277.         return this.headers;
  278.     }
  279. }//end HeaderInfoVisitor
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement