提交 286b6822 authored 作者: Raymond Chandler's avatar Raymond Chandler

don't split content body by :'s, thanks Alberto

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk/contrib@14999 d0543943-73ff-0310-b7d9-9358b9ac24b2
上级 2340b62e
...@@ -9,11 +9,11 @@ ...@@ -9,11 +9,11 @@
* *
*/ */
class fs_sock { class fs_sock {
/** /**
* file handler for FreeSWITCH socket connection * file handler for FreeSWITCH socket connection
* *
* @var file pointer * @var file pointer
*/ */
public $sock; public $sock;
/** /**
...@@ -43,10 +43,10 @@ class fs_sock { ...@@ -43,10 +43,10 @@ class fs_sock {
*/ */
function fs_sock($vars=null) { function fs_sock($vars=null) {
if (!defined('BUFFER_SIZE')) { if (!defined('BUFFER_SIZE')) {
/** /**
* This is the buffer size for fread/fgets operations (default 4096) * This is the buffer size for fread/fgets operations (default 4096)
* Define BUFFER_SIZE before instantiation to use a different size * Define BUFFER_SIZE before instantiation to use a different size
*/ */
define('BUFFER_SIZE', 4096); define('BUFFER_SIZE', 4096);
} }
$vars_array = is_array($vars) ? $vars : array(); $vars_array = is_array($vars) ? $vars : array();
...@@ -77,23 +77,23 @@ class fs_sock { ...@@ -77,23 +77,23 @@ class fs_sock {
*/ */
function set_initial_vars($var_array) { function set_initial_vars($var_array) {
$defaults = array( $defaults = array(
'host' => '127.0.0.1', 'host' => '127.0.0.1',
'port' => '8021', 'port' => '8021',
'pass' => 'ClueCon', 'pass' => 'ClueCon',
'timeout' => 30, 'timeout' => 30,
'stream_timeout' => 5 'stream_timeout' => 5
); );
if (array_key_exists('host', $var_array) if (array_key_exists('host', $var_array)
&& $var_array['host'] == 'localhost') { && $var_array['host'] == 'localhost') {
//$this -> debug('Replacing localhost with 127.0.0.1'); //$this -> debug('Replacing localhost with 127.0.0.1');
$var_array['host'] = '127.0.0.1'; $var_array['host'] = '127.0.0.1';
} }
foreach ($defaults as $key => $val) { foreach ($defaults as $key => $val) {
if (array_key_exists($key, $var_array)) { if (array_key_exists($key, $var_array)) {
//$this -> debug("$key found in vars"); //$this -> debug("$key found in vars");
$connection_settings[$key] = $var_array[$key]; $connection_settings[$key] = $var_array[$key];
} else { } else {
//$this -> debug("$key not found in vars"); //$this -> debug("$key not found in vars");
$connection_settings[$key] = $val; $connection_settings[$key] = $val;
} }
} }
...@@ -108,19 +108,19 @@ class fs_sock { ...@@ -108,19 +108,19 @@ class fs_sock {
* @return boolean * @return boolean
*/ */
function sock_connect($sock_array) { function sock_connect($sock_array) {
//$this -> debug($sock_array); //$this -> debug($sock_array);
$host = $sock_array['host']; $host = $sock_array['host'];
$port = $sock_array['port']; $port = $sock_array['port'];
$timeout = $sock_array['timeout']; $timeout = $sock_array['timeout'];
$this -> sock = fsockopen($host, $port, $errno, $errstr, $timeout); $this -> sock = fsockopen($host, $port, $errno, $errstr, $timeout);
if (!$this -> sock) { if (!$this -> sock) {
$error = sprintf('Unable to connect to %s:%s Error #%s: %s' $error = sprintf('Unable to connect to %s:%s Error #%s: %s'
, $host, $port, $errno, $errstr , $host, $port, $errno, $errstr
); );
trigger_error($error, E_USER_ERROR); trigger_error($error, E_USER_ERROR);
return false; return false;
} else { } else {
//$this -> debug(stream_get_meta_data($this -> sock)); //$this -> debug(stream_get_meta_data($this -> sock));
$this -> set_stream_opts($sock_array); $this -> set_stream_opts($sock_array);
return true; return true;
} }
...@@ -133,9 +133,9 @@ class fs_sock { ...@@ -133,9 +133,9 @@ class fs_sock {
* @return void * @return void
*/ */
function set_stream_opts($opts) { function set_stream_opts($opts) {
//$this -> debug($opts); //$this -> debug($opts);
if (ereg('^[0-9]*\.[0-9]+$', $opts['stream_timeout'])) { if (ereg('^[0-9]*\.[0-9]+$', $opts['stream_timeout'])) {
//$this -> debug($opts['stream_timeout'] . ' seems to be a float'); //$this -> debug($opts['stream_timeout'] . ' seems to be a float');
$time_opts = split('\.', $opts['stream_timeout']); $time_opts = split('\.', $opts['stream_timeout']);
//$this -> debug($time_opts); //$this -> debug($time_opts);
$secs = sprintf('%d', $time_opts[0]); $secs = sprintf('%d', $time_opts[0]);
...@@ -146,7 +146,7 @@ class fs_sock { ...@@ -146,7 +146,7 @@ class fs_sock {
} }
if (!stream_set_timeout($this -> sock, $secs, $ms)) { if (!stream_set_timeout($this -> sock, $secs, $ms)) {
$this -> debug( $this -> debug(
"Failed to set timeout to $secs seconds and $ms microseconds" "Failed to set timeout to $secs seconds and $ms microseconds"
); );
} }
} }
...@@ -160,7 +160,7 @@ class fs_sock { ...@@ -160,7 +160,7 @@ class fs_sock {
function sock_auth($pass) { function sock_auth($pass) {
$reply = $this -> send_command("auth $pass"); $reply = $this -> send_command("auth $pass");
if (is_array($reply) && array_key_exists('Reply-Text', $reply) if (is_array($reply) && array_key_exists('Reply-Text', $reply)
&& ereg('^\+?OK', $reply['Reply-Text'])) { && ereg('^\+?OK', $reply['Reply-Text'])) {
$this -> debug('Successfully authenticated'); $this -> debug('Successfully authenticated');
$this -> debug($reply); $this -> debug($reply);
$this -> auth = true; $this -> auth = true;
...@@ -200,7 +200,7 @@ class fs_sock { ...@@ -200,7 +200,7 @@ class fs_sock {
$event[$header] = $value; $event[$header] = $value;
} }
} elseif (is_array($event) } elseif (is_array($event)
&& array_key_exists('Content-Length', $event)) { && array_key_exists('Content-Length', $event)) {
$this -> debug("line is empty: " . (empty($trim_line) ? 'true' : 'false')); $this -> debug("line is empty: " . (empty($trim_line) ? 'true' : 'false'));
$event['Body'] = $this -> sock_get_length($event['Content-Length']); $event['Body'] = $this -> sock_get_length($event['Content-Length']);
break; break;
...@@ -214,7 +214,7 @@ class fs_sock { ...@@ -214,7 +214,7 @@ class fs_sock {
} }
/** /**
* Read $content_len bytes from the socket * Read $content_len bytes from the socket splitting by :
* *
* @param integer $content_len * @param integer $content_len
* @return array * @return array
...@@ -234,8 +234,8 @@ class fs_sock { ...@@ -234,8 +234,8 @@ class fs_sock {
$value = trim($split[1]); $value = trim($split[1]);
$content[$attribute] = urldecode($value); $content[$attribute] = urldecode($value);
} elseif (empty($trim_line) && is_array($content) } elseif (empty($trim_line) && is_array($content)
&& array_key_exists('Content-Length', $content)) { && array_key_exists('Content-Length', $content)) {
$content['Body'] = $this -> sock_get_length($content['Content-Length']); $content['Body'] = $this -> sock_get_body($content['Content-Length']);
$len += $content['Content-Length']; $len += $content['Content-Length'];
break; break;
} elseif (!empty($trim_line)) { } elseif (!empty($trim_line)) {
...@@ -250,6 +250,32 @@ class fs_sock { ...@@ -250,6 +250,32 @@ class fs_sock {
return $content; return $content;
} }
/**
* Read $content_len bytes from the socket
*
* @param integer $content_len
* @return array
*/
private function sock_get_body($content_len) {
$len = 0;
$content = null;
while ($orig_line = fgets($this -> sock, BUFFER_SIZE)) {
$len += strlen($orig_line);
$trim_line = trim($orig_line);
$content .= $trim_line;
if ($len >= $content_len) {
break;
}
}
return $content;
}
/** /**
* Send a command to the FreeSWITCH event socket * Send a command to the FreeSWITCH event socket
* This method sends a command string to the FreeSWITCH event socket and * This method sends a command string to the FreeSWITCH event socket and
...@@ -271,7 +297,7 @@ class fs_sock { ...@@ -271,7 +297,7 @@ class fs_sock {
$this -> sock_put("$cmd\r\n\r\n", $sock); $this -> sock_put("$cmd\r\n\r\n", $sock);
if ($this -> command != 'exit') { if ($this -> command != 'exit') {
$reply = $this -> sock_get($sock); $reply = $this -> sock_get($sock);
//$this -> debug($reply); //$this -> debug($reply);
} else { } else {
unset($this -> command); unset($this -> command);
return; return;
...@@ -298,8 +324,8 @@ class fs_sock { ...@@ -298,8 +324,8 @@ class fs_sock {
while ($reply['Content-Type'] != 'api/response') { while ($reply['Content-Type'] != 'api/response') {
if (count($reply) > 0) { if (count($reply) > 0) {
$debug_text = sprintf( $debug_text = sprintf(
"%s - (%s) != (api/response) adding an event to the output buffer" "%s - (%s) != (api/response) adding an event to the output buffer"
, count($reply), $reply['Content-Type'] , count($reply), $reply['Content-Type']
); );
$this -> debug($debug_text); $this -> debug($debug_text);
$this -> output_buffer[] = $reply; $this -> output_buffer[] = $reply;
...@@ -320,8 +346,8 @@ class fs_sock { ...@@ -320,8 +346,8 @@ class fs_sock {
while ($reply['Content-Type'] != 'command/reply') { while ($reply['Content-Type'] != 'command/reply') {
if (count($reply) > 0) { if (count($reply) > 0) {
$debug_text = sprintf( $debug_text = sprintf(
"%s - (%s) != (command/reply) adding an event to the output buffer" "%s - (%s) != (command/reply) adding an event to the output buffer"
, count($reply), $reply['Content-Type'] , count($reply), $reply['Content-Type']
); );
$this -> debug($debug_text); $this -> debug($debug_text);
$this -> output_buffer[] = $reply; $this -> output_buffer[] = $reply;
...@@ -410,10 +436,10 @@ class fs_sock { ...@@ -410,10 +436,10 @@ class fs_sock {
$this -> debug(")", $spaces); $this -> debug(")", $spaces);
} else { } else {
if (is_array($_SERVER) if (is_array($_SERVER)
&& array_key_exists('HTTP_HOST', $_SERVER)) { && array_key_exists('HTTP_HOST', $_SERVER)) {
printf("<!--%s%s-->\r\n" printf("<!--%s%s-->\r\n"
, str_repeat(' ', $spaces) , str_repeat(' ', $spaces)
, htmlentities($input) , htmlentities($input)
); );
} else { } else {
$input = trim($input); $input = trim($input);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论