From d1a72435d5bd3528f3c334cd4d1da16dcead47bf Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Sat, 13 Oct 2007 23:55:20 +0000 Subject: New release. --- src/http.lua | 28 +++++++++++++++++++++++----- src/usocket.c | 2 +- 2 files changed, 24 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/http.lua b/src/http.lua index 9d739a4..3a386a6 100644 --- a/src/http.lua +++ b/src/http.lua @@ -142,7 +142,12 @@ function metat.__index:sendbody(headers, source, step) end function metat.__index:receivestatusline() - local status = self.try(self.c:receive()) + local status = self.try(self.c:receive(5)) + -- identify HTTP/0.9 responses, which do not contain a status line + -- this is just a heuristic, but is what the RFC recommends + if status ~= "HTTP/" then return nil, status end + -- otherwise proceed reading a status line + status = self.try(self.c:receive("*l", status)) local code = socket.skip(2, string.find(status, "HTTP/%d*%.%d* (%d%d%d)")) return self.try(base.tonumber(code), status) end @@ -163,6 +168,12 @@ function metat.__index:receivebody(headers, sink, step) sink, step)) end +function metat.__index:receive09body(status, sink, step) + local source = ltn12.source.rewind(socket.source("until-closed", self.c)) + source(status) + return self.try(ltn12.pump.all(source, sink, step)) +end + function metat.__index:close() return self.c:close() end @@ -271,6 +282,7 @@ function tredirect(reqt, location) create = reqt.create } -- pass location header back as a hint we redirected + headers = headers or {} headers.location = headers.location or location return result, code, headers, status end @@ -283,17 +295,23 @@ function trequest(reqt) -- send request line and headers h:sendrequestline(nreqt.method, nreqt.uri) h:sendheaders(nreqt.headers) - local code = 100 - local headers, status - -- if there is a body, check for server status + -- if there is a body, send it if nreqt.source then h:sendbody(nreqt.headers, nreqt.source, nreqt.step) end + local code, status = h:receivestatusline() + -- if it is an HTTP/0.9 server, simply get the body and we are done + if not code then + h:receive09body(status, nreqt.sink, nreqt.step) + return 1, 200 + end + local headers -- ignore any 100-continue messages while code == 100 do - code, status = h:receivestatusline() headers = h:receiveheaders() + code, status = h:receivestatusline() end + headers = h:receiveheaders() -- at this point we should have a honest reply from the server -- we can't redirect if we already used the source, so we report the error if shouldredirect(nreqt, code, headers) and not nreqt.source then diff --git a/src/usocket.c b/src/usocket.c index ef275b4..2c30b9a 100644 --- a/src/usocket.c +++ b/src/usocket.c @@ -359,7 +359,7 @@ const char *socket_strerror(int err) { case ECONNREFUSED: return "connection refused"; case ECONNABORTED: return "closed"; case ECONNRESET: return "closed"; - case ETIMEDOUT: return "timedout"; + case ETIMEDOUT: return "timeout"; default: return strerror(errno); } } -- cgit v1.2.3-55-g6feb