encoding.php 17 KB

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