- switch (message->type) {
- case PHP_HTTP_RESPONSE:
- {
- php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
- zval **val;
- HashPosition pos;
-
- FOREACH_HASH_KEYVAL(pos, &message->hdrs, key, val) {
- if (key.type == HASH_KEY_IS_STRING) {
- http_send_header_zval_ex(key.str, key.len-1, val, 1);
- }
- }
- rs = SUCCESS == http_send_status(message->http.info.response.code) &&
- SUCCESS == http_send_data(PHP_HTTP_BUFFER_VAL(message), PHP_HTTP_BUFFER_LEN(message)) ?
- SUCCESS : FAILURE;
- break;
- }
-
- case PHP_HTTP_REQUEST:
- {
-#ifdef PHP_HTTP_HAVE_CURL
- char *uri = NULL;
- http_request request;
- zval **zhost, *options, *headers;
-
- MAKE_STD_ZVAL(options);
- MAKE_STD_ZVAL(headers);
- array_init(options);
- array_init(headers);
- zend_hash_copy(Z_ARRVAL_P(headers), &message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
- add_assoc_zval(options, "headers", headers);
-
- if (SUCCESS == zend_hash_find(&message->hdrs, "Host", sizeof("Host"), (void *) &zhost) && Z_TYPE_PP(zhost) == IS_STRING) {
- char *colon = NULL;
- php_url parts, *url = php_url_parse(message->http.info.request.url);
-
- memset(&parts, 0, sizeof(php_url));
-
- if ((colon = strchr(Z_STRVAL_PP(zhost), ':'))) {
- parts.port = atoi(colon + 1);
- parts.host = estrndup(Z_STRVAL_PP(zhost), (Z_STRVAL_PP(zhost) - colon - 1));
- } else {
- parts.host = estrndup(Z_STRVAL_PP(zhost), Z_STRLEN_PP(zhost));
- }
-
- http_build_url(PHP_HTTP_URL_REPLACE, url, &parts, NULL, &uri, NULL);
- php_url_free(url);
- efree(parts.host);
- } else {
- uri = http_absolute_url(message->http.info.request.url);
- }
-
- if ((request.meth = http_request_method_exists(1, 0, message->http.info.request.method))) {
- http_request_body body;
-
- http_request_init_ex(&request, NULL, request.meth, uri);
- request.body = http_request_body_init_ex(&body, PHP_HTTP_REQUEST_BODY_CSTRING, PHP_HTTP_BUFFER_VAL(message), PHP_HTTP_BUFFER_LEN(message), 0);
- if (SUCCESS == (rs = http_request_prepare(&request, Z_ARRVAL_P(options)))) {
- http_request_exec(&request);
- }
- http_request_dtor(&request);
- } else {
- http_error_ex(HE_WARNING, PHP_HTTP_E_REQUEST_METHOD,
- "Cannot send HttpMessage. Request method %s not supported",
- message->http.info.request.method);
- }
- efree(uri);
- zval_ptr_dtor(&options);
-#else
- http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "HTTP requests not supported - ext/http was not linked against libcurl.");
-#endif
- break;
- }
-
- case PHP_HTTP_NONE:
- default:
- php_http_error(HE_WARNING, PHP_HTTP_E_MESSAGE_TYPE, "HttpMessage is neither of type PHP_HTTP_REQUEST nor PHP_HTTP_RESPONSE");
- break;
- }
-
- return rs;