Overview

Namespaces

  • Contrib
    • Bundle
      • CoverallsBundle
        • Console
        • Entity
      • CoverallsV1Bundle
        • Api
        • Collector
        • Command
        • Config
        • Entity
          • Git
    • Component
      • File
      • Log
      • System
        • Git
  • Guzzle
    • Batch
      • Exception
    • Cache
    • Common
      • Exception
    • Http
      • Curl
      • Exception
      • Message
      • QueryAggregator
    • Inflection
    • Iterator
    • Log
    • Parser
      • Cookie
      • Message
      • UriTemplate
      • Url
    • Plugin
      • Async
      • Backoff
      • Cache
      • Cookie
        • CookieJar
        • Exception
      • CurlAuth
      • ErrorResponse
        • Exception
      • History
      • Log
      • Md5
      • Mock
      • Oauth
    • Service
      • Builder
      • Command
        • Factory
        • LocationVisitor
          • Request
          • Response
      • Description
      • Exception
      • Resource
    • Stream
  • PHP
  • Psr
    • Log
  • Symfony
    • Component
      • Config
        • Definition
          • Builder
          • Exception
        • Exception
        • Loader
        • Resource
        • Util
      • Console
        • Command
        • Formatter
        • Helper
        • Input
        • Output
        • Tester
      • EventDispatcher
        • Debug
      • Finder
        • Adapter
        • Comparator
        • Exception
        • Expression
        • Iterator
        • Shell
      • Stopwatch
      • Yaml
        • Exception

Classes

  • AbstractMessage
  • EntityEnclosingRequest
  • Header
  • HeaderComparison
  • PostFile
  • Request
  • RequestFactory
  • Response

