Public Member Functions | Data Fields

ilBMFTransport_HTTP Class Reference

Inheritance diagram for ilBMFTransport_HTTP:
Collaboration diagram for ilBMFTransport_HTTP:

Public Member Functions

 ilBMFTransport_HTTP ($URL, $encoding=SOAP_DEFAULT_ENCODING)
 ilBMFTransport_HTTP Constructor
send (&$msg, $options=NULL)
 send and receive soap data
 setCredentials ($username, $password)
 set data for http authentication creates Authorization header
 _validateUrl ()
 validate url data passed to constructor
 _parseEncoding ($headers)
 _parseResponse ()
 remove http headers from response
_getRequest (&$msg, $options)
 create http request, including headers, for outgoing request
_sendHTTP (&$msg, $options)
 send outgoing request, and read/parse response
_sendHTTPS (&$msg, $options)
 send outgoing request, and read/parse response, via HTTPS

Data Fields

 $headers = array()
 $timeout = 4
 $urlparts = NULL
 $url = ''
 $incoming_payload = ''
 $_userAgent = SOAP_LIBRARY_NAME
 $encoding = SOAP_DEFAULT_ENCODING
 $result_encoding = 'UTF-8'
 $result_content_type

Detailed Description

Definition at line 32 of file class.ilBMFTransport_HTTP.php.


Member Function Documentation

& ilBMFTransport_HTTP::_getRequest ( &$  msg,
options 
)

create http request, including headers, for outgoing request

Returns:
string outgoing_payload private

Definition at line 240 of file class.ilBMFTransport_HTTP.php.

References $headers.

Referenced by _sendHTTP(), and _sendHTTPS().

    {
        $action = isset($options['soapaction'])?$options['soapaction']:'';
        $fullpath = $this->urlparts['path'].
                        (isset($this->urlparts['query'])?'?'.$this->urlparts['query']:'').
                        (isset($this->urlparts['fragment'])?'#'.$this->urlparts['fragment']:'');
        if (isset($options['proxy_user'])) {
            $this->headers['Proxy-Authorization'] = 'Basic ' . base64_encode($options['proxy_user'].":".$options['proxy_pass']);
        }
        $this->headers['User-Agent'] = $this->_userAgent;
        $this->headers['Host'] = $this->urlparts['host'];
        $this->headers['Content-Type'] = "text/xml; charset=$this->encoding";
        $this->headers['Content-Length'] = strlen($msg);
        $this->headers['SOAPAction'] = "\"$action\"";
        if (isset($options['headers'])) {
            $this->headers = array_merge($this->headers, $options['headers']);
        }
        $headers = '';
        foreach ($this->headers as $k => $v) {
            $headers .= "$k: $v\r\n";
        }
        $this->outgoing_payload = 
                "POST $fullpath HTTP/1.0\r\n".
                $headers."\r\n".
                $msg;
        return $this->outgoing_payload;
    }

Here is the caller graph for this function:

ilBMFTransport_HTTP::_parseEncoding ( headers  ) 

Definition at line 179 of file class.ilBMFTransport_HTTP.php.

References $headers.

Referenced by _parseResponse().

    {
        $h = stristr($headers,'Content-Type');
        preg_match('/^Content-Type:\s*(.*)$/im',$h,$ct);
        $this->result_content_type = str_replace("\r","",$ct[1]);
        if (preg_match('/(.*?)(?:;\s?charset=)(.*)/i',$this->result_content_type,$m)) {
            // strip the string of \r
            $this->result_content_type = $m[1];
            if (count($m) > 2) {
                $enc = strtoupper(str_replace('"',"",$m[2]));
                if (in_array($enc, $this->_encodings)) {
                    $this->result_encoding = $enc;
                }
            }
        }
        // deal with broken servers that don't set content type on faults
        if (!$this->result_content_type) $this->result_content_type = 'text/xml';
    }

Here is the caller graph for this function:

ilBMFTransport_HTTP::_parseResponse (  ) 

remove http headers from response

Returns:
boolean private

Definition at line 204 of file class.ilBMFTransport_HTTP.php.

References ilBMFBase::_decodeDIMEMessage(), ilBMFBase::_decodeMimeMessage(), _parseEncoding(), and ilBMFBase::_raiseSoapFault().

