Your IP : 216.73.216.172


Current Path : /proc/thread-self/root/proc/thread-self/root/proc/1908984/root/proc/1147586/cwd/
Upload File :
Current File : //proc/thread-self/root/proc/thread-self/root/proc/1908984/root/proc/1147586/cwd/HttpRequest.tar

HttpException.php000064400000001537152435232560010070 0ustar00<?php

/*******************************************************************************
 *  Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *
 *  You may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *  http://aws.amazon.com/apache2.0
 *  This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 *  CONDITIONS OF ANY KIND, either express or implied. See the License
 *  for the
 *  specific language governing permissions and limitations under the
 *  License.
 * *****************************************************************************
 */

/**
 * Exception thrown when an error is encountered with
 * the curl http library
 * 
 */
class OffAmazonPayments_HttpException
    extends Exception
{
}IHttpRequest.php000064400000001700152435232640007662 0ustar00<?php

/*******************************************************************************
 *  Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *
 *  You may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *  http://aws.amazon.com/apache2.0
 *  This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 *  CONDITIONS OF ANY KIND, either express or implied. See the License
 *  for the
 *  specific language governing permissions and limitations under the
 *  License.
 * *****************************************************************************
 */

interface IHttpRequest {

    /**
     * Create a http get request for the resource
     * at the given uri
     *
     * @param execute the underlying http request
     *
     * @return response header + body
     */
    public function execute();
};IHttpRequestFactory.php000064400000002313152435232640011213 0ustar00<?php

/*******************************************************************************
 *  Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *
 *  You may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *  http://aws.amazon.com/apache2.0
 *  This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 *  CONDITIONS OF ANY KIND, either express or implied. See the License
 *  for the
 *  specific language governing permissions and limitations under the
 *  License.
 * *****************************************************************************
 */

interface IHttpRequestFactory {

    /**
     * Create a http get request for the resource
     * at the given uri
     *
     * @param url uniform resource locator to get
     *
     * @return IHttpRequest object
     */
    public function createGetRequest($url);

    /**
     * Create a http post request using to given
     * given uri & body content
     *
     * @param url uniform resource locator to post
     *
     * @return IHttpRequest object
     */
    public function createPostRequest($url, $body);
};Impl/HttpRequestCurlImpl.php000064400000010375152435232710012130 0ustar00<?php

/*******************************************************************************
 *  Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *
 *  You may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *  http://aws.amazon.com/apache2.0
 *  This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 *  CONDITIONS OF ANY KIND, either express or implied. See the License
 *  for the
 *  specific language governing permissions and limitations under the
 *  License.
 * *****************************************************************************
 */

require_once 'OffAmazonPayments/HttpRequest/HttpException.php';
require_once 'OffAmazonPayments/HttpRequest/IHttpRequest.php';

class HttpRequestCurlImpl implements IHttpRequest 
{

    /**
     * Reference to the underlying curl handle
     */
    private $_ch = null;

    /*
     * Default headers for curl requests
     */
    private $_headers = array(
        'Expect' => null // Don't expect 100 Continue
    );

    /**
     * Create a new instane of the class + underlying curl handle
     * 
     */
    public function __construct()
    {
        $this->_ch = curl_init();
        curl_setopt($this->_ch, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($this->_ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($this->_ch, CURLOPT_HEADER, true);
        curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, true);
    }

    /**
     * Set the url for the curl handle
     *
     * @param url resource to request
     */
    public function setUrl($url) {
        curl_setopt($this->_ch, CURLOPT_URL, $url);
    }

    /**
     * Set the port for the curl handle
     *
     * @param port to use
     */
    public function setPort($port) {
        curl_setopt($this->_ch, CURLOPT_PORT, $port);
    }

    /**
     * Set the useragent for the curl handle
     *
     * @param userAgent for request
     */
    public function setUserAgent($userAgent) {
        curl_setopt($this->_ch, CURLOPT_USERAGENT, $userAgent);
    }

    /**
     * Make this request a post request with the given body
     *
     * @param body http POST request body
     */
    public function makePost($body) {
        curl_setopt($this->_ch, CURLOPT_POST, true);
        curl_setopt($this->_ch, CURLOPT_POSTFIELDS, $body);

        array_push($this->_headers, 'Content-Type', "application/x-www-form-urlencoded; charset=utf-8");
    }

    /**
     * Setup the ca bundle file
     *
     * @param caBundleFile file containing trusted ca certs
     */
    public function setCaBundleFile($caBundleFile)
    {
        curl_setopt($this->_ch, CURLOPT_CAINFO, $caBundleFile);
    }

    /**
     * Setup the proxy hostname and port
     *
     * @param hostnameport username and password in <hostname>:<port> format
     *
     */
    public function setupProxy($hostnameport)
    {
        curl_setopt($this->_ch, CURLOPT_PROXY, $hostnameport);
    }

    /**
     * Setup the proxy username and password
     *
     * @param usernamepwd username and password in <username>:<password> format
     *
     */
    public function setupProxyUsernameAndPassword($usernamepwd)
    {
        curl_setopt($this->_ch, CURLOPT_PROXYUSERPWD, $usernamepwd);
    }

    /**
     * Create a http get request for the resource
     * at the given uri
     *
     * @param execute the underlying http request
     *
     * @return response header + body
     */
    public function execute() 
    {
        $this->setRequestHeaders();
        $response = '';
        if (!$response = curl_exec($this->_ch)) {
            $errorNo = curl_error($this->_ch);
            curl_close($this->_ch);
            throw new OffAmazonPayments_HttpException($errorNo);
        }

        curl_close($this->_ch);

        return $response;
    }

    /**
     * Setup request header information
     *
     */
    private function setRequestHeaders()
    {
        $allHeadersStr = array();
        foreach($this->_headers as $name => $val) {
            $str = $name . ": ";
            if(isset($val)) {
                $str = $str . $val;
            }
            $allHeadersStr[] = $str;
        }

        curl_setopt($this->_ch, CURLOPT_HTTPHEADER, $allHeadersStr);
    }
};

?>Impl/HttpRequestFactoryCurlImpl.php000064400000011373152435232760013464 0ustar00<?php

/*******************************************************************************
 *  Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *
 *  You may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *  http://aws.amazon.com/apache2.0
 *  This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 *  CONDITIONS OF ANY KIND, either express or implied. See the License
 *  for the
 *  specific language governing permissions and limitations under the
 *  License.
 * *****************************************************************************
 */

require_once 'OffAmazonPayments/HttpRequest/IHttpRequestFactory.php';
require_once 'OffAmazonPayments/HttpRequest/Impl/HttpRequestCurlImpl.php';
require_once 'OffAmazonPayments/OffAmazonPaymentsServiceUtils.php';
require_once 'OffAmazonPaymentsService/MerchantValues.php';

/**
 * Wrapper to simplify curl functions for http get/set
 * 
 */
class HttpRequestFactoryCurlImpl implements IHttpRequestFactory
{

