Reads response meta tags to guess content-type charset.

@param Response $response

@return Response
767871
protected function filterResponse($response)
    {
        $contentType = $response->getHeader('Content-Type');

        if (!$contentType || false === strpos($contentType, 'charset=')) {
            if (preg_match('/\<meta[^\>]+charset *= *["\']?([a-zA-Z\-0-9]+)/i', $response->getContent(), $matches)) {
                $headers = $response->getHeaders();
                $headers['Content-Type'] = $contentType.';charset='.$matches[1];

                $response = new Response($response->getContent(), $response->getStatus(), $headers);
            }
        }

        return parent::filterResponse($response);
    }
554984
private function parseResponse(string $response)
    {
        if (0 === preg_match_all('~--uuid:[a-zA-Z0-9-]+~', $response, $matches, PREG_OFFSET_CAPTURE)) {
            return [[
                'headers' => [
                    'Content-Type'              => 'application/json;charset=UTF-8',
                    'Content-Transfer-Encoding' => 'binary',
                    'Content-ID'                => '<jsonInfos>',
                ],
                'body'    => $response,
            ]];
        }

        $uuids = $matches[0];

        $parts = [];

        for ($i = 0; $i < count($uuids) - 1; $i++) {
            $start = $uuids[$i][1] + strlen($uuids[$i][0]) + 2;
            $parts[] = substr($response, $start, $uuids[$i + 1][1] - $start);
        }

        $responses = [];

        foreach ($parts as $part) {
            list($header, $body) = explode("\r\n\r\n", $part);

            $headers = [];
            foreach (explode("\r\n", $header) as $line) {
                list($key, $value) = explode(':', $line);
                $headers[trim($key)] = trim($value);
            }

            $responses[] = [
                'headers' => $headers,
                'body'    => $body,
            ];
        }

        return $responses;
    }
1333320
private function _buildResponse($response)
    {
        $sections = explode("\r\n\r\n", $response, 2);
        $body = isset($sections[1]) ? $sections[1] : NULL;
        $headers = array();
        $headerlines = explode("\n", $sections[0]);

        // process status
        list($http, $code, $message) = explode(' ', $headerlines[0], 3);

        // process headers
        foreach (array_slice($headerlines, 1) as $headerline) {
            $headers[] = HeaderField::fromString($headerline);
        }

        $response = new Response($code, $headers, $body);

        return $response;
    }
1187998
protected function decode_response($response) {
        // XMLRPC server in Moodle encodes response using function xmlrpc_encode_request() with method==null
        // see {@link webservice_xmlrpc_server::prepare_response()} . We should use xmlrpc_decode_request() for decoding too.
        $method = null;
        $encoding = null;
        if (preg_match('/^<\?xml version="1.0" encoding="([^"]*)"\?>/', $response, $matches)) {
            // Sometimes xmlrpc_decode_request() fails to recognise encoding, let's help it.
            $encoding = $matches[1];
        }
        $r = xmlrpc_decode_request($response, $method, $encoding);
        return $r;
    }
1096424
protected function parseResponse(\Buzz\Message\Response $response)
    {
        if (null !== $response->getStatusCode() && $response->getStatusCode() != 200) {
            return false;
        }
        $doc = new \DOMDocument();
        $doc->loadXML($response->getContent());
        $elems = $doc->getElementsByTagName("response-result");
        if (!$elems->length) {
            $this->logger->error('Response is empty');
            return false;
        }
        $responseElement = $elems->item(0);
        if ($responseElement->getAttribute("code") != "1001") {
            $this->logger->error($responseElement->getAttribute("code"). ' : '. $responseElement->getAttribute("desc"));
        }

        return ($responseElement->getAttribute("code") == "1001");
    }
563262
public function parse_tag_response( $response ) {
		if ( $this->validate_response( $response ) ) {
			return $response;
		}

		$arr = [];
		array_map(
			function ( $e ) use ( &$arr ) {
				$arr[] = $e->name;

				return $arr;
			},
			(array) $response
		);

		return $arr;
	}
670089
public static function parseResponse($response)
    {
        $response = is_array($response) ? $response : json_decode($response, true);

        if ( $response && $response['code'] == self::COSAPI_SUCCESS) {
            return $response;
        }

        return false;
    }
578021
protected function decodeBody(Response $response)
    {
        if ($response->isServerError()) {
            throw new Exception\ClientException(sprintf(
                "'%s' provider encountered a '%s' error while querying '%s'",
                $this->getIdentifier(),
                $response->getReasonPhrase(),
                $this->uri
            ));
        }

        $body = Json::decode($response->getBody());

        if ($response->isClientError()) {
            throw new Exception\ClientException($body->error->message, $response->getStatusCode());
        }

        return $body;
    }
