[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/guzzlehttp/guzzle/src/Message/ -> MessageParser.php (source)

   1  <?php
   2  namespace GuzzleHttp\Message;
   3  
   4  /**
   5   * Request and response parser used by Guzzle
   6   */
   7  class MessageParser
   8  {
   9      /**
  10       * Parse an HTTP request message into an associative array of parts.
  11       *
  12       * @param string $message HTTP request to parse
  13       *
  14       * @return array|bool Returns false if the message is invalid
  15       */
  16      public function parseRequest($message)
  17      {
  18          if (!($parts = $this->parseMessage($message))) {
  19              return false;
  20          }
  21  
  22          // Parse the protocol and protocol version
  23          if (isset($parts['start_line'][2])) {
  24              $startParts = explode('/', $parts['start_line'][2]);
  25              $protocol = strtoupper($startParts[0]);
  26              $version = isset($startParts[1]) ? $startParts[1] : '1.1';
  27          } else {
  28              $protocol = 'HTTP';
  29              $version = '1.1';
  30          }
  31  
  32          $parsed = [
  33              'method'   => strtoupper($parts['start_line'][0]),
  34              'protocol' => $protocol,
  35              'protocol_version' => $version,
  36              'headers'  => $parts['headers'],
  37              'body'     => $parts['body']
  38          ];
  39  
  40          $parsed['request_url'] = $this->getUrlPartsFromMessage(
  41              (isset($parts['start_line'][1]) ? $parts['start_line'][1] : ''), $parsed);
  42  
  43          return $parsed;
  44      }
  45  
  46      /**
  47       * Parse an HTTP response message into an associative array of parts.
  48       *
  49       * @param string $message HTTP response to parse
  50       *
  51       * @return array|bool Returns false if the message is invalid
  52       */
  53      public function parseResponse($message)
  54      {
  55          if (!($parts = $this->parseMessage($message))) {
  56              return false;
  57          }
  58  
  59          list($protocol, $version) = explode('/', trim($parts['start_line'][0]));
  60  
  61          return [
  62              'protocol'         => $protocol,
  63              'protocol_version' => $version,
  64              'code'             => $parts['start_line'][1],
  65              'reason_phrase'    => isset($parts['start_line'][2]) ? $parts['start_line'][2] : '',
  66              'headers'          => $parts['headers'],
  67              'body'             => $parts['body']
  68          ];
  69      }
  70  
  71      /**
  72       * Parse a message into parts
  73       *
  74       * @param string $message Message to parse
  75       *
  76       * @return array|bool
  77       */
  78      private function parseMessage($message)
  79      {
  80          if (!$message) {
  81              return false;
  82          }
  83  
  84          $startLine = null;
  85          $headers = [];
  86          $body = '';
  87  
  88          // Iterate over each line in the message, accounting for line endings
  89          $lines = preg_split('/(\\r?\\n)/', $message, -1, PREG_SPLIT_DELIM_CAPTURE);
  90          for ($i = 0, $totalLines = count($lines); $i < $totalLines; $i += 2) {
  91  
  92              $line = $lines[$i];
  93  
  94              // If two line breaks were encountered, then this is the end of body
  95              if (empty($line)) {
  96                  if ($i < $totalLines - 1) {
  97                      $body = implode('', array_slice($lines, $i + 2));
  98                  }
  99                  break;
 100              }
 101  
 102              // Parse message headers
 103              if (!$startLine) {
 104                  $startLine = explode(' ', $line, 3);
 105              } elseif (strpos($line, ':')) {
 106                  $parts = explode(':', $line, 2);
 107                  $key = trim($parts[0]);
 108                  $value = isset($parts[1]) ? trim($parts[1]) : '';
 109                  if (!isset($headers[$key])) {
 110                      $headers[$key] = $value;
 111                  } elseif (!is_array($headers[$key])) {
 112                      $headers[$key] = [$headers[$key], $value];
 113                  } else {
 114                      $headers[$key][] = $value;
 115                  }
 116              }
 117          }
 118  
 119          return [
 120              'start_line' => $startLine,
 121              'headers'    => $headers,
 122              'body'       => $body
 123          ];
 124      }
 125  
 126      /**
 127       * Create URL parts from HTTP message parts
 128       *
 129       * @param string $requestUrl Associated URL
 130       * @param array  $parts      HTTP message parts
 131       *
 132       * @return array
 133       */
 134      private function getUrlPartsFromMessage($requestUrl, array $parts)
 135      {
 136          // Parse the URL information from the message
 137          $urlParts = ['path' => $requestUrl, 'scheme' => 'http'];
 138  
 139          // Check for the Host header
 140          if (isset($parts['headers']['Host'])) {
 141              $urlParts['host'] = $parts['headers']['Host'];
 142          } elseif (isset($parts['headers']['host'])) {
 143              $urlParts['host'] = $parts['headers']['host'];
 144          } else {
 145              $urlParts['host'] = null;
 146          }
 147  
 148          if (false === strpos($urlParts['host'], ':')) {
 149              $urlParts['port'] = '';
 150          } else {
 151              $hostParts = explode(':', $urlParts['host']);
 152              $urlParts['host'] = trim($hostParts[0]);
 153              $urlParts['port'] = (int) trim($hostParts[1]);
 154              if ($urlParts['port'] == 443) {
 155                  $urlParts['scheme'] = 'https';
 156              }
 157          }
 158  
 159          // Check if a query is present
 160          $path = $urlParts['path'];
 161          $qpos = strpos($path, '?');
 162          if ($qpos) {
 163              $urlParts['query'] = substr($path, $qpos + 1);
 164              $urlParts['path'] = substr($path, 0, $qpos);
 165          } else {
 166              $urlParts['query'] = '';
 167          }
 168  
 169          return $urlParts;
 170      }
 171  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1