401e0ac453500c15d9b5a34d616224c2632263c9
[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-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
18
19 #define HTTP_WANT_CURL
20 #include "php_http.h"
21
22 #ifdef HTTP_HAVE_CURL
23
24 #include "php_http_api.h"
25 #include "php_http_url_api.h"
26 #include "php_http_request_body_api.h"
27
28 /* {{{ http_request_body *http_request_body_new() */
29 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)
30 {
31 if (!body) {
32 body = emalloc_rel(sizeof(http_request_body));
33 }
34
35 body->type = type;
36 body->free = free;
37 body->data = data;
38 body->size = size;
39
40 return body;
41 }
42 /* }}} */
43
44 /* {{{ http_request_body *http_request_body_fill(http_request_body *body, HashTable *, HashTable *) */
45 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)
46 {
47 if (files && (zend_hash_num_elements(files) > 0)) {
48 char *key = NULL;
49 ulong idx;
50 zval **data;
51 HashPosition pos;
52 struct curl_httppost *http_post_data[2] = {NULL, NULL};
53
54 /* normal data */
55 if (fields) {
56 FOREACH_HASH_KEYVAL(pos, fields, key, idx, data) {
57 if (key) {
58 CURLcode err;
59 zval *orig = *data;
60
61 convert_to_string_ex(data);
62 err = curl_formadd(&http_post_data[0], &http_post_data[1],
63 CURLFORM_COPYNAME, key,
64 CURLFORM_COPYCONTENTS, Z_STRVAL_PP(data),
65 CURLFORM_CONTENTSLENGTH, (long) Z_STRLEN_PP(data),
66 CURLFORM_END
67 );
68
69 if (orig != *data) {
70 zval_ptr_dtor(data);
71 }
72
73 if (CURLE_OK != err) {
74 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not encode post fields: %s", curl_easy_strerror(err));
75 curl_formfree(http_post_data[0]);
76 return NULL;
77 }
78
79 /* reset */
80 key = NULL;
81 }
82 }
83 }
84
85 /* file data */
86 FOREACH_HASH_VAL(pos, files, data) {
87 zval **file, **type, **name;
88
89 if (Z_TYPE_PP(data) != IS_ARRAY) {
90 http_error(HE_NOTICE, HTTP_E_INVALID_PARAM, "Unrecognized type of post file array entry");
91 } else if ( SUCCESS != zend_hash_find(Z_ARRVAL_PP(data), "name", sizeof("name"), (void **) &name) ||
92 SUCCESS != zend_hash_find(Z_ARRVAL_PP(data), "type", sizeof("type"), (void **) &type) ||
93 SUCCESS != zend_hash_find(Z_ARRVAL_PP(data), "file", sizeof("file"), (void **) &file)) {
94 http_error(HE_NOTICE, HTTP_E_INVALID_PARAM, "Post file array entry misses either 'name', 'type' or 'file' entry");
95 } else {
96 CURLcode err;
97 const char *path;
98 zval *ofile = *file, *otype = *type, *oname = *name;
99
100 convert_to_string_ex(file);
101 convert_to_string_ex(type);
102 convert_to_string_ex(name);
103
104 HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_PP(file), curl_formfree(http_post_data[0]); return NULL);
105
106 /* this is blatant but should be sufficient for most cases */
107 if (strncasecmp(Z_STRVAL_PP(file), "file://", lenof("file://"))) {
108 path = Z_STRVAL_PP(file);
109 } else {
110 path = Z_STRVAL_PP(file) + lenof("file://");
111 }
112
113 err = curl_formadd(&http_post_data[0], &http_post_data[1],
114 CURLFORM_COPYNAME, Z_STRVAL_PP(name),
115 CURLFORM_FILE, path,
116 CURLFORM_CONTENTTYPE, Z_STRVAL_PP(type),
117 CURLFORM_END
118 );
119
120 if (ofile != *file) zval_ptr_dtor(file);
121 if (otype != *type) zval_ptr_dtor(type);
122 if (oname != *name) zval_ptr_dtor(name);
123
124 if (CURLE_OK != err) {
125 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not encode post files: %s", curl_easy_strerror(err));
126 curl_formfree(http_post_data[0]);
127 return NULL;
128 }
129 }
130 }
131
132 return http_request_body_init_rel(body, HTTP_REQUEST_BODY_CURLPOST, http_post_data[0], 0, 1);
133
134 } else if (fields) {
135 char *encoded;
136 size_t encoded_len;
137
138 if (SUCCESS != http_urlencode_hash_ex(fields, 1, NULL, 0, &encoded, &encoded_len)) {
139 http_error(HE_WARNING, HTTP_E_ENCODING, "Could not encode post data");
140 return NULL;
141 }
142
143 return http_request_body_init_rel(body, HTTP_REQUEST_BODY_CSTRING, encoded, encoded_len, 1);
144 } else {
145 return http_request_body_init_rel(body, HTTP_REQUEST_BODY_CSTRING, estrndup("", 0), 0, 1);
146 }
147 }
148
149
150 /* {{{ void http_request_body_dtor(http_request_body *) */
151 PHP_HTTP_API void _http_request_body_dtor(http_request_body *body TSRMLS_DC)
152 {
153 if (body) {
154 if (body->free) {
155 switch (body->type)
156 {
157 case HTTP_REQUEST_BODY_CSTRING:
158 if (body->data) {
159 efree(body->data);
160 }
161 break;
162
163 case HTTP_REQUEST_BODY_CURLPOST:
164 curl_formfree(body->data);
165 break;
166
167 case HTTP_REQUEST_BODY_UPLOADFILE:
168 php_stream_close(body->data);
169 break;
170 }
171 }
172 memset(body, 0, sizeof(http_request_body));
173 }
174 }
175 /* }}} */
176
177 /* {{{ void http_request_body_free(http_request_body *) */
178 PHP_HTTP_API void _http_request_body_free(http_request_body **body TSRMLS_DC)
179 {
180 if (*body) {
181 http_request_body_dtor(*body);
182 efree(*body);
183 *body = NULL;
184 }
185 }
186 /* }}} */
187
188 #endif /* HTTP_HAVE_CURL */
189
190 /*
191 * Local variables:
192 * tab-width: 4
193 * c-basic-offset: 4
194 * End:
195 * vim600: noet sw=4 ts=4 fdm=marker
196 * vim<600: noet sw=4 ts=4
197 */