Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
103 - 1
<?php
2
/*
3
 * Copyright 2012 Google Inc.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
 
18
use GuzzleHttp\Psr7;
19
use GuzzleHttp\Psr7\Request;
20
use GuzzleHttp\Psr7\Response;
21
use Psr\Http\Message\RequestInterface;
22
use Psr\Http\Message\ResponseInterface;
23
 
24
/**
25
 * Class to handle batched requests to the Google API service.
26
 */
27
class Google_Http_Batch
28
{
29
  const BATCH_PATH = 'batch';
30
 
31
  private static $CONNECTION_ESTABLISHED_HEADERS = array(
32
    "HTTP/1.0 200 Connection established\r\n\r\n",
33
    "HTTP/1.1 200 Connection established\r\n\r\n",
34
  );
35
 
36
  /** @var string Multipart Boundary. */
37
  private $boundary;
38
 
39
  /** @var array service requests to be executed. */
40
  private $requests = array();
41
 
42
  /** @var Google_Client */
43
  private $client;
44
 
45
  private $rootUrl;
46
 
47
  private $batchPath;
48
 
49
  public function __construct(
50
      Google_Client $client,
51
      $boundary = false,
52
      $rootUrl = null,
53
      $batchPath = null
54
  ) {
55
    $this->client = $client;
56
    $this->boundary = $boundary ?: mt_rand();
57
    $this->rootUrl = rtrim($rootUrl ?: $this->client->getConfig('base_path'), '/');
58
    $this->batchPath = $batchPath ?: self::BATCH_PATH;
59
  }
60
 
61
  public function add(RequestInterface $request, $key = false)
62
  {
63
    if (false == $key) {
64
      $key = mt_rand();
65
    }
66
 
67
    $this->requests[$key] = $request;
68
  }
69
 
70
  public function execute()
71
  {
72
    $body = '';
73
    $classes = array();
74
    $batchHttpTemplate = <<<EOF
75
--%s
76
Content-Type: application/http
77
Content-Transfer-Encoding: binary
78
MIME-Version: 1.0
79
Content-ID: %s
80
 
81
%s
82
%s%s
83
 
84
 
85
EOF;
86
 
87
    /** @var Google_Http_Request $req */
88
    foreach ($this->requests as $key => $request) {
89
      $firstLine = sprintf(
90
          '%s %s HTTP/%s',
91
          $request->getMethod(),
92
          $request->getRequestTarget(),
93
          $request->getProtocolVersion()
94
      );
95
 
96
      $content = (string) $request->getBody();
97
 
98
      $headers = '';
99
      foreach ($request->getHeaders() as $name => $values) {
100
          $headers .= sprintf("%s:%s\r\n", $name, implode(', ', $values));
101
      }
102
 
103
      $body .= sprintf(
104
          $batchHttpTemplate,
105
          $this->boundary,
106
          $key,
107
          $firstLine,
108
          $headers,
109
          $content ? "\n".$content : ''
110
      );
111
 
112
      $classes['response-' . $key] = $request->getHeaderLine('X-Php-Expected-Class');
113
    }
114
 
115
    $body .= "--{$this->boundary}--";
116
    $body = trim($body);
117
    $url = $this->rootUrl . '/' . $this->batchPath;
118
    $headers = array(
119
      'Content-Type' => sprintf('multipart/mixed; boundary=%s', $this->boundary),
120
      'Content-Length' => strlen($body),
121
    );
122
 
123
    $request = new Request(
124
        'POST',
125
        $url,
126
        $headers,
127
        $body
128
    );
129
 
130
    $response = $this->client->execute($request);
131
 
132
    return $this->parseResponse($response, $classes);
133
  }
134
 
135
  public function parseResponse(ResponseInterface $response, $classes = array())
136
  {
137
    $contentType = $response->getHeaderLine('content-type');
138
    $contentType = explode(';', $contentType);
139
    $boundary = false;
140
    foreach ($contentType as $part) {
141
      $part = explode('=', $part, 2);
142
      if (isset($part[0]) && 'boundary' == trim($part[0])) {
143
        $boundary = $part[1];
144
      }
145
    }
146
 
147
    $body = (string) $response->getBody();
148
    if (!empty($body)) {
149
      $body = str_replace("--$boundary--", "--$boundary", $body);
150
      $parts = explode("--$boundary", $body);
151
      $responses = array();
152
      $requests = array_values($this->requests);
153
 
154
      foreach ($parts as $i => $part) {
155
        $part = trim($part);
156
        if (!empty($part)) {
157
          list($rawHeaders, $part) = explode("\r\n\r\n", $part, 2);
158
          $headers = $this->parseRawHeaders($rawHeaders);
159
 
160
          $status = substr($part, 0, strpos($part, "\n"));
161
          $status = explode(" ", $status);
162
          $status = $status[1];
163
 
164
          list($partHeaders, $partBody) = $this->parseHttpResponse($part, false);
165
          $response = new Response(
166
              $status,
167
              $partHeaders,
168
              Psr7\stream_for($partBody)
169
          );
170
 
171
          // Need content id.
172
          $key = $headers['content-id'];
173
 
174
          try {
175
            $response = Google_Http_REST::decodeHttpResponse($response, $requests[$i-1]);
176
          } catch (Google_Service_Exception $e) {
177
            // Store the exception as the response, so successful responses
178
            // can be processed.
179
            $response = $e;
180
          }
181
 
182
          $responses[$key] = $response;
183
        }
184
      }
185
 
186
      return $responses;
187
    }
188
 
189
    return null;
190
  }
191
 
192
  private function parseRawHeaders($rawHeaders)
193
  {
194
    $headers = array();
195
    $responseHeaderLines = explode("\r\n", $rawHeaders);
196
    foreach ($responseHeaderLines as $headerLine) {
197
      if ($headerLine && strpos($headerLine, ':') !== false) {
198
        list($header, $value) = explode(': ', $headerLine, 2);
199
        $header = strtolower($header);
200
        if (isset($headers[$header])) {
201
          $headers[$header] .= "\n" . $value;
202
        } else {
203
          $headers[$header] = $value;
204
        }
205
      }
206
    }
207
    return $headers;
208
  }
209
 
210
  /**
211
   * Used by the IO lib and also the batch processing.
212
   *
213
   * @param $respData
214
   * @param $headerSize
215
   * @return array
216
   */
217
  private function parseHttpResponse($respData, $headerSize)
218
  {
219
    // check proxy header
220
    foreach (self::$CONNECTION_ESTABLISHED_HEADERS as $established_header) {
221
      if (stripos($respData, $established_header) !== false) {
222
        // existed, remove it
223
        $respData = str_ireplace($established_header, '', $respData);
224
        // Subtract the proxy header size unless the cURL bug prior to 7.30.0
225
        // is present which prevented the proxy header size from being taken into
226
        // account.
227
        // @TODO look into this
228
        // if (!$this->needsQuirk()) {
229
        //   $headerSize -= strlen($established_header);
230
        // }
231
        break;
232
      }
233
    }
234
 
235
    if ($headerSize) {
236
      $responseBody = substr($respData, $headerSize);
237
      $responseHeaders = substr($respData, 0, $headerSize);
238
    } else {
239
      $responseSegments = explode("\r\n\r\n", $respData, 2);
240
      $responseHeaders = $responseSegments[0];
241
      $responseBody = isset($responseSegments[1]) ? $responseSegments[1] :
242
                                                    null;
243
    }
244
 
245
    $responseHeaders = $this->parseRawHeaders($responseHeaders);
246
 
247
    return array($responseHeaders, $responseBody);
248
  }
249
}