/usr/share/lua/5.1/soap/server.lua is in lua-soap 3.0+git91419a7-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | ---------------------------------------------------------------------
-- LuaSOAP support for service development.
-- See Copyright Notice in license.html
-- $Id:$
---------------------------------------------------------------------
local next, type, tostring, pcall, unpack, pairs, ipairs = next, type, tostring, pcall, unpack, pairs, ipairs
local error, assert = error, assert
local table = require"table"
local string = require"string"
local cgilua = cgilua
local soap = require"soap"
local encoding = "iso-8859-1"
local xml_header = '<?xml version="1.0" encoding="'..encoding..'"?>\n'
local __methods
__methods = {
listMethods = function (namespace)
local l = {}
for name in pairs(__methods) do
table.insert(l, { tag = "methodName", name })
end
return soap.encode({method = "listMethodsResponse", entries = l})
end,
}
local __service = {
-- name
-- namespace
-- url
-- soap_action
-- wsdl (opt)
-- disco (opt)
}
---------------------------------------------------------------------
local function respond(resp, header)
cgilua.header("Content-length", string.len(resp))
cgilua.header("Connection", "close")
cgilua.contentheader("text", "xml")
if header then
cgilua.put(header)
end
cgilua.put(resp)
end
---------------------------------------------------------------------
local function builderrorenvelope(faultcode, faultstring, extra)
faultstring = faultstring:gsub("([<>])", { ["<"] = "<", [">"] = ">", })
return soap.encode({
entries = {
{ tag = "faultcode", faultcode, },
{ tag = "faultstring", faultstring, },
extra,
},
method = "soap:Fault",
})
end
---------------------------------------------------------------------
local function decodedata(doc)
local namespace, elem_name, elems = soap.decode(doc)
local func = __methods[elem_name].method
assert(type(func) == "function", "Unavailable method: `"..tostring(elem_name).."'")
return namespace, func, (elems or {})
end
---------------------------------------------------------------------
local function callfunc(func, namespace, arg_table)
local result = { pcall(func, namespace, arg_table) }
local ok = result[1]
if not ok then
result = builderrorenvelope("soap:ServiceError", result[2])
else
table.remove(result, 1)
if #result == 1 then
result = result[1]
end
end
return ok, result
end
---------------------------------------------------------------------
local function wsdl_gen_type_aux(message)
if not next(message, nil) then
return ""
end
local buffer = { string.format([[
<s:element name="%s">
<s:complexType>
<s:sequence>]],
message.name)
}
for _, field in ipairs(message) do
local min, max
if type(field.occurrence) == "table" then
min, max = field.occurrence[1], field.occurrence[2]
elseif type(field.occurrence) == "string" then
min, max = field.occurrence, field.occurrence
else
min, max = 1, 1
end
local _type = field.type or "string"
table.insert(buffer, string.format([[
<s:element minOccurs="%d" maxOccurs="%d" name="%s" type="s:%s" />]],
min, max, field.name, _type))
end
table.insert(buffer, [[
</s:sequence>
</s:complexType>
</s:element>]])
return table.concat(buffer)
end
local function wsdl_gen_type(desc)
return wsdl_gen_type_aux(desc.message) ..
wsdl_gen_type_aux(desc.response)
end
---------------------------------------------------------------------
local function wsdl_gen_message(desc)
local in_params, out_params = "", ""
if desc.message.name then
in_params = '\n <wsdl:part name="parameters" element="tns:'
..desc.message.name..'" />\n '
end
if desc.response.name then
out_params = '\n <wsdl:part name="parameters" element="tns:'
..desc.response.name..'" />\n '
end
return string.format([[
<wsdl:message name="%sSoapIn">%s</wsdl:message>
<wsdl:message name="%sSoapOut">%s</wsdl:message>]],
desc.name, in_params,
desc.name, out_params)
end
---------------------------------------------------------------------
local function wsdl_gen_port_type(desc)
return string.format([[
<wsdl:operation name="%s">
<wsdl:input message="tns:%sSoapIn" />
<wsdl:output message="tns:%sSoapOut" />
</wsdl:operation>]],
desc.name, desc.name, desc.name)
end
---------------------------------------------------------------------
local function wsdl_gen_binding(desc)
local soap_action = ''
if __service.soap_action then
soap_action = 'soapAction="'..__service.soap_action..desc.name..'"'
end
return string.format([[
<wsdl:operation name="%s">
<soap:operation %s style="document" />
<wsdl:input><soap:body use="literal" /></wsdl:input>
<wsdl:output><soap:body use="literal" /></wsdl:output>
</wsdl:operation>]],
desc.name, soap_action)
end
---------------------------------------------------------------------
local function wsdl_gen_http_binding(desc)
return string.format([[
<wsdl:operation name="%s">
<http:operation location="/%s"/>
<wsdl:input><mime:content type="application/x-www-form-urlencoded"/></wsdl:input>
<wsdl:output><mime:mimeXml part="Body"/></wsdl:output>
</wsdl:operation>]],
desc.name, desc.name)
end
---------------------------------------------------------------------
local function generate_wsdl()
local buffer = {
xml_header,
string.format([[
<wsdl:definitions
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
xmlns:s="http://www.w3.org/2001/XMLSchema"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:tns="%s"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
targetNamespace="%s"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">]], __service.namespace, __service.namespace) }
-- types
---------------------------------------------
table.insert(buffer, string.format([[
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="%s">]], __service.namespace))
for _, method in pairs(__methods) do
if type(method) == "table" then
table.insert(buffer, method.wsdl_type)
end
end
table.insert(buffer, [[
</s:schema>
</wsdl:types>]])
-- message
---------------------------------------------
for _, method in pairs(__methods) do
if type(method) == "table" then
table.insert(buffer, method.wsdl_message)
end
end
-- portType
---------------------------------------------
table.insert(buffer, string.format([[
<wsdl:portType name="%sServiceSoap">]], __service.name))
for _, method in pairs(__methods) do
if type(method) == "table" then
table.insert(buffer, method.wsdl_port_type)
end
end
table.insert(buffer, "\n </wsdl:portType>")
table.insert(buffer, string.format([[
<wsdl:portType name="%sServiceHttpPost">]], __service.name))
for _, method in pairs(__methods) do
if type(method) == "table" then
table.insert(buffer, (method.wsdl_port_type:gsub("Soap", "HttpPost")))
end
end
table.insert(buffer, "\n </wsdl:portType>")
-- binding
---------------------------------------------
-- Soap
table.insert(buffer, string.format([[
<wsdl:binding name="%sServiceSoap" type="tns:%sServiceSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />]],
__service.name, __service.name))
for _, method in pairs(__methods) do
if type(method) == "table" then
table.insert(buffer, method.wsdl_binding)
end
end
table.insert(buffer, " </wsdl:binding>")
-- Soap12
table.insert(buffer, string.format([[
<wsdl:binding name="%sServiceSoap12" type="tns:%sServiceSoap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />]],
__service.name, __service.name))
for _, method in pairs(__methods) do
if type(method) == "table" then
table.insert(buffer, (method.wsdl_binding:gsub("<soap:", "<soap12:")))
end
end
table.insert(buffer, " </wsdl:binding>")
-- HttpPost
table.insert(buffer, string.format([[
<wsdl:binding name="%sServiceHttpPost" type="tns:%sServiceHttpPost">
<http:binding verb="POST"/>]],
__service.name, __service.name))
for _, method in pairs(__methods) do
if type(method) == "table" then
table.insert(buffer, method.wsdl_http_binding)
end
end
table.insert(buffer, " </wsdl:binding>")
-- service
---------------------------------------------
table.insert(buffer, string.format([[
<wsdl:service name="%sService">
<wsdl:port name="%sServiceSoap" binding="tns:%sServiceSoap">
<soap:address location="%s" />
</wsdl:port>
<wsdl:port name="%sServiceSoap12" binding="tns:%sServiceSoap12">
<soap12:address location="%s" />
</wsdl:port>
<wsdl:port name="%sServiceHttpPost" binding="tns:%sServiceHttpPost">
<http:address location="%s" />
</wsdl:port>
</wsdl:service>]], __service.name, __service.name, __service.name, __service.url, __service.name, __service.name, __service.url, __service.name, __service.name, __service.url))
table.insert(buffer, [[
</wsdl:definitions>]])
return table.concat(buffer)
end
---------------------------------------------------------------------
local function generate_disco()
return xml_header.."<discovery></discovery>"
end
---------------------------------------------------------------------
-- Registers information needed to respond to WSDL and discovery
-- requests
-- @param name String with the name of the service
-- @param namespace String with the namespace of the service
-- @param url String with the url of the service
-- @param wsdl String with a WSDL response message (optional)
-- @param disco String with a discovery response message (optional)
---------------------------------------------------------------------
local function register_service_info(name, namespace, url, wsdl, disco)
__service.name = name
__service.namespace = namespace
__service.url = url
__service.wsdl = wsdl
__service.disco = disco
__service.soap_action = string.gsub(url, "[^/]*$", "")
end
---------------------------------------------------------------------
-- Exports methods that can be used by the server.
-- @param desc Table with the method description.
---------------------------------------------------------------------
local function export(desc)
desc.response.name = desc.response.name or desc.message.name.."Response"
__methods[desc.name] = {
message = desc.message.name,
response = desc.response.name,
wsdl_type = wsdl_gen_type(desc),
wsdl_message = wsdl_gen_message(desc),
wsdl_port_type = wsdl_gen_port_type(desc),
wsdl_binding = wsdl_gen_binding(desc),
wsdl_http_binding = wsdl_gen_http_binding(desc),
method = function (...) -- ( namespace, unpack(arguments) )
local res = desc.method(...)
return soap.encode({namespace = __service.namespace, method = desc.response.name, entries = res})
end,
}
end
---------------------------------------------------------------------
local function fatalerrorfunction(msg)
respond(builderrorenvelope("soap:ServerError", msg))
end
---------------------------------------------------------------------
-- Handles the request received by the calling script.
-- @param postdata String with POST data from the server.
-- @param querystring String with the query string.
---------------------------------------------------------------------
local function handle_request(postdata, querystring)
cgilua.seterroroutput(fatalerrorfunction)
local namespace, func, arg_table
local header
if postdata then
namespace, func, arg_table = decodedata(postdata)
header = xml_header
else
if querystring and querystring:lower() == "wsdl" then -- WSDL service
func = function ()
return __service.wsdl or generate_wsdl()
end
elseif querystring == "disco" then -- discovery service
func = function ()
return __service.disco or generate_disco()
end
else
func = __methods["listMethods"]
header = xml_header
end
arg_table = {}
end
local ok, result = callfunc(func, namespace, arg_table)
respond(result, header)
end
---------------------------------------------------------------------
return {
_COPYRIGHT = "Copyright (C) 2004-2011 Kepler Project",
_DESCRIPTION = "LuaSOAP provides a very simple API that convert Lua tables to and from XML documents",
_VERSION = "LuaSOAP 3.0 service helping functions",
builderrorenvelope = builderrorenvelope,
export = export,
generate_disco = generate_disco,
generate_wsdl = generate_wsdl,
handle_request = handle_request,
register_service_info = register_service_info,
}
|