914734
public static function decode_response($response)
    {
        if (!$response) {
            return PASSWORD_CONNECT_ERROR;
        }

        // $result should be `null` or `stdClass` object
        $result = json_decode($response);

        // The UAPI may return HTML instead of JSON on missing authentication
        if ($result && $result->status === 1) {
            return PASSWORD_SUCCESS;
        }

        if ($result && is_array($result->errors) && count($result->errors) > 0) {
            return array(
                'code'    => PASSWORD_ERROR,
                'message' => $result->errors[0],
            );
        }

        return PASSWORD_ERROR;
    }
254860
public function parseResponse($response)
    {
        $array = [];

        $exploded = explode('&', $response);

        foreach ($exploded as $explode) {
            $line = explode('=', $explode);

            if (isset($line[1])) {
                $array[$line[0]] = urldecode($line[1]);
            } else {
                $array[] = $explode;
            }
        }

        return $array;
    }
1076700
protected function parseResponse( Response$response ):array
	{
		$headers= $response->headers;
		$body= $this->achieveSerializer( $this->response_content_type )->decode( $response->body );

		return $this->response_transformer->transform(
			[
				'headers'=> $headers,
				'body'=> $body,
			]
		);
	}
880174
private static function getHeaderAndBody($response) {
    $header = self::http_parse_headers(substr($response, 0, strrpos($response,"\r\n\r\n")));
    $body = substr($response, strrpos($response,"\r\n\r\n")+4);

    return array($header,$body);
  }
500734
private function _loadLinks($response) {
    $this->_links = array();

    if (isset($response->headers['link'])) {
      $links = $response->headers['link'];
      preg_match_all('/\<([^>]+)\>; rel=\"([^"]+)\"/', $links, $matches);
      if (sizeof($matches) > 2) {
        for ($i = 0; $i < sizeof($matches[1]); $i++) {
          $this->_links[$matches[2][$i]] = $matches[1][$i];
        }
      }
    }
  }
250455
public static function parseLinkHeader(Response $response)
    {
        $linkHeader = $response->getHeader('Link');

        if (!strpos($linkHeader, 'rel')) {
            return null;
        }

        $out = [
            "next" => null,
            "last" => null,
            "prev" => null,
            "first" => null,
        ];

        $links = explode(',', $linkHeader);
        foreach ($links as $link) {
            $parts = explode(';', $link);
            if (count($parts) < 2) {
                continue;
            }

            // Get the URL
            $url = trim(array_shift($parts), '<> ');
            $relParts = explode('=', trim(array_shift($parts)));

            if (count($relParts) !== 2 || $relParts[0] != 'rel') {
                continue;
            }

            // Get the rel="" value (next, prev, first, last)
            $rel = trim($relParts[1], ' "\'');
            $out[$rel] = $url;
        }

        return $out;
    }
835128
public static function getResponseBodySummary(Response $response): string
    {
        $body = $response->getBody();
        $size = $body->getSize();
        if ($size === 0) {
            return '';
        }
        $summary = $body->read(120);
        $body->rewind();
        if ($size > 120) {
            $summary .= ' (truncated...)';
        }
        // Matches any printable character, including unicode characters:
        // letters, marks, numbers, punctuation, spacing, and separators.
        if (preg_match('/[^\pL\pM\pN\pP\pS\pZ\n\r\t]/', $summary)) {
            return '';
        }

        return $summary;
    }
1127599
protected function processTogglResponse($response)
    {
        // We only need to do something if results are paginated
        if ($response->total_count <= 50) {
            return $response->data;
        }

        $reqdata = [];
        $reqdata = json_encode($reqdata);
        $pages = ceil($response->total_count / 50);
        $curr_page = 1;
        $data = $response->data;

        $orig_uri = $this->uri;

        while ($curr_page < $pages) {
            $curr_page++;
            $this->uri .= '&page=' . $curr_page;
            $response = $this->processRequest($reqdata, self::GET);
            $this->_handleError($reqdata, $response);
            foreach ($response->data as $entry) {
                $data[] = $entry;
            }
            $this->uri = $orig_uri;
        }

        return $data;
    }
715178
public function parse_meta_response( $response ) {
		if ( $this->validate_response( $response ) ) {
			return $response;
		}
		$arr      = [];
		$response = [ $response ];

		array_filter(
			$response,
			function ( $e ) use ( &$arr ) {
				$arr['private']      = $e->private;
				$arr['last_updated'] = $e->pushed_at;
				$arr['watchers']     = $e->watchers;
				$arr['forks']        = $e->forks;
				$arr['open_issues']  = $e->open_issues;
			}
		);

		return $arr;
	}