BaiduBCS::authenticate PHP Method

authenticate() private method

将消息发往Baidu BCS.
private authenticate ( array $opt ) : BCS_ResponseCore
$opt array
return BCS_ResponseCore
    private function authenticate($opt)
    {
        //set common param into opt
        $opt[self::AK] = $this->ak;
        $opt[self::SK] = $this->sk;
        // Validate the S3 bucket name, only list_bucket didnot need validate_bucket
        if (!('/' == $opt[self::OBJECT] && '' == $opt[self::BUCKET] && 'GET' == $opt[self::METHOD] && !isset($opt[self::QUERY_STRING][self::ACL])) && !self::validate_bucket($opt[self::BUCKET])) {
            throw new BCS_Exception($opt[self::BUCKET] . 'is not valid, please check!');
        }
        //Validate object
        if (isset($opt[self::OBJECT]) && !self::validate_object($opt[self::OBJECT])) {
            throw new BCS_Exception("Invalid object param[" . $opt[self::OBJECT] . "], please check.", -1);
        }
        //construct url
        $url = $this->format_url($opt);
        if ($url === false) {
            throw new BCS_Exception('Can not format url, please check your param!', -1);
        }
        $opt['url'] = $url;
        //var_dump($opt);die();
        //$this->log ( "[method:" . $opt [self::METHOD] . "][url:$url]", $opt );	//未寒2013.12.24
        //build request
        $request = new BCS_RequestCore($opt['url']);
        $headers = array('Content-Type' => 'application/x-www-form-urlencoded');
        $request->set_method($opt[self::METHOD]);
        //Write get_object content to fileWriteTo
        if (isset($opt['fileWriteTo'])) {
            $request->set_write_file($opt['fileWriteTo']);
        }
        // Merge the HTTP headers
        if (isset($opt[self::HEADERS])) {
            $headers = array_merge($headers, $opt[self::HEADERS]);
        }
        // Set content to Http-Body
        if (isset($opt['content'])) {
            $request->set_body($opt['content']);
        }
        // Upload file
        if (isset($opt['fileUpload'])) {
            if (!file_exists($opt['fileUpload'])) {
                throw new BCS_Exception('File[' . $opt['fileUpload'] . '] not found!', -1);
            }
            $request->set_read_file($opt['fileUpload']);
            // Determine the length to read from the file
            $length = $request->read_stream_size;
            // The file size by default
            $file_size = $length;
            if (isset($opt["length"])) {
                if ($opt["length"] > $file_size) {
                    throw new BCS_Exception("Input opt[length] invalid! It can not bigger than file-size", -1);
                }
                $length = $opt['length'];
            }
            if (isset($opt['seekTo']) && !isset($opt["length"])) {
                // Read from seekTo until EOF by default, when set seekTo but not set $opt["length"]
                $length -= (int) $opt['seekTo'];
            }
            $request->set_read_stream_size($length);
            // Attempt to guess the correct mime-type
            if ($headers['Content-Type'] === 'application/x-www-form-urlencoded') {
                $extension = explode('.', $opt['fileUpload']);
                $extension = array_pop($extension);
                $mime_type = BCS_MimeTypes::get_mimetype($extension);
                $headers['Content-Type'] = $mime_type;
            }
            $headers['Content-MD5'] = '';
        }
        // Handle streaming file offsets
        if (isset($opt['seekTo'])) {
            // Pass the seek position to BCS_RequestCore
            $request->set_seek_position((int) $opt['seekTo']);
        }
        // Add headers to request and compute the string to sign
        foreach ($headers as $header_key => $header_value) {
            // Strip linebreaks from header values as they're illegal and can allow for security issues
            $header_value = str_replace(array("\r", "\n"), '', $header_value);
            // Add the header if it has a value
            if ($header_value !== '') {
                $request->add_header($header_key, $header_value);
            }
        }
        // Set the curl options.
        if (isset($opt['curlopts']) && count($opt['curlopts'])) {
            $request->set_curlopts($opt['curlopts']);
        }
        $request->send_request();
        return new BCS_ResponseCore($request->get_response_header(), $request->get_response_body(), $request->get_response_code());
    }