Thursday, September 30, 2010

Best of New in Kamailio 3.1.0 - #3: Embedded HTTP Server

There is no doubt that SIP and HTTP integration brings lot of benefits for servicing VoIP, IM and Presence.


Being able to request information directly from SIP server via HTTP, without the need to develop intermediate layers and APIs, opens the doors for tighter integration between web and realtime voice, im & presence.

Kamailio 3.1.0 introduces a new module, named xhttp, that extends its capabilities with the ability of handling HTTP requests.

Usage is straightforward, you have to load the module and define a special route called event_route[xhttp:request]. Every time an HTTP request is received, the actions in this event route are executed. You can authenticate the HTTP requests, using same username and passwords like for SIP accounts, being sure that only local subscribers can access HTTP resources exported by SIP server.

Next is a simple route that welcomes visitors over HTTP and prints their IP address and port:
event_route[xhttp:request] {
if (!www_authorize("kamailio.org", "subscriber"))
{
www_challenge("kamailio.org", "0");
exit;
}
xdbg("===== xhttp: request [$rv:$au] $rm => $hu\n");
$var(html) = "Welcome " + $au + "! Your address is [" + $si + ":" + $sp + "]";
xhttp_reply("200", "OK", "text/html", "$var(html)");
}
The above example intercepts all HTTP requests sent to SIP server, no matter the URL, but you can offer different replies based on HTTP URL - its value is available in config variable $hu:
switch ($hu) {
case "/mycontacts":
# send back the online contacts of authenticated user
...
break;
case "/myaccount":
# send back the contacts of authenticated user
...
break;
default:
# invalid resource - page not found
send_reply("404", "Page not found");
exit;
}
In HTTP event route, you can use any function exported by Kamailio core and modules, access its database and memory. Also, you can use embedded languages such as Lua or Perl to build the HTTP reply, having full language tools to construct rich HTML documents.

You can browse the documentation of xhttp module at:

No comments:

Post a Comment