util.httpstream: Fixed a possible string to number comparison error.
[prosody.git] / util / httpstream.lua
index c96abe22f0a8999375f8e63844008e69b00f991a..b567712811b56f0ffe9a254856f916a7d1998668 100644 (file)
@@ -67,20 +67,28 @@ local function parser(success_cb, parser_type, options_cb)
                        -- read status line
                        local status_line = readline();
                        local httpversion, status_code, reason_phrase = status_line:match("^HTTP/(%S+)%s+(%d%d%d)%s+(.*)$");
-                       if not httpversion then coroutine.yield("invalid-status-line"); end
+                       status_code = tonumber(status_code);
+                       if not status_code then coroutine.yield("invalid-status-line"); end
                        local headers = readheaders();
                        
                        -- read body
+                       local have_body = not
+                                ( (options_cb and options_cb().method == "HEAD")
+                               or (status_code == 204 or status_code == 304 or status_code == 301)
+                               or (status_code >= 100 and status_code < 200) );
+                       
                        local body;
-                       local len = tonumber(headers["content-length"]);
-                       if len then -- TODO check for invalid len
-                               body = readlength(len);
-                       else -- read to end
-                               repeat
-                                       local newdata = coroutine.yield();
-                                       data = data..newdata;
-                               until newdata == "";
-                               body, data = data, "";
+                       if have_body then
+                               local len = tonumber(headers["content-length"]);
+                               if len then -- TODO check for invalid len
+                                       body = readlength(len);
+                               else -- read to end
+                                       repeat
+                                               local newdata = coroutine.yield();
+                                               data = data..newdata;
+                                       until newdata == "";
+                                       body, data = data, "";
+                               end
                        end
                        
                        success_cb({