curl.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php namespace Lib;
  2. class HTTPUnexpectedResponse extends \Exception {
  3. static $ecode = array(
  4. 100 => 'Continue',
  5. 101 => 'Switching Protocols',
  6. 102 => 'Processing', // WebDAV; RFC 2518
  7. 200 => 'OK',
  8. 201 => 'Created',
  9. 202 => 'Accepted',
  10. 203 => 'Non-Authoritative Information', // since HTTP/1.1
  11. 204 => 'No Content',
  12. 205 => 'Reset Content',
  13. 206 => 'Partial Content',
  14. 207 => 'Multi-Status', // WebDAV; RFC 4918
  15. 208 => 'Already Reported', // WebDAV; RFC 5842
  16. 226 => 'IM Used', // RFC 3229
  17. 300 => 'Multiple Choices',
  18. 301 => 'Moved Permanently',
  19. 302 => 'Found',
  20. 303 => 'See Other', // since HTTP/1.1
  21. 304 => 'Not Modified',
  22. 305 => 'Use Proxy', // since HTTP/1.1
  23. 306 => 'Switch Proxy',
  24. 307 => 'Temporary Redirect', // since HTTP/1.1
  25. 308 => 'Permanent Redirect', // approved as experimental RFC
  26. 400 => 'Bad Request',
  27. 401 => 'Unauthorized',
  28. 402 => 'Payment Required',
  29. 403 => 'Forbidden',
  30. 404 => 'Not Found',
  31. 405 => 'Method Not Allowed',
  32. 406 => 'Not Acceptable',
  33. 407 => 'Proxy Authentication Required',
  34. 408 => 'Request Timeout',
  35. 409 => 'Conflict',
  36. 410 => 'Gone',
  37. 411 => 'Length Required',
  38. 412 => 'Precondition Failed',
  39. 413 => 'Request Entity Too Large',
  40. 414 => 'Request-URI Too Long',
  41. 415 => 'Unsupported Media Type',
  42. 416 => 'Requested Range Not Satisfiable',
  43. 417 => 'Expectation Failed',
  44. 418 => 'I\'m a teapot', // RFC 2324
  45. 419 => 'Authentication Timeout', // not in RFC 2616
  46. 420 => 'Enhance Your Calm', // Twitter
  47. 420 => 'Method Failure', // Spring Framework
  48. 422 => 'Unprocessable Entity', // WebDAV; RFC 4918
  49. 423 => 'Locked', // WebDAV; RFC 4918
  50. 424 => 'Failed Dependency', // WebDAV; RFC 4918
  51. 424 => 'Method Failure', // WebDAV)
  52. 425 => 'Unordered Collection', // Internet draft
  53. 426 => 'Upgrade Required', // RFC 2817
  54. 428 => 'Precondition Required', // RFC 6585
  55. 429 => 'Too Many Requests', // RFC 6585
  56. 431 => 'Request Header Fields Too Large', // RFC 6585
  57. 444 => 'No Response', // Nginx
  58. 449 => 'Retry With', // Microsoft
  59. 450 => 'Blocked by Windows Parental Controls', // Microsoft
  60. 451 => 'Redirect', // Microsoft
  61. 451 => 'Unavailable For Legal Reasons', // Internet draft
  62. 494 => 'Request Header Too Large', // Nginx
  63. 495 => 'Cert Error', // Nginx
  64. 496 => 'No Cert', // Nginx
  65. 497 => 'HTTP to HTTPS', // Nginx
  66. 499 => 'Client Closed Request', // Nginx
  67. 500 => 'Internal Server Error',
  68. 501 => 'Not Implemented',
  69. 502 => 'Bad Gateway',
  70. 503 => 'Service Unavailable',
  71. 504 => 'Gateway Timeout',
  72. 505 => 'HTTP Version Not Supported',
  73. 506 => 'Variant Also Negotiates', // RFC 2295
  74. 507 => 'Insufficient Storage', // WebDAV; RFC 4918
  75. 508 => 'Loop Detected', // WebDAV; RFC 5842
  76. 509 => 'Bandwidth Limit Exceeded', // Apache bw/limited extension
  77. 510 => 'Not Extended', // RFC 2774
  78. 511 => 'Network Authentication Required', // RFC 6585
  79. 598 => 'Network read timeout error', // Unknown
  80. 599 => 'Network connect timeout error', // Unknown
  81. );
  82. }
  83. class NotAuthorizedException extends \Exception{}
  84. trait Curl {
  85. protected $url;
  86. protected $token;
  87. protected $user_agent = "Gogs PHP Api Client/0.0 (compatible; LINUX)";
  88. protected $timeout = 30;
  89. protected $max_redirects = 4;
  90. protected function method(&$req, $scope, $params, $post, $ret) {
  91. echo sprintf("%s: %s%s\n", $post ? "POST" : "GET", $this->url, $scope);
  92. $c = curl_init();
  93. if (!$c) {
  94. return false;
  95. }
  96. if ($post) {
  97. curl_setopt($c, CURLOPT_POST, 1);
  98. curl_setopt($c, CURLOPT_POSTFIELDS, $params);
  99. }
  100. curl_setopt($c, CURLOPT_USERAGENT, $this->user_agent);
  101. curl_setopt($c, CURLOPT_URL, sprintf("%s%s", $this->url, $scope));
  102. curl_setopt($c, CURLOPT_HTTPHEADER, array(
  103. sprintf("Authorization: token %s", $this->token)
  104. ));
  105. curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  106. curl_setopt($c, CURLOPT_TIMEOUT, $this->timeout);
  107. curl_setopt($c, CURLOPT_MAXREDIRS, $this->max_redirects);
  108. curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
  109. $req = curl_exec($c);
  110. $status_code = curl_getinfo($c, CURLINFO_HTTP_CODE);
  111. curl_close($c);
  112. return $status_code;
  113. }
  114. protected function authorized($scope) {
  115. $ret = "";
  116. if ($this->method($ret, $scope, array(), false, false) == 401) {
  117. throw new NotAuthorizedException("Not authorized", 401);
  118. }
  119. return true;
  120. }
  121. private function post($scope = "", $params = array()) {
  122. $req = "";
  123. $code = $this->method($req, $scope, $params, true, true);
  124. switch ($code) {
  125. case 200:
  126. return $req;
  127. case 401:
  128. throw new NotAuthorizedException(HTTPUnexpectedResponse::$ecode[401], 401);
  129. default:
  130. throw new HTTPUnexpectedResponse(HTTPUnexpectedResponse::$ecode[$code], $code);
  131. }
  132. }
  133. private function get($scope = "", $params = array()) {
  134. $req = "";
  135. $code = $this->method($req, $scope, $params, false, true);
  136. switch ($code) {
  137. case 200:
  138. return $req;
  139. case 401:
  140. throw new NotAuthorizedException(HTTPUnexpectedResponse::$ecode[401], 401);
  141. default:
  142. throw new HTTPUnexpectedResponse(HTTPUnexpectedResponse::$ecode[$code], $code);
  143. }
  144. }
  145. }