Software /
code /
prosody
Comparison
net/http.lua @ 5776:bd0ff8ae98a8
Remove all trailing whitespace
author | Florian Zeitz <florob@babelmonkeys.de> |
---|---|
date | Fri, 09 Aug 2013 17:48:21 +0200 |
parent | 5714:520671c3159c |
child | 5950:bd1d1c29a7e7 |
comparison
equal
deleted
inserted
replaced
5775:a6c2b8933507 | 5776:bd0ff8ae98a8 |
---|---|
1 -- Prosody IM | 1 -- Prosody IM |
2 -- Copyright (C) 2008-2010 Matthew Wild | 2 -- Copyright (C) 2008-2010 Matthew Wild |
3 -- Copyright (C) 2008-2010 Waqas Hussain | 3 -- Copyright (C) 2008-2010 Waqas Hussain |
4 -- | 4 -- |
5 -- This project is MIT/X11 licensed. Please see the | 5 -- This project is MIT/X11 licensed. Please see the |
6 -- COPYING file in the source package for more information. | 6 -- COPYING file in the source package for more information. |
7 -- | 7 -- |
8 | 8 |
9 local socket = require "socket" | 9 local socket = require "socket" |
34 -- Send the request | 34 -- Send the request |
35 local request_line = { req.method or "GET", " ", req.path, " HTTP/1.1\r\n" }; | 35 local request_line = { req.method or "GET", " ", req.path, " HTTP/1.1\r\n" }; |
36 if req.query then | 36 if req.query then |
37 t_insert(request_line, 4, "?"..req.query); | 37 t_insert(request_line, 4, "?"..req.query); |
38 end | 38 end |
39 | 39 |
40 conn:write(t_concat(request_line)); | 40 conn:write(t_concat(request_line)); |
41 local t = { [2] = ": ", [4] = "\r\n" }; | 41 local t = { [2] = ": ", [4] = "\r\n" }; |
42 for k, v in pairs(req.headers) do | 42 for k, v in pairs(req.headers) do |
43 t[1], t[3] = k, v; | 43 t[1], t[3] = k, v; |
44 conn:write(t_concat(t)); | 44 conn:write(t_concat(t)); |
45 end | 45 end |
46 conn:write("\r\n"); | 46 conn:write("\r\n"); |
47 | 47 |
48 if req.body then | 48 if req.body then |
49 conn:write(req.body); | 49 conn:write(req.body); |
50 end | 50 end |
51 end | 51 end |
52 | 52 |
78 request.callback(reason or "connection-closed", 0, request); | 78 request.callback(reason or "connection-closed", 0, request); |
79 request.callback = nil; | 79 request.callback = nil; |
80 end | 80 end |
81 destroy_request(request); | 81 destroy_request(request); |
82 end | 82 end |
83 | 83 |
84 if not data then | 84 if not data then |
85 error_cb(err); | 85 error_cb(err); |
86 return; | 86 return; |
87 end | 87 end |
88 | 88 |
89 local function success_cb(r) | 89 local function success_cb(r) |
90 if request.callback then | 90 if request.callback then |
91 request.callback(r.body, r.code, r, request); | 91 request.callback(r.body, r.code, r, request); |
92 request.callback = nil; | 92 request.callback = nil; |
93 end | 93 end |
102 end | 102 end |
103 | 103 |
104 local function handleerr(err) log("error", "Traceback[http]: %s", traceback(tostring(err), 2)); end | 104 local function handleerr(err) log("error", "Traceback[http]: %s", traceback(tostring(err), 2)); end |
105 function request(u, ex, callback) | 105 function request(u, ex, callback) |
106 local req = url.parse(u); | 106 local req = url.parse(u); |
107 | 107 |
108 if not (req and req.host) then | 108 if not (req and req.host) then |
109 callback(nil, 0, req); | 109 callback(nil, 0, req); |
110 return nil, "invalid-url"; | 110 return nil, "invalid-url"; |
111 end | 111 end |
112 | 112 |
113 if not req.path then | 113 if not req.path then |
114 req.path = "/"; | 114 req.path = "/"; |
115 end | 115 end |
116 | 116 |
117 local method, headers, body; | 117 local method, headers, body; |
118 | 118 |
119 local host, port = req.host, req.port; | 119 local host, port = req.host, req.port; |
120 local host_header = host; | 120 local host_header = host; |
121 if (port == "80" and req.scheme == "http") | 121 if (port == "80" and req.scheme == "http") |
122 or (port == "443" and req.scheme == "https") then | 122 or (port == "443" and req.scheme == "https") then |
123 port = nil; | 123 port = nil; |
127 | 127 |
128 headers = { | 128 headers = { |
129 ["Host"] = host_header; | 129 ["Host"] = host_header; |
130 ["User-Agent"] = "Prosody XMPP Server"; | 130 ["User-Agent"] = "Prosody XMPP Server"; |
131 }; | 131 }; |
132 | 132 |
133 if req.userinfo then | 133 if req.userinfo then |
134 headers["Authorization"] = "Basic "..b64(req.userinfo); | 134 headers["Authorization"] = "Basic "..b64(req.userinfo); |
135 end | 135 end |
136 | 136 |
137 if ex then | 137 if ex then |
147 for k, v in pairs(ex.headers) do | 147 for k, v in pairs(ex.headers) do |
148 headers[k] = v; | 148 headers[k] = v; |
149 end | 149 end |
150 end | 150 end |
151 end | 151 end |
152 | 152 |
153 -- Attach to request object | 153 -- Attach to request object |
154 req.method, req.headers, req.body = method, headers, body; | 154 req.method, req.headers, req.body = method, headers, body; |
155 | 155 |
156 local using_https = req.scheme == "https"; | 156 local using_https = req.scheme == "https"; |
157 if using_https and not ssl_available then | 157 if using_https and not ssl_available then |
158 error("SSL not available, unable to contact https URL"); | 158 error("SSL not available, unable to contact https URL"); |
159 end | 159 end |
160 local port_number = port and tonumber(port) or (using_https and 443 or 80); | 160 local port_number = port and tonumber(port) or (using_https and 443 or 80); |
161 | 161 |
162 -- Connect the socket, and wrap it with net.server | 162 -- Connect the socket, and wrap it with net.server |
163 local conn = socket.tcp(); | 163 local conn = socket.tcp(); |
164 conn:settimeout(10); | 164 conn:settimeout(10); |
165 local ok, err = conn:connect(host, port_number); | 165 local ok, err = conn:connect(host, port_number); |
166 if not ok and err ~= "timeout" then | 166 if not ok and err ~= "timeout" then |
167 callback(nil, 0, req); | 167 callback(nil, 0, req); |
168 return nil, err; | 168 return nil, err; |
169 end | 169 end |
170 | 170 |
171 local sslctx = false; | 171 local sslctx = false; |
172 if using_https then | 172 if using_https then |
173 sslctx = ex and ex.sslctx or { mode = "client", protocol = "sslv23", options = { "no_sslv2" } }; | 173 sslctx = ex and ex.sslctx or { mode = "client", protocol = "sslv23", options = { "no_sslv2" } }; |
174 end | 174 end |
175 | 175 |
176 req.handler, req.conn = server.wrapclient(conn, host, port_number, listener, "*a", sslctx); | 176 req.handler, req.conn = server.wrapclient(conn, host, port_number, listener, "*a", sslctx); |
177 req.write = function (...) return req.handler:write(...); end | 177 req.write = function (...) return req.handler:write(...); end |
178 | 178 |
179 req.callback = function (content, code, request, response) log("debug", "Calling callback, status %s", code or "---"); return select(2, xpcall(function () return callback(content, code, request, response) end, handleerr)); end | 179 req.callback = function (content, code, request, response) log("debug", "Calling callback, status %s", code or "---"); return select(2, xpcall(function () return callback(content, code, request, response) end, handleerr)); end |
180 req.reader = request_reader; | 180 req.reader = request_reader; |
181 req.state = "status"; | 181 req.state = "status"; |
182 | 182 |
183 requests[req.handler] = req; | 183 requests[req.handler] = req; |