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-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
13 #include "php_http_api.h"
15 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpClientRequest, method, 0, req_args)
16 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpClientRequest, method, 0)
17 #define PHP_HTTP_CLIENT_REQUEST_ME(method, visibility) PHP_ME(HttpClientRequest, method, PHP_HTTP_ARGS(HttpClientRequest, method), visibility)
18 #define PHP_HTTP_CLIENT_REQUEST_ALIAS(method, func) PHP_HTTP_STATIC_ME_ALIAS(method, func, PHP_HTTP_ARGS(HttpClientRequest, method))
19 #define PHP_HTTP_CLIENT_REQUEST_MALIAS(me, al, vis) ZEND_FENTRY(me, ZEND_MN(HttpClientRequest_##al), PHP_HTTP_ARGS(HttpClientRequest, al), vis)
21 PHP_HTTP_BEGIN_ARGS(__construct
, 0)
22 PHP_HTTP_ARG_VAL(method
, 0)
23 PHP_HTTP_ARG_VAL(url
, 0)
24 PHP_HTTP_ARG_ARR(headers
, 1, 0)
25 PHP_HTTP_ARG_OBJ(http
\\Message
\\Body
, body
, 1)
28 PHP_HTTP_EMPTY_ARGS(getContentType
);
29 PHP_HTTP_BEGIN_ARGS(setContentType
, 1)
30 PHP_HTTP_ARG_VAL(content_type
, 0)
33 PHP_HTTP_EMPTY_ARGS(getQuery
);
34 PHP_HTTP_BEGIN_ARGS(setQuery
, 0)
35 PHP_HTTP_ARG_VAL(query_data
, 0)
38 PHP_HTTP_BEGIN_ARGS(addQuery
, 1)
39 PHP_HTTP_ARG_VAL(query_data
, 0)
42 PHP_HTTP_EMPTY_ARGS(getOptions
);
43 PHP_HTTP_BEGIN_ARGS(setOptions
, 0)
44 PHP_HTTP_ARG_ARR(options
, 1, 0)
47 PHP_HTTP_EMPTY_ARGS(getSslOptions
);
48 PHP_HTTP_BEGIN_ARGS(setSslOptions
, 0)
49 PHP_HTTP_ARG_ARR(ssl_options
, 1, 0)
52 PHP_HTTP_BEGIN_ARGS(addSslOptions
, 0)
53 PHP_HTTP_ARG_ARR(ssl_options
, 1, 0)
57 static zend_class_entry
*php_http_client_request_class_entry
;
59 zend_class_entry
* php_http_client_request_get_class_entry(void)
61 return php_http_client_request_class_entry
;
64 static zend_function_entry php_http_client_request_method_entry
[] = {
65 PHP_HTTP_CLIENT_REQUEST_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
66 PHP_HTTP_CLIENT_REQUEST_ME(getQuery
, ZEND_ACC_PUBLIC
)
67 PHP_HTTP_CLIENT_REQUEST_ME(setQuery
, ZEND_ACC_PUBLIC
)
68 PHP_HTTP_CLIENT_REQUEST_ME(addQuery
, ZEND_ACC_PUBLIC
)
69 PHP_HTTP_CLIENT_REQUEST_ME(getContentType
, ZEND_ACC_PUBLIC
)
70 PHP_HTTP_CLIENT_REQUEST_ME(setContentType
, ZEND_ACC_PUBLIC
)
71 PHP_HTTP_CLIENT_REQUEST_ME(setOptions
, ZEND_ACC_PUBLIC
)
72 PHP_HTTP_CLIENT_REQUEST_ME(getOptions
, ZEND_ACC_PUBLIC
)
73 PHP_HTTP_CLIENT_REQUEST_ME(setSslOptions
, ZEND_ACC_PUBLIC
)
74 PHP_HTTP_CLIENT_REQUEST_ME(getSslOptions
, ZEND_ACC_PUBLIC
)
75 PHP_HTTP_CLIENT_REQUEST_ME(addSslOptions
, ZEND_ACC_PUBLIC
)
80 PHP_METHOD(HttpClientRequest
, __construct
)
82 char *meth_str
= NULL
, *url_str
= NULL
;
83 int meth_len
= 0, url_len
= 0;
84 zval
*zheaders
= NULL
, *zbody
= NULL
;
86 with_error_handling(EH_THROW
, php_http_exception_get_class_entry()) {
87 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s!s!a!O!", &meth_str
, &meth_len
, &url_str
, &url_len
, &zheaders
, &zbody
, php_http_message_body_get_class_entry())) {
88 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
91 php_http_message_set_type(obj
->message
, PHP_HTTP_REQUEST
);
93 obj
->message
= php_http_message_init(NULL
, PHP_HTTP_REQUEST TSRMLS_CC
);
96 if (meth_str
&& meth_len
) {
97 PHP_HTTP_INFO(obj
->message
).request
.method
= estrndup(meth_str
, meth_len
);
99 if (url_str
&& url_len
) {
100 PHP_HTTP_INFO(obj
->message
).request
.url
= estrndup(url_str
, url_len
);
103 array_copy(Z_ARRVAL_P(zheaders
), &obj
->message
->hdrs
);
106 php_http_message_body_object_t
*body_obj
= zend_object_store_get_object(zbody TSRMLS_CC
);
108 php_http_message_body_dtor(&obj
->message
->body
);
109 php_http_message_body_copy(body_obj
->body
, &obj
->message
->body
, 0);
110 Z_OBJ_ADDREF_P(zbody
);
111 obj
->body
= Z_OBJVAL_P(zbody
);
114 } end_error_handling();
118 PHP_METHOD(HttpClientRequest
, setContentType
)
123 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ct_str
, &ct_len
)) {
127 PHP_HTTP_CHECK_CONTENT_TYPE(ct_str
, invalid
= 1);
131 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
135 ZVAL_STRINGL(zct
, ct_str
, ct_len
, 1);
136 zend_hash_update(&obj
->message
->hdrs
, "Content-Type", sizeof("Content-Type"), (void *) &zct
, sizeof(void *), NULL
);
139 RETVAL_ZVAL(getThis(), 1, 0);
142 PHP_METHOD(HttpClientRequest
, getContentType
)
144 if (SUCCESS
== zend_parse_parameters_none()) {
145 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
146 zval
*zct
= php_http_message_header(obj
->message
, ZEND_STRL("Content-Type"), 1);
148 RETURN_ZVAL(zct
, 0, 0);
153 PHP_METHOD(HttpClientRequest
, setQuery
)
157 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z!", &qdata
)) {
158 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
159 php_url
*old_url
= NULL
, new_url
= {NULL
};
170 php_http_querystring_update(&arr
, qdata
, &str TSRMLS_CC
);
171 new_url
.query
= Z_STRVAL(str
);
174 new_url
.query
= &empty
[0];
177 if (obj
->message
->http
.info
.request
.url
) {
178 old_url
= php_url_parse(obj
->message
->http
.info
.request
.url
);
179 efree(obj
->message
->http
.info
.request
.url
);
182 php_http_url(PHP_HTTP_URL_REPLACE
, old_url
, &new_url
, NULL
, &obj
->message
->http
.info
.request
.url
, NULL TSRMLS_CC
);
185 php_url_free(old_url
);
187 if (new_url
.query
!= &empty
[0]) {
188 STR_FREE(new_url
.query
);
191 RETVAL_ZVAL(getThis(), 1, 0);
194 PHP_METHOD(HttpClientRequest
, getQuery
)
196 if (SUCCESS
== zend_parse_parameters_none()) {
197 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
199 if (obj
->message
->http
.info
.request
.url
) {
200 php_url
*purl
= php_url_parse(obj
->message
->http
.info
.request
.url
);
204 RETVAL_STRING(purl
->query
, 0);
213 PHP_METHOD(HttpClientRequest
, addQuery
)
217 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &qdata
)) {
218 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
219 php_url
*old_url
= NULL
, new_url
= {NULL
};
228 php_http_querystring_update(&arr
, qdata
, &str TSRMLS_CC
);
229 new_url
.query
= Z_STRVAL(str
);
232 if (obj
->message
->http
.info
.request
.url
) {
233 old_url
= php_url_parse(obj
->message
->http
.info
.request
.url
);
234 efree(obj
->message
->http
.info
.request
.url
);
237 php_http_url(PHP_HTTP_URL_JOIN_QUERY
, old_url
, &new_url
, NULL
, &obj
->message
->http
.info
.request
.url
, NULL TSRMLS_CC
);
240 php_url_free(old_url
);
242 STR_FREE(new_url
.query
);
244 RETVAL_ZVAL(getThis(), 1, 0);
247 PHP_METHOD(HttpClientRequest
, setOptions
)
251 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!/", &opts
)) {
252 php_http_client_options_set(getThis(), opts TSRMLS_CC
);
254 RETVAL_ZVAL(getThis(), 1, 0);
258 PHP_METHOD(HttpClientRequest
, getOptions
)
260 if (SUCCESS
== zend_parse_parameters_none()) {
261 RETURN_PROP(php_http_client_request_class_entry
, "options");
266 PHP_METHOD(HttpClientRequest
, setSslOptions
)
270 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!/", &opts
)) {
271 php_http_client_options_set_subr(getThis(), ZEND_STRS("ssl"), opts
, 1 TSRMLS_CC
);
273 RETVAL_ZVAL(getThis(), 1, 0);
277 PHP_METHOD(HttpClientRequest
, addSslOptions
)
281 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!/", &opts
)) {
282 php_http_client_options_set_subr(getThis(), ZEND_STRS("ssl"), opts
, 0 TSRMLS_CC
);
284 RETVAL_ZVAL(getThis(), 1, 0);
288 PHP_METHOD(HttpClientRequest
, getSslOptions
)
290 if (SUCCESS
== zend_parse_parameters_none()) {
291 php_http_client_options_get_subr(getThis(), ZEND_STRS("ssl"), return_value TSRMLS_CC
);
295 PHP_MINIT_FUNCTION(http_client_request
)
297 PHP_HTTP_REGISTER_CLASS(http
\\Client
, Request
, http_client_request
, php_http_message_get_class_entry(), 0);
299 zend_declare_property_null(php_http_client_request_class_entry
, ZEND_STRL("options"), ZEND_ACC_PROTECTED TSRMLS_CC
);
309 * vim600: noet sw=4 ts=4 fdm=marker
310 * vim<600: noet sw=4 ts=4