forked from nickl-/pecl-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_request_body_api.c
332 lines (282 loc) · 9.34 KB
/
http_request_body_api.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
+--------------------------------------------------------------------+
| PECL :: http |
+--------------------------------------------------------------------+
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the conditions mentioned |
| in the accompanying LICENSE file are met. |
+--------------------------------------------------------------------+
| Copyright (c) 2004-2010, Michael Wallner <mike@php.net> |
+--------------------------------------------------------------------+
*/
/* $Id$ */
#define HTTP_WANT_CURL
#include "php_http.h"
#ifdef HTTP_HAVE_CURL
#include "php_http_api.h"
#include "php_http_url_api.h"
#include "php_http_request_body_api.h"
/* {{{ */
typedef struct curl_httppost *post_data[2];
static STATUS recursive_fields(post_data http_post_data, HashTable *fields, const char *prefix TSRMLS_DC);
static STATUS recursive_files(post_data http_post_data, HashTable *files, const char *prefix TSRMLS_DC);
/* }}} */
/* {{{ http_request_body *http_request_body_new() */
PHP_HTTP_API http_request_body *_http_request_body_init_ex(http_request_body *body, int type, void *data, size_t size, zend_bool free ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
{
if (!body) {
body = emalloc_rel(sizeof(http_request_body));
}
body->type = type;
body->free = free;
body->priv = 0;
body->data = data;
body->size = size;
return body;
}
/* }}} */
/* {{{ http_request_body *http_request_body_fill(http_request_body *body, HashTable *, HashTable *) */
PHP_HTTP_API http_request_body *_http_request_body_fill(http_request_body *body, HashTable *fields, HashTable *files ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
{
if (files && (zend_hash_num_elements(files) > 0)) {
struct curl_httppost *http_post_data[2] = {NULL, NULL};
if (fields && SUCCESS != recursive_fields(http_post_data, fields, NULL TSRMLS_CC)) {
return NULL;
}
if (SUCCESS != recursive_files(http_post_data, files, NULL TSRMLS_CC)) {
return NULL;
}
return http_request_body_init_rel(body, HTTP_REQUEST_BODY_CURLPOST, http_post_data[0], 0, 1);
} else if (fields) {
char *encoded;
size_t encoded_len;
if (SUCCESS != http_urlencode_hash_ex(fields, 1, NULL, 0, &encoded, &encoded_len)) {
http_error(HE_WARNING, HTTP_E_ENCODING, "Could not encode post data");
return NULL;
}
return http_request_body_init_rel(body, HTTP_REQUEST_BODY_CSTRING, encoded, encoded_len, 1);
} else {
return http_request_body_init_rel(body, HTTP_REQUEST_BODY_CSTRING, estrndup("", 0), 0, 1);
}
}
/* STATUS http_request_body_encode(http_request_body *, char**, size_t *) */
PHP_HTTP_API STATUS _http_request_body_encode(http_request_body *body, char **buf, size_t *len TSRMLS_DC)
{
switch (body->type) {
case HTTP_REQUEST_BODY_CURLPOST:
{
#ifdef HAVE_CURL_FORMGET
phpstr str;
phpstr_init_ex(&str, 0x8000, 0);
if (curl_formget(body->data, &str, (curl_formget_callback) phpstr_append)) {
phpstr_dtor(&str);
} else {
phpstr_fix(&str);
*buf = PHPSTR_VAL(&str);
*len = PHPSTR_LEN(&str);
return SUCCESS;
}
#endif
break;
}
case HTTP_REQUEST_BODY_CSTRING:
*buf = estrndup(body->data, *len = body->size);
return SUCCESS;
default:
break;
}
return FAILURE;
}
/* }}} */
/* {{{ void http_request_body_dtor(http_request_body *) */
PHP_HTTP_API void _http_request_body_dtor(http_request_body *body TSRMLS_DC)
{
if (body) {
if (body->free) {
switch (body->type) {
case HTTP_REQUEST_BODY_CSTRING:
if (body->data) {
efree(body->data);
}
break;
case HTTP_REQUEST_BODY_CURLPOST:
curl_formfree(body->data);
break;
case HTTP_REQUEST_BODY_UPLOADFILE:
php_stream_close(body->data);
break;
}
}
memset(body, 0, sizeof(http_request_body));
}
}
/* }}} */
/* {{{ void http_request_body_free(http_request_body *) */
PHP_HTTP_API void _http_request_body_free(http_request_body **body TSRMLS_DC)
{
if (*body) {
http_request_body_dtor(*body);
efree(*body);
*body = NULL;
}
}
/* }}} */
static inline char *format_key(uint type, char *str, ulong num, const char *prefix, int numeric_key_for_empty_prefix) {
char *new_key = NULL;
if (prefix && *prefix) {
if (type == HASH_KEY_IS_STRING) {
spprintf(&new_key, 0, "%s[%s]", prefix, str);
} else {
spprintf(&new_key, 0, "%s[%lu]", prefix, num);
}
} else if (type == HASH_KEY_IS_STRING) {
new_key = estrdup(str);
} else if (numeric_key_for_empty_prefix) {
spprintf(&new_key, 0, "%lu", num);
}
return new_key;
}
/* {{{ static STATUS recursive_fields(post_data d, HashTable *f, const char *p TSRMLS_DC) */
static STATUS recursive_fields(post_data http_post_data, HashTable *fields, const char *prefix TSRMLS_DC) {
HashKey key = initHashKey(0);
zval **data_ptr;
HashPosition pos;
char *new_key = NULL;
CURLcode err = 0;
if (fields && !fields->nApplyCount) {
FOREACH_HASH_KEYVAL(pos, fields, key, data_ptr) {
if (key.type != HASH_KEY_IS_STRING || *key.str) {
new_key = format_key(key.type, key.str, key.num, prefix, 1);
switch (Z_TYPE_PP(data_ptr)) {
case IS_ARRAY:
case IS_OBJECT: {
STATUS status;
++fields->nApplyCount;
status = recursive_fields(http_post_data, HASH_OF(*data_ptr), new_key TSRMLS_CC);
--fields->nApplyCount;
if (SUCCESS != status) {
goto error;
}
break;
}
default: {
zval *data = http_zsep(IS_STRING, *data_ptr);
err = curl_formadd(&http_post_data[0], &http_post_data[1],
CURLFORM_COPYNAME, new_key,
CURLFORM_COPYCONTENTS, Z_STRVAL_P(data),
CURLFORM_CONTENTSLENGTH, (long) Z_STRLEN_P(data),
CURLFORM_END
);
zval_ptr_dtor(&data);
if (CURLE_OK != err) {
goto error;
}
break;
}
}
STR_FREE(new_key);
}
}
}
return SUCCESS;
error:
if (new_key) {
efree(new_key);
}
if (http_post_data[0]) {
curl_formfree(http_post_data[0]);
}
if (err) {
http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not encode post fields: %s", curl_easy_strerror(err));
} else {
http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not encode post fields: unknown error");
}
return FAILURE;
}
/* }}} */
/* {{{ static STATUS recursive_files(post_data d, HashTable *f, const char *p TSRMLS_DC) */
static STATUS recursive_files(post_data http_post_data, HashTable *files, const char *prefix TSRMLS_DC) {
HashKey key = initHashKey(0);
zval **data_ptr;
HashPosition pos;
char *new_key = NULL;
CURLcode err = 0;
if (files && !files->nApplyCount) {
FOREACH_HASH_KEYVAL(pos, files, key, data_ptr) {
zval **file_ptr, **type_ptr, **name_ptr;
if (key.type != HASH_KEY_IS_STRING || *key.str) {
new_key = format_key(key.type, key.str, key.num, prefix, 0);
if (Z_TYPE_PP(data_ptr) != IS_ARRAY && Z_TYPE_PP(data_ptr) != IS_OBJECT) {
if (new_key || key.type == HASH_KEY_IS_STRING) {
http_error_ex(HE_NOTICE, HTTP_E_INVALID_PARAM, "Unrecognized type of post file array entry '%s'", new_key ? new_key : key.str);
} else {
http_error_ex(HE_NOTICE, HTTP_E_INVALID_PARAM, "Unrecognized type of post file array entry '%lu'", key.num);
}
} else if ( SUCCESS != zend_hash_find(HASH_OF(*data_ptr), "name", sizeof("name"), (void *) &name_ptr) ||
SUCCESS != zend_hash_find(HASH_OF(*data_ptr), "type", sizeof("type"), (void *) &type_ptr) ||
SUCCESS != zend_hash_find(HASH_OF(*data_ptr), "file", sizeof("file"), (void *) &file_ptr)) {
STATUS status;
++files->nApplyCount;
status = recursive_files(http_post_data, HASH_OF(*data_ptr), new_key TSRMLS_CC);
--files->nApplyCount;
if (SUCCESS != status) {
goto error;
}
} else {
const char *path;
zval *file = http_zsep(IS_STRING, *file_ptr);
zval *type = http_zsep(IS_STRING, *type_ptr);
zval *name = http_zsep(IS_STRING, *name_ptr);
HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_P(file), goto error);
/* this is blatant but should be sufficient for most cases */
if (strncasecmp(Z_STRVAL_P(file), "file://", lenof("file://"))) {
path = Z_STRVAL_P(file);
} else {
path = Z_STRVAL_P(file) + lenof("file://");
}
if (new_key) {
char *tmp_key = format_key(HASH_KEY_IS_STRING, Z_STRVAL_P(name), 0, new_key, 0);
STR_SET(new_key, tmp_key);
}
err = curl_formadd(&http_post_data[0], &http_post_data[1],
CURLFORM_COPYNAME, new_key ? new_key : Z_STRVAL_P(name),
CURLFORM_FILE, path,
CURLFORM_CONTENTTYPE, Z_STRVAL_P(type),
CURLFORM_END
);
zval_ptr_dtor(&file);
zval_ptr_dtor(&type);
zval_ptr_dtor(&name);
if (CURLE_OK != err) {
goto error;
}
}
STR_FREE(new_key);
}
}
}
return SUCCESS;
error:
if (new_key) {
efree(new_key);
}
if (http_post_data[0]) {
curl_formfree(http_post_data[0]);
}
if (err) {
http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not encode post files: %s", curl_easy_strerror(err));
} else {
http_error(HE_WARNING, HTTP_E_ENCODING, "Could not encode post files: unknown error");
}
return FAILURE;
}
/* }}} */
#endif /* HTTP_HAVE_CURL */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/