PageRenderTime 43ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/runtime/ext/curl/ext_curl.php

https://gitlab.com/iranjith4/hhvm
PHP | 440 lines | 103 code | 33 blank | 304 comment | 13 complexity | 8dfcbdcdc4479dd9733e10453d6579de MD5 | raw file
  1. <?hh
  2. namespace {
  3. /**
  4. * Close a cURL session
  5. *
  6. * @param resource $ch -
  7. *
  8. * @return void -
  9. */
  10. <<__Native>>
  11. function curl_close(resource $ch): ?bool;
  12. /**
  13. * Copy a cURL handle along with all of its preferences
  14. *
  15. * @param resource $ch -
  16. *
  17. * @return resource - Returns a new cURL handle.
  18. */
  19. <<__Native>>
  20. function curl_copy_handle(resource $ch): mixed;
  21. /**
  22. * Return the last error number
  23. *
  24. * @param resource $ch -
  25. *
  26. * @return int - Returns the error number or 0 (zero) if no error
  27. * occurred.
  28. */
  29. <<__Native>>
  30. function curl_errno(resource $ch): mixed;
  31. /**
  32. * Return a string containing the last error for the current session
  33. *
  34. * @param resource $ch -
  35. *
  36. * @return string - Returns the error message or '' (the empty string) if
  37. * no error occurred.
  38. */
  39. <<__Native>>
  40. function curl_error(resource $ch): mixed;
  41. /**
  42. * Returns a string description of a cURL error code
  43. *
  44. * @param int $errno - a curl error code, e.g. returned by curl_errno()
  45. *
  46. * @return string - Returns a string description of a cURL error code
  47. */
  48. <<__Native>>
  49. function curl_strerror(int $errno): string;
  50. /**
  51. * Perform a cURL session
  52. *
  53. * @param resource $ch -
  54. *
  55. * @return mixed - However, if the CURLOPT_RETURNTRANSFER option is set,
  56. * it will return the result on success, FALSE on failure.
  57. */
  58. <<__Native>>
  59. function curl_exec(resource $ch): mixed;
  60. /**
  61. * Get information regarding a specific transfer
  62. *
  63. * @param resource $ch -
  64. * @param int $opt - This may be one of the following constants:
  65. * CURLINFO_EFFECTIVE_URL - Last effective URL CURLINFO_HTTP_CODE -
  66. * Last received HTTP code CURLINFO_FILETIME - Remote time of the
  67. * retrieved document, if -1 is returned the time of the document is
  68. * unknown CURLINFO_TOTAL_TIME - Total transaction time in seconds
  69. * for last transfer CURLINFO_NAMELOOKUP_TIME - Time in seconds until
  70. * name resolving was complete CURLINFO_CONNECT_TIME - Time in
  71. * seconds it took to establish the connection
  72. * CURLINFO_PRETRANSFER_TIME - Time in seconds from start until just
  73. * before file transfer begins CURLINFO_STARTTRANSFER_TIME - Time in
  74. * seconds until the first byte is about to be transferred
  75. * CURLINFO_REDIRECT_COUNT - Number of redirects
  76. * CURLINFO_REDIRECT_TIME - Time in seconds of all redirection steps
  77. * before final transaction was started CURLINFO_SIZE_UPLOAD - Total
  78. * number of bytes uploaded CURLINFO_SIZE_DOWNLOAD - Total number of
  79. * bytes downloaded CURLINFO_SPEED_DOWNLOAD - Average download speed
  80. * CURLINFO_SPEED_UPLOAD - Average upload speed
  81. * CURLINFO_HEADER_SIZE - Total size of all headers received
  82. * CURLINFO_HEADER_OUT - The request string sent. For this to work, add
  83. * the CURLINFO_HEADER_OUT option to the handle by calling curl_setopt()
  84. * CURLINFO_REQUEST_SIZE - Total size of issued requests, currently
  85. * only for HTTP requests CURLINFO_SSL_VERIFYRESULT - Result of SSL
  86. * certification verification requested by setting CURLOPT_SSL_VERIFYPEER
  87. * CURLINFO_CONTENT_LENGTH_DOWNLOAD - content-length of download,
  88. * read from Content-Length: field CURLINFO_CONTENT_LENGTH_UPLOAD -
  89. * Specified size of upload CURLINFO_CONTENT_TYPE - Content-Type: of
  90. * the requested document, NULL indicates server did not send valid
  91. * Content-Type: header
  92. *
  93. * @return mixed - If opt is given, returns its value. Otherwise, returns
  94. * an associative array with the following elements (which correspond to
  95. * opt), or FALSE on failure: "url" "content_type" "http_code"
  96. * "header_size" "request_size" "filetime"
  97. * "ssl_verify_result" "redirect_count" "total_time"
  98. * "namelookup_time" "connect_time" "pretransfer_time"
  99. * "size_upload" "size_download" "speed_download"
  100. * "speed_upload" "download_content_length"
  101. * "upload_content_length" "starttransfer_time" "redirect_time"
  102. * "certinfo" "request_header" (This is only set if the
  103. * CURLINFO_HEADER_OUT is set by a previous call to curl_setopt())
  104. */
  105. <<__Native>>
  106. function curl_getinfo(resource $ch,
  107. int $opt = 0): mixed;
  108. /**
  109. * Initialize a cURL session
  110. *
  111. * @param string $url - If provided, the CURLOPT_URL option will be set
  112. * to its value. You can manually set this using the curl_setopt()
  113. * function. The file protocol is disabled by cURL if open_basedir is
  114. * set.
  115. *
  116. * @return resource - Returns a cURL handle on success, FALSE on errors.
  117. */
  118. <<__Native>>
  119. function curl_init(?string $url = null): mixed;
  120. /**
  121. * Initialize a cURL session using a pooled curl handle. When this resource
  122. * is garbage collected, the curl handle will be saved for reuse later.
  123. * Pooled curl handles persist between requests.
  124. *
  125. * @param string $poolName - The name of the connection pool to use.
  126. * Named connection pools are initialized via the 'curl.namedPools' ini
  127. * setting, which is a comma separated list of named pools to create.
  128. * @param string $url - If provided, the CURLOPT_URL option will be set
  129. * to its value. You can manually set this using the curl_setopt()
  130. * function. The file protocol is disabled by cURL if open_basedir is
  131. * set.
  132. *
  133. * @return resource - Returns a cURL handle on success, FALSE on errors.
  134. */
  135. <<__Native, __HipHopSpecific>>
  136. function curl_init_pooled(string $poolName, ?string $url = null): mixed;
  137. /**
  138. * Add a normal cURL handle to a cURL multi handle
  139. *
  140. * @param resource $mh -
  141. * @param resource $ch -
  142. *
  143. * @return int - Returns 0 on success, or one of the CURLM_XXX errors
  144. * code.
  145. */
  146. <<__Native>>
  147. function curl_multi_add_handle(resource $mh,
  148. resource $ch): ?int;
  149. /**
  150. * Close a set of cURL handles
  151. *
  152. * @param resource $mh -
  153. *
  154. * @return void -
  155. */
  156. <<__Native>>
  157. function curl_multi_close(resource $mh): mixed;
  158. /**
  159. * Run the sub-connections of the current cURL handle
  160. *
  161. * @param resource $mh -
  162. * @param int $still_running - A reference to a flag to tell whether the
  163. * operations are still running.
  164. *
  165. * @return int - A cURL code defined in the cURL Predefined Constants.
  166. * This only returns errors regarding the whole multi stack. There might
  167. * still have occurred problems on individual transfers even when this
  168. * function returns CURLM_OK.
  169. */
  170. <<__Native>>
  171. function curl_multi_exec(resource $mh,
  172. mixed &$still_running): ?int;
  173. /**
  174. * Return the content of a cURL handle if is set
  175. *
  176. * @param resource $ch -
  177. *
  178. * @return string - Return the content of a cURL handle if
  179. * CURLOPT_RETURNTRANSFER is set.
  180. */
  181. <<__Native>>
  182. function curl_multi_getcontent(resource $ch): ?string;
  183. /**
  184. * Get information about the current transfers
  185. *
  186. * @param resource $mh -
  187. * @param int $msgs_in_queue - Number of messages that are still in the
  188. * queue
  189. *
  190. * @return array - On success, returns an associative array for the
  191. * message, FALSE on failure. Contents of the returned array Key:
  192. * Value: msg The CURLMSG_DONE constant. Other return values are
  193. * currently not available. result One of the CURLE_* constants. If
  194. * everything is OK, the CURLE_OK will be the result. handle Resource
  195. * of type curl indicates the handle which it concerns.
  196. */
  197. <<__Native>>
  198. function curl_multi_info_read(resource $mh,
  199. mixed &$msgs_in_queue = NULL): mixed;
  200. /**
  201. * Returns a new cURL multi handle
  202. *
  203. * @return resource - Returns a cURL multi handle resource on success,
  204. * FALSE on failure.
  205. */
  206. <<__Native>>
  207. function curl_multi_init(): resource;
  208. /**
  209. * Returns a text error message describing the given CURLM error code.
  210. *
  211. * @return string - Returns error string for valid error code,
  212. * NULL otherwise.
  213. */
  214. <<__Native>>
  215. function curl_multi_strerror(int $errornum): mixed;
  216. /**
  217. * Remove a multi handle from a set of cURL handles
  218. *
  219. * @param resource $mh -
  220. * @param resource $ch -
  221. *
  222. * @return int - Returns 0 on success, or one of the CURLM_XXX error
  223. * codes.
  224. */
  225. <<__Native>>
  226. function curl_multi_remove_handle(resource $mh,
  227. resource $ch): ?int;
  228. /**
  229. * Wait for activity on any curl_multi connection
  230. *
  231. * @param resource $mh -
  232. * @param float $timeout - Time, in seconds, to wait for a response.
  233. *
  234. * @return int - On success, returns the number of descriptors contained
  235. * in the descriptor sets. On failure, this function will return -1 on a
  236. * select failure or timeout (from the underlying select system call).
  237. */
  238. <<__Native>>
  239. function curl_multi_select(resource $mh,
  240. float $timeout = 1.0): ?int;
  241. /**
  242. * The async equivalent to
  243. * [`curl_multi_select`](http://php.net/manual/en/function.curl-multi-select.php)
  244. *
  245. * This function waits until there is activity on a cURL handle within `$mh`.
  246. * Once there is activity, you process the result with
  247. * [`curl_multi_exec`](http://php.net/manual/en/function.curl-multi-exec.php)
  248. *
  249. * @param $mh - A cURL multi handle returned from
  250. * [`curl_multi_init`](http://php.net/manual/en/function.curl-multi-init.php).
  251. * @param $timeout - The time to wait for a response indicating some activity.
  252. *
  253. * @return Awaitable<int> - An `Awaitable` representing the `int` result of the
  254. * activity. If returned `int` is positive, that
  255. * represents the number of handles on which there
  256. * was activity. If `0`, that means no activity
  257. * occurred. If negative, then there was a select
  258. * failure.
  259. *
  260. * @guide /hack/async/introduction
  261. * @guide /hack/async/extensions
  262. */
  263. <<__Native>>
  264. function curl_multi_await(resource $mh,
  265. float $timeout = 1.0): Awaitable<int>;
  266. /**
  267. * Wait for activity on any curl_multi connection
  268. *
  269. * @param resource $mh -
  270. * @param int $option - One of the CURLMOPT_* constants.
  271. * @param int $option - The value to be set on option.
  272. *
  273. * @return Returns TRUE on success or FALSE on failure.
  274. */
  275. <<__Native>>
  276. function curl_multi_setopt(resource $mh, int $option, mixed $value) : bool;
  277. /**
  278. * Set multiple options for a cURL transfer
  279. *
  280. * @param resource $ch -
  281. * @param array $options - An array specifying which options to set and
  282. * their values. The keys should be valid curl_setopt() constants or
  283. * their integer equivalents.
  284. *
  285. * @return bool - Returns TRUE if all options were successfully set. If
  286. * an option could not be successfully set, FALSE is immediately
  287. * returned, ignoring any future options in the options array.
  288. */
  289. <<__Native>>
  290. function curl_setopt_array(resource $ch,
  291. array $options): bool;
  292. /**
  293. * Set an option for a cURL transfer
  294. *
  295. * @param resource $ch -
  296. * @param int $option - The CURLOPT_XXX option to set.
  297. * @param mixed $value - The value to be set on option.
  298. *
  299. * @return bool -
  300. */
  301. <<__Native>>
  302. function curl_setopt(resource $ch,
  303. int $option,
  304. mixed $value): bool;
  305. /**
  306. * Gets cURL version information
  307. *
  308. * @param int $age -
  309. *
  310. * @return array - Returns an associative array with the following
  311. * elements: Indice Value description version_number cURL 24 bit
  312. * version number version cURL version number, as a string
  313. * ssl_version_number OpenSSL 24 bit version number ssl_version OpenSSL
  314. * version number, as a string libz_version zlib version number, as a
  315. * string host Information about the host where cURL was built age
  316. * features A bitmask of the CURL_VERSION_XXX constants protocols An
  317. * array of protocols names supported by cURL
  318. */
  319. <<__Native>>
  320. function curl_version(int $age = CURLVERSION_NOW): mixed;
  321. <<__Native>>
  322. function curl_reset(resource $ch): void;
  323. /**
  324. * Gets options on the given cURL session handle.
  325. *
  326. * @param resource $ch - A cURL handle returned by curl_init().
  327. * @param int $opt - This should be one of the CURLOPT_* values.
  328. *
  329. * @return mixed - If opt is given, returns its value. Otherwise, returns an
  330. * associative array.
  331. */
  332. <<__Native, __HipHopSpecific>>
  333. function fb_curl_getopt(resource $ch, int $opt = 0): mixed;
  334. /**
  335. * extracts file descriptor information from a multi handle.
  336. *
  337. * @param resource $mh - A cURL multi handle returned by
  338. * curl_multi_init().
  339. * @param array& $read_fd_set - read set
  340. * @param array& $write_fd_set - write set
  341. * @param array& $exc_fd_set - exception set
  342. * @param int& $max_fd - If no file descriptors are set, $max_fd will
  343. * contain -1. Otherwise it will contain the higher descriptor number.
  344. *
  345. * @return mixed - Returns 0 on success, or one of the CURLM_XXX errors code.
  346. */
  347. <<__Native, __HipHopSpecific>>
  348. function fb_curl_multi_fdset(resource $mh, mixed &$read_fd_set,
  349. mixed &$write_fd_set, mixed &$exc_fd_set,
  350. ?int &$max_fd = null): mixed;
  351. } // root namespace
  352. namespace HH\Asio {
  353. /**
  354. * A convenience wrapper around
  355. * [`curl_multi_await`](/hack/reference/function/curl_multi_await/).
  356. *
  357. * Pass a cURL handle, or, more simply, a string containing a URL (and the
  358. * cURL handle will be created for you), and the cURL request will be executed
  359. * via async and the `string` result will be retuned.
  360. *
  361. * @param $urlOrHandle - An existing cURL handle or a URL as a `string`. String
  362. * URLs will create a default cURL GET handle.
  363. * @return Awaitable<string> - An `Awaitable` representing the `string` result
  364. * of the cURL request.
  365. *
  366. * @guide /hack/async/introduction
  367. * @guide /hack/async/extensions
  368. */
  369. async function curl_exec(mixed $urlOrHandle): Awaitable<string> {
  370. if (is_string($urlOrHandle)) {
  371. $ch = curl_init($urlOrHandle);
  372. } else if (is_resource($urlOrHandle) &&
  373. (get_resource_type($urlOrHandle) == "curl")) {
  374. $ch = $urlOrHandle;
  375. } else {
  376. throw new Exception(__FUNCTION__." expects string of cURL handle");
  377. }
  378. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  379. $mh = curl_multi_init();
  380. curl_multi_add_handle($mh, $ch);
  381. $sleep_ms = 10;
  382. do {
  383. $active = 1;
  384. do {
  385. $status = curl_multi_exec($mh, $active);
  386. } while ($status == CURLM_CALL_MULTI_PERFORM);
  387. if (!$active) break;
  388. $select = await curl_multi_await($mh);
  389. /* If cURL is built without ares support, DNS queries don't have a socket
  390. * to wait on, so curl_multi_await() (and curl_select() in PHP5) will return
  391. * -1, and polling is required.
  392. */
  393. if ($select == -1) {
  394. await SleepWaitHandle::create($sleep_ms * 1000);
  395. if ($sleep_ms < 1000) {
  396. $sleep_ms *= 2;
  397. }
  398. } else {
  399. $sleep_ms = 10;
  400. }
  401. } while ($status === CURLM_OK);
  402. $content = (string)curl_multi_getcontent($ch);
  403. curl_multi_remove_handle($mh, $ch);
  404. curl_multi_close($mh);
  405. return $content;
  406. }
  407. } // namespace HH\Asio