1: <?php
2:
3: namespace OpenCloud\Common\Request\Response;
4:
5: use OpenCloud\Common\Base;
6:
7: /**
8: * The HttpResponse returns an object with status information, separated
9: * headers, and any response body necessary.
10: *
11: * @api
12: * @author Glen Campbell <glen.campbell@rackspace.com>
13: */
14:
15: class Http extends Base
16: {
17:
18: private $errno;
19: private $error;
20: private $info = array();
21: protected $body;
22: protected $headers = array();
23:
24: /**
25: * The constructor parses everything necessary
26: */
27: public function __construct($request, $data)
28: {
29: // save the raw data (who knows? we might need it)
30: $this->setBody($data);
31:
32: // and split each line into name: value pairs
33: foreach($request->returnHeaders() as $line) {
34: if (preg_match('/^([^:]+):\s+(.+?)\s*$/', $line, $matches)) {
35: $this->headers[$matches[1]] = $matches[2];
36: } else {
37: $this->headers[$line] = trim($line);
38: }
39: }
40:
41: // @codeCoverageIgnoreStart
42: if (isset($this->headers['Cache-Control'])) {
43: $this->getLogger()->info('Cache-Control: {header}', array(
44: 'headers' => $this->headers['Cache-Control']
45: ));
46: }
47: if (isset($this->headers['Expires'])) {
48: $this->getLogger()->info('Expires: {header}', array(
49: 'headers' => $this->headers['Expires']
50: ));
51: }
52: // @codeCoverageIgnoreEnd
53:
54: // set some other data
55: $this->info = $request->info();
56: $this->errno = $request->errno();
57: $this->error = $request->error();
58: }
59:
60: /**
61: * Returns the full body of the request
62: *
63: * @return string
64: */
65: public function httpBody()
66: {
67: return $this->body;
68: }
69:
70: /**
71: * Sets the body.
72: *
73: * @param string $body
74: */
75: public function setBody($body)
76: {
77: $this->body = $body;
78: }
79:
80: /**
81: * Returns an array of headers
82: *
83: * @return associative array('header'=>value)
84: */
85: public function headers()
86: {
87: return $this->headers;
88: }
89:
90: /**
91: * Returns a single header
92: *
93: * @return string with the value of the requested header, or NULL
94: */
95: public function header($name)
96: {
97: return isset($this->headers[$name]) ? $this->headers[$name] : null;
98: }
99:
100: /**
101: * Returns an array of information
102: *
103: * @return array
104: */
105: public function info()
106: {
107: return $this->info;
108: }
109:
110: /**
111: * Returns the most recent error number
112: *
113: * @return integer
114: */
115: public function errno()
116: {
117: return $this->errno;
118: }
119:
120: /**
121: * Returns the most recent error message
122: *
123: * @return string
124: */
125: public function error()
126: {
127: return $this->error;
128: }
129:
130: /**
131: * Returns the HTTP status code
132: *
133: * @return integer
134: */
135: public function httpStatus()
136: {
137: return $this->info['http_code'];
138: }
139:
140: }
141: