xref: /PHP-7.4/ext/curl/share.c (revision 92ac598a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) The PHP Group                                          |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Author: Pierrick Charron <pierrick@php.net>                          |
16    +----------------------------------------------------------------------+
17 */
18 
19 #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
20 
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include "php.h"
26 
27 #if HAVE_CURL
28 
29 #include "php_curl.h"
30 
31 #include <curl/curl.h>
32 
33 #define SAVE_CURLSH_ERROR(__handle, __err) (__handle)->err.no = (int) __err;
34 
35 /* {{{ proto void curl_share_init()
36    Initialize a share curl handle */
PHP_FUNCTION(curl_share_init)37 PHP_FUNCTION(curl_share_init)
38 {
39 	php_curlsh *sh;
40 
41 	if (zend_parse_parameters_none() == FAILURE) {
42 		return;
43 	}
44 
45 	sh = ecalloc(1, sizeof(php_curlsh));
46 
47 	sh->share = curl_share_init();
48 
49 	RETURN_RES(zend_register_resource(sh, le_curl_share_handle));
50 }
51 /* }}} */
52 
53 /* {{{ proto void curl_share_close(resource sh)
54    Close a set of cURL handles */
PHP_FUNCTION(curl_share_close)55 PHP_FUNCTION(curl_share_close)
56 {
57 	zval *z_sh;
58 	php_curlsh *sh;
59 
60 	ZEND_PARSE_PARAMETERS_START(1,1)
61 		Z_PARAM_RESOURCE(z_sh)
62 	ZEND_PARSE_PARAMETERS_END();
63 
64 	if ((sh = (php_curlsh *)zend_fetch_resource(Z_RES_P(z_sh), le_curl_share_handle_name, le_curl_share_handle)) == NULL) {
65 		RETURN_FALSE;
66 	}
67 
68 	zend_list_close(Z_RES_P(z_sh));
69 }
70 /* }}} */
71 
_php_curl_share_setopt(php_curlsh * sh,zend_long option,zval * zvalue,zval * return_value)72 static int _php_curl_share_setopt(php_curlsh *sh, zend_long option, zval *zvalue, zval *return_value) /* {{{ */
73 {
74 	CURLSHcode error = CURLSHE_OK;
75 
76 	switch (option) {
77 		case CURLSHOPT_SHARE:
78 		case CURLSHOPT_UNSHARE:
79 			error = curl_share_setopt(sh->share, option, zval_get_long(zvalue));
80 			break;
81 
82 		default:
83 			php_error_docref(NULL, E_WARNING, "Invalid curl share configuration option");
84 			error = CURLSHE_BAD_OPTION;
85 			break;
86 	}
87 
88 	SAVE_CURLSH_ERROR(sh, error);
89 
90 	return error != CURLSHE_OK;
91 }
92 /* }}} */
93 
94 /* {{{ proto bool curl_share_setopt(resource sh, int option, mixed value)
95       Set an option for a cURL transfer */
PHP_FUNCTION(curl_share_setopt)96 PHP_FUNCTION(curl_share_setopt)
97 {
98 	zval       *zid, *zvalue;
99 	zend_long        options;
100 	php_curlsh *sh;
101 
102 	ZEND_PARSE_PARAMETERS_START(3,3)
103 		Z_PARAM_RESOURCE(zid)
104 		Z_PARAM_LONG(options)
105 		Z_PARAM_ZVAL(zvalue)
106 	ZEND_PARSE_PARAMETERS_END();
107 
108 	if ((sh = (php_curlsh *)zend_fetch_resource(Z_RES_P(zid), le_curl_share_handle_name, le_curl_share_handle)) == NULL) {
109 		RETURN_FALSE;
110 	}
111 
112 	if (!_php_curl_share_setopt(sh, options, zvalue, return_value)) {
113 		RETURN_TRUE;
114 	} else {
115 		RETURN_FALSE;
116 	}
117 }
118 /* }}} */
119 
_php_curl_share_close(zend_resource * rsrc)120 void _php_curl_share_close(zend_resource *rsrc) /* {{{ */
121 {
122 	php_curlsh *sh = (php_curlsh *)rsrc->ptr;
123 	if (sh) {
124 		curl_share_cleanup(sh->share);
125 		efree(sh);
126 		rsrc->ptr = NULL;
127 	}
128 }
129 /* }}} */
130 
131 /* {{{ proto int curl_share_errno(resource mh)
132          Return an integer containing the last share curl error number */
PHP_FUNCTION(curl_share_errno)133 PHP_FUNCTION(curl_share_errno)
134 {
135 	zval        *z_sh;
136 	php_curlsh  *sh;
137 
138 	ZEND_PARSE_PARAMETERS_START(1,1)
139 		Z_PARAM_RESOURCE(z_sh)
140 	ZEND_PARSE_PARAMETERS_END();
141 
142 	if ((sh = (php_curlsh *)zend_fetch_resource(Z_RES_P(z_sh), le_curl_share_handle_name, le_curl_share_handle)) == NULL) {
143 		RETURN_FALSE;
144 	}
145 
146 	RETURN_LONG(sh->err.no);
147 }
148 /* }}} */
149 
150 
151 /* {{{ proto bool curl_share_strerror(int code)
152          return string describing error code */
PHP_FUNCTION(curl_share_strerror)153 PHP_FUNCTION(curl_share_strerror)
154 {
155 	zend_long code;
156 	const char *str;
157 
158 	ZEND_PARSE_PARAMETERS_START(1,1)
159 		Z_PARAM_LONG(code)
160 	ZEND_PARSE_PARAMETERS_END();
161 
162 	str = curl_share_strerror(code);
163 	if (str) {
164 		RETURN_STRING(str);
165 	} else {
166 		RETURN_NULL();
167 	}
168 }
169 /* }}} */
170 
171 #endif
172