From a216cc2910ae493e82a01a8f552c9586839f69fa Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Tue, 27 Feb 2018 17:32:45 +0100 Subject: [PATCH] implement #58 Client observers are now informed with an "prepare" event. Note, though, that http\Client::getProgressInfo() does not return the progress state received for this event. --- src/php_http_client.c | 15 ++++++++++++--- src/php_http_client_curl.c | 10 ++++++++++ tests/client002.phpt | 2 +- tests/client013.phpt | 3 +++ 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/php_http_client.c b/src/php_http_client.c index b7cf2b9..0749825 100644 --- a/src/php_http_client.c +++ b/src/php_http_client.c @@ -689,7 +689,7 @@ static PHP_METHOD(HttpClient, enqueue) zend_fcall_info_cache fcc = empty_fcall_info_cache; php_http_client_object_t *obj; php_http_message_object_t *msg_obj; - php_http_client_enqueue_t q; + php_http_client_enqueue_t q = {0}; php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O|f", &request, php_http_get_client_request_class_entry(), &fci, &fcc), invalid_arg, return); @@ -701,6 +701,17 @@ static PHP_METHOD(HttpClient, enqueue) return; } + /* set early for progress callback */ + q.opaque = msg_obj; + + if (obj->client->callback.progress.func) { + php_http_client_progress_state_t progress = {0}; + + progress.info = "prepare"; + obj->client->callback.progress.func(obj->client->callback.progress.arg, obj->client, &q, &progress); + } + + Z_ADDREF_P(request); q.request = msg_obj->message; q.options = combined_options(getThis(), request); q.dtor = msg_queue_dtor; @@ -715,8 +726,6 @@ static PHP_METHOD(HttpClient, enqueue) } } - Z_ADDREF_P(request); - php_http_expect(SUCCESS == php_http_client_enqueue(obj->client, &q), runtime, msg_queue_dtor(&q); return; diff --git a/src/php_http_client_curl.c b/src/php_http_client_curl.c index f07b5b8..b17d8a6 100644 --- a/src/php_http_client_curl.c +++ b/src/php_http_client_curl.c @@ -218,6 +218,7 @@ static int php_http_curle_raw_callback(CURL *ch, curl_infotype type, char *data, switch (type) { case CURLINFO_TEXT: if (data[0] == '-') { + goto text; } else if (php_memnstr(data, ZEND_STRL("Adding handle:"), data + length)) { h->progress.info = "setup"; } else if (php_memnstr(data, ZEND_STRL("addHandle"), data + length)) { @@ -234,8 +235,16 @@ static int php_http_curle_raw_callback(CURL *ch, curl_infotype type, char *data, h->progress.info = "connected"; } else if (php_memnstr(data, ZEND_STRL("blacklisted"), data + length)) { h->progress.info = "blacklist check"; + } else if (php_memnstr(data, ZEND_STRL("TLS"), data + length)) { + h->progress.info = "ssl negotiation"; } else if (php_memnstr(data, ZEND_STRL("SSL"), data + length)) { h->progress.info = "ssl negotiation"; + } else if (php_memnstr(data, ZEND_STRL("certificate"), data + length)) { + h->progress.info = "ssl negotiation"; + } else if (php_memnstr(data, ZEND_STRL("ALPN"), data + length)) { + h->progress.info = "alpn"; + } else if (php_memnstr(data, ZEND_STRL("NPN"), data + length)) { + h->progress.info = "npn"; } else if (php_memnstr(data, ZEND_STRL("upload"), data + length)) { h->progress.info = "uploaded"; } else if (php_memnstr(data, ZEND_STRL("left intact"), data + length)) { @@ -247,6 +256,7 @@ static int php_http_curle_raw_callback(CURL *ch, curl_infotype type, char *data, } else if (php_memnstr(data, ZEND_STRL("Operation timed out"), data + length)) { h->progress.info = "timeout"; } else { + text:; #if 0 h->progress.info = data; data[length - 1] = '\0'; diff --git a/tests/client002.phpt b/tests/client002.phpt index aeb2d8a..6f01a44 100644 --- a/tests/client002.phpt +++ b/tests/client002.phpt @@ -16,7 +16,7 @@ class Observer implements SplObserver { function update(SplSubject $client, http\Client\Request $request = null, StdClass $progress = null) { echo "P"; - if ($client->getProgressInfo($request) != $progress) { + if ($progress->info !== "prepare" && $client->getProgressInfo($request) != $progress) { var_dump($progress); } } diff --git a/tests/client013.phpt b/tests/client013.phpt index fdf6c96..477edf7 100644 --- a/tests/client013.phpt +++ b/tests/client013.phpt @@ -43,6 +43,9 @@ server("proxy.inc", function($port) { $o3 = new CallbackObserver( function ($c, $r) { $p = (array) $c->getProgressInfo($r); + if (!$p) { + return; + } var_dump(array_key_exists("started", $p)); var_dump(array_key_exists("finished", $p)); var_dump(array_key_exists("dlnow", $p)); -- 2.30.2