comparison mod_auth_http_async/mod_auth_http_async.lua @ 2630:96eb1c4f9ff7

mod_auth_http_async: Use "net.http" for async case.
author JC Brand <jc@opkode.com>
date Tue, 21 Mar 2017 09:31:13 +0000
parents a11568bfaf4c
children 1d139e33c502
comparison
equal deleted inserted replaced
2629:a11568bfaf4c 2630:96eb1c4f9ff7
6 -- This project is MIT/X11 licensed. Please see the 6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information. 7 -- COPYING file in the source package for more information.
8 -- 8 --
9 9
10 local new_sasl = require "util.sasl".new; 10 local new_sasl = require "util.sasl".new;
11 local http = require "socket.http";
12 local https = require "ssl.https";
13 local base64 = require "util.encodings".base64.encode; 11 local base64 = require "util.encodings".base64.encode;
14 local have_async, async = pcall(require, "util.async"); 12 local have_async, async = pcall(require, "util.async");
15 13
16 local log = module._log; 14 local log = module._log;
17 local host = module.host; 15 local host = module.host;
28 if rawget(_G, "base_parsed") == nil then 26 if rawget(_G, "base_parsed") == nil then
29 rawset(_G, "base_parsed", false) 27 rawset(_G, "base_parsed", false)
30 end 28 end
31 29
32 local function async_http_auth(url, username, password) 30 local function async_http_auth(url, username, password)
31 local http = require "net.http";
33 local wait, done = async.waiter(); 32 local wait, done = async.waiter();
34 local content, code, request, response; 33 local content, code, request, response;
35 local ex = { 34 local ex = {
36 headers = { Authorization = "Basic "..base64(username..":"..password); }; 35 headers = { Authorization = "Basic "..base64(username..":"..password); };
37 } 36 }
49 end 48 end
50 return nil, "Auth failed. Invalid username or password."; 49 return nil, "Auth failed. Invalid username or password.";
51 end 50 end
52 51
53 local function sync_http_auth(url) 52 local function sync_http_auth(url)
53 local http = require "socket.http";
54 local https = require "ssl.https";
54 local request; 55 local request;
55 if string.sub(url, 1, string.len('https')) == 'https' then 56 if string.sub(url, 1, string.len('https')) == 'https' then
56 request = https.request; 57 request = https.request;
57 else 58 else
58 request = http.request; 59 request = http.request;