- no pecl/event for ya
[m6w6/ext-http] / http_request_body_api.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2007, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_CURL
16 #include "php_http.h"
17
18 #ifdef HTTP_HAVE_CURL
19
20 #include "php_http_api.h"
21 #include "php_http_url_api.h"
22 #include "php_http_request_body_api.h"
23
24 /* {{{ http_request_body *http_request_body_new() */
25 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)
26 {
27 if (!body) {
28 body = emalloc_rel(sizeof(http_request_body));
29 }
30
31 body->type = type;
32 body->free = free;
33 body->priv = 0;
34 body->data = data;
35 body->size = size;
36
37 return body;
38 }
39 /* }}} */
40
41 /* {{{ http_request_body *http_request_body_fill(http_request_body *body, HashTable *, HashTable *) */
42 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)
43 {
44 if (files && (zend_hash_num_elements(files) > 0)) {
45 HashKey key = initHashKey(0);
46 zval **data;
47 HashPosition pos;
48 struct curl_httppost *http_post_data[2] = {NULL, NULL};
49
50 /* normal data */
51 if (fields) {
52 FOREACH_HASH_KEYVAL(pos, fields, key, data) {
53 if (key.type == HASH_KEY_IS_STRING) {
54 CURLcode err;
55 zval *orig = *data;
56
57 convert_to_string_ex(data);
58 err = curl_formadd(&http_post_data[0], &http_post_data[1],
59 CURLFORM_COPYNAME, key.str,
60 CURLFORM_COPYCONTENTS, Z_STRVAL_PP(data),
61 CURLFORM_CONTENTSLENGTH, (long) Z_STRLEN_PP(data),
62 CURLFORM_END
63 );
64
65 if (orig != *data) {
66 zval_ptr_dtor(data);
67 }
68
69 if (CURLE_OK != err) {
70 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not encode post fields: %s", curl_easy_strerror(err));
71 curl_formfree(http_post_data[0]);
72 return NULL;
73 }
74 }
75 }
76 }
77
78 /* file data */
79 FOREACH_HASH_VAL(pos, files, data) {
80 zval **file, **type, **name;
81
82 if (Z_TYPE_PP(data) != IS_ARRAY) {
83 http_error(HE_NOTICE, HTTP_E_INVALID_PARAM, "Unrecognized type of post file array entry");
84 } else if ( SUCCESS != zend_hash_find(Z_ARRVAL_PP(data), "name", sizeof("name"), (void *) &name) ||
85 SUCCESS != zend_hash_find(Z_ARRVAL_PP(data), "type", sizeof("type"), (void *) &type) ||
86 SUCCESS != zend_hash_find(Z_ARRVAL_PP(data), "file", sizeof("file"), (void *) &file)) {
87 http_error(HE_NOTICE, HTTP_E_INVALID_PARAM, "Post file array entry misses either 'name', 'type' or 'file' entry");
88 } else {
89 CURLcode err;
90 const char *path;
91 zval *ofile = *file, *otype = *type, *oname = *name;
92
93 convert_to_string_ex(file);
94 convert_to_string_ex(type);
95 convert_to_string_ex(name);
96
97 HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_PP(file), curl_formfree(http_post_data[0]); return NULL);
98
99 /* this is blatant but should be sufficient for most cases */
100 if (strncasecmp(Z_STRVAL_PP(file), "file://", lenof("file://"))) {
101 path = Z_STRVAL_PP(file);
102 } else {
103 path = Z_STRVAL_PP(file) + lenof("file://");
104 }
105
106 err = curl_formadd(&http_post_data[0], &http_post_data[1],
107 CURLFORM_COPYNAME, Z_STRVAL_PP(name),
108 CURLFORM_FILE, path,
109 CURLFORM_CONTENTTYPE, Z_STRVAL_PP(type),
110 CURLFORM_END
111 );
112
113 if (ofile != *file) zval_ptr_dtor(file);
114 if (otype != *type) zval_ptr_dtor(type);
115 if (oname != *name) zval_ptr_dtor(name);
116
117 if (CURLE_OK != err) {
118 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not encode post files: %s", curl_easy_strerror(err));
119 curl_formfree(http_post_data[0]);
120 return NULL;
121 }
122 }
123 }
124
125 return http_request_body_init_rel(body, HTTP_REQUEST_BODY_CURLPOST, http_post_data[0], 0, 1);
126
127 } else if (fields) {
128 char *encoded;
129 size_t encoded_len;
130
131 if (SUCCESS != http_urlencode_hash_ex(fields, 1, NULL, 0, &encoded, &encoded_len)) {
132 http_error(HE_WARNING, HTTP_E_ENCODING, "Could not encode post data");
133 return NULL;
134 }
135
136 return http_request_body_init_rel(body, HTTP_REQUEST_BODY_CSTRING, encoded, encoded_len, 1);
137 } else {
138 return http_request_body_init_rel(body, HTTP_REQUEST_BODY_CSTRING, estrndup("", 0), 0, 1);
139 }
140 }
141
142 /* STATUS http_request_body_encode(http_request_body *, char**, size_t *) */
143 PHP_HTTP_API STATUS _http_request_body_encode(http_request_body *body, char **buf, size_t *len TSRMLS_DC)
144 {
145 switch (body->type) {
146 case HTTP_REQUEST_BODY_CURLPOST:
147 {
148 #if defined(HAVE_CURL_FORMGET)
149 phpstr str;
150
151 phpstr_init_ex(&str, 0x8000, 0);
152 if (curl_formget(body->data, &str, (curl_formget_callback) phpstr_append)) {
153 phpstr_dtor(&str);
154 } else {
155 phpstr_fix(&str);
156 *buf = PHPSTR_VAL(&str);
157 *len = PHPSTR_LEN(&str);
158 return SUCCESS;
159 }
160 #endif
161 break;
162 }
163
164 case HTTP_REQUEST_BODY_CSTRING:
165 *buf = estrndup(body->data, *len = body->size);
166 return SUCCESS;
167
168 default:
169 break;
170 }
171 return FAILURE;
172 }
173 /* }}} */
174
175 /* {{{ void http_request_body_dtor(http_request_body *) */
176 PHP_HTTP_API void _http_request_body_dtor(http_request_body *body TSRMLS_DC)
177 {
178 if (body) {
179 if (body->free) {
180 switch (body->type) {
181 case HTTP_REQUEST_BODY_CSTRING:
182 if (body->data) {
183 efree(body->data);
184 }
185 break;
186
187 case HTTP_REQUEST_BODY_CURLPOST:
188 curl_formfree(body->data);
189 break;
190
191 case HTTP_REQUEST_BODY_UPLOADFILE:
192 php_stream_close(body->data);
193 break;
194 }
195 }
196 memset(body, 0, sizeof(http_request_body));
197 }
198 }
199 /* }}} */
200
201 /* {{{ void http_request_body_free(http_request_body *) */
202 PHP_HTTP_API void _http_request_body_free(http_request_body **body TSRMLS_DC)
203 {
204 if (*body) {
205 http_request_body_dtor(*body);
206 efree(*body);
207 *body = NULL;
208 }
209 }
210 /* }}} */
211
212 #endif /* HTTP_HAVE_CURL */
213
214 /*
215 * Local variables:
216 * tab-width: 4
217 * c-basic-offset: 4
218 * End:
219 * vim600: noet sw=4 ts=4 fdm=marker
220 * vim<600: noet sw=4 ts=4
221 */