Interfaces

  • EntityEnclosingRequestInterface
  • MessageInterface
  • PostFileInterface
  • RequestFactoryInterface
  • RequestInterface
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
  1: <?php
  2: 
  3: namespace Guzzle\Http\Message;
  4: 
  5: use Guzzle\Common\Collection;
  6: use Guzzle\Common\Exception\InvalidArgumentException;
  7: 
  8: /**
  9:  * Abstract HTTP request/response message
 10:  */
 11: abstract class AbstractMessage implements MessageInterface
 12: {
 13:     /**
 14:      * @var array HTTP headers
 15:      */
 16:     protected $headers = array();
 17: 
 18:     /**
 19:      * @var Collection Custom message parameters that are extendable by plugins
 20:      */
 21:     protected $params;
 22: 
 23:     /**
 24:      * @var array Cache-Control directive information
 25:      */
 26:     private $cacheControl = array();
 27: 
 28:     /*
 29:      * @var string HTTP protocol version of the message
 30:      */
 31:     protected $protocolVersion = '1.1';
 32: 
 33:     /**
 34:      * {@inheritdoc}
 35:      */
 36:     public function getParams()
 37:     {
 38:         return $this->params;
 39:     }
 40: 
 41:     /**
 42:      * {@inheritdoc}
 43:      */
 44:     public function addHeader($header, $value)
 45:     {
 46:         $key = strtolower($header);
 47:         if (!isset($this->headers[$key])) {
 48:             $this->headers[$key] = new Header($header, $value);
 49:         } else {
 50:             $this->headers[$key]->add($value, $header);
 51:         }
 52:         $this->changedHeader($key);
 53: 
 54:         return $this;
 55:     }
 56: 
 57:     /**
 58:      * {@inheritdoc}
 59:      */
 60:     public function addHeaders(array $headers)
 61:     {
 62:         foreach ($headers as $key => $value) {
 63:             $this->addHeader($key, $value);
 64:         }
 65: 
 66:         return $this;
 67:     }
 68: 
 69:     /**
 70:      * {@inheritdoc}
 71:      */
 72:     public function getHeader($header, $string = false)
 73:     {
 74:         $key = strtolower($header);
 75:         if (!isset($this->headers[$key])) {
 76:             return null;
 77:         }
 78: 
 79:         return $string ? (string) $this->headers[$key] : $this->headers[$key];
 80:     }
 81: 
 82:     /**
 83:      * {@inheritdoc}
 84:      */
 85:     public function getHeaders($asObjects = false)
 86:     {
 87:         if ($asObjects) {
 88:             $result = $this->headers;
 89:         } else {
 90:             $result = array();
 91:             // Convert all of the headers into a collection
 92:             foreach ($this->headers as $header) {
 93:                 foreach ($header->raw() as $key => $value) {
 94:                     $result[$key] = $value;
 95:                 }
 96:             }
 97:         }
 98: 
 99:         return new Collection($result);
100:     }
101: 
102:     /**
103:      * {@inheritdoc}
104:      */
105:     public function getHeaderLines()
106:     {
107:         $headers = array();
108:         foreach ($this->headers as $value) {
109:             $glue = $value->getGlue();
110:             foreach ($value->raw() as $key => $v) {
111:                 $headers[] = rtrim($key . ': ' . implode($glue, $v));
112:             }
113:         }
114: 
115:         return $headers;
116:     }
117: 
118:     /**
119:      * {@inheritdoc}
120:      */
121:     public function setHeader($header, $value)
122:     {
123:         // Remove any existing header
124:         $key = strtolower($header);
125:         unset($this->headers[$key]);
126: 
127:         if ($value instanceof Header) {
128:             $this->headers[$key] = $value;
129:         } else {
130:             // Allow for 0, '', and NULL to be set
131:             if (!$value) {
132:                 $value = array($value);
133:             }
134:             $this->headers[$key] = new Header($header, $value);
135:         }
136:         $this->changedHeader($key);
137: 
138:         return $this;
139:     }
140: 
141:     /**
142:      * {@inheritdoc}
143:      */
144:     public function setHeaders(array $headers)
145:     {
146:         // Get the keys that are changing
147:         $changed = array_keys($this->headers);
148:         // Erase the old headers
149:         $this->headers = array();
150:         // Add the new headers
151:         foreach ($headers as $key => $value) {
152:             $changed[] = $key;
153:             $this->addHeader($key, $value);
154:         }
155: 
156:         // Notify of the changed headers
157:         foreach (array_unique($changed) as $header) {
158:             $this->changedHeader(strtolower($header));
159:         }
160: 
161:         return $this;
162:     }
163: 
164:     /**
165:      * {@inheritdoc}
166:      */
167:     public function hasHeader($header)
168:     {
169:         return array_key_exists(strtolower($header), $this->headers);
170:     }
171: 
172:     /**
173:      * {@inheritdoc}
174:      */
175:     public function removeHeader($header)
176:     {
177:         $header = strtolower($header);
178:         unset($this->headers[$header]);
179:         $this->changedHeader($header);
180: 
181:         return $this;
182:     }
183: 
184:     /**
185:      * {@inheritdoc}
186:      */
187:     public function getTokenizedHeader($header, $token = ';')
188:     {
189:         if (!$this->hasHeader($header)) {
190:             return null;
191:         }
192: 
193:         $data = new Collection();
194: 
195:         foreach ($this->getHeader($header) as $singleValue) {
196:             foreach (explode($token, $singleValue) as $kvp) {
197:                 $parts = explode('=', $kvp, 2);
198:                 if (!isset($parts[1])) {
199:                     $data[count($data)] = trim($parts[0]);
200:                 } else {
201:                     $data->add(trim($parts[0]), trim($parts[1]));
202:                 }
203:             }
204:         }
205: 
206:         foreach ($data as $key => $value) {
207:             if (is_array($value)) {
208:                 $data->set($key, array_unique($value));
209:             }
210:         }
211: 
212:         return $data;
213:     }
214: 
215:     /**
216:      * {@inheritdoc}
217:      */
218:     public function setTokenizedHeader($header, $data, $token = ';')
219:     {
220:         if (!($data instanceof Collection) && !is_array($data)) {
221:             throw new InvalidArgumentException('Data must be a Collection or array');
222:         }
223: 
224:         $values = array();
225:         foreach ($data as $key => $value) {
226:             foreach ((array) $value as $v) {
227:                 $values[] = is_int($key) ? $v : $key . '=' . $v;
228:             }
229:         }
230: 
231:         return $this->setHeader($header, implode($token, $values));
232:     }
233: 
234:     /**
235:      * {@inheritdoc}
236:      */
237:     public function getCacheControlDirective($directive)
238:     {
239:         if (!isset($this->cacheControl[$directive])) {
240:             return null;
241:         }
242: 
243:         $directive = $this->cacheControl[$directive];
244: 
245:         if (is_array($directive) && !empty($directive)) {
246:             return $directive[0];
247:         }
248: 
249:         return $directive;
250:     }
251: 
252:     /**
253:      * {@inheritdoc}
254:      */
255:     public function hasCacheControlDirective($directive)
256:     {
257:         return isset($this->cacheControl[$directive]);
258:     }
259: 
260:     /**
261:      * {@inheritdoc}
262:      */
263:     public function addCacheControlDirective($directive, $value = true)
264:     {
265:         $this->cacheControl[$directive] = $value;
266:         $this->rebuildCacheControlDirective();
267: 
268:         return $this;
269:     }
270: 
271:     /**
272:      * {@inheritdoc}
273:      */
274:     public function removeCacheControlDirective($directive)
275:     {
276:         if (array_key_exists($directive, $this->cacheControl)) {
277:             unset($this->cacheControl[$directive]);
278:             $this->rebuildCacheControlDirective();
279:         }
280: 
281:         return $this;
282:     }
283: 
284:     /**
285:      * Check to see if the modified headers need to reset any of the managed
286:      * headers like cache-control
287:      *
288:      * @param string $header Header that changed
289:      */
290:     protected function changedHeader($header)
291:     {
292:         if ($header == 'cache-control') {
293:             $this->parseCacheControlDirective();
294:         }
295:     }
296: 
297:     /**
298:      * Parse the Cache-Control HTTP header into an array
299:      */
300:     private function parseCacheControlDirective()
301:     {
302:         $this->cacheControl = array();
303:         $tokenized = $this->getTokenizedHeader('Cache-Control', ',') ?: array();
304:         foreach ($tokenized as $key => $value) {
305:             if (is_numeric($key)) {
306:                 $this->cacheControl[$value] = true;
307:             } else {
308:                 $this->cacheControl[$key] = $value;
309:             }
310:         }
311:     }
312: 
313:     /**
314:      * Rebuild the Cache-Control HTTP header using the user-specified values
315:      */
316:     private function rebuildCacheControlDirective()
317:     {
318:         $cacheControl = array();
319:         foreach ($this->cacheControl as $key => $value) {
320:             $cacheControl[] = ($value === true) ? $key : ($key . '=' . $value);
321:         }
322:         $this->headers['cache-control'] = new Header('Cache-Control', $cacheControl, ', ');
323:     }
324: }
325: 
php-coveralls API documentation generated by ApiGen 2.8.0