/** * Get all of the received requests as a RingPHP request structure. * * @return array * @throws \RuntimeException */ public static function received() { if (!self::$started) { return []; } $response = self::send('GET', '/guzzle-server/requests'); $body = Core::body($response); $result = json_decode($body, true); if ($result === false) { throw new \RuntimeException('Error decoding response: ' . json_last_error()); } foreach ($result as &$res) { if (isset($res['uri'])) { $res['resource'] = $res['uri']; } if (isset($res['query_string'])) { $res['resource'] .= '?' . $res['query_string']; } if (!isset($res['resource'])) { $res['resource'] = ''; } // Ensure that headers are all arrays if (isset($res['headers'])) { foreach ($res['headers'] as &$h) { $h = (array) $h; } unset($h); } } unset($res); return $result; }
/** * @param string $url * * @return boolean * * @throws \RuntimeException */ public static function download($url) { $handler = new Ring\Client\CurlHandler(); $response = $handler(['http_method' => 'GET', 'uri' => sprintf(':%s/%s', parse_url($url, PHP_URL_PORT), parse_url($url, PHP_URL_PATH)), 'headers' => ['scheme' => [parse_url($url, PHP_URL_SCHEME)], 'host' => [parse_url($url, PHP_URL_HOST)]]]); $response->wait(); if ($response['status'] != 200) { throw new \RuntimeException(sprintf('%s: %s (%s)', $response['effective_url'], $response['reason'], $response['status'])); } $json = Ring\Core::body($response); return self::load($json); }
private function getDefaultOptions(array $request) { $headers = ""; foreach ($request['headers'] as $name => $value) { foreach ((array) $value as $val) { $headers .= "{$name}: {$val}\r\n"; } } $context = array('http' => array('method' => $request['http_method'], 'header' => $headers, 'protocol_version' => isset($request['version']) ? $request['version'] : 1.1, 'ignore_errors' => true, 'follow_location' => 0)); $body = Core::body($request); if (isset($body)) { $context['http']['content'] = $body; // Prevent the HTTP handler from adding a Content-Type header. if (!Core::hasHeader($request, 'Content-Type')) { $context['http']['header'] .= "Content-Type:\r\n"; } } $context['http']['header'] = rtrim($context['http']['header']); return $context; }
public function testSupports100Continue() { Server::flush(); Server::enqueue([['status' => '200', 'reason' => 'OK', 'headers' => ['Test' => ['Hello'], 'Content-Length' => ['4']], 'body' => 'test']]); $request = ['http_method' => 'PUT', 'headers' => ['Host' => [Server::$host], 'Expect' => ['100-Continue']], 'body' => 'test']; $handler = new StreamHandler(); $response = $handler($request); $this->assertEquals(200, $response['status']); $this->assertEquals('OK', $response['reason']); $this->assertEquals(['Hello'], $response['headers']['Test']); $this->assertEquals(['4'], $response['headers']['Content-Length']); $this->assertEquals('test', Core::body($response)); }
private function applyBody(array $request, array &$options) { $contentLength = Core::firstHeader($request, 'Content-Length'); $size = $contentLength !== null ? (int) $contentLength : null; // Send the body as a string if the size is less than 1MB OR if the // [client][curl][body_as_string] request value is set. if ($size !== null && $size < 1000000 || isset($request['client']['curl']['body_as_string']) || is_string($request['body'])) { $options[CURLOPT_POSTFIELDS] = Core::body($request); // Don't duplicate the Content-Length header $this->removeHeader('Content-Length', $options); $this->removeHeader('Transfer-Encoding', $options); } else { $options[CURLOPT_UPLOAD] = true; if ($size !== null) { // Let cURL handle setting the Content-Length header $options[CURLOPT_INFILESIZE] = $size; $this->removeHeader('Content-Length', $options); } $this->addStreamingBody($request, $options); } // If the Expect header is not present, prevent curl from adding it if (!Core::hasHeader($request, 'Expect')) { $options[CURLOPT_HTTPHEADER][] = 'Expect:'; } // cURL sometimes adds a content-type by default. Prevent this. if (!Core::hasHeader($request, 'Content-Type')) { $options[CURLOPT_HTTPHEADER][] = 'Content-Type:'; } }
/** * @expectedException \InvalidArgumentException */ public function testEnsuresBodyIsValid() { Core::body(['body' => false]); }
private function getDefaultOptions(array $request) { $headers = []; foreach ($request['headers'] as $name => $value) { foreach ((array) $value as $val) { $headers[] = "{$name}: {$val}"; } } $context = ['http' => ['method' => $request['http_method'], 'header' => $headers, 'protocol_version' => '1.1', 'ignore_errors' => true, 'follow_location' => 0]]; $body = Core::body($request); if (isset($body)) { $context['http']['content'] = $body; // Prevent the HTTP handler from adding a Content-Type header. if (!Core::hasHeader($request, 'Content-Type')) { $context['http']['header'][] .= "Content-Type:"; } } return $context; }