Software / code / prosody
Comparison
net/http.lua @ 617:b17fd66b7e10
HTTP request callbacks now: handler(code, content) (where code may be 0, and content an error message)
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Fri, 12 Dec 2008 04:16:35 +0000 |
| parent | 616:69bc5782b25e |
| child | 618:69de678792d6 |
comparison
equal
deleted
inserted
replaced
| 616:69bc5782b25e | 617:b17fd66b7e10 |
|---|---|
| 25 end | 25 end |
| 26 | 26 |
| 27 local function request_reader(request, data, startpos) | 27 local function request_reader(request, data, startpos) |
| 28 if not data then | 28 if not data then |
| 29 if request.body then | 29 if request.body then |
| 30 request.callback(t_concat(request.body)); | 30 request.callback(request.code, t_concat(request.body)); |
| 31 else | 31 else |
| 32 -- Error.. connection was closed prematurely | 32 -- Error.. connection was closed prematurely |
| 33 request.callback(nil, "connection-closed"); | 33 request.callback(0, "connection-closed"); |
| 34 end | 34 end |
| 35 destroy_request(request); | 35 destroy_request(request); |
| 36 return; | 36 return; |
| 37 end | 37 end |
| 38 if request.state == "body" then | 38 if request.state == "body" then |
| 45 if request.bodylength then | 45 if request.bodylength then |
| 46 request.havebodylength = request.havebodylength + #data; | 46 request.havebodylength = request.havebodylength + #data; |
| 47 if request.havebodylength >= request.bodylength then | 47 if request.havebodylength >= request.bodylength then |
| 48 -- We have the body | 48 -- We have the body |
| 49 if request.callback then | 49 if request.callback then |
| 50 request.callback(t_concat(request.body)); | 50 request.callback(request.code, t_concat(request.body)); |
| 51 end | 51 end |
| 52 end | 52 end |
| 53 print("", "Have "..request.havebodylength.." bytes out of "..request.bodylength); | 53 print("", "Have "..request.havebodylength.." bytes out of "..request.bodylength); |
| 54 end | 54 end |
| 55 elseif request.state == "headers" then | 55 elseif request.state == "headers" then |
| 76 end | 76 end |
| 77 elseif request.state == "status" then | 77 elseif request.state == "status" then |
| 78 print("Reading status...") | 78 print("Reading status...") |
| 79 local http, code, text, linelen = data:match("^HTTP/(%S+) (%d+) (.-)\r\n()", startpos); | 79 local http, code, text, linelen = data:match("^HTTP/(%S+) (%d+) (.-)\r\n()", startpos); |
| 80 if not code then | 80 if not code then |
| 81 return request.callback(nil, "invalid-status-line", data); | 81 return request.callback(0, "invalid-status-line"); |
| 82 end | 82 end |
| 83 | 83 |
| 84 request.responsecode, request.responseversion = code, http; | 84 request.responsecode, request.responseversion = code, http; |
| 85 | 85 |
| 86 if request.onlystatus or not expectbody(request, tonumber(code)) then | 86 if request.onlystatus or not expectbody(request, tonumber(code)) then |
| 87 if request.callback then | 87 if request.callback then |
| 88 request.callback(code); | 88 request.callback(code, nil); |
| 89 end | 89 end |
| 90 destroy_request(request); | 90 destroy_request(request); |
| 91 return; | 91 return; |
| 92 end | 92 end |
| 93 | 93 |