Пример #1
0
 /**
  * Generate BCE Authorization Signature
  *
  * @param string $method HTTP Request Method.
  * @param string $resource The resource path.
  * @param array $params The query strings.
  * @param array $headers The extra http request headers.
  * @param number $timestamp The customized timestamp, if it's 0, the will use time() instead.
  * @param number $expiration_in_seconds The valid time in seconds.
  * @param array $headers_to_sign The extra http request headers will be included in the signature
  *
  * @return string
  */
 public function generateAuthorization($method, $resource, $params = array(), $headers = array(), $timestamp = 0, $expiration_in_seconds = 1800, $headers_to_sign = null)
 {
     $raw_session_key = sprintf("bce-auth-v%s/%s/%s/%d", "1", $this->access_key, baidubce_util_Time::bceTimeNow($timestamp), $expiration_in_seconds);
     $session_key = hash_hmac("sha256", $raw_session_key, $this->access_key_secret, false);
     $canonical_uri = "/v1" . baidubce_util_Coder::urlEncodeExceptSlash($resource);
     $canonical_query_string = $this->queryStringCanonicalization($params);
     list($canonical_headers, $signed_headers) = $this->headersCanonicalization($headers, $headers_to_sign);
     $raw_signature = $method . "\n" . $canonical_uri . "\n" . $canonical_query_string . "\n" . $canonical_headers;
     $signature = hash_hmac("sha256", $raw_signature, $session_key, false);
     if (count($signed_headers) > 0) {
         return sprintf('%s/%s/%s', $raw_session_key, implode(';', $signed_headers), $signature);
     }
     return sprintf('%s//%s', $raw_session_key, $signature);
 }
Пример #2
0
 /**
  * @param string $method one of PUT, GET, DELETE, HEAD, POST.
  * @param string $bucket The bucket name.
  * @param string $object The object name.
  * @param array $headers The http request headers.
  * @param string $body The request body.
  * @param array $params The query string
  * @param ?mixed $input_stream Read the http request body from this stream.
  * @param ?mixed $output_stream Write the http response to this stream.
  *
  * @return mixed status, body, http_headers
  */
 public function sendRequest($method, $bucket = '', $object = '', $headers = array(), $body = '', $params = array(), $input_stream = null, $output_stream = null)
 {
     $meta_size = 0;
     foreach ($headers as $k => $v) {
         if (strpos($k, 'x-bce-meta-') === 0) {
             $meta_size += strlen($v);
         } else {
             if (strcmp($k, 'x-bce-copy-source') === 0) {
                 $headers[$k] = baidubce_util_Coder::urlEncodeExceptSlash($v);
             }
         }
     }
     if ($meta_size > 2 * 1024) {
         throw new Exception('Meta data is too large.');
     }
     $resource = empty($object) ? sprintf('/%s', $bucket) : sprintf('/%s/%s', $bucket, $object);
     $content_length = 0;
     if (is_resource($input_stream)) {
         $stat = fstat($input_stream);
         $content_length = $stat['size'];
     } else {
         if (!is_null($input_stream) && method_exists($input_stream, 'getSize')) {
             $content_length = $input_stream->getSize();
         } else {
             $content_length = strlen($body);
         }
     }
     $user_agent = isset($this->config['User-Agent']) ? $this->config['User-Agent'] : 'BOS PHP SDK v1';
     $default_headers = array('x-bce-date' => baidubce_util_Time::bceTimeNow(), 'x-bce-request-id' => baidubce_util_BceTools::genUUid(), 'Expect' => '', 'Transfer-Encoding' => '', 'Content-Type' => 'application/json; charset=utf-8', 'Content-Length' => $content_length, 'User-Agent' => $user_agent);
     foreach ($default_headers as $k => $v) {
         if (!isset($headers[$k])) {
             $headers[$k] = $v;
         }
     }
     $authorization = $this->auth->generateAuthorization($method, $resource, $params, $headers);
     $headers['Authorization'] = $authorization;
     $url = sprintf('http://%s%s', $this->config['Host'], $this->getRequestUrl($resource, $params));
     $response = parent::sendRequest($method, $url, $body, $headers, $input_stream, $output_stream);
     $body = $response['body'];
     if (is_array($body) && isset($body['code']) && isset($body['message'])) {
         // Error hanppend.
         throw new baidubce_exception_BceServiceException($body['requestId'], $body['code'], $body['message'], $response['status']);
     }
     return $response;
 }