http.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <?php
  2. require_once("conf.php");
  3. require_once("utils.php");
  4. require_once("digest.php");
  5. $CURL_TIMEOUT=30;
  6. // --------------------------------------------------------------------------------
  7. class HTTP_Request
  8. {
  9. public $URL;
  10. public $RawQuerys;
  11. public $Header;
  12. public $Body;
  13. public $UA;
  14. public $METHOD;
  15. public $Params; //map
  16. public $Bucket;
  17. public $Key;
  18. public $Timeout;
  19. public function __construct($method, $url, $body, $bucket, $key, $action_type = ActionType::NONE)
  20. {
  21. $this->URL = $url;
  22. if (isset($url["query"])) {
  23. $this->RawQuerys = $url["query"];
  24. }
  25. $this->Header = array();
  26. $this->Body = $body;
  27. $this->UA = UCloud_UserAgent();
  28. $this->METHOD = $method;
  29. $this->Bucket = $bucket;
  30. $this->Key = $key;
  31. global $CURL_TIMEOUT;
  32. global $UFILE_ACTION_TYPE;
  33. if ($CURL_TIMEOUT == null && $action_type !== ActionType::PUTFILE
  34. && $action_type !== ActionType::POSTFILE) {
  35. $CURL_TIMEOUT = 10;
  36. }
  37. $this->Timeout = $CURL_TIMEOUT;
  38. }
  39. public function EncodedQuery() {
  40. if ($this->RawQuerys != null) {
  41. $q = "";
  42. foreach ($this->RawQuerys as $k => $v) {
  43. $q = $q . "&" . rawurlencode($k) . "=" . rawurlencode($v);
  44. }
  45. return $q;
  46. }
  47. return "";
  48. }
  49. }
  50. class HTTP_Response
  51. {
  52. public $StatusCode;
  53. public $Header;
  54. public $ContentLength;
  55. public $Body;
  56. public $Timeout;
  57. public function __construct($code, $body)
  58. {
  59. $this->StatusCode = $code;
  60. $this->Header = array();
  61. $this->Body = $body;
  62. $this->ContentLength = strlen($body);
  63. global $CURL_TIMEOUT;
  64. if ($CURL_TIMEOUT == null) {
  65. $CURL_TIMEOUT = 10;
  66. }
  67. $this->Timeout = $CURL_TIMEOUT;
  68. }
  69. }
  70. //@results: $val
  71. function UCloud_Header_Get($header, $key)
  72. {
  73. $val = @$header[$key];
  74. if (isset($val)) {
  75. if (is_array($val)) {
  76. return $val[0];
  77. }
  78. return $val;
  79. } else {
  80. return '';
  81. }
  82. }
  83. //@results: $error
  84. function UCloud_ResponseError($resp)
  85. {
  86. $header = $resp->Header;
  87. $err = new UCloud_Error($resp->StatusCode, null);
  88. if ($err->Code > 299) {
  89. if ($resp->ContentLength !== 0) {
  90. if (UCloud_Header_Get($header, 'Content-Type') === 'application/json') {
  91. $ret = json_decode($resp->Body, true);
  92. $err->ErrRet = $ret['ErrRet'];
  93. $err->ErrMsg = $ret['ErrMsg'];
  94. }
  95. }
  96. }
  97. $err->Reqid = UCloud_Header_Get($header, 'X-SessionId');
  98. return $err;
  99. }
  100. // --------------------------------------------------------------------------------
  101. //@results: ($resp, $error)
  102. function UCloud_Client_Do($req)
  103. {
  104. $ch = curl_init();
  105. $url = $req->URL;
  106. $options = array(
  107. CURLOPT_USERAGENT => $req->UA,
  108. CURLOPT_RETURNTRANSFER => true,
  109. CURLOPT_SSL_VERIFYPEER => false,
  110. CURLOPT_SSL_VERIFYHOST => false,
  111. CURLOPT_HEADER => true,
  112. CURLOPT_NOBODY => false,
  113. CURLOPT_CUSTOMREQUEST => $req->METHOD,
  114. //CURLOPT_URL => $url['host'] . "/" . rawurlencode($url['path']) . "?" . $req->EncodedQuery(),
  115. CURLOPT_TIMEOUT => $req->Timeout,
  116. CURLOPT_CONNECTTIMEOUT => $req->Timeout
  117. );
  118. if($req->METHOD =="HEAD"){
  119. $options[CURLOPT_NOBODY]=true;
  120. }
  121. if($req->EncodedQuery() !== ""){
  122. $options[CURLOPT_URL] = $url['host'] . "/" . $url['path'] . "?" . $req->EncodedQuery();
  123. }else{
  124. $options[CURLOPT_URL] = $url['host'] . "/" . $url['path'];
  125. }
  126. $httpHeader = $req->Header;
  127. if (!empty($httpHeader))
  128. {
  129. $header = array();
  130. foreach($httpHeader as $key => $parsedUrlValue) {
  131. $header[] = "$key: $parsedUrlValue";
  132. }
  133. $options[CURLOPT_HTTPHEADER] = $header;
  134. }
  135. $body = $req->Body;
  136. if (!empty($body)) {
  137. $options[CURLOPT_POSTFIELDS] = $body;
  138. } else {
  139. $options[CURLOPT_POSTFIELDS] = "";
  140. }
  141. curl_setopt_array($ch, $options);
  142. $result = curl_exec($ch);
  143. $ret = curl_errno($ch);
  144. if ($ret !== 0) {
  145. $err = new UCloud_Error(0, $ret, curl_error($ch));
  146. curl_close($ch);
  147. return array(null, $err);
  148. }
  149. $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  150. $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
  151. curl_close($ch);
  152. $responseArray = explode("\r\n\r\n", $result);
  153. $responseArraySize = sizeof($responseArray);
  154. $headerString = $responseArray[$responseArraySize-2];
  155. $respBody = $responseArray[$responseArraySize-1];
  156. $headers = parseHeaders($headerString);
  157. $resp = new HTTP_Response($code, $respBody);
  158. $resp->Header = $headers;
  159. $err = null;
  160. if (floor($resp->StatusCode/100) != 2) {
  161. list($r, $m) = parseError($respBody);
  162. $err = new UCloud_Error($resp->StatusCode, $r, $m);
  163. }
  164. return array($resp, $err);
  165. }
  166. function parseError($bodyString) {
  167. $r = 0;
  168. $m = '';
  169. $mp = json_decode($bodyString);
  170. if (isset($mp->{'ErrRet'})) $r = $mp->{'ErrRet'};
  171. if (isset($mp->{'ErrMsg'})) $m = $mp->{'ErrMsg'};
  172. return array($r, $m);
  173. }
  174. function parseHeaders($headerString) {
  175. $headers = explode("\r\n", $headerString);
  176. foreach($headers as $header) {
  177. if (strstr($header, ":")) {
  178. $header = trim($header);
  179. list($k, $v) = explode(":", $header);
  180. $headers[$k] = trim($v);
  181. }
  182. }
  183. return $headers;
  184. }
  185. class UCloud_HttpClient
  186. {
  187. //@results: ($resp, $error)
  188. public function RoundTrip($req)
  189. {
  190. return UCloud_Client_Do($req);
  191. }
  192. }
  193. class UCloud_AuthHttpClient
  194. {
  195. public $Auth;
  196. public $Type;
  197. public $MimeType;
  198. public function __construct($auth, $mimetype = null, $type = HEAD_FIELD_CHECK)
  199. {
  200. $this->Type = $type;
  201. $this->MimeType = $mimetype;
  202. $this->Auth = UCloud_MakeAuth($auth, $type);
  203. }
  204. //@results: ($resp, $error)
  205. public function RoundTrip($req)
  206. {
  207. if ($this->Type === HEAD_FIELD_CHECK) {
  208. $token = $this->Auth->SignRequest($req, $this->MimeType, $this->Type);
  209. $req->Header['Authorization'] = $token;
  210. }
  211. return UCloud_Client_Do($req);
  212. }
  213. }
  214. // --------------------------------------------------------------------------------
  215. //@results: ($data, $error)
  216. function UCloud_Client_Ret($resp)
  217. {
  218. $code = $resp->StatusCode;
  219. $data = null;
  220. if ($code >= 200 && $code <= 299) {
  221. if ($resp->ContentLength !== 0 && UCloud_Header_Get($resp->Header, 'Content-Type') == 'application/json') {
  222. $data = json_decode($resp->Body, true);
  223. if ($data === null) {
  224. $err = new UCloud_Error($code, 0, "");
  225. return array(null, $err);
  226. }
  227. }
  228. }
  229. $etag = UCloud_Header_Get($resp->Header, 'ETag');
  230. if ($etag != ''){
  231. $data['ETag'] = $etag;
  232. }else{
  233. $data['ETag'] = UCloud_Header_Get($resp->Header, 'Etag');
  234. }
  235. if (floor($code/100) == 2) {
  236. return array($data, null);
  237. }
  238. return array($data, UCloud_ResponseError($resp));
  239. }
  240. //@results: ($data, $error)
  241. function UCloud_Client_Call($self, $req, $type = HEAD_FIELD_CHECK)
  242. {
  243. list($resp, $err) = $self->RoundTrip($req, $type);
  244. if ($err !== null) {
  245. return array(null, $err);
  246. }
  247. return UCloud_Client_Ret($resp);
  248. }
  249. //@results: ($header,$data, $error)
  250. function UCloud_Client_Call_ReHeader($self, $req, $type = HEAD_FIELD_CHECK)
  251. {
  252. list($resp, $err) = $self->RoundTrip($req, $type);
  253. if ($err !== null) {
  254. return array(null, null, $err);
  255. }
  256. $code = $resp->StatusCode;
  257. $data = null;
  258. if ($code >= 200 && $code <= 299) {
  259. if ($resp->ContentLength !== 0 && UCloud_Header_Get($resp->Header, 'Content-Type') == 'application/json') {
  260. $data = json_decode($resp->Body, true);
  261. if ($data === null) {
  262. $err = new UCloud_Error($code, 0, "");
  263. return array($resp->Header, null, $err);
  264. }
  265. }
  266. }
  267. if (floor($code/100) == 2) {
  268. return array($resp->Header, $data, null);
  269. }
  270. return array($resp->Header, $data, UCloud_ResponseError($resp));
  271. }
  272. //@results: $error
  273. function UCloud_Client_CallNoRet($self, $req, $type = HEAD_FIELD_CHECK)
  274. {
  275. list($resp, $err) = $self->RoundTrip($req, $type);
  276. if ($err !== null) {
  277. return array(null, $err);
  278. }
  279. if (floor($resp->StatusCode/100) == 2) {
  280. return null;
  281. }
  282. return UCloud_ResponseError($resp);
  283. }
  284. //@results: ($data, $error)
  285. function UCloud_Client_CallWithForm(
  286. $self, $req, $body, $contentType = 'application/x-www-form-urlencoded')
  287. {
  288. if ($contentType === 'application/x-www-form-urlencoded') {
  289. if (is_array($req->Params)) {
  290. $body = http_build_query($req->Params);
  291. }
  292. }
  293. if ($contentType !== 'multipart/form-data') {
  294. $req->Header['Content-Type'] = $contentType;
  295. }
  296. $req->Body = $body;
  297. list($resp, $err) = $self->RoundTrip($req, HEAD_FIELD_CHECK);
  298. if ($err !== null) {
  299. return array(null, $err);
  300. }
  301. return UCloud_Client_Ret($resp);
  302. }
  303. // --------------------------------------------------------------------------------
  304. function UCloud_Client_CallWithMultipartForm($self, $req, $fields, $files)
  305. {
  306. list($contentType, $body) = UCloud_Build_MultipartForm($fields, $files);
  307. return UCloud_Client_CallWithForm($self, $req, $body, $contentType);
  308. }
  309. //@results: ($contentType, $body)
  310. function UCloud_Build_MultipartForm($fields, $files)
  311. {
  312. $data = array();
  313. $boundary = md5(microtime());
  314. foreach ($fields as $name => $val) {
  315. array_push($data, '--' . $boundary);
  316. array_push($data, "Content-Disposition: form-data; name=\"$name\"");
  317. array_push($data, '');
  318. array_push($data, $val);
  319. }
  320. foreach ($files as $file) {
  321. array_push($data, '--' . $boundary);
  322. list($name, $fileName, $fileBody, $mimeType) = $file;
  323. $mimeType = empty($mimeType) ? 'application/octet-stream' : $mimeType;
  324. $fileName = UCloud_EscapeQuotes($fileName);
  325. array_push($data, "Content-Disposition: form-data; name=\"$name\"; filename=\"$fileName\"");
  326. array_push($data, "Content-Type: $mimeType");
  327. array_push($data, '');
  328. array_push($data, $fileBody);
  329. }
  330. array_push($data, '--' . $boundary . '--');
  331. array_push($data, '');
  332. $body = implode("\r\n", $data);
  333. $contentType = 'multipart/form-data; boundary=' . $boundary;
  334. return array($contentType, $body);
  335. }
  336. function UCloud_UserAgent() {
  337. global $SDK_VER;
  338. $sdkInfo = "UCloudPHP/$SDK_VER";
  339. $systemInfo = php_uname("s");
  340. $machineInfo = php_uname("m");
  341. $envInfo = "($systemInfo/$machineInfo)";
  342. $phpVer = phpversion();
  343. $ua = "$sdkInfo $envInfo PHP/$phpVer";
  344. return $ua;
  345. }
  346. function UCloud_EscapeQuotes($str)
  347. {
  348. $find = array("\\", "\"");
  349. $replace = array("\\\\", "\\\"");
  350. return str_replace($find, $replace, $str);
  351. }
  352. // --------------------------------------------------------------------------------