[Reconnoiter-devel] Lua Fixes

Dan Di Spaltro dan.dispaltro at gmail.com
Wed Sep 23 13:19:51 EDT 2009


Team,

I made a couple "fixes" but they need some Theo feedback.

* The timeout event log's the results yet there is no
status,available, and state set.  I added three lines to that
* I added a bit more to the http parser to match on worse user input.
And to also match on port.
* I changed the status to err or "unknown error" from str since that
seems to be the desired effect.
* I made the consume callback get called no matter the body as long as
the request is okay at this point.
* I made it display the error in the status field as the actual error
text not the path + line_no, by going up the stack twice
          err = lua_tostring(ci->coro_state, base - 2);

I would love some feedback,

-- 
Dan Di Spaltro
-------------- next part --------------
Index: src/modules/lua.c
===================================================================
--- src/modules/lua.c	(revision 866)
+++ src/modules/lua.c	(working copy)
@@ -619,7 +619,8 @@
       if(base>0) {
         if(lua_isstring(ci->coro_state, base)) {
           const char *err;
-          err = lua_tostring(ci->coro_state, base);
+          noitL(nlerr, "lua err: %s\n", lua_tostring(ci->coro_state, base));
+          err = lua_tostring(ci->coro_state, base - 2);
           if(err) {
             free(ci->current.status);
             ci->current.status = strdup(err);
@@ -661,6 +662,9 @@
      */
     cancel_coro(ci);
   }
+  ci->current.status = strdup("timeout");
+  ci->current.available = NP_UNAVAILABLE;
+  ci->current.state = NP_BAD;
 
   noit_lua_log_results(self, check);
   noit_lua_module_cleanup(self, check);
Index: src/modules-lua/noit/module/http.lua
===================================================================
--- src/modules-lua/noit/module/http.lua	(revision 866)
+++ src/modules-lua/noit/module/http.lua	(working copy)
@@ -112,8 +112,7 @@
 
 function initiate(module, check)
     local url = check.config.url or 'http:///'
-    local schema, host, uri = string.match(url, "^(https?)://([^/]*)(.+)$");
-    local port
+    local schema, host, port, uri = string.match(url, "^(https?)://([^:/]*):?([0-9]*)(/?.*)$");
     local use_ssl = false
     local codere = noit.pcre(check.config.code or '^200$')
     local good = false
@@ -124,14 +123,19 @@
         schema = 'http'
         uri = '/'
     end
-    if schema == 'http' then
-        port = check.config.port or 80
-    elseif schema == 'https' then
-        port = check.config.port or 443
-        use_ssl = true
-    else
-        error(schema .. " not supported")
-    end 
+    if uri == '' then
+        uri = '/'
+    end
+    if port == '' then
+        if schema == 'http' then
+            port = check.config.port or 80
+        elseif schema == 'https' then
+            port = check.config.port or 443
+            use_ssl = true
+        else
+            error(schema .. " not supported")
+        end 
+    end
 
     local output = ''
     local connecttime, firstbytetime
@@ -160,7 +164,7 @@
     if rv ~= 0 then
         check.bad()
         check.unavailable()
-        check.status(str or "unknown error")
+        check.status(err or "unknown error")
         return
     end
 
Index: src/modules-lua/noit/HttpClient.lua
===================================================================
--- src/modules-lua/noit/HttpClient.lua	(revision 866)
+++ src/modules-lua/noit/HttpClient.lua	(working copy)
@@ -14,11 +14,11 @@
     self.target = target
     self.port = port
     local rv, err = self.e:connect(self.target, self.port)
-    if self.hooks.connected ~= nil then self.hooks.connected() end
-    if ssl == false then return rv, err end
     if rv ~= 0 then
         return rv, err
     end
+    if self.hooks.connected ~= nil then self.hooks.connected() end
+    if ssl == false then return rv, err end
     return self.e:ssl_upgrade_socket(self.hooks.certfile and self.hooks.certfile(),
                                      self.hooks.keyfile and self.hooks.keyfile(),
                                      self.hooks.cachain and self.hooks.cachain(),
@@ -83,9 +83,13 @@
             self.raw_bytes = self.raw_bytes + string.len(str)
         end
     until str == nil or string.len(str) ~= len
-    local decoded = content_enc_func(str)
-    self.content_bytes = self.content_bytes + string.len(decoded)
-    if self.hooks.consume ~= nil then self.hooks.consume(decoded) end
+    if str ~= nil then
+      local decoded = content_enc_func(str)
+      self.content_bytes = self.content_bytes + string.len(decoded)
+      if self.hooks.consume ~= nil then self.hooks.consume(decoded) end
+    else
+      if self.hooks.consume ~= nil then self.hooks.consume('') end
+    end
 end
 
 function te_length(self, content_enc_func)
@@ -109,7 +113,10 @@
         local hexlen = string.match(str, "^([0-9a-fA-F]+)")
         if hexlen == nil then error("bad chunk length: " .. str) end
         local len = tonumber(hexlen, 16)
-        if len == 0 then break end
+        if len == 0 then 
+          if self.hooks.consume ~= nil then self.hooks.consume("") end
+          break 
+        end
         str = self.e:read(len)
         if string.len(str or "") ~= len then error("short chunked read") end
         self.raw_bytes = self.raw_bytes + string.len(str)


More information about the Reconnoiter-devel mailing list