2 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
19 #define HTTP_WANT_CURL
24 #include "php_http_api.h"
25 #include "php_http_url_api.h"
26 #include "php_http_request_body_api.h"
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
)
32 body
= emalloc_rel(sizeof(http_request_body
));
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
)
47 if (files
&& (zend_hash_num_elements(files
) > 0)) {
52 struct curl_httppost
*http_post_data
[2] = {NULL
, NULL
};
55 FOREACH_HASH_KEYVAL(pos
, fields
, key
, idx
, data
) {
60 convert_to_string_ex(data
);
61 err
= curl_formadd(&http_post_data
[0], &http_post_data
[1],
62 CURLFORM_COPYNAME
, key
,
63 CURLFORM_COPYCONTENTS
, Z_STRVAL_PP(data
),
64 CURLFORM_CONTENTSLENGTH
, (long) Z_STRLEN_PP(data
),
72 if (CURLE_OK
!= err
) {
73 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Could not encode post fields: %s", curl_easy_strerror(err
));
74 curl_formfree(http_post_data
[0]);
84 FOREACH_HASH_VAL(pos
, files
, data
) {
85 zval
**file
, **type
, **name
;
87 if (Z_TYPE_PP(data
) != IS_ARRAY
) {
88 http_error(HE_NOTICE
, HTTP_E_INVALID_PARAM
, "Unrecognized type of post file array entry");
89 } else if ( SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(data
), "name", sizeof("name"), (void **) &name
) ||
90 SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(data
), "type", sizeof("type"), (void **) &type
) ||
91 SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(data
), "file", sizeof("file"), (void **) &file
)) {
92 http_error(HE_NOTICE
, HTTP_E_INVALID_PARAM
, "Post file array entry misses either 'name', 'type' or 'file' entry");
96 zval
*ofile
= *file
, *otype
= *type
, *oname
= *name
;
98 convert_to_string_ex(file
);
99 convert_to_string_ex(type
);
100 convert_to_string_ex(name
);
102 HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_PP(file
), curl_formfree(http_post_data
[0]); return NULL
);
104 /* this is blatant but should be sufficient for most cases */
105 if (strncasecmp(Z_STRVAL_PP(file
), "file://", lenof("file://"))) {
106 path
= Z_STRVAL_PP(file
);
108 path
= Z_STRVAL_PP(file
) + lenof("file://");
111 err
= curl_formadd(&http_post_data
[0], &http_post_data
[1],
112 CURLFORM_COPYNAME
, Z_STRVAL_PP(name
),
114 CURLFORM_CONTENTTYPE
, Z_STRVAL_PP(type
),
118 if (ofile
!= *file
) zval_ptr_dtor(file
);
119 if (otype
!= *type
) zval_ptr_dtor(type
);
120 if (oname
!= *name
) zval_ptr_dtor(name
);
122 if (CURLE_OK
!= err
) {
123 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Could not encode post files: %s", curl_easy_strerror(err
));
124 curl_formfree(http_post_data
[0]);
130 return http_request_body_init_rel(body
, HTTP_REQUEST_BODY_CURLPOST
, http_post_data
[0], 0, 1);
136 if (SUCCESS
!= http_urlencode_hash_ex(fields
, 1, NULL
, 0, &encoded
, &encoded_len
)) {
137 http_error(HE_WARNING
, HTTP_E_ENCODING
, "Could not encode post data");
141 return http_request_body_init_rel(body
, HTTP_REQUEST_BODY_CSTRING
, encoded
, encoded_len
, 1);
146 /* {{{ void http_request_body_dtor(http_request_body *) */
147 PHP_HTTP_API
void _http_request_body_dtor(http_request_body
*body TSRMLS_DC
)
153 case HTTP_REQUEST_BODY_CSTRING
:
159 case HTTP_REQUEST_BODY_CURLPOST
:
160 curl_formfree(body
->data
);
163 case HTTP_REQUEST_BODY_UPLOADFILE
:
164 php_stream_close(body
->data
);
168 memset(body
, 0, sizeof(http_request_body
));
173 /* {{{ void http_request_body_free(http_request_body *) */
174 PHP_HTTP_API
void _http_request_body_free(http_request_body
**body TSRMLS_DC
)
177 http_request_body_dtor(*body
);
184 #endif /* HTTP_HAVE_CURL */
191 * vim600: noet sw=4 ts=4 fdm=marker
192 * vim<600: noet sw=4 ts=4