/** * */ private function doHttpRequest($url, $method, $headers, $body) { $req = new \google\appengine\URLFetchRequest(); $req->setUrl($url); $req->setMethod(self::$request_map[$method]); $req->setMustValidateServerCertificate(true); if (isset($body)) { $req->setPayload($body); } foreach ($headers as $key => $value) { $h = $req->addHeader(); $h->setKey($key); $h->setValue($value); } $resp = new \google\appengine\URLFetchResponse(); for ($num_retries = 0;; $num_retries++) { try { ApiProxy::makeSyncCall('urlfetch', 'Fetch', $req, $resp); } catch (ApplicationError $e) { syslog(LOG_ERR, sprintf("Call to URLFetch failed with application error %d.", $e->getApplicationError())); return false; } $status_code = $resp->getStatusCode(); if ($num_retries < $this->context_options['max_retries'] && in_array($status_code, self::$retryable_statuses) && (connection_status() & CONNECTION_TIMEOUT) == 0) { usleep(rand(0, 1000000 * pow(2, $num_retries))); if ((connection_status() & CONNECTION_TIMEOUT) == CONNECTION_TIMEOUT) { break; } } else { break; } } $response_headers = []; foreach ($resp->getHeaderList() as $header) { // TODO: Do we need to support multiple headers with the same key? $response_headers[trim($header->getKey())] = trim($header->getValue()); } return ['status_code' => $resp->getStatusCode(), 'headers' => $response_headers, 'body' => $resp->getContent()]; }
/** * */ private function doHttpRequest($url, $method, $headers, $body) { $req = new \google\appengine\URLFetchRequest(); $req->setUrl($url); $req->setMethod(self::$request_map[$method]); $req->setMustValidateServerCertificate(true); if (isset($body)) { $req->setPayload($body); } foreach ($headers as $key => $value) { $h = $req->addHeader(); $h->setKey($key); $h->setValue($value); } $resp = new \google\appengine\URLFetchResponse(); try { ApiProxy::makeSyncCall('urlfetch', 'Fetch', $req, $resp); } catch (ApplicationError $e) { syslog(LOG_ERR, sprintf("Call to URLFetch failed with application error %d.", $e->getApplicationError())); return false; } $response_headers = []; foreach ($resp->getHeaderList() as $header) { // TODO: Do we need to support multiple headers with the same key? $response_headers[trim($header->getKey())] = trim($header->getValue()); } return ['status_code' => $resp->getStatusCode(), 'headers' => $response_headers, 'body' => $resp->getContent()]; }
/** * */ private function doHttpRequest($url, $method, $headers, $body) { $connection_timeout = $this->context_options['connection_timeout_seconds']; $req = new \google\appengine\URLFetchRequest(); $req->setUrl($url); $req->setMethod(self::$request_map[$method]); $req->setMustValidateServerCertificate(true); $req->setDeadline($connection_timeout); $req->setFollowRedirects(false); if (isset($body)) { $req->setPayload($body); } foreach ($headers as $key => $value) { $h = $req->addHeader(); $h->setKey($key); $h->setValue($value); } $resp = new \google\appengine\URLFetchResponse(); for ($num_retries = 0;; $num_retries++) { try { ApiProxy::makeSyncCall('urlfetch', 'Fetch', $req, $resp, $connection_timeout); } catch (ApplicationError $e) { if (in_array($e->getApplicationError(), self::$retry_exception_codes)) { // We need to set a plausible value in the URLFetchResponse proto in // case the retry loop falls through - this will also cause a retry // if one is available. $resp->setStatusCode(HttpResponse::GATEWAY_TIMEOUT); } else { syslog(LOG_ERR, sprintf("Call to URLFetch failed with application error %d " . "for url %s.", $e->getApplicationError(), $url)); return false; } } $status_code = $resp->getStatusCode(); if ($num_retries < $this->context_options['max_retries'] && in_array($status_code, self::$retryable_statuses) && (connection_status() & CONNECTION_TIMEOUT) == 0) { usleep(rand(0, 1000000 * pow(2, $num_retries))); if ((connection_status() & CONNECTION_TIMEOUT) == CONNECTION_TIMEOUT) { break; } } else { break; } } $response_headers = []; foreach ($resp->getHeaderList() as $header) { // TODO: Do we need to support multiple headers with the same key? $response_headers[trim($header->getKey())] = trim($header->getValue()); } return ['status_code' => $resp->getStatusCode(), 'headers' => $response_headers, 'body' => $resp->getContent()]; }
/** * Make a HTTP request to a supplied URL. * * Much of the logic here is plagiarized from the WP_Http_Curl as to avoid * trying to re-invent the wheel. * * @param $url * @param $args * @return bool */ public function request($url, $args) { $r = wp_parse_args($args, self::$request_defaults); // Construct Cookie: header if any cookies are set. WP_Http::buildCookieHeader($r); $is_local = isset($r['local']) && $r['local']; $ssl_verify = isset($r['sslverify']) && $r['sslverify']; if ($is_local) { $ssl_verify = apply_filters('https_local_ssl_verify', $ssl_verify); } else { $ssl_verify = apply_filters('https_ssl_verify', $ssl_verify); } // For now, lets not support streaming into a file and see what breaks if (isset($r['filename'])) { return new WP_Error('http_request_failed', __('Saving to a file is not currently supported.')); } $req = new \google\appengine\URLFetchRequest(); $req->setUrl($url); $req->setMethod(self::$request_map[$r['method']]); $req->setMustValidateServerCertificate($ssl_verify); if (isset($r['body'])) { $req->setPayload($r['body']); } if (isset($r['timeout'])) { $req->setDeadline($r['timeout']); } // App Engine does not allow setting the number of redirects, only if we // follow redirects or not. $req->setFollowRedirects(isset($r['redirection']) && $r['redirection'] != 0); foreach ($r['headers'] as $key => $value) { $header = $req->addHeader(); $header->setKey($key); $header->setValue($value); } $resp = new \google\appengine\URLFetchResponse(); try { ApiProxy::makeSyncCall('urlfetch', 'Fetch', $req, $resp); } catch (ApplicationError $e) { syslog(LOG_ERR, sprintf("Call to URLFetch failed with application error %d for url %s.", $e->getApplicationError(), $url)); return new \WP_Error('http_request_failed', $e->getMessage()); } $response = []; $response['code'] = $resp->getStatusCode(); $response['message'] = \get_status_header_desc($resp->getStatusCode()); $headers = []; $cookies = []; foreach ($resp->getHeaderList() as $header) { $key = strtolower($header->getKey()); $value = trim($header->getValue()); // If a header has multiple values then it is stored in an array if (isset($headers[$key])) { if (!is_array($headers[$key])) { $headers[$key] = [$headers[$key]]; } $headers[$key][] = $value; } else { $headers[$key] = $value; } if ('set-cookie' == $key) { $cookies[] = new \WP_Http_Cookie($value, $url); } } $theBody = $resp->getContent(); if (true === $r['decompress'] && true === WP_Http_Encoding::should_decode($headers)) { $theBody = WP_Http_Encoding::decompress($theBody); } if (isset($r['limit_response_size']) && strlen($theBody) > $r['limit_response_size']) { $theBody = substr($theBody, 0, $r['limit_response_size']); } $response = ['response' => $response, 'body' => $theBody, 'cookies' => $cookies, 'headers' => $headers]; return $response; }