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. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
Prepare.php
001 <?php
002 namespace GuzzleHttp\Subscriber;
003
004 use GuzzleHttp\Event\BeforeEvent;
005 use GuzzleHttp\Event\RequestEvents;
006 use GuzzleHttp\Event\SubscriberInterface;
007 use GuzzleHttp\Message\AppliesHeadersInterface;
008 use GuzzleHttp\Message\RequestInterface;
009 use GuzzleHttp\Mimetypes;
010 use GuzzleHttp\Stream\StreamInterface;
011
012 /**
013 * Prepares requests with a body before sending
014 *
015 * **Request Options**
016 *
017 * - expect: Set to true to enable the "Expect: 100-Continue" header for a
018 * request that send a body. Set to false to disable "Expect: 100-Continue".
019 * Set to a number so that the size of the payload must be greater than the
020 * number in order to send the Expect header. Setting to a number will send
021 * the Expect header for all requests in which the size of the payload cannot
022 * be determined or where the body is not rewindable.
023 */
024 class Prepare implements SubscriberInterface
025 {
026 public function getEvents()
027 {
028 return ['before' => ['onBefore', RequestEvents::PREPARE_REQUEST]];
029 }
030
031 public function onBefore(BeforeEvent $event)
032 {
033 $request = $event->getRequest();
034
035 // Set the appropriate Content-Type for a request if one is not set and
036 // there are form fields
037 if (!($body = $request->getBody())) {
038 return;
039 }
040
041 $this->addContentLength($request, $body);
042
043 if ($body instanceof AppliesHeadersInterface) {
044 // Synchronize the body with the request headers
045 $body->applyRequestHeaders($request);
046 } elseif (!$request->hasHeader('Content-Type')) {
047 $this->addContentType($request, $body);
048 }
049
050 $this->addExpectHeader($request, $body);
051 }
052
053 private function addContentType(
054 RequestInterface $request,
055 StreamInterface $body
056 ) {
057 if (!($uri = $body->getMetadata('uri'))) {
058 return;
059 }
060
061 // Guess the content-type based on the stream's "uri" metadata value.
062 // The file extension is used to determine the appropriate mime-type.
063 if ($contentType = Mimetypes::getInstance()->fromFilename($uri)) {
064 $request->setHeader('Content-Type', $contentType);
065 }
066 }
067
068 private function addContentLength(
069 RequestInterface $request,
070 StreamInterface $body
071 ) {
072 // Set the Content-Length header if it can be determined, and never
073 // send a Transfer-Encoding: chunked and Content-Length header in
074 // the same request.
075 if ($request->hasHeader('Content-Length')) {
076 // Remove transfer-encoding if content-length is set.
077 $request->removeHeader('Transfer-Encoding');
078 return;
079 }
080
081 if ($request->hasHeader('Transfer-Encoding')) {
082 return;
083 }
084
085 if (null !== ($size = $body->getSize())) {
086 $request->setHeader('Content-Length', $size);
087 $request->removeHeader('Transfer-Encoding');
088 } elseif ('1.1' == $request->getProtocolVersion()) {
089 // Use chunked Transfer-Encoding if there is no determinable
090 // content-length header and we're using HTTP/1.1.
091 $request->setHeader('Transfer-Encoding', 'chunked');
092 $request->removeHeader('Content-Length');
093 }
094 }
095
096 private function addExpectHeader(
097 RequestInterface $request,
098 StreamInterface $body
099 ) {
100 // Determine if the Expect header should be used
101 if ($request->hasHeader('Expect')) {
102 return;
103 }
104
105 $expect = $request->getConfig()['expect'];
106
107 // Return if disabled or if you're not using HTTP/1.1
108 if ($expect === false || $request->getProtocolVersion() !== '1.1') {
109 return;
110 }
111
112 // The expect header is unconditionally enabled
113 if ($expect === true) {
114 $request->setHeader('Expect', '100-Continue');
115 return;
116 }
117
118 // By default, send the expect header when the payload is > 1mb
119 if ($expect === null) {
120 $expect = 1048576;
121 }
122
123 // Always add if the body cannot be rewound, the size cannot be
124 // determined, or the size is greater than the cutoff threshold
125 $size = $body->getSize();
126 if ($size === null || $size >= (int) $expect || !$body->isSeekable()) {
127 $request->setHeader('Expect', '100-Continue');
128 }
129 }
130 }
131