1: <?php
2:
3: namespace Guzzle\Http\Message;
4:
5: use Guzzle\Http\EntityBody;
6: use Guzzle\Http\EntityBodyInterface;
7: use Guzzle\Http\QueryString;
8: use Guzzle\Http\RedirectPlugin;
9: use Guzzle\Http\Exception\RequestException;
10: use Guzzle\Http\Mimetypes;
11:
12: 13: 14:
15: class EntityEnclosingRequest extends Request implements EntityEnclosingRequestInterface
16: {
17: 18: 19:
20: protected $expectCutoff = 1048576;
21:
22: 23: 24:
25: protected $body;
26:
27: 28: 29:
30: protected $postFields;
31:
32: 33: 34:
35: protected $postFiles = array();
36:
37: 38: 39:
40: public function __construct($method, $url, $headers = array())
41: {
42: $this->postFields = new QueryString();
43: parent::__construct($method, $url, $headers);
44: }
45:
46: 47: 48: 49: 50:
51: public function __toString()
52: {
53:
54: if (count($this->postFields) && empty($this->postFiles)) {
55: return parent::__toString() . (string) $this->postFields;
56: }
57:
58: return parent::__toString() . $this->body;
59: }
60:
61: 62: 63:
64: public function setState($state, array $context = array())
65: {
66: parent::setState($state, $context);
67: if ($state == self::STATE_TRANSFER && !$this->body && !count($this->postFields) && !count($this->postFiles)) {
68: $this->setHeader('Content-Length', 0)->removeHeader('Transfer-Encoding');
69: }
70:
71: return $this;
72: }
73:
74: 75: 76:
77: public function setBody($body, $contentType = null, $tryChunkedTransfer = false)
78: {
79: $this->body = EntityBody::factory($body);
80:
81:
82: if ($contentType === null && !$this->hasHeader('Content-Type')) {
83: $contentType = $this->body->getContentType() ?: Mimetypes::getInstance()->fromFilename($this->getPath());
84: }
85:
86: if ($contentType) {
87: $this->setHeader('Content-Type', (string) $contentType);
88: }
89:
90:
91: if (!$this->body->isSeekable() && $this->expectCutoff !== false) {
92: $this->setHeader('Expect', '100-Continue');
93: }
94:
95: if ($tryChunkedTransfer) {
96: $this->removeHeader('Content-Length');
97: $this->setHeader('Transfer-Encoding', 'chunked');
98: } else {
99:
100: $size = $this->body->getContentLength();
101: if ($size !== null && $size !== false) {
102: $this->setHeader('Content-Length', $size);
103: if ($size > $this->expectCutoff) {
104: $this->setHeader('Expect', '100-Continue');
105: }
106: } elseif (!$this->hasHeader('Content-Length')) {
107: if ('1.1' == $this->protocolVersion) {
108: $this->setHeader('Transfer-Encoding', 'chunked');
109: } else {
110: throw new RequestException(
111: 'Cannot determine Content-Length and cannot use chunked Transfer-Encoding when using HTTP/1.0'
112: );
113: }
114: }
115: }
116:
117: return $this;
118: }
119:
120: 121: 122:
123: public function getBody()
124: {
125: return $this->body;
126: }
127:
128: 129: 130: 131: 132: 133: 134:
135: public function ($size)
136: {
137: $this->expectCutoff = $size;
138: if ($size === false || !$this->body) {
139: $this->removeHeader('Expect');
140: } elseif ($this->body && $this->body->getSize() && $this->body->getSize() > $size) {
141: $this->setHeader('Expect', '100-Continue');
142: }
143:
144: return $this;
145: }
146:
147: 148: 149:
150: public function configureRedirects($strict = false, $maxRedirects = 5)
151: {
152: $this->getParams()->set(RedirectPlugin::STRICT_REDIRECTS, $strict);
153: if ($maxRedirects == 0) {
154: $this->getParams()->set(RedirectPlugin::DISABLE, true);
155: } else {
156: $this->getParams()->set(RedirectPlugin::MAX_REDIRECTS, $maxRedirects);
157: }
158:
159: return $this;
160: }
161:
162: 163: 164:
165: public function getPostField($field)
166: {
167: return $this->postFields->get($field);
168: }
169:
170: 171: 172:
173: public function getPostFields()
174: {
175: return $this->postFields;
176: }
177:
178: 179: 180:
181: public function setPostField($key, $value)
182: {
183: $this->postFields->set($key, $value);
184: $this->processPostFields();
185:
186: return $this;
187: }
188:
189: 190: 191:
192: public function addPostFields($fields)
193: {
194: $this->postFields->merge($fields);
195: $this->processPostFields();
196:
197: return $this;
198: }
199:
200: 201: 202:
203: public function removePostField($field)
204: {
205: $this->postFields->remove($field);
206: $this->processPostFields();
207:
208: return $this;
209: }
210:
211: 212: 213:
214: public function getPostFiles()
215: {
216: return $this->postFiles;
217: }
218:
219: 220: 221:
222: public function getPostFile($fieldName)
223: {
224: return isset($this->postFiles[$fieldName]) ? $this->postFiles[$fieldName] : null;
225: }
226:
227: 228: 229:
230: public function removePostFile($fieldName)
231: {
232: unset($this->postFiles[$fieldName]);
233: $this->processPostFields();
234:
235: return $this;
236: }
237:
238: 239: 240:
241: public function addPostFile($field, $filename = null, $contentType = null)
242: {
243: $data = null;
244:
245: if ($field instanceof PostFileInterface) {
246: $data = $field;
247: } elseif (is_array($filename)) {
248:
249: foreach ($filename as $file) {
250: $this->addPostFile($field, $file, $contentType);
251: }
252: return $this;
253: } elseif (!is_string($filename)) {
254: throw new RequestException('The path to a file must be a string');
255: } elseif (!empty($filename)) {
256:
257: $data = new PostFile($field, $filename, $contentType);
258: }
259:
260: if ($data) {
261: if (!isset($this->postFiles[$data->getFieldName()])) {
262: $this->postFiles[$data->getFieldName()] = array($data);
263: } else {
264: $this->postFiles[$data->getFieldName()][] = $data;
265: }
266: $this->processPostFields();
267: }
268:
269: return $this;
270: }
271:
272: 273: 274:
275: public function addPostFiles(array $files)
276: {
277: foreach ($files as $key => $file) {
278: if ($file instanceof PostFileInterface) {
279: $this->addPostFile($file, null, null, false);
280: } elseif (is_string($file)) {
281:
282: if (is_numeric($key)) {
283: $key = 'file';
284: }
285: $this->addPostFile($key, $file, null, false);
286: } else {
287: throw new RequestException('File must be a string or instance of PostFileInterface');
288: }
289: }
290:
291: return $this;
292: }
293:
294: 295: 296:
297: protected function processPostFields()
298: {
299: if (empty($this->postFiles)) {
300: $this->removeHeader('Expect')->setHeader('Content-Type', self::URL_ENCODED);
301: } else {
302: $this->setHeader('Content-Type', self::MULTIPART);
303: if ($this->expectCutoff !== false) {
304: $this->setHeader('Expect', '100-Continue');
305: }
306: }
307: }
308: }
309: