Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

      (Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

MessageParser.php

Zuletzt modifiziert: 09.10.2024, 12:57 - Dateigröße: 5.13 KiB


 001  <?php
 002  namespace GuzzleHttp\Message;
 003   
 004  /**
 005   * Request and response parser used by Guzzle
 006   */
 007  class MessageParser
 008  {
 009      /**
 010       * Parse an HTTP request message into an associative array of parts.
 011       *
 012       * @param string $message HTTP request to parse
 013       *
 014       * @return array|bool Returns false if the message is invalid
 015       */
 016      public function parseRequest($message)
 017      {
 018          if (!($parts = $this->parseMessage($message))) {
 019              return false;
 020          }
 021   
 022          // Parse the protocol and protocol version
 023          if (isset($parts['start_line'][2])) {
 024              $startParts = explode('/', $parts['start_line'][2]);
 025              $protocol = strtoupper($startParts[0]);
 026              $version = isset($startParts[1]) ? $startParts[1] : '1.1';
 027          } else {
 028              $protocol = 'HTTP';
 029              $version = '1.1';
 030          }
 031   
 032          $parsed = [
 033              'method'   => strtoupper($parts['start_line'][0]),
 034              'protocol' => $protocol,
 035              'protocol_version' => $version,
 036              'headers'  => $parts['headers'],
 037              'body'     => $parts['body']
 038          ];
 039   
 040          $parsed['request_url'] = $this->getUrlPartsFromMessage(
 041              (isset($parts['start_line'][1]) ? $parts['start_line'][1] : ''), $parsed);
 042   
 043          return $parsed;
 044      }
 045   
 046      /**
 047       * Parse an HTTP response message into an associative array of parts.
 048       *
 049       * @param string $message HTTP response to parse
 050       *
 051       * @return array|bool Returns false if the message is invalid
 052       */
 053      public function parseResponse($message)
 054      {
 055          if (!($parts = $this->parseMessage($message))) {
 056              return false;
 057          }
 058   
 059          list($protocol, $version) = explode('/', trim($parts['start_line'][0]));
 060   
 061          return [
 062              'protocol'         => $protocol,
 063              'protocol_version' => $version,
 064              'code'             => $parts['start_line'][1],
 065              'reason_phrase'    => isset($parts['start_line'][2]) ? $parts['start_line'][2] : '',
 066              'headers'          => $parts['headers'],
 067              'body'             => $parts['body']
 068          ];
 069      }
 070   
 071      /**
 072       * Parse a message into parts
 073       *
 074       * @param string $message Message to parse
 075       *
 076       * @return array|bool
 077       */
 078      private function parseMessage($message)
 079      {
 080          if (!$message) {
 081              return false;
 082          }
 083   
 084          $startLine = null;
 085          $headers = [];
 086          $body = '';
 087   
 088          // Iterate over each line in the message, accounting for line endings
 089          $lines = preg_split('/(\\r?\\n)/', $message, -1, PREG_SPLIT_DELIM_CAPTURE);
 090          for ($i = 0, $totalLines = count($lines); $i < $totalLines; $i += 2) {
 091   
 092              $line = $lines[$i];
 093   
 094              // If two line breaks were encountered, then this is the end of body
 095              if (empty($line)) {
 096                  if ($i < $totalLines - 1) {
 097                      $body = implode('', array_slice($lines, $i + 2));
 098                  }
 099                  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  }
 172