[ Index ] |
|
Code source de PRADO 3.0.6 |
1 <?php 2 /** 3 * base include file for SimpleTest 4 * @package SimpleTest 5 * @subpackage WebTester 6 * @version $Id: encoding.php 1397 2006-09-07 07:55:53Z wei $ 7 */ 8 9 /**#@+ 10 * include other SimpleTest class files 11 */ 12 require_once(dirname(__FILE__) . '/socket.php'); 13 /**#@-*/ 14 15 /** 16 * Single post parameter. 17 * @package SimpleTest 18 * @subpackage WebTester 19 */ 20 class SimpleEncodedPair { 21 protected $_key; 22 protected $_value; 23 24 /** 25 * Stashes the data for rendering later. 26 * @param string $key Form element name. 27 * @param string $value Data to send. 28 */ 29 function SimpleEncodedPair($key, $value) { 30 $this->_key = $key; 31 $this->_value = $value; 32 } 33 34 /** 35 * The pair as a single string. 36 * @return string Encoded pair. 37 * @access public 38 */ 39 function asRequest() { 40 return $this->_key . '=' . urlencode($this->_value); 41 } 42 43 /** 44 * The MIME part as a string. 45 * @return string MIME part encoding. 46 * @access public 47 */ 48 function asMime() { 49 $part = 'Content-Disposition: form-data; '; 50 $part .= "name=\"" . $this->_key . "\"\r\n"; 51 $part .= "\r\n" . $this->_value; 52 return $part; 53 } 54 55 /** 56 * Is this the value we are looking for? 57 * @param string $key Identifier. 58 * @return boolean True if matched. 59 * @access public 60 */ 61 function isKey($key) { 62 return $key == $this->_key; 63 } 64 65 /** 66 * Is this the value we are looking for? 67 * @return string Identifier. 68 * @access public 69 */ 70 function getKey() { 71 return $this->_key; 72 } 73 74 /** 75 * Is this the value we are looking for? 76 * @return string Content. 77 * @access public 78 */ 79 function getValue() { 80 return $this->_value; 81 } 82 } 83 84 /** 85 * Single post parameter. 86 * @package SimpleTest 87 * @subpackage WebTester 88 */ 89 class SimpleAttachment { 90 protected $_key; 91 protected $_content; 92 protected $_filename; 93 94 /** 95 * Stashes the data for rendering later. 96 * @param string $key Key to add value to. 97 * @param string $content Raw data. 98 * @param hash $filename Original filename. 99 */ 100 function SimpleAttachment($key, $content, $filename) { 101 $this->_key = $key; 102 $this->_content = $content; 103 $this->_filename = $filename; 104 } 105 106 /** 107 * The pair as a single string. 108 * @return string Encoded pair. 109 * @access public 110 */ 111 function asRequest() { 112 return ''; 113 } 114 115 /** 116 * The MIME part as a string. 117 * @return string MIME part encoding. 118 * @access public 119 */ 120 function asMime() { 121 $part = 'Content-Disposition: form-data; '; 122 $part .= 'name="' . $this->_key . '"; '; 123 $part .= 'filename="' . $this->_filename . '"'; 124 $part .= "\r\nContent-Type: " . $this->_deduceMimeType(); 125 $part .= "\r\n\r\n" . $this->_content; 126 return $part; 127 } 128 129 /** 130 * Attempts to figure out the MIME type from the 131 * file extension and the content. 132 * @return string MIME type. 133 * @access private 134 */ 135 function _deduceMimeType() { 136 if ($this->_isOnlyAscii($this->_content)) { 137 return 'text/plain'; 138 } 139 return 'application/octet-stream'; 140 } 141 142 /** 143 * Tests each character is in the range 0-127. 144 * @param string $ascii String to test. 145 * @access private 146 */ 147 function _isOnlyAscii($ascii) { 148 for ($i = 0, $length = strlen($ascii); $i < $length; $i++) { 149 if (ord($ascii[$i]) > 127) { 150 return false; 151 } 152 } 153 return true; 154 } 155 156 /** 157 * Is this the value we are looking for? 158 * @param string $key Identifier. 159 * @return boolean True if matched. 160 * @access public 161 */ 162 function isKey($key) { 163 return $key == $this->_key; 164 } 165 166 /** 167 * Is this the value we are looking for? 168 * @return string Identifier. 169 * @access public 170 */ 171 function getKey() { 172 return $this->_key; 173 } 174 175 /** 176 * Is this the value we are looking for? 177 * @return string Content. 178 * @access public 179 */ 180 function getValue() { 181 return $this->_filename; 182 } 183 } 184 185 /** 186 * Bundle of GET/POST parameters. Can include 187 * repeated parameters. 188 * @package SimpleTest 189 * @subpackage WebTester 190 */ 191 class SimpleEncoding { 192 protected $_request; 193 194 /** 195 * Starts empty. 196 * @param array $query Hash of parameters. 197 * Multiple values are 198 * as lists on a single key. 199 * @access public 200 */ 201 function SimpleEncoding($query = false) { 202 if (! $query) { 203 $query = array(); 204 } 205 $this->clear(); 206 $this->merge($query); 207 } 208 209 /** 210 * Empties the request of parameters. 211 * @access public 212 */ 213 function clear() { 214 $this->_request = array(); 215 } 216 217 /** 218 * Adds a parameter to the query. 219 * @param string $key Key to add value to. 220 * @param string/array $value New data. 221 * @access public 222 */ 223 function add($key, $value) { 224 if ($value === false) { 225 return; 226 } 227 if (is_array($value)) { 228 foreach ($value as $item) { 229 $this->_addPair($key, $item); 230 } 231 } else { 232 $this->_addPair($key, $value); 233 } 234 } 235 236 /** 237 * Adds a new value into the request. 238 * @param string $key Key to add value to. 239 * @param string/array $value New data. 240 * @access private 241 */ 242 function _addPair($key, $value) { 243 $this->_request[] = new SimpleEncodedPair($key, $value); 244 } 245 246 /** 247 * Adds a MIME part to the query. Does nothing for a 248 * form encoded packet. 249 * @param string $key Key to add value to. 250 * @param string $content Raw data. 251 * @param hash $filename Original filename. 252 * @access public 253 */ 254 function attach($key, $content, $filename) { 255 $this->_request[] = new SimpleAttachment($key, $content, $filename); 256 } 257 258 /** 259 * Adds a set of parameters to this query. 260 * @param array/SimpleQueryString $query Multiple values are 261 * as lists on a single key. 262 * @access public 263 */ 264 function merge($query) { 265 if (is_object($query)) { 266 $this->_request = array_merge($this->_request, $query->getAll()); 267 } elseif (is_array($query)) { 268 foreach ($query as $key => $value) { 269 $this->add($key, $value); 270 } 271 } 272 } 273 274 /** 275 * Accessor for single value. 276 * @return string/array False if missing, string 277 * if present and array if 278 * multiple entries. 279 * @access public 280 */ 281 function getValue($key) { 282 $values = array(); 283 foreach ($this->_request as $pair) { 284 if ($pair->isKey($key)) { 285 $values[] = $pair->getValue(); 286 } 287 } 288 if (count($values) == 0) { 289 return false; 290 } elseif (count($values) == 1) { 291 return $values[0]; 292 } else { 293 return $values; 294 } 295 } 296 297 /** 298 * Accessor for listing of pairs. 299 * @return array All pair objects. 300 * @access public 301 */ 302 function getAll() { 303 return $this->_request; 304 } 305 306 /** 307 * Renders the query string as a URL encoded 308 * request part. 309 * @return string Part of URL. 310 * @access protected 311 */ 312 function _encode() { 313 $statements = array(); 314 foreach ($this->_request as $pair) { 315 if ($statement = $pair->asRequest()) { 316 $statements[] = $statement; 317 } 318 } 319 return implode('&', $statements); 320 } 321 } 322 323 /** 324 * Bundle of GET parameters. Can include 325 * repeated parameters. 326 * @package SimpleTest 327 * @subpackage WebTester 328 */ 329 class SimpleGetEncoding extends SimpleEncoding { 330 331 /** 332 * Starts empty. 333 * @param array $query Hash of parameters. 334 * Multiple values are 335 * as lists on a single key. 336 * @access public 337 */ 338 function SimpleGetEncoding($query = false) { 339 $this->SimpleEncoding($query); 340 } 341 342 /** 343 * HTTP request method. 344 * @return string Always GET. 345 * @access public 346 */ 347 function getMethod() { 348 return 'GET'; 349 } 350 351 /** 352 * Writes no extra headers. 353 * @param SimpleSocket $socket Socket to write to. 354 * @access public 355 */ 356 function writeHeadersTo($socket) { 357 } 358 359 /** 360 * No data is sent to the socket as the data is encoded into 361 * the URL. 362 * @param SimpleSocket $socket Socket to write to. 363 * @access public 364 */ 365 function writeTo($socket) { 366 } 367 368 /** 369 * Renders the query string as a URL encoded 370 * request part for attaching to a URL. 371 * @return string Part of URL. 372 * @access public 373 */ 374 function asUrlRequest() { 375 return $this->_encode(); 376 } 377 } 378 379 /** 380 * Bundle of URL parameters for a HEAD request. 381 * @package SimpleTest 382 * @subpackage WebTester 383 */ 384 class SimpleHeadEncoding extends SimpleGetEncoding { 385 386 /** 387 * Starts empty. 388 * @param array $query Hash of parameters. 389 * Multiple values are 390 * as lists on a single key. 391 * @access public 392 */ 393 function SimpleHeadEncoding($query = false) { 394 $this->SimpleGetEncoding($query); 395 } 396 397 /** 398 * HTTP request method. 399 * @return string Always HEAD. 400 * @access public 401 */ 402 function getMethod() { 403 return 'HEAD'; 404 } 405 } 406 407 /** 408 * Bundle of POST parameters. Can include 409 * repeated parameters. 410 * @package SimpleTest 411 * @subpackage WebTester 412 */ 413 class SimplePostEncoding extends SimpleEncoding { 414 415 /** 416 * Starts empty. 417 * @param array $query Hash of parameters. 418 * Multiple values are 419 * as lists on a single key. 420 * @access public 421 */ 422 function SimplePostEncoding($query = false) { 423 $this->SimpleEncoding($query); 424 } 425 426 /** 427 * HTTP request method. 428 * @return string Always POST. 429 * @access public 430 */ 431 function getMethod() { 432 return 'POST'; 433 } 434 435 /** 436 * Dispatches the form headers down the socket. 437 * @param SimpleSocket $socket Socket to write to. 438 * @access public 439 */ 440 function writeHeadersTo($socket) { 441 $socket->write("Content-Length: " . (integer)strlen($this->_encode()) . "\r\n"); 442 $socket->write("Content-Type: application/x-www-form-urlencoded\r\n"); 443 } 444 445 /** 446 * Dispatches the form data down the socket. 447 * @param SimpleSocket $socket Socket to write to. 448 * @access public 449 */ 450 function writeTo($socket) { 451 $socket->write($this->_encode()); 452 } 453 454 /** 455 * Renders the query string as a URL encoded 456 * request part for attaching to a URL. 457 * @return string Part of URL. 458 * @access public 459 */ 460 function asUrlRequest() { 461 return ''; 462 } 463 } 464 465 /** 466 * Bundle of POST parameters in the multipart 467 * format. Can include file uploads. 468 * @package SimpleTest 469 * @subpackage WebTester 470 */ 471 class SimpleMultipartEncoding extends SimplePostEncoding { 472 protected $_boundary; 473 474 /** 475 * Starts empty. 476 * @param array $query Hash of parameters. 477 * Multiple values are 478 * as lists on a single key. 479 * @access public 480 */ 481 function SimpleMultipartEncoding($query = false, $boundary = false) { 482 $this->SimplePostEncoding($query); 483 $this->_boundary = ($boundary === false ? uniqid('st') : $boundary); 484 } 485 486 /** 487 * Dispatches the form headers down the socket. 488 * @param SimpleSocket $socket Socket to write to. 489 * @access public 490 */ 491 function writeHeadersTo($socket) { 492 $socket->write("Content-Length: " . (integer)strlen($this->_encode()) . "\r\n"); 493 $socket->write("Content-Type: multipart/form-data, boundary=" . $this->_boundary . "\r\n"); 494 } 495 496 /** 497 * Dispatches the form data down the socket. 498 * @param SimpleSocket $socket Socket to write to. 499 * @access public 500 */ 501 function writeTo($socket) { 502 $socket->write($this->_encode()); 503 } 504 505 /** 506 * Renders the query string as a URL encoded 507 * request part. 508 * @return string Part of URL. 509 * @access public 510 */ 511 function _encode() { 512 $stream = ''; 513 foreach ($this->_request as $pair) { 514 $stream .= "--" . $this->_boundary . "\r\n"; 515 $stream .= $pair->asMime() . "\r\n"; 516 } 517 $stream .= "--" . $this->_boundary . "--\r\n"; 518 return $stream; 519 } 520 } 521 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 21:07:04 2007 | par Balluche grâce à PHPXref 0.7 |