Referenced by _sendHTTP().

    {
        if (preg_match("/^(.*?)\r?\n\r?\n(.*)/s", $this->incoming_payload, $match)) {
            #$this->response = preg_replace("/[\r|\n]/", '', $match[2]);
            $this->response = $match[2];
            // find the response error, some servers response with 500 for soap faults
            if (preg_match("/^HTTP\/1\.. (\d+).*/s",$match[1],$status) &&
                $status[1] >= 400 && $status[1] < 500) {
                    $this->_raiseSoapFault("HTTP Response $status[1] Not Found");
                    return FALSE;
            }
            $this->_parseEncoding($match[1]);
            if ($this->result_content_type == 'application/dime') {
                // XXX quick hack insertion of DIME
                $this->_decodeDIMEMessage($this->response,$this->headers,$this->attachments);
                $this->result_content_type = $this->headers['content-type'];
            } else if (stristr($this->result_content_type,'multipart/related')) {
                $this->response = $this->incoming_payload;
                $this->_decodeMimeMessage($this->response,$this->headers,$this->attachments);
            } else if ($this->result_content_type != 'text/xml') {
                $this->_raiseSoapFault($this->response);
                return FALSE;
            }
            // if no content, return false
            return strlen($this->response) > 0;
        }
        $this->_raiseSoapFault('Invalid HTTP Response');
        return FALSE;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

& ilBMFTransport_HTTP::_sendHTTP ( &$  msg,
options 
)

send outgoing request, and read/parse response

Parameters:
string &$msg outgoing SOAP package
string $action SOAP Action
Returns:
string &$response response data, minus http headers private

Definition at line 277 of file class.ilBMFTransport_HTTP.php.

References $data, $host, _getRequest(), _parseResponse(), and ilBMFBase::_raiseSoapFault().

Referenced by send().

    {
        $this->_getRequest($msg, $options);
        $host = $this->urlparts['host'];
        $port = $this->urlparts['port'];
        if (isset($options['proxy_host'])) {
            $host = $options['proxy_host'];
            $port = isset($options['proxy_port'])?$options['proxy_port']:8080;
        }
        // send
        if ($this->timeout > 0) {
            $fp = fsockopen($host, $port, $this->errno, $this->errmsg, $this->timeout);
        } else {
            $fp = fsockopen($host, $port, $this->errno, $this->errmsg);
        }
        if (!$fp) {
            return $this->_raiseSoapFault("Connect Error to $host:$port");
        }
        if ($this->timeout > 0) {
            // some builds of php do not support this, silence
            // the warning
            @socket_set_timeout($fp, $this->timeout);
        }
        if (!fputs($fp, $this->outgoing_payload, strlen($this->outgoing_payload))) {
            return $this->_raiseSoapFault("Error POSTing Data to $host");
        }
        
        // get reponse
        // XXX time consumer
        while ($data = fread($fp, 32768)) {
            $this->incoming_payload .= $data;
        }

        fclose($fp);

        if (!$this->_parseResponse()) {
            return $this->fault;
        }
        return $this->response;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

& ilBMFTransport_HTTP::_sendHTTPS ( &$  msg,
options 
)

send outgoing request, and read/parse response, via HTTPS

Parameters:
string &$msg outgoing SOAP package
string $action SOAP Action
Returns:
string &$response response data, minus http headers private

Definition at line 327 of file class.ilBMFTransport_HTTP.php.

References $host, $key, _getRequest(), and ilBMFBase::_raiseSoapFault().

Referenced by send().

    {
        /* NOTE This function uses the CURL functions
        *  Your php must be compiled with CURL
        */
        if (!extension_loaded('curl')) {
            return $this->_raiseSoapFault('CURL Extension is required for HTTPS');
        }
        
        $this->_getRequest($msg, $options);

        $ch = curl_init();

        // XXX don't know if this proxy stuff is right for CURL
        if (isset($options['proxy_host'])) {
            // $options['http_proxy'] == 'hostname:port'
            $host = $options['proxy_host'];
            $port = isset($options['proxy_port'])?$options['proxy_port']:8080;
            curl_setopt($ch, CURLOPT_PROXY, $host.":".$port);
        }
        if (isset($options['proxy_user'])) {
            // $options['http_proxy_userpw'] == 'username:password'
            curl_setopt($ch, CURLOPT_PROXYUSERPWD, $options['proxy_user'].':'.$options['proxy_pass']);
        }

        if ($this->timeout) {
            //curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); //times out after 4s
        }
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->outgoing_payload);
        curl_setopt($ch, CURLOPT_URL, $this->url);
        curl_setopt($ch, CURLOPT_FAILONERROR, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_VERBOSE, 0);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);

        if (isset($options['curl'])) {
          reset($options['curl']);
          while (list($key, $val) = each ($options['curl'])) {
            curl_setopt($ch, $key, $val);
          }
        }


        $this->response = curl_exec($ch);
        echo curl_error ( $ch);
        curl_close($ch);

        return $this->response;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

ilBMFTransport_HTTP::_validateUrl (  ) 

validate url data passed to constructor

Returns:
boolean private

Definition at line 153 of file class.ilBMFTransport_HTTP.php.

References ilBMFBase::_raiseSoapFault(), and setCredentials().

Referenced by send().

    {
        if ( ! is_array($this->urlparts) ) {
            $this->_raiseSoapFault("Unable to parse URL $url");
            return FALSE;
        }
        if (!isset($this->urlparts['host'])) {
            $this->_raiseSoapFault("No host in URL $url");
            return FALSE;
        }
        if (!isset($this->urlparts['port'])) {
            
            if (strcasecmp($this->urlparts['scheme'], 'HTTP') == 0)
                $this->urlparts['port'] = 80;
            else if (strcasecmp($this->urlparts['scheme'], 'HTTPS') == 0) 
                $this->urlparts['port'] = 443;
                
        }
        if (isset($this->urlparts['user'])) {
            $this->setCredentials($this->urlparts['user'], $this->urlparts['pass']);
        }
        if (!isset($this->urlparts['path']) || !$this->urlparts['path'])
            $this->urlparts['path'] = '/';
        return TRUE;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

ilBMFTransport_HTTP::ilBMFTransport_HTTP ( URL,
encoding = SOAP_DEFAULT_ENCODING 
)

ilBMFTransport_HTTP Constructor

Parameters:
string $URL http url to soap endpoint

public

Definition at line 94 of file class.ilBMFTransport_HTTP.php.

References $encoding, and ilBMFBase::ilBMFBase().

    {
        parent::ilBMFBase('HTTP');
        $this->urlparts = @parse_url($URL);
        $this->url = $URL;
        $this->encoding = $encoding;
    }

Here is the call graph for this function:

& ilBMFTransport_HTTP::send ( &$  msg,
options = NULL 
)

send and receive soap data

Parameters:
string &$msg outgoing post data
string $action SOAP Action header data
int $timeout socket timeout, default 0 or off
Returns:
string|fault response public

Definition at line 112 of file class.ilBMFTransport_HTTP.php.

References ilBMFBase::_raiseSoapFault(), _sendHTTP(), _sendHTTPS(), and _validateUrl().

    {
        if (!$this->_validateUrl()) {
            return $this->fault;
        }
        
        if (isset($options['timeout'])) 
            $this->timeout = (int)$options['timeout'];
    
        if (strcasecmp($this->urlparts['scheme'], 'HTTP') == 0) {
            return $this->_sendHTTP($msg, $options);
        } else if (strcasecmp($this->urlparts['scheme'], 'HTTPS') == 0) {
            return $this->_sendHTTPS($msg, $options);
        }
        
        return $this->_raiseSoapFault('Invalid url scheme '.$this->url);
    }

Here is the call graph for this function:

ilBMFTransport_HTTP::setCredentials ( username,
password 
)

set data for http authentication creates Authorization header

Parameters:
string $username username
string $password response data, minus http headers
Returns:
none public

Definition at line 140 of file class.ilBMFTransport_HTTP.php.

Referenced by _validateUrl().

    {
        $this->headers['Authorization'] = 'Basic ' . base64_encode($username . ':' . $password);
    }

Here is the caller graph for this function:


Field Documentation

ilBMFTransport_HTTP::$_userAgent = SOAP_LIBRARY_NAME

Definition at line 74 of file class.ilBMFTransport_HTTP.php.

ilBMFTransport_HTTP::$encoding = SOAP_DEFAULT_ENCODING

Definition at line 76 of file class.ilBMFTransport_HTTP.php.

Referenced by ilBMFTransport_HTTP().

ilBMFTransport_HTTP::$headers = array()

Definition at line 40 of file class.ilBMFTransport_HTTP.php.

Referenced by _getRequest(), and _parseEncoding().

ilBMFTransport_HTTP::$incoming_payload = ''

Definition at line 67 of file class.ilBMFTransport_HTTP.php.

ilBMFTransport_HTTP::$result_content_type

Definition at line 86 of file class.ilBMFTransport_HTTP.php.

ilBMFTransport_HTTP::$result_encoding = 'UTF-8'

Definition at line 84 of file class.ilBMFTransport_HTTP.php.

ilBMFTransport_HTTP::$timeout = 4

Definition at line 46 of file class.ilBMFTransport_HTTP.php.

ilBMFTransport_HTTP::$url = ''

Definition at line 60 of file class.ilBMFTransport_HTTP.php.

ilBMFTransport_HTTP::$urlparts = NULL

Definition at line 53 of file class.ilBMFTransport_HTTP.php.


The documentation for this class was generated from the following file: