1. Jeremy Sandell
  2. http_parser_binding

Commits

Jeremy Sandell  committed d506977 Draft

split up parsing and connecting logic

  • Participants
  • Parent commits 719da09
  • Branches default

Comments (0)

Files changed (1)

File parse.d

View file
  • Ignore whitespace
     auth.encoded = b64encode(format("%s:%s", auth.username, auth.password));
 
     string user_agent = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11";
-    auto parser = new Parser;
-    auto address = new InternetAddress(c.domain, c.port);
+
 
     bool connected = false;
-
-    scope(exit)
-    {
-        delete address;
-        delete parser;
-    }
+    bool parsed = false;
+    auto response = appender!string();
 
     try
     {
             writefln("url is %s", c.url);
         }
 
+        auto address = new InternetAddress(c.domain, c.port);
+
+        scope(exit)
+        {
+            delete address;
+        }
+
         auto sock = new TcpSocket(address);
         scope(exit) sock.close();
 
-        connected=true;
-
-
+        connected = true;
         debug(connection) writefln("connected!");
 
         // send the request
                                 c.url, c.domain, auth.encoded, user_agent);
 
 
-        auto response = appender!string();
 
         debug(connection) writefln("sending:\n%s", request);
 
         string receive_buffer;
         receive_buffer.length = 8192;
 
-
         ptrdiff_t r;
 
         while ((r = sock.receive(cast(void[])receive_buffer)) > 0)
             response.put(receive_buffer[0 .. r]);
         }
 
+    }
+    catch(Exception e)
+    {
+        writefln("connection failed: %s", e.msg);
+    }
+
+
+    try
+    {
+        auto parser = new Parser;
 
         // get the header
         parser.parse(response.data);
 
+        scope(exit)
+        {
+            delete parser;
+        }
+
+        parsed = true;
+
         debug(summary)
         {
             writefln("content_length: %s", parser.content_length);
 
         if (parser.code != 200)
             writefln(response.data);
-
     }
     catch(Exception e)
     {
-        writefln("connection failed: %s", e.msg);
+        writefln("parsing failed: %s", e.msg);
     }
 
-    return connected ? EXIT_SUCCESS : EXIT_FAILURE;
+    return (connected && parsed) ? EXIT_SUCCESS : EXIT_FAILURE;
 }