Comparison

net/http.lua @ 6823:3b07f38d70f5

net/http: Use server.addclient
author daurnimator <quae@daurnimator.com>
date Wed, 18 Dec 2013 19:00:24 -0500
parent 6780:647adfd8f738
child 6826:17a4e89a4780
child 7463:3b6e7ce9431f
comparison
equal deleted inserted replaced
6821:5de30376bf98 6823:3b07f38d70f5
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"
10 local b64 = require "util.encodings".base64.encode; 9 local b64 = require "util.encodings".base64.encode;
11 local url = require "socket.url" 10 local url = require "socket.url"
12 local httpstream_new = require "net.http.parser".new; 11 local httpstream_new = require "net.http.parser".new;
13 local util_http = require "util.http"; 12 local util_http = require "util.http";
14 13
169 if using_https and not ssl_available then 168 if using_https and not ssl_available then
170 error("SSL not available, unable to contact https URL"); 169 error("SSL not available, unable to contact https URL");
171 end 170 end
172 local port_number = port and tonumber(port) or (using_https and 443 or 80); 171 local port_number = port and tonumber(port) or (using_https and 443 or 80);
173 172
174 -- Connect the socket, and wrap it with net.server
175 local conn = socket.tcp();
176 conn:settimeout(10);
177 local ok, err = conn:connect(host, port_number);
178 if not ok and err ~= "timeout" then
179 callback(nil, 0, req);
180 return nil, err;
181 end
182
183 local sslctx = false; 173 local sslctx = false;
184 if using_https then 174 if using_https then
185 sslctx = ex and ex.sslctx or { mode = "client", protocol = "sslv23", options = { "no_sslv2", "no_sslv3" } }; 175 sslctx = ex and ex.sslctx or { mode = "client", protocol = "sslv23", options = { "no_sslv2", "no_sslv3" } };
186 end 176 end
187 177
188 req.handler, req.conn = assert(server.wrapclient(conn, host, port_number, listener, "*a", sslctx)); 178 local handler, conn = server.addclient(host, port_number, listener, "*a", sslctx)
179 if not handler then
180 callback(nil, 0, req);
181 return nil, conn;
182 end
183 req.handler, req.conn = handler, conn
189 req.write = function (...) return req.handler:write(...); end 184 req.write = function (...) return req.handler:write(...); end
190 185
191 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 186 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
192 req.reader = request_reader; 187 req.reader = request_reader;
193 req.state = "status"; 188 req.state = "status";