Completed
Push — master ( 67852b...2cf6f5 )
by
unknown
9s
created

Client::ucwordsHyphen()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Qiniu\Http;
3
4
use Qiniu\Config;
5
use Qiniu\Http\Request;
6
use Qiniu\Http\Response;
7
8
final class Client
9
{
10 57
    public static function get($url, array $headers = array())
11
    {
12 57
        $request = new Request('GET', $url, $headers);
13 57
        return self::sendRequest($request);
14
    }
15
16 45
    public static function post($url, $body, array $headers = array())
17
    {
18 45
        $request = new Request('POST', $url, $headers, $body);
19 45
        return self::sendRequest($request);
20
    }
21
22
    public static function multipartPost(
23
        $url,
24
        $fields,
25
        $name,
26
        $fileName,
27
        $fileBody,
28
        $mimeType = null,
29
        array $headers = array()
30
    ) {
31
        $data = array();
32
        $mimeBoundary = md5(microtime());
33
34
        foreach ($fields as $key => $val) {
35
            array_push($data, '--' . $mimeBoundary);
36
            array_push($data, "Content-Disposition: form-data; name=\"$key\"");
37
            array_push($data, '');
38
            array_push($data, $val);
39
        }
40
41
        array_push($data, '--' . $mimeBoundary);
42
        $finalMimeType = empty($mimeType) ? 'application/octet-stream' : $mimeType;
43
        $finalFileName = self::escapeQuotes($fileName);
44
        array_push($data, "Content-Disposition: form-data; name=\"$name\"; filename=\"$finalFileName\"");
45
        array_push($data, "Content-Type: $finalMimeType");
46
        array_push($data, '');
47
        array_push($data, $fileBody);
48
49
        array_push($data, '--' . $mimeBoundary . '--');
50
        array_push($data, '');
51
52
        $body = implode("\r\n", $data);
53
        $contentType = 'multipart/form-data; boundary=' . $mimeBoundary;
54
        $headers['Content-Type'] = $contentType;
55
        $request = new Request('POST', $url, $headers, $body);
56
        return self::sendRequest($request);
57
    }
58
59 102
    private static function userAgent()
60
    {
61 102
        $sdkInfo = "QiniuPHP/" . Config::SDK_VER;
62
63 102
        $systemInfo = php_uname("s");
64 102
        $machineInfo = php_uname("m");
65
66 102
        $envInfo = "($systemInfo/$machineInfo)";
67
68 102
        $phpVer = phpversion();
69
70 102
        $ua = "$sdkInfo $envInfo PHP/$phpVer";
71 102
        return $ua;
72
    }
73
74 102
    public static function sendRequest($request)
75
    {
76 102
        $t1 = microtime(true);
77 102
        $ch = curl_init();
78
        $options = array(
79 102
            CURLOPT_USERAGENT => self::userAgent(),
80 102
            CURLOPT_RETURNTRANSFER => true,
81 102
            CURLOPT_SSL_VERIFYPEER => false,
82 102
            CURLOPT_SSL_VERIFYHOST => false,
83 102
            CURLOPT_HEADER => true,
84 102
            CURLOPT_NOBODY => false,
85 102
            CURLOPT_CUSTOMREQUEST => $request->method,
86 102
            CURLOPT_URL => $request->url,
87 102
        );
88
89
        // Handle open_basedir & safe mode
90 102
        if (!ini_get('safe_mode') && !ini_get('open_basedir')) {
91 102
            $options[CURLOPT_FOLLOWLOCATION] = true;
92 102
        }
93
94 102
        if (!empty($request->headers)) {
95 48
            $headers = array();
96 48
            foreach ($request->headers as $key => $val) {
97 48
                array_push($headers, "$key: $val");
98 48
            }
99 48
            $options[CURLOPT_HTTPHEADER] = $headers;
100 48
        }
101 102
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
102
103 102
        if (!empty($request->body)) {
104 24
            $options[CURLOPT_POSTFIELDS] = $request->body;
105 24
        }
106 102
        curl_setopt_array($ch, $options);
107 102
        $result = curl_exec($ch);
108 102
        $t2 = microtime(true);
109 102
        $duration = round($t2 - $t1, 3);
110 102
        $ret = curl_errno($ch);
111 102
        if ($ret !== 0) {
112 4
            $r = new Response(-1, $duration, array(), null, curl_error($ch));
113 4
            curl_close($ch);
114 4
            return $r;
115
        }
116 101
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
117 101
        $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
118 101
        $headers = self::parseHeaders(substr($result, 0, $header_size));
119 101
        $body = substr($result, $header_size);
120 101
        curl_close($ch);
121 101
        return new Response($code, $duration, $headers, $body, null);
122
    }
123
124 101
    private static function parseHeaders($raw)
125
    {
126 101
        $headers = array();
127 101
        $headerLines = explode("\r\n", $raw);
128 101
        foreach ($headerLines as $line) {
129 101
            $headerLine = trim($line);
130 101
            $kv = explode(':', $headerLine);
131 101
            if (count($kv) > 1) {
132 101
                $kv[0] =self::ucwordsHyphen($kv[0]);
133 101
                $headers[$kv[0]] = trim($kv[1]);
134 101
            }
135 101
        }
136 101
        return $headers;
137
    }
138
139
    private static function escapeQuotes($str)
140
    {
141
        $find = array("\\", "\"");
142
        $replace = array("\\\\", "\\\"");
143
        return str_replace($find, $replace, $str);
144
    }
145
    
146 101
    private static function ucwordsHyphen($str)
147
    {
148 101
        return str_replace('- ', '-', ucwords(str_replace('-', '- ', $str)));
149
    }
150
}
151