lua https request 调用
网上资料
引用ssl.https 包
local https = require("ssl.https")
之后按同http一样调用。
但是,这种只最基本的实现了访问https服务的要求,却没有验证数字证书的示例说明。
数字证书的调用
类似
wget --private-key /root/client.key --certificate /root/client.crt --ca-certificate /root/ca.crt https://www.test.com -O wgetssl curl --key /root/client.key --cert /root/client.crt --cacert /root/ca.crt https://www.test.com
必须要传入证书文件
再搜 资料很少
最有用的是
http://notebook.kulchenko.com/programming/https-ssl-calls-with-lua-and-luasec
讲的是socket 建立连接,和https差了一层。
就差一步了……
再也没找到任何资料。
所以,查lua包源码
https的部分内容
local ssl = require("ssl")
function request(url, body)
local result_table = {}
local stringrequest = type(url) == "string"
if stringrequest then
url = urlstring_totable(url, body, result_table)
else
url.url = default_https_port(url.url)
end
if http.PROXY or url.proxy then
return nil, "proxy not supported"
elseif url.redirect then
return nil, "redirect not supported"
elseif url.create then
return nil, "create function not permitted"
end
-- New 'create' function to establish a secure connection
url.create = tcp(url)
local res, code, headers, status = http.request(url)
if res and stringrequest then
return table.concat(result_table), code, headers, status
end
return res, code, headers, status
end
-- Return a function which performs the SSL/TLS connection.
local function tcp(params)
params = params or {}
-- Default settings
for k, v in pairs(cfg) do
params[k] = params[k] or v
end
-- Force client mode
params.mode = "client"
-- 'create' function for LuaSocket
return function ()
local conn = {}
conn.sock = try(socket.tcp())
local st = getmetatable(conn.sock).__index.settimeout
function conn:settimeout(...)
return st(self.sock, ...)
end
-- Replace TCP's connection function
function conn:connect(host, port)
try(self.sock:connect(host, port))
self.sock = try(ssl.wrap(self.sock, params))
try(self.sock:dohandshake())
reg(self, getmetatable(self.sock))
return
end
return conn
end
end
https.request
url.create = tcp(url)
会调用tcp函数。
params = params or {}
-- Default settings
for k, v in pairs(cfg) do
params[k] = params[k] or v
end
self.sock = try(ssl.wrap(self.sock, params))
而tcp函数又用requset传入的参数创建名为 params的table类对象,传入params调用ssl.warp函数
好吧,再去ssl函数看源码
function newcontext(cfg)
local succ, msg, ctx
-- Create the context
ctx, msg = context.create(cfg.protocol)
if not ctx then return nil, msg end
-- Mode
succ, msg = context.setmode(ctx, cfg.mode)
if not succ then return nil, msg end
-- Load the key
if cfg.key then
succ, msg = context.loadkey(ctx, cfg.key, cfg.password)
if not succ then return nil, msg end
end
-- Load the certificate
if cfg.certificate then
succ, msg = context.loadcert(ctx, cfg.certificate)
if not succ then return nil, msg end
end
-- Load the CA certificates
if cfg.cafile or cfg.capath then
succ, msg = context.locations(ctx, cfg.cafile, cfg.capath)
if not succ then return nil, msg end
end
-- Set the verification options
succ, msg = optexec(context.setverify, cfg.verify, ctx)
if not succ then return nil, msg end
-- Set SSL options
succ, msg = optexec(context.setoptions, cfg.options, ctx)
if not succ then return nil, msg end
-- Set the depth for certificate verification
if cfg.depth then
succ, msg = context.setdepth(ctx, cfg.depth)
if not succ then return nil, msg end
end
return ctx
end
--
--
--
function wrap(sock, cfg)
local ctx, msg
if type(cfg) == "table" then
ctx, msg = newcontext(cfg)
if not ctx then return nil, msg end
else
ctx = cfg
end
local s, msg = core.create(ctx)
if s then
core.setfd(s, sock:getfd())
sock:setfd(core.invalidfd)
return s
end
return nil, msg
end
眼前一亮,看到熟悉的证书参数了,key,password,ca...
懂的看代码就该如何作了。
首先wrap调用newcontext
而newcontext应用 之前request传入的参数
那把key,password,ca等,写入https.request就全通了。
写demo
测试通过。
不传证书
#!/usr/bin/lua
require("socket")
local https = require("ssl.https") local one, code, headers, status = https.request{
url = "https://www.test.com"
}
print(code)
print(header)
print(status)
print(one)
结果为
root@LeWiFi:~# lua luahttps.test
nil
nil
nil
nil
传入证书
#!/usr/bin/lua
require("socket")
local https = require("ssl.https")
local one, code, headers, status = https.request{
url = "https://www.test.com",
key = "/root/client.key",
certificate="/root/client.crt",
cafile="/root/ca.crt"
}
print(code)
print(header)
print(status)
print(one)
结果
root@LeWiFi:~# lua luahttps.test nil
HTTP/1.1 OK
success
lua https request 调用的更多相关文章
- tengine lua 开源一 调用内部接口高效发送文件
tengine lua 开源一 调用内部接口高效发送文件 开源自己封装的sendfile 模块,可以高效的通过lua发送文件 源码地址:https://github.com/weinyzhou/Lu ...
- C程序与Lua脚本相互调用
Lua脚本是一种可用于C程序开发/测试的工具,本篇介绍一下C程序与Lua脚本如何进行相互调用,更加详细的操作参见<Programing in Lua>.本文分为3个部分:1.Windows ...
- 使用soapUI代替WSDL2JAVA生成cxf HTTPS 客户端调用代码
如果直接用cxf下面的wsdl2java生成https服务调用代码,会报https证书的错误.在你不想导入证书的情况下,可以使用soapUI进行客户端代码的生成,步骤如下: 1.设置CXF,如下图: ...
- InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised.解决办法
最近使用requests进行get请求的时候,控制台输出如下错误. InsecureRequestWarning: Unverified HTTPS request is being made. Ad ...
- ubuntu配置lua环境,并进行c与lua的相互调用
1.安装lua环境 先查看一下apt可获取的lua版本 我们选择lua5.1版本进行安装 sudo apt install lua5.1 安装完之后测试一下是否安装成功,如果可以正常使用,则lua环境 ...
- InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings In
InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is s ...
- pytest文档46-关于https请求警告问题(InsecureRequestWarning: Unverified HTTPS request is being made)
前言 使用 pytest 执行 https 请求用例的时候,控制台会出现警告:InsecureRequestWarning: Unverified HTTPS request is being mad ...
- (原)lua使用ffi调用c程序的函数
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5812763.html 参考网址: http://luajit.freelists.narkive.co ...
- openresty+lua做接口调用权限限制
说明:openresty可以理解为一个服务器它将nginx的核心包含了过来,并结合lua脚本语言实现一些对性能要求高的功能,该篇文章介绍了使用openresty 1.purview.lua --调用j ...
随机推荐
- 深入分析Java反射(八)-优化反射调用性能
Java反射的API在JavaSE1.7的时候已经基本完善,但是本文编写的时候使用的是Oracle JDK11,因为JDK11对于sun包下的源码也上传了,可以直接通过IDE查看对应的源码和进行Deb ...
- 操作实践,IDEA自定义toString()方法模板
声明:迁移自本人CSDN博客https://blog.csdn.net/u013365635 Java POJO在日志中常会用于打印,经常会将POJO的内容全部或部分打印出来,所以POJO类的toSt ...
- Spring学习之Aspectj开发实现AOP
Aspectj是一个基于Java语言的Aop框架,它提供了强大的Aop功能. Aspectj简介: 1.Aspectj是一个面向切面的框架,它扩展了Java语言,它定义了一个Aop语法. 2.所以它有 ...
- UML-为什么要画领域模型?
不熟悉业务的情况下, 1).找到关键概念和词汇 2).概念间的关系 熟悉业务的情况下, 1).梳理思路 完美不是目的. 另外,减少标示差异.如下图
- python-day8爬虫基础之数据存储
数据存储,在爬虫中也是十分的重要,因为我们要把我们想要的数据保存到本地,其中最简单直接的就是保存为文件文本,比如:TXT.JSON.CSV等等,除此之外,我们还可以将其保存到数据库中,常见的数据库类型 ...
- JS导出、导入EXCEL(案例)
插件下载地址:http://oss.sheetjs.com/js-xlsx/xlsx.full.min.js 1.导出excel <!DOCTYPE html> <html> ...
- lower_bound()和upper_bound()用法详解
lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. lower_bound( begin,end,num):从数组的begin位置到end ...
- python 知识点补充
python 知识点补充 简明 python 教程 r 或 R 来指定一个 原始(Raw) 字符串 Python 是强(Strongly)面向对象的,因为所有的一切都是对象, 包括数字.字符串与 函数 ...
- Python笔记_第二篇_面向过程_第二部分_1.函数
函数:这个词属于一个数学概念,在编程语言借鉴了这个概念,表现形式是一段程序代码的组合,也叫“程序集”.有过编程基础的人很容易理解这个概念,当我们编写程序越来越多的时候,程序设计大师们会把散乱的程序进行 ...
- LibraryBuilder——从元器件datasheet到Library
LibraryBuilder是Cadence推出的元件库管理工具,可以从PDF自动创建器件的原理图符号及PCB封装. 软件可以从“吴川斌的博客”下载到. 这里大致记录一下创建元件库的过程,以Beagl ...