<h1>Project "FreeSWITCH user-contributed scripts, etc" received a push.</h1>
<h2>branch: master updated</h2>
<pre>
via: d93d8a0e353b8a112167961178bffda485356ee7 (commit)
from: f192c27939941db18bdf2c543a9e14b21ae16fd0 (commit)
</pre>= COMMIT LOG ===========================================================
<div class="highlight"><pre>committer: Chad Phillips
comments:
adding service module, for accessing external services -- intial code has an http_request action, which allows GET requests using the HTTP protocol.
<span style="color: #000080; font-weight: bold">diff --git a/hunmonk/jester/conf.lua b/hunmonk/jester/conf.lua</span>
<span style="color: #000080; font-weight: bold">index 721c602..3cc79f1 100644</span>
<span style="color: #A00000">--- a/hunmonk/jester/conf.lua</span>
<span style="color: #00A000">+++ b/hunmonk/jester/conf.lua</span>
<span style="color: #800080; font-weight: bold">@@ -61,6 +61,7 @@ modules = {</span>
"navigation",
"play",
"record",
<span style="color: #00A000">+ "service",</span>
"system",
"tracker",
}
<span style="color: #000080; font-weight: bold">diff --git a/hunmonk/jester/modules/service/conf.lua b/hunmonk/jester/modules/service/conf.lua</span>
new file mode 100644
<span style="color: #000080; font-weight: bold">index 0000000..20d75b8</span>
<span style="color: #A00000">--- /dev/null</span>
<span style="color: #00A000">+++ b/hunmonk/jester/modules/service/conf.lua</span>
<span style="color: #800080; font-weight: bold">@@ -0,0 +1,5 @@</span>
<span style="color: #00A000">+jester.action_map.http_request = {</span>
<span style="color: #00A000">+ mod = "service",</span>
<span style="color: #00A000">+ func = "http_request",</span>
<span style="color: #00A000">+}</span>
<span style="color: #00A000">+</span>
<span style="color: #000080; font-weight: bold">diff --git a/hunmonk/jester/modules/service/help.lua b/hunmonk/jester/modules/service/help.lua</span>
new file mode 100644
<span style="color: #000080; font-weight: bold">index 0000000..41ce8a8</span>
<span style="color: #A00000">--- /dev/null</span>
<span style="color: #00A000">+++ b/hunmonk/jester/modules/service/help.lua</span>
<span style="color: #800080; font-weight: bold">@@ -0,0 +1,39 @@</span>
<span style="color: #00A000">+jester.help_map.service = {}</span>
<span style="color: #00A000">+jester.help_map.service.description_short = [[Access external services.]]</span>
<span style="color: #00A000">+jester.help_map.service.description_long = [[This module provides actions for accessing external services, such as webservices.]]</span>
<span style="color: #00A000">+</span>
<span style="color: #00A000">+jester.help_map.service.actions = {}</span>
<span style="color: #00A000">+</span>
<span style="color: #00A000">+jester.help_map.service.actions.http_request = {}</span>
<span style="color: #00A000">+jester.help_map.service.actions.http_request.description_short = [[Make an HTTP request to a external server.]]</span>
<span style="color: #00A000">+jester.help_map.service.actions.http_request.description_long = [[Make an HTTP request to an external server.</span>
<span style="color: #00A000">+</span>
<span style="color: #00A000">+Only the http:// protocol is supported. The resulting status of the request is stored in the 'last_http_request' storage area, with the following keys:</span>
<span style="color: #00A000">+</span>
<span style="color: #00A000">+ code:</span>
<span style="color: #00A000">+ An HTTP/1.1 return code, or 'error' if the request fails.</span>
<span style="color: #00A000">+ description:</span>
<span style="color: #00A000">+ A human-readable description of the code.</span>
<span style="color: #00A000">+</span>
<span style="color: #00A000">+If the request returns a '200' code, then the body of the response will be stored according to the parameter settings.]]</span>
<span style="color: #00A000">+jester.help_map.service.actions.http_request.params = {</span>
<span style="color: #00A000">+ server = [[(Optional) The server to send the request to, eg. 'www.example.com'. Default is 'localhost'.]],</span>
<span style="color: #00A000">+ port = [[(Optional) The port to send the request to. Default is 80.]],</span>
<span style="color: #00A000">+ path = [[(Optional) The server path to send the request to, no leading or trailing slash, eg. 'path/to/resource'.]],</span>
<span style="color: #00A000">+ query = [[(Optional) A table of query parameters to append to the URL. Keys must be only numbers, letters, and underscores, values can be any string and will be properly URL escaped before sending, eg. '{foo = "bar", baz = "two words"}'.]],</span>
<span style="color: #00A000">+ fragment = [[(Optional) A fragment to append to the URL. It will be properly escaped before sending.]],</span>
<span style="color: #00A000">+ user = [[(Optional) A user name to use for basic authentication.]],</span>
<span style="color: #00A000">+ password = [[(Optional) A password to use for basic authentication. 'user' parameter must also be provided.]],</span>
<span style="color: #00A000">+ response = [[(Optional) The format to expect the response in:</span>
<span style="color: #00A000">+ lua:</span>
<span style="color: #00A000">+ A string that represents a Lua table, of the following format:</span>
<span style="color: #00A000">+ return {</span>
<span style="color: #00A000">+ foo = 'bar',</span>
<span style="color: #00A000">+ }</span>
<span style="color: #00A000">+ The table data will be loaded into the specified storage area. Table values cannot contain a table.</span>
<span style="color: #00A000">+ raw:</span>
<span style="color: #00A000">+ A string of raw data. This will be stored in the specified storage area under the 'raw' storage key.</span>
<span style="color: #00A000">+Default is 'raw'.]],</span>
<span style="color: #00A000">+ storage_area = [[(Optional) The storage area to store the response in. Defaults to 'service'.]],</span>
<span style="color: #00A000">+}</span>
<span style="color: #00A000">+</span>
<span style="color: #000080; font-weight: bold">diff --git a/hunmonk/jester/modules/service/service.lua b/hunmonk/jester/modules/service/service.lua</span>
new file mode 100644
<span style="color: #000080; font-weight: bold">index 0000000..b4190d5</span>
<span style="color: #A00000">--- /dev/null</span>
<span style="color: #00A000">+++ b/hunmonk/jester/modules/service/service.lua</span>
<span style="color: #800080; font-weight: bold">@@ -0,0 +1,91 @@</span>
<span style="color: #00A000">+module(..., package.seeall)</span>
<span style="color: #00A000">+</span>
<span style="color: #00A000">+--[[</span>
<span style="color: #00A000">+ Log to the console.</span>
<span style="color: #00A000">+]]</span>
<span style="color: #00A000">+function http_request(action)</span>
<span style="color: #00A000">+ local server = action.server or "localhost"</span>
<span style="color: #00A000">+ local port = action.port or "80"</span>
<span style="color: #00A000">+ local path = action.path or "/"</span>
<span style="color: #00A000">+ local query = action.query</span>
<span style="color: #00A000">+ local fragment = action.fragment or ""</span>
<span style="color: #00A000">+ local user = action.user</span>
<span style="color: #00A000">+ local password = action.password</span>
<span style="color: #00A000">+ local response = action.response or "raw"</span>
<span style="color: #00A000">+ local area = action.storage_area or "service"</span>
<span style="color: #00A000">+ local query_string = ""</span>
<span style="color: #00A000">+ local user_string = ""</span>
<span style="color: #00A000">+</span>
<span style="color: #00A000">+ local http = require("socket.http")</span>
<span style="color: #00A000">+ local url = require("socket.url")</span>
<span style="color: #00A000">+</span>
<span style="color: #00A000">+ -- Transform the query params into a query string.</span>
<span style="color: #00A000">+ if path ~= "/" then</span>
<span style="color: #00A000">+ path = "/" .. path .. "/"</span>
<span style="color: #00A000">+ end</span>
<span style="color: #00A000">+ if query then</span>
<span style="color: #00A000">+ local query_parts = {}</span>
<span style="color: #00A000">+ for k, v in pairs(query) do</span>
<span style="color: #00A000">+ -- Escape values but not keys.</span>
<span style="color: #00A000">+ query_parts[#query_parts + 1] = k .. "=" .. url.escape(v)</span>
<span style="color: #00A000">+ end</span>
<span style="color: #00A000">+ query_string = "?" .. table.concat(query_parts, "&")</span>
<span style="color: #00A000">+ end</span>
<span style="color: #00A000">+ if fragment ~= "" then</span>
<span style="color: #00A000">+ fragment = "#" .. fragment</span>
<span style="color: #00A000">+ end</span>
<span style="color: #00A000">+ if user then</span>
<span style="color: #00A000">+ user_string = user</span>
<span style="color: #00A000">+ if password then</span>
<span style="color: #00A000">+ user_string = user_string .. ":" .. password</span>
<span style="color: #00A000">+ end</span>
<span style="color: #00A000">+ user_string = user_string .. "@"</span>
<span style="color: #00A000">+ end</span>
<span style="color: #00A000">+ url = "http://" .. user_string .. server .. ":" .. port .. path .. query_string .. fragment</span>
<span style="color: #00A000">+ jester.debug_log("HTTP request, URL: %s", url)</span>
<span style="color: #00A000">+</span>
<span style="color: #00A000">+ local code, description, data</span>
<span style="color: #00A000">+ -- Send the request.</span>
<span style="color: #00A000">+ local body, status_code, headers, status_description = http.request(url)</span>
<span style="color: #00A000">+ -- Request succeeded.</span>
<span style="color: #00A000">+ if body then</span>
<span style="color: #00A000">+ data = body</span>
<span style="color: #00A000">+ code = status_code</span>
<span style="color: #00A000">+ description = status_description</span>
<span style="color: #00A000">+ -- Only store response data if we get a 200 OK code.</span>
<span style="color: #00A000">+ if code == 200 then</span>
<span style="color: #00A000">+ -- Store raw data.</span>
<span style="color: #00A000">+ if response == "raw" then</span>
<span style="color: #00A000">+ jester.set_storage(area, "raw", data)</span>
<span style="color: #00A000">+ -- Data is a Lua table.</span>
<span style="color: #00A000">+ elseif response == "lua" then</span>
<span style="color: #00A000">+ -- Load the data string as Lua code.</span>
<span style="color: #00A000">+ local func = loadstring(data)</span>
<span style="color: #00A000">+ if func then</span>
<span style="color: #00A000">+ -- Sandbox the function to protect against malicious code.</span>
<span style="color: #00A000">+ setfenv(func, {})</span>
<span style="color: #00A000">+ local table_data = func()</span>
<span style="color: #00A000">+ -- Make sure a table was returned.</span>
<span style="color: #00A000">+ if type(table_data) == "table" then</span>
<span style="color: #00A000">+ for k, v in pairs(table_data) do</span>
<span style="color: #00A000">+ jester.set_storage(area, k, v)</span>
<span style="color: #00A000">+ end</span>
<span style="color: #00A000">+ else</span>
<span style="color: #00A000">+ jester.debug_log("ERROR: Returned data is not a Lua table.")</span>
<span style="color: #00A000">+ end</span>
<span style="color: #00A000">+ else</span>
<span style="color: #00A000">+ jester.debug_log("ERROR: Failed to parse response body as Lua code.")</span>
<span style="color: #00A000">+ end</span>
<span style="color: #00A000">+ end</span>
<span style="color: #00A000">+ end</span>
<span style="color: #00A000">+ -- Request failed.</span>
<span style="color: #00A000">+ else</span>
<span style="color: #00A000">+ code = "error"</span>
<span style="color: #00A000">+ description = status_code</span>
<span style="color: #00A000">+ data = ""</span>
<span style="color: #00A000">+ end</span>
<span style="color: #00A000">+ jester.set_storage("last_http_request", "code", code)</span>
<span style="color: #00A000">+ jester.set_storage("last_http_request", "description", description)</span>
<span style="color: #00A000">+ jester.debug_log("HTTP response, Code: %s, Description: %s, Data: %s", code, description, data)</span>
<span style="color: #00A000">+end</span>
<span style="color: #00A000">+</span>
</pre></div>
========================================================================<pre>
Summary of changes:
hunmonk/jester/conf.lua | 1 +
hunmonk/jester/modules/service/conf.lua | 5 ++
hunmonk/jester/modules/service/help.lua | 39 ++++++++++++
hunmonk/jester/modules/service/service.lua | 91 ++++++++++++++++++++++++++++
4 files changed, 136 insertions(+), 0 deletions(-)
create mode 100644 hunmonk/jester/modules/service/conf.lua
create mode 100644 hunmonk/jester/modules/service/help.lua
create mode 100644 hunmonk/jester/modules/service/service.lua
</pre>
<p>this email was generated because of /git/your-repo.git/hooks/post-receive by the file /git-core/contrib/hooks/post-receive-email<br />
For more info, see <a href="http://blog.chomperstomp.com/?p=630">http://blog.chomperstomp.com/?p=630</a>
-- <br />
FreeSWITCH user-contributed scripts, etc</p>