fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
7e0ab3b
v2.1.5
1---------------------------------------------------------------------------- 2-- LuaSec 1.2.0 3-- Copyright (C) 2009-2022 PUC-Rio 4-- 5-- Author: Pablo Musa 6-- Author: Tomas Guisasola 7--------------------------------------------------------------------------- 8 9local socket =require ( "socket" ) 10local ssl =require ( "ssl" ) 11local ltn12 =require ( "ltn12" ) 12local http =require ( "socket.http" ) 13local url =require ( "socket.url" ) 14 15local try =socket . try 16 17-- 18-- Module 19-- 20local _M ={ 21_VERSION ="1.2.0" , 22_COPYRIGHT ="LuaSec 1.2.0 - Copyright (C) 2009-2022 PUC-Rio" , 23PORT =443 , 24TIMEOUT =60 25} 26 27-- TLS configuration 28local cfg ={ 29protocol ="any" , 30options ={ "all" , "no_sslv2" , "no_sslv3" , "no_tlsv1" } , 31verify ="none" , 32} 33 34-------------------------------------------------------------------- 35-- Auxiliar Functions 36-------------------------------------------------------------------- 37 38-- Insert default HTTPS port. 39local function default_https_port ( u ) 40return url . build ( url . parse ( u , { port =_M . PORT } )) 41end 42 43-- Convert an URL to a table according to Luasocket needs. 44local function urlstring_totable ( url , body , result_table ) 45url ={ 46url =default_https_port ( url ), 47method =body and "POST" or "GET" , 48sink =ltn12 . sink . table ( result_table ) 49} 50if body then 51url . source =ltn12 . source . string ( body ) 52url . headers ={ 53[ "content-length" ] =# body , 54[ "content-type" ] ="application/json" , 55} 56end 57return url 58end 59 60-- Forward calls to the real connection object. 61local function reg ( conn ) 62local mt =getmetatable ( conn . sock ). __index 63for name , method in pairs ( mt ) do 64if type ( method ) == "function" then 65conn [ name ] =function ( self , ... ) 66return method ( self . sock , ... ) 67end 68end 69end 70end 71 72-- Return a function which performs the SSL/TLS connection. 73local function tcp ( params ) 74params =params or {} 75-- Default settings 76for k , v in pairs ( cfg ) do 77params [ k ] =params [ k ] or v 78end 79-- Force client mode 80params . mode ="client" 81-- 'create' function for LuaSocket 82return function () 83local conn ={} 84conn . sock =try ( socket . tcp ()) 85local st =getmetatable ( conn . sock ). __index . settimeout 86function conn : settimeout ( ... ) 87return st ( self . sock , _M . TIMEOUT ) 88end 89-- Replace TCP's connection function 90function conn : connect ( host , port ) 91try ( self . sock : connect ( host , port )) 92self . sock =try ( ssl . wrap ( self . sock , params )) 93self . sock : sni ( host ) 94self . sock : settimeout ( _M . TIMEOUT ) 95try ( self . sock : dohandshake ()) 96reg ( self ) 97return 1 98end 99return conn 100end 101end 102 103-------------------------------------------------------------------- 104-- Main Function 105-------------------------------------------------------------------- 106 107-- Make a HTTP request over secure connection. This function receives 108-- the same parameters of LuaSocket's HTTP module (except 'proxy' and 109-- 'redirect') plus LuaSec parameters. 110-- 111-- @param url mandatory (string or table) 112-- @param body optional (string) 113-- @return (string if url == string or 1), code, headers, status 114-- 115local function request ( url , body ) 116local result_table ={} 117local stringrequest =type ( url ) == "string" 118if stringrequest then 119url =urlstring_totable ( url , body , result_table ) 120else 121url . url =default_https_port ( url . url ) 122end 123if http . PROXY or url . proxy then 124return nil , "proxy not supported" 125elseif url . redirect then 126return nil , "redirect not supported" 127elseif url . create then 128return nil , "create function not permitted" 129end 130-- New 'create' function to establish a secure connection 131url . create =tcp ( url ) 132local res , code , headers , status =http . request ( url ) 133if res and stringrequest then 134return table . concat ( result_table ), code , headers , status 135end 136return res , code , headers , status 137end 138 139-------------------------------------------------------------------------------- 140-- Export module 141-- 142 143_M . request =request 144_M . tcp =tcp 145 146return _M