[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [meek/master] Refactoring in Firefox helper.



commit bf9386d409f7027bce9434b53b24c7bff74dc17a
Author: David Fifield <david@xxxxxxxxxxxxxxx>
Date:   Sun Feb 22 14:08:41 2015 -0800

    Refactoring in Firefox helper.
    
    Use exceptions instead of dumping to console.
---
 firefox/components/main.js |   36 +++++++++++++++++++-----------------
 1 file changed, 19 insertions(+), 17 deletions(-)

diff --git a/firefox/components/main.js b/firefox/components/main.js
index e135d64..f231cd6 100644
--- a/firefox/components/main.js
+++ b/firefox/components/main.js
@@ -328,25 +328,23 @@ MeekHTTPHelper.RequestReader.prototype = {
 
     // nsIInputStreamCallback implementation.
     onInputStreamReady: function(inputStream) {
-        let input = Components.classes["@mozilla.org/binaryinputstream;1"]
-            .createInstance(Components.interfaces.nsIBinaryInputStream);
-        input.setInputStream(inputStream);
         try {
+            let input = Components.classes["@mozilla.org/binaryinputstream;1"]
+                .createInstance(Components.interfaces.nsIBinaryInputStream);
+            input.setInputStream(inputStream);
             switch (this.state) {
             case this.STATE_READING_LENGTH:
                 this.doStateReadingLength(input);
-                this.asyncWait();
                 break;
             case this.STATE_READING_OBJECT:
                 this.doStateReadingObject(input);
-                if (this.bytesToRead > 0)
-                    this.asyncWait();
                 break;
             }
+            if (this.state !== this.STATE_DONE)
+                this.asyncWait();
         } catch (e) {
-            dump("got exception " + e + "\n");
             this.transport.close(0);
-            return;
+            throw e;
         }
     },
 
@@ -363,14 +361,14 @@ MeekHTTPHelper.RequestReader.prototype = {
         if (this.bytesToRead > 0)
             return;
 
-        this.state = this.STATE_READING_OBJECT;
         let b = this.buf;
-        this.bytesToRead = (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3];
-        if (this.bytesToRead > 1000000) {
-            dump("Object length is too long (" + this.bytesToRead + "), ignoring.\n");
-            throw Components.results.NS_ERROR_FAILURE;
-        }
-        this.buf = new Uint8Array(this.bytesToRead);
+        let len = (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3];
+        if (len > 1000000)
+            throw Components.Exception("Object length is too large (" + len + " bytes)", Components.results.NS_ERROR_ILLEGAL_VALUE);
+
+        this.state = this.STATE_READING_OBJECT;
+        this.buf = new Uint8Array(len);
+        this.bytesToRead = this.buf.length;
     },
 
     doStateReadingObject: function(input) {
@@ -378,12 +376,16 @@ MeekHTTPHelper.RequestReader.prototype = {
         if (this.bytesToRead > 0)
             return;
 
-        this.state = this.STATE_DONE;
         let converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
             .createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
         converter.charset = "UTF-8";
         let s = converter.convertFromByteArray(this.buf, this.buf.length);
-        let req = JSON.parse(s);
+        req = JSON.parse(s);
+
+        this.state = this.STATE_DONE;
+        this.buf = null;
+        this.bytesToRead = 0;
+
         MeekHTTPHelper.refreshDeadline(this.transport, null);
         this.callback(req);
     },



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits