Mod curl

From FreeSWITCH Wiki

(Redirected from Mod http)
Jump to: navigation, search

Contents

mod_curl (HTTP request API)

mod_curl allows one to make a http request and receive the response. Output can be plain text (headers optional) or a json object.

Installing

To use mod_curl:

Tell FreeSWITCH to compile in this module by editing modules.conf in /usr/src/freeswitch/trunk and uncomment:

#applications/mod_curl

Now go recompile FreeSWITCH.

make
make install

Tell FreeSWITCH to actually use the curl module when running by adding the module to modules.conf.xml in /usr/local/freeswitch/conf/autoload_configs:

<load module="mod_curl"/>

There is no separate config file for mod_curl.

Now load up FreeSWITCH!

Create mod_curl.dll (Windows)

Deprecated, the newest FS version has already include it

Must be download last json project from this link http://oss.metaparadigm.com/json-c/ Now create the VCPROJ for json Project and add to _Libraries node.

FILES MODIFY

After download it, modify the followings files

 json_object.h 
 json_object.c
 json_object_private.h

Exchange the complete word "boolean" to "boolean2", i advice this way, however yo can modify to your way.

IMPORTANT: Change "boolean" to "boolean2" have a reason, already exist anhoter one "boolean" definition into FreeSwitch project raising compile's problems.

Now append in arraylist.c "stdio.h" library to define NULL, only a suggest you can do it to your way; and finally append in json_util.c "fcntl.h" library to define, among other tokens; "O_RDONLY".

Mod_curl.vcproj

Same way, create the VCPROJ for mod_curl and add at Application node, and project properties append commandline at C/C++

/D "MOD_EXPORTS"  
/D "CURL_STATICLIB"
/W4 

add the switch.h, curl.h, json.h path, finally append index and rindex functions in mod_curl.c, you can find it at the nexts link,

 http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/ucblib/libucb/port/gen/index.c
 http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/ucblib/libucb/port/gen/rindex.c

INDEX and RINDEX

INDEX Function

 index(char *sp, char c){
 do {
   if (*sp == c) return (sp);
 } while (*sp++);
 return (NULL);
 }

RINDEX Function

 rindex(char *sp, char c){
 char *r;
 r = NULL;
 do {
   if (*sp == c) r = sp;
 } while (*sp++);
 return (r);
 }

FINALLY, add in commandline at Linker the next

 "..\..\..\..\w32\library\debug\freeswitchcore.lib"
 "..\..\..\..\libs\win32\curl\debug\curllib.lib"
 "..\..\..\..\Debug\json.lib"
 Ws2_32.lib 
 Winmm.lib

Build and enjoy.

NOTE

 See the README json file.
 Could you rename "config.h.w32" to "config.h", at json.
 Remember define the output path LIB file (json) and output path DLL file (mod_curl).

Regards Arturo Monroy.

CLI / API

The CLI uses the api interface. The syntax is:

curl url [headers|json] [get|head|post [url_encode_data]]

From the commandline line issue:

curl http://www.google.com

This will return google's home page.

curl http://www.google.com/ headers

Will give you the headers followed by the body. And

curl http://www.google.com/ json

Will give you the headers and body in a structured json stream.

Send POST and GET request.

curl http://www.myhost.com/?get=myGetValue
curl http://www.myhost.com post post=myPostValue%20a%20space

Mix with headers or JSON.

curl http://www.myhost.com/?get=myGetValue json
curl http://www.myhost.com headers post post=myPostValue%20a%20space

Application

Dialplan

The curl application sets the variables curl_response and curl_response_code. curl_response can also be headers/body or json if requested.

<action application="curl" data="http://www.google.com"/>
<action application="info"/>
<action application="curl" data="http://www.google.com headers"/>
<action application="info"/>
<action application="curl" data="http://www.google.com json"/>
<action application="info"/>

Lua Usage

function codificaParametros(paramsT)
	function escape (s)
	  s = string.gsub(
		s, 
		'([\r\n"#%%&+:;<=>?@^`{|}%\\%[%]%(%)$!~,/\'])', 
		function (c)
			return '%'..string.format("%X", string.byte(c))
		end
	)
	  s = string.gsub(s, "%s", "+")
	  return s
	end

	function encode (t)
	  local s = ""
	  for k , v in pairs(t) do
		s = s .. "&" .. escape(k) .. "=" .. escape(v)
	  end
	  return string.sub(s, 2)     -- remove first `&'
	end
	
	if type(paramsT) == 'table' then
		return encode(paramsT)
	else
		local tmp = Utils:commaText(paramsT, '&'); 
		local myParamsT = {};
		for k, v in pairs(tmp) do
			local pos = 0
			pos = string.find(v, '=')
			if not pos then return '' end
			myParamsT[string.sub(v, 1, pos-1 )] = string.sub(v, pos+1 )
		end
		return encode(myParamsT)
	end
end

 
 local paramsT  = {
    param1 = 'valor1',
    param2 = '\nh@la<>?'
 }
 local encode = codificaParametros (paramsT)
 session:answer()
 print (string.format("\n%s\n", encode));
--param1=valor1&param2=%Ah%40la%3C%3E%3F
 session:hangup();
Personal tools
Community
Support FreeSWITCH