Comparison

net/http.lua @ 5964:ad04170d6533

net/http: Use server.addclient
author daurnimator <quae@daurnimator.com>
date Wed, 18 Dec 2013 19:00:24 -0500
parent 5950:bd1d1c29a7e7
child 6383:ec8878113907
comparison
equal deleted inserted replaced
5963:3bd90108cc3c 5964:ad04170d6533
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
158 if using_https and not ssl_available then 157 if using_https and not ssl_available then
159 error("SSL not available, unable to contact https URL"); 158 error("SSL not available, unable to contact https URL");
160 end 159 end
161 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);
162 161
163 -- Connect the socket, and wrap it with net.server
164 local conn = socket.tcp();
165 conn:settimeout(10);
166 local ok, err = conn:connect(host, port_number);
167 if not ok and err ~= "timeout" then
168 callback(nil, 0, req);
169 return nil, err;
170 end
171
172 local sslctx = false; 162 local sslctx = false;
173 if using_https then 163 if using_https then
174 sslctx = ex and ex.sslctx or { mode = "client", protocol = "sslv23", options = { "no_sslv2" } }; 164 sslctx = ex and ex.sslctx or { mode = "client", protocol = "sslv23", options = { "no_sslv2" } };
175 end 165 end
176 166
177 req.handler, req.conn = assert(server.wrapclient(conn, host, port_number, listener, "*a", sslctx)); 167 local handler, conn = server.addclient(host, port_number, listener, "*a", sslctx)
168 if not handler then
169 callback(nil, 0, req);
170 return nil, conn;
171 end
172 req.handler, req.conn = handler, conn
178 req.write = function (...) return req.handler:write(...); end 173 req.write = function (...) return req.handler:write(...); end
179 174
180 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 175 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
181 req.reader = request_reader; 176 req.reader = request_reader;
182 req.state = "status"; 177 req.state = "status";