X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=http_request_object.c;h=1ed0e2c154d71d12c14dc69bd47a825f01238927;hp=f2e6c463af8ef5bdd07e362cf5dded308550e95a;hb=d83edb65a0097de2d5baa1a9fd9dbcebed34cc79;hpb=0d925a2820b9d75b8e32c451fd400863e51e22fe diff --git a/http_request_object.c b/http_request_object.c index f2e6c46..1ed0e2c 100644 --- a/http_request_object.c +++ b/http_request_object.c @@ -276,7 +276,7 @@ zend_function_entry http_request_object_fe[] = { HTTP_REQUEST_ALIAS(methodName, http_request_method_name) HTTP_REQUEST_ALIAS(methodExists, http_request_method_exists) - {NULL, NULL, NULL} + EMPTY_FUNCTION_ENTRY }; static zend_object_handlers http_request_object_handlers; @@ -394,9 +394,9 @@ void _http_request_object_free(zend_object *object TSRMLS_DC) STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_ptr, http_request_body *body TSRMLS_DC) { - zval *meth, *URL, *qdata, *opts; + zval *meth, *URL; char *request_uri; - STATUS status; + STATUS status = SUCCESS; if (!body) { return FAILURE; @@ -406,46 +406,30 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_ return FAILURE; } - meth = GET_PROP(obj, method); - URL = GET_PROP(obj, url); - qdata = GET_PROP(obj, queryData); - opts = GET_PROP(obj, options); - + URL = GET_PROP(obj, url); // HTTP_URI_MAXLEN+1 long char * if (!(request_uri = http_absolute_uri_ex(Z_STRVAL_P(URL), Z_STRLEN_P(URL), NULL, 0, NULL, 0, 0))) { return FAILURE; } - - if (Z_STRLEN_P(qdata) && (strlen(request_uri) < HTTP_URI_MAXLEN)) { - if (!strchr(request_uri, '?')) { - strcat(request_uri, "?"); - } else { - strcat(request_uri, "&"); - } - strncat(request_uri, Z_STRVAL_P(qdata), HTTP_URI_MAXLEN - strlen(request_uri)); - } - + + meth = GET_PROP(obj, method); switch (Z_LVAL_P(meth)) { case HTTP_GET: case HTTP_HEAD: body->type = -1; - status = http_request_init(obj->ch, Z_LVAL_P(meth), request_uri, NULL, Z_ARRVAL_P(opts)); + body = NULL; break; case HTTP_PUT: { - php_stream *stream; php_stream_statbuf ssb; - zval *file = GET_PROP(obj, putFile); - - if ( (stream = php_stream_open_wrapper(Z_STRVAL_P(file), "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL)) && - !php_stream_stat(stream, &ssb)) { + php_stream *stream = php_stream_open_wrapper(Z_STRVAL_P(GET_PROP(obj, putFile)), "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL); + + if (stream && !php_stream_stat(stream, &ssb)) { body->type = HTTP_REQUEST_BODY_UPLOADFILE; body->data = stream; body->size = ssb.sb.st_size; - - status = http_request_init(obj->ch, HTTP_PUT, request_uri, body, Z_ARRVAL_P(opts)); } else { status = FAILURE; } @@ -453,34 +437,32 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_ break; case HTTP_POST: - { - zval *fields = GET_PROP(obj, postFields), *files = GET_PROP(obj, postFiles); - - if (SUCCESS == (status = http_request_body_fill(body, Z_ARRVAL_P(fields), Z_ARRVAL_P(files)))) { - status = http_request_init(obj->ch, HTTP_POST, request_uri, body, Z_ARRVAL_P(opts)); - } - } - break; - default: - { - zval *post = GET_PROP(obj, postData); - - body->type = HTTP_REQUEST_BODY_CSTRING; - body->data = Z_STRVAL_P(post); - body->size = Z_STRLEN_P(post); - - status = http_request_init(obj->ch, Z_LVAL_P(meth), request_uri, body, Z_ARRVAL_P(opts)); - } + status = http_request_body_fill(body, Z_ARRVAL_P(GET_PROP(obj, postFields)), Z_ARRVAL_P(GET_PROP(obj, postFiles))); break; } + + if (status == SUCCESS) { + zval *qdata = GET_PROP(obj, queryData); + + if (Z_STRLEN_P(qdata) && (strlen(request_uri) < HTTP_URI_MAXLEN)) { + if (!strchr(request_uri, '?')) { + strcat(request_uri, "?"); + } else { + strcat(request_uri, "&"); + } + strncat(request_uri, Z_STRVAL_P(qdata), HTTP_URI_MAXLEN - strlen(request_uri)); + } + + status = http_request_init(obj->ch, Z_LVAL_P(meth), request_uri, body, Z_ARRVAL_P(GET_PROP(obj, options))); + } + efree(request_uri); /* clean previous response */ phpstr_dtor(&obj->response); /* clean previous request */ phpstr_dtor(&obj->request); - efree(request_uri); return status; } @@ -490,8 +472,12 @@ STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this phpstr_fix(&obj->request); phpstr_fix(&obj->response); - - if (msg = http_message_parse(PHPSTR_VAL(&obj->response), PHPSTR_LEN(&obj->response))) { + + msg = http_message_parse(PHPSTR_VAL(&obj->response), PHPSTR_LEN(&obj->response)); + + if (!msg) { + return FAILURE; + } else { char *body; size_t body_len; zval *headers, *message, @@ -501,7 +487,7 @@ STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this /* should we record history? */ if (Z_TYPE_P(hist) != IS_BOOL) { - convert_to_boolean_ex(&hist); + convert_to_boolean(hist); } if (Z_LVAL_P(hist)) { /* we need to act like a zipper, as we'll receive @@ -541,9 +527,7 @@ STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this add_assoc_stringl(resp, "body", body, body_len, 0); MAKE_STD_ZVAL(message); - message->type = IS_OBJECT; - message->is_ref = 1; - message->value.obj = http_message_object_from_msg(msg); + ZVAL_OBJVAL(message, http_message_object_from_msg(msg)); SET_PROP(obj, responseMessage, message); zval_ptr_dtor(&message); @@ -552,7 +536,6 @@ STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this return SUCCESS; } - return FAILURE; } #define http_request_object_set_options_subr(key, ow) \ @@ -1425,7 +1408,7 @@ PHP_METHOD(HttpRequest, getResponseInfo) if (info_len && info_name) { if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), pretty_key(info_name, info_len, 0, 0), info_len + 1, (void **) &infop)) { - RETURN_ZVAL(*infop, 1, ZVAL_PTR_DTOR); + RETURN_ZVAL(*infop, 1, 0); } else { http_error_ex(HE_NOTICE, HTTP_E_INVALID_PARAM, "Could not find response info named %s", info_name); RETURN_FALSE; @@ -1537,7 +1520,6 @@ PHP_METHOD(HttpRequest, getHistory) */ PHP_METHOD(HttpRequest, send) { - STATUS status = FAILURE; http_request_body body = {0, NULL, 0}; getObject(http_request_object, obj); @@ -1551,17 +1533,15 @@ PHP_METHOD(HttpRequest, send) RETURN_FALSE; } - if (SUCCESS == (status = http_request_object_requesthandler(obj, getThis(), &body))) { - status = http_request_exec(obj->ch, NULL, &obj->response, &obj->request); + RETVAL_NULL(); + + if ( (SUCCESS == http_request_object_requesthandler(obj, getThis(), &body)) && + (SUCCESS == http_request_exec(obj->ch, NULL, &obj->response, &obj->request)) && + (SUCCESS == http_request_object_responsehandler(obj, getThis()))) { + RETVAL_OBJECT(GET_PROP(obj, responseMessage)); } http_request_body_dtor(&body); - /* final data handling */ - if (SUCCESS == status) { - status = http_request_object_responsehandler(obj, getThis()); - } - - RETVAL_OBJECT(GET_PROP(obj, responseMessage)); SET_EH_NORMAL(); } /* }}} */