xref: /PHP-8.0/ext/curl/share.c (revision b63ea104)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | http://www.php.net/license/3_01.txt                                  |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Author: Pierrick Charron <pierrick@php.net>                          |
14    +----------------------------------------------------------------------+
15 */
16 
17 #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
18 
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 
23 #include "php.h"
24 #include "Zend/zend_interfaces.h"
25 
26 #include "curl_private.h"
27 
28 #include <curl/curl.h>
29 
30 #define SAVE_CURLSH_ERROR(__handle, __err) (__handle)->err.no = (int) __err;
31 
32 /* {{{ Initialize a share curl handle */
PHP_FUNCTION(curl_share_init)33 PHP_FUNCTION(curl_share_init)
34 {
35 	php_curlsh *sh;
36 
37 	ZEND_PARSE_PARAMETERS_NONE();
38 
39 	object_init_ex(return_value, curl_share_ce);
40 	sh = Z_CURL_SHARE_P(return_value);
41 
42 	sh->share = curl_share_init();
43 }
44 /* }}} */
45 
46 /* {{{ Close a set of cURL handles */
PHP_FUNCTION(curl_share_close)47 PHP_FUNCTION(curl_share_close)
48 {
49 	zval *z_sh;
50 
51 	ZEND_PARSE_PARAMETERS_START(1,1)
52 		Z_PARAM_OBJECT_OF_CLASS(z_sh, curl_share_ce)
53 	ZEND_PARSE_PARAMETERS_END();
54 }
55 /* }}} */
56 
_php_curl_share_setopt(php_curlsh * sh,zend_long option,zval * zvalue,zval * return_value)57 static int _php_curl_share_setopt(php_curlsh *sh, zend_long option, zval *zvalue, zval *return_value) /* {{{ */
58 {
59 	CURLSHcode error = CURLSHE_OK;
60 
61 	switch (option) {
62 		case CURLSHOPT_SHARE:
63 		case CURLSHOPT_UNSHARE:
64 			error = curl_share_setopt(sh->share, option, zval_get_long(zvalue));
65 			break;
66 
67 		default:
68 			zend_argument_value_error(2, "is not a valid cURL share option");
69 			error = CURLSHE_BAD_OPTION;
70 			break;
71 	}
72 
73 	SAVE_CURLSH_ERROR(sh, error);
74 
75 	return error != CURLSHE_OK;
76 }
77 /* }}} */
78 
79 /* {{{ Set an option for a cURL transfer */
PHP_FUNCTION(curl_share_setopt)80 PHP_FUNCTION(curl_share_setopt)
81 {
82 	zval       *z_sh, *zvalue;
83 	zend_long        options;
84 	php_curlsh *sh;
85 
86 	ZEND_PARSE_PARAMETERS_START(3,3)
87 		Z_PARAM_OBJECT_OF_CLASS(z_sh, curl_share_ce)
88 		Z_PARAM_LONG(options)
89 		Z_PARAM_ZVAL(zvalue)
90 	ZEND_PARSE_PARAMETERS_END();
91 
92 	sh = Z_CURL_SHARE_P(z_sh);
93 
94 	if (!_php_curl_share_setopt(sh, options, zvalue, return_value)) {
95 		RETURN_TRUE;
96 	} else {
97 		RETURN_FALSE;
98 	}
99 }
100 /* }}} */
101 
102 /* {{{ Return an integer containing the last share curl error number */
PHP_FUNCTION(curl_share_errno)103 PHP_FUNCTION(curl_share_errno)
104 {
105 	zval        *z_sh;
106 	php_curlsh  *sh;
107 
108 	ZEND_PARSE_PARAMETERS_START(1,1)
109 		Z_PARAM_OBJECT_OF_CLASS(z_sh, curl_share_ce)
110 	ZEND_PARSE_PARAMETERS_END();
111 
112 	sh = Z_CURL_SHARE_P(z_sh);
113 
114 	RETURN_LONG(sh->err.no);
115 }
116 /* }}} */
117 
118 
119 /* {{{ return string describing error code */
PHP_FUNCTION(curl_share_strerror)120 PHP_FUNCTION(curl_share_strerror)
121 {
122 	zend_long code;
123 	const char *str;
124 
125 	ZEND_PARSE_PARAMETERS_START(1,1)
126 		Z_PARAM_LONG(code)
127 	ZEND_PARSE_PARAMETERS_END();
128 
129 	str = curl_share_strerror(code);
130 	if (str) {
131 		RETURN_STRING(str);
132 	} else {
133 		RETURN_NULL();
134 	}
135 }
136 /* }}} */
137 
138 /* CurlShareHandle class */
139 
140 static zend_object_handlers curl_share_handlers;
141 
curl_share_create_object(zend_class_entry * class_type)142 static zend_object *curl_share_create_object(zend_class_entry *class_type) {
143 	php_curlsh *intern = zend_object_alloc(sizeof(php_curlsh), class_type);
144 
145 	zend_object_std_init(&intern->std, class_type);
146 	object_properties_init(&intern->std, class_type);
147 	intern->std.handlers = &curl_share_handlers;
148 
149 	return &intern->std;
150 }
151 
curl_share_get_constructor(zend_object * object)152 static zend_function *curl_share_get_constructor(zend_object *object) {
153 	zend_throw_error(NULL, "Cannot directly construct CurlShareHandle, use curl_share_init() instead");
154 	return NULL;
155 }
156 
curl_share_free_obj(zend_object * object)157 void curl_share_free_obj(zend_object *object)
158 {
159 	php_curlsh *sh = curl_share_from_obj(object);
160 
161 	curl_share_cleanup(sh->share);
162 	zend_object_std_dtor(&sh->std);
163 }
164 
curl_share_register_class(const zend_function_entry * method_entries)165 void curl_share_register_class(const zend_function_entry *method_entries) {
166 	zend_class_entry ce_share;
167 	INIT_CLASS_ENTRY(ce_share, "CurlShareHandle", method_entries);
168 	curl_share_ce = zend_register_internal_class(&ce_share);
169 	curl_share_ce->ce_flags |= ZEND_ACC_FINAL | ZEND_ACC_NO_DYNAMIC_PROPERTIES;
170 	curl_share_ce->create_object = curl_share_create_object;
171 	curl_share_ce->serialize = &zend_class_serialize_deny;
172 	curl_share_ce->unserialize = &zend_class_unserialize_deny;
173 
174 	memcpy(&curl_share_handlers, &std_object_handlers, sizeof(zend_object_handlers));
175 	curl_share_handlers.offset = XtOffsetOf(php_curlsh, std);
176 	curl_share_handlers.free_obj = curl_share_free_obj;
177 	curl_share_handlers.get_constructor = curl_share_get_constructor;
178 	curl_share_handlers.clone_obj = NULL;
179 	curl_share_handlers.compare = zend_objects_not_comparable;
180 }
181