コード例 #1
0
ファイル: functions.php プロジェクト: ringcentral/psr7
/**
 * Parses a request message string into a request object.
 *
 * @param string $message Request message string.
 *
 * @return Request
 */
function parse_request($message)
{
    $data = _parse_message($message);
    $matches = array();
    if (!preg_match('/^[a-zA-Z]+\\s+([a-zA-Z]+:\\/\\/|\\/).*/', $data['start-line'], $matches)) {
        throw new \InvalidArgumentException('Invalid request string');
    }
    $parts = explode(' ', $data['start-line'], 3);
    $subParts = isset($parts[2]) ? explode('/', $parts[2]) : array();
    $version = isset($parts[2]) ? $subParts[1] : '1.1';
    $request = new Request($parts[0], $matches[1] === '/' ? _parse_request_uri($parts[1], $data['headers']) : $parts[1], $data['headers'], $data['body'], $version);
    return $matches[1] === '/' ? $request : $request->withRequestTarget($parts[1]);
}
 public function wizard_easy_setup_callback()
 {
     $headers = array();
     array_walk($_SERVER, function ($value, $key) use(&$headers) {
         if (preg_match('/^HTTP\\_(.+)$/', $key, $matches)) {
             $headers[str_replace('_', '-', $matches[1])] = $value;
         }
     });
     preg_match('/^[^\\/]+\\/(.*)$/', $_SERVER['SERVER_PROTOCOL'], $matches);
     $protocol_version = $matches ? $matches[1] : null;
     $request = new Request($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], $headers, $this->wp_facade->fopen('php://input', 'rb'), $protocol_version);
     $http_response = new Response();
     if ($request->hasHeader('signature')) {
         try {
             // Have the SDK client handle the callback
             $response = $this->launchkey_client->serverSentEvent()->handleEvent($request, $http_response);
             if ($response instanceof \LaunchKey\SDK\Domain\RocketCreated) {
                 $config = $this->get_option(LaunchKey_WP_Configuration_Wizard::EASY_SETUP_OPTION);
                 if (empty($config['nonce']) || !$config['nonce'] instanceof \LaunchKey\SDK\Domain\NonceResponse) {
                     throw new \LaunchKey\SDK\Service\Exception\InvalidRequestError(sprintf('Easy config request with no valid "nonce" in option "%s"', LaunchKey_WP_Configuration_Wizard::EASY_SETUP_OPTION));
                 }
                 // Delete the option, valid or not.
                 $this->wp_facade->delete_option(LaunchKey_WP_Configuration_Wizard::EASY_SETUP_OPTION);
                 // Check for expiration of the nonce
                 $expires = $config['nonce']->getExpiration();
                 if ($expires <= new DateTime("now", new DateTimeZone("UTC"))) {
                     throw new \LaunchKey\SDK\Service\Exception\InvalidRequestError('Easy config "nonce" has expired');
                 }
                 $rocketConfig = $response->getRocketConfig($this->crypt_service, $config['nonce']->getNonce());
                 $expected_callback_url = $this->wp_facade->admin_url('admin-ajax.php?action=' . LaunchKey_WP_Native_Client::CALLBACK_AJAX_ACTION);
                 // Verify the callback URL before attempting to decrypt the data
                 $actual_callback_url = $rocketConfig->getCallbackURL();
                 if ($actual_callback_url !== $expected_callback_url) {
                     throw new \LaunchKey\SDK\Service\Exception\InvalidRequestError(sprintf('Easy config is not for this site based on callback. Expected: %s, Actual: %s.', $expected_callback_url, $actual_callback_url));
                 }
                 $options = $this->get_option(LaunchKey_WP_Admin::OPTION_KEY);
                 $rocket_type = $rocketConfig->isWhiteLabel() ? LaunchKey_WP_Implementation_Type::WHITE_LABEL : LaunchKey_WP_Implementation_Type::NATIVE;
                 // Update options from server sent event service response
                 $options[LaunchKey_WP_Options::OPTION_IMPLEMENTATION_TYPE] = $rocket_type;
                 $options[LaunchKey_WP_Options::OPTION_ROCKET_KEY] = $rocketConfig->getKey();
                 $options[LaunchKey_WP_Options::OPTION_SECRET_KEY] = $rocketConfig->getSecret();
                 $options[LaunchKey_WP_Options::OPTION_PRIVATE_KEY] = $rocketConfig->getPrivateKey();
                 $this->update_option(LaunchKey_WP_Admin::OPTION_KEY, $options);
                 $response_string = "";
                 $body = $http_response->getBody();
                 $body->rewind();
                 while ($segment = $body->read(256)) {
                     $response_string .= $segment;
                 }
                 $this->wp_facade->header("Content-Type: text/plain", true, $http_response->getStatusCode());
                 $this->wp_facade->wp_die($response_string);
             }
         } catch (\Exception $e) {
             if ($this->wp_facade->is_debug_log()) {
                 $this->wp_facade->error_log('Callback Exception: ' . $e->getMessage());
             }
             if ($e instanceof \LaunchKey\SDK\Service\Exception\InvalidRequestError) {
                 $this->wp_facade->http_response_code(400);
                 $this->wp_facade->wp_die('Invalid Request');
             } else {
                 $this->wp_facade->http_response_code(500);
                 $this->wp_facade->wp_die('Server Error');
             }
         }
     }
 }
コード例 #3
0
ファイル: RequestTest.php プロジェクト: ringcentral/psr7
 public function testAddsPortToHeader()
 {
     $r = new Request('GET', 'http://foo.com:8124/bar');
     $this->assertEquals('foo.com:8124', $r->getHeaderLine('host'));
 }