PageRenderTime 24ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/phpcouch/http/HttpRequest.php

http://github.com/dzuelke/PHPCouch
PHP | 138 lines | 65 code | 18 blank | 55 comment | 7 complexity | c0e15fc77972657ad9130d1a3957c991 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. namespace phpcouch\http;
  3. class HttpRequest extends HttpMessage
  4. {
  5. const METHOD_DELETE = 'DELETE';
  6. const METHOD_GET = 'GET';
  7. const METHOD_POST = 'POST';
  8. const METHOD_PUT = 'PUT';
  9. /**
  10. * @var string The destination URL of this request.
  11. */
  12. protected $destination;
  13. /**
  14. * @var string The HTTP request method of this request.
  15. */
  16. protected $method;
  17. /**
  18. * Constructor.
  19. *
  20. * @param string The destination URL of this request.
  21. * @param string The HTTP request method of this request.
  22. *
  23. * @author David Zülke <david.zuelke@bitextender.com>
  24. * @since 1.0.0
  25. */
  26. public function __construct($destination = null, $method = self::METHOD_GET)
  27. {
  28. if($destination) {
  29. $this->setDestination($destination);
  30. }
  31. $this->setMethod($method);
  32. }
  33. /**
  34. * Returns the destination URL of this request.
  35. *
  36. * @return string The destination URL of this request.
  37. *
  38. * @author David Zülke <david.zuelke@bitextender.com>
  39. * @since 1.0.0
  40. */
  41. public function getDestination()
  42. {
  43. return $this->destination;
  44. }
  45. /**
  46. * Returns the HTTP request method of this request.
  47. *
  48. * @return string The HTTP request method of this request.
  49. *
  50. * @author David Zülke <david.zuelke@bitextender.com>
  51. * @since 1.0.0
  52. */
  53. public function getMethod()
  54. {
  55. return $this->method;
  56. }
  57. /**
  58. * Sets the destination URL of this request.
  59. *
  60. * @param string The destination URL of this request.
  61. *
  62. * @author David Zülke <david.zuelke@bitextender.com>
  63. * @since 1.0.0
  64. */
  65. public function setDestination($destination)
  66. {
  67. $this->destination = $destination;
  68. }
  69. /**
  70. * Sets the HTTP request method of this request.
  71. *
  72. * @param string The HTTP request method of this request.
  73. *
  74. * @author David Zülke <david.zuelke@bitextender.com>
  75. * @since 1.0.0
  76. */
  77. public function setMethod($method)
  78. {
  79. $this->method = $method;
  80. }
  81. /**
  82. * Set the content for this message.
  83. *
  84. * @param mixed The content to be sent in this message.
  85. */
  86. public function setContent($content) {
  87. if(is_array($content)) {
  88. // Create a multipart/related request
  89. // (use a temp stream as the data might be huge and concatting it would double the memory usage)
  90. $fp = fopen('php://temp', 'w+');
  91. $boundary = md5(uniqid(mt_rand(), true));
  92. foreach($content as $i => $part) {
  93. fwrite($fp, '--');
  94. fwrite($fp, $boundary);
  95. fwrite($fp, "\r\n");
  96. if($i == 0 && ($contentType = $this->getContentType())) {
  97. // Use content type for first part only (the document)
  98. fwrite($fp, 'Content-Type: ');
  99. fwrite($fp, $contentType);
  100. fwrite($fp, "\r\n");
  101. }
  102. fwrite($fp, "\r\n");
  103. if(is_resource($part)) {
  104. stream_copy_to_stream($part, $fp, -1, 0);
  105. } else {
  106. fwrite($fp, $part);
  107. }
  108. fwrite($fp, "\r\n");
  109. }
  110. fwrite($fp, '--');
  111. fwrite($fp, $boundary);
  112. fwrite($fp, '--');
  113. fwrite($fp, "\r\n");
  114. $content = $fp;
  115. $this->setContentType('multipart/related;boundary="' . $boundary . '"');
  116. }
  117. parent::setContent($content);
  118. }
  119. }
  120. ?>