    /**
     * Merchant values configuration instance
     *
     */
    private $_merchantValues = null;

    
    /**
     * Create an instance of the client class
     *
     * @param instance of OffAmazonPayments_MerchantValues class
     * 
     * @return void
     */
    public function __construct($merchantValues) {

        if(!isset($merchantValues)) {
            throw new InvalidArgumentException("merchantValue object not injected");
        }

        $this->_merchantValues = $merchantValues;
    }

    /**
     * Create a http get request for the resource
     * at the given uri
     *
     * @param url uniform resource locator to get
     *
     * @return HttpRequest object
     */
    public function createGetRequest($url) 
    {
        return $this->createNewRequest($url);
    }

    /**
     * Create a http post request using to given
     * given uri & body content
     *
     * @param url uniform resource locator to post
     *
     * @return HttpRequest object
     */
    public function createPostRequest($url, $body) 
    {
        $httpRequest = $this->createNewRequest($url);
        $httpRequest->makePost($body);

        return $httpRequest;
    }

    /**
     * Create a new curl handle and set up with default
     * options for all requests
     *
     * @param url resource to request
     *
     * @return curl handle
     */
    private function createNewRequest($url)
    {
        $httpRequest = new HttpRequestCurlImpl();

        $parsedUrl = $this->_setupConnectionInfo($url);

        $httpRequest->setUrl($parsedUrl['url']);
        $httpRequest->setPort($parsedUrl['port']);
        $httpRequest->setUserAgent($this->_merchantValues->getUserAgentString());

        # if a ca bundle is configured, use it as opposed to the default ca 
        # configured for the server
        if ($this->_merchantValues->isCaBundleConfigured()) {
            $httpRequest->setCaBundleFile($this->_merchantValues->getCaBundleFile());
        }

        if ($this->_merchantValues->isProxyConfigured()) {
            $this->setupProxyForCurl($httpRequest);
        }

        return $httpRequest;
    }

    /**
     * Setup the connection parameters for the request
     * 
     * @param url resource to request
     *
     */
    private function _setupConnectionInfo($url)
    {
        $parsed_url = parse_url($url);

        $uri = array_key_exists('path', $parsed_url) ? $parsed_url['path'] : null;
        if (!isset($uri)) {
            $uri = "/";
        }

        $scheme = '';

        switch ($parsed_url['scheme']) {
            case 'https':
                $scheme = 'https://';
                $port = array_key_exists('port', $parsed_url) && (isset($parsed_url['port'])) ? $parsed_url['port'] : 443;
                break;
            default:
                $scheme = 'http://';
                $port = array_key_exists('port', $parsed_url) && (isset($parsed_url['port'])) ? $parsed_url['port'] : 80;
        }

        $retVal = array(
            'port' => $port,
            'url' => $scheme . $parsed_url['host'] . $uri
        );

        return $retVal;
    }

    /**
     * Setup proxy options for curl handle
     *
     * @param httpRequest httpRequestObject
     *
     */
    private function setupProxyForCurl($httpRequest)
    {
        $proxy = $this->_merchantValues->getProxyHost() . ':' . $this->_merchantValues->getProxyPort();
        $httpRequest->setupProxy($proxy);

        if ($this->_merchantValues->isProxyAuthenticationConfigured()) {
            $proxyUserPwd = $this->_merchantValues->getProxyUsername() . ':' . $this->_merchantValues->getProxyPassword();
            $httpRequest->setupProxyUsernameAndPassword($proxyUserPwd);
        }
    }
}

?>