encoding.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. <?php
  2. /**
  3. * base include file for SimpleTest
  4. * @package SimpleTest
  5. * @subpackage WebTester
  6. * @version $Id: encoding.php 1784 2008-04-26 13:07:14Z pp11 $
  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 SimpleHeadEncoding($query = false) {
  359. $this->SimpleGetEncoding($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 POST parameters. Can include
  372. * repeated parameters.
  373. * @package SimpleTest
  374. * @subpackage WebTester
  375. */
  376. class SimplePostEncoding extends SimpleEncoding {
  377. /**
  378. * Starts empty.
  379. * @param array $query Hash of parameters.
  380. * Multiple values are
  381. * as lists on a single key.
  382. * @access public
  383. */
  384. function __construct($query = false) {
  385. if (is_array($query) and $this->hasMoreThanOneLevel($query)) {
  386. $query = $this->rewriteArrayWithMultipleLevels($query);
  387. }
  388. parent::__construct($query);
  389. }
  390. function hasMoreThanOneLevel($query) {
  391. foreach ($query as $key => $value) {
  392. if (is_array($value)) {
  393. return true;
  394. }
  395. }
  396. return false;
  397. }
  398. function rewriteArrayWithMultipleLevels($query) {
  399. $query_ = array();
  400. foreach ($query as $key => $value) {
  401. if (is_array($value)) {
  402. foreach ($value as $sub_key => $sub_value) {
  403. $query_[$key."[".$sub_key."]"] = $sub_value;
  404. }
  405. } else {
  406. $query_[$key] = $value;
  407. }
  408. }
  409. if ($this->hasMoreThanOneLevel($query_)) {
  410. $query_ = $this->rewriteArrayWithMultipleLevels($query_);
  411. }
  412. return $query_;
  413. }
  414. /**
  415. * HTTP request method.
  416. * @return string Always POST.
  417. * @access public
  418. */
  419. function getMethod() {
  420. return 'POST';
  421. }
  422. /**
  423. * Dispatches the form headers down the socket.
  424. * @param SimpleSocket $socket Socket to write to.
  425. * @access public
  426. */
  427. function writeHeadersTo(&$socket) {
  428. $socket->write("Content-Length: " . (integer)strlen($this->encode()) . "\r\n");
  429. $socket->write("Content-Type: application/x-www-form-urlencoded\r\n");
  430. }
  431. /**
  432. * Dispatches the form data down the socket.
  433. * @param SimpleSocket $socket Socket to write to.
  434. * @access public
  435. */
  436. function writeTo(&$socket) {
  437. $socket->write($this->encode());
  438. }
  439. /**
  440. * Renders the query string as a URL encoded
  441. * request part for attaching to a URL.
  442. * @return string Part of URL.
  443. * @access public
  444. */
  445. function asUrlRequest() {
  446. return '';
  447. }
  448. }
  449. /**
  450. * Bundle of POST parameters in the multipart
  451. * format. Can include file uploads.
  452. * @package SimpleTest
  453. * @subpackage WebTester
  454. */
  455. class SimpleMultipartEncoding extends SimplePostEncoding {
  456. private $boundary;
  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, $boundary = false) {
  465. parent::__construct($query);
  466. $this->boundary = ($boundary === false ? uniqid('st') : $boundary);
  467. }
  468. /**
  469. * Dispatches the form headers down the socket.
  470. * @param SimpleSocket $socket Socket to write to.
  471. * @access public
  472. */
  473. function writeHeadersTo(&$socket) {
  474. $socket->write("Content-Length: " . (integer)strlen($this->encode()) . "\r\n");
  475. $socket->write("Content-Type: multipart/form-data, boundary=" . $this->boundary . "\r\n");
  476. }
  477. /**
  478. * Dispatches the form data down the socket.
  479. * @param SimpleSocket $socket Socket to write to.
  480. * @access public
  481. */
  482. function writeTo(&$socket) {
  483. $socket->write($this->encode());
  484. }
  485. /**
  486. * Renders the query string as a URL encoded
  487. * request part.
  488. * @return string Part of URL.
  489. * @access public
  490. */
  491. function encode() {
  492. $stream = '';
  493. foreach ($this->getAll() as $pair) {
  494. $stream .= "--" . $this->boundary . "\r\n";
  495. $stream .= $pair->asMime() . "\r\n";
  496. }
  497. $stream .= "--" . $this->boundary . "--\r\n";
  498. return $stream;
  499. }
  500. }
  501. ?>