Merge branch 'v2.6.x' of ../ext-http
authorMichael Wallner <mike@php.net>
Tue, 4 Oct 2016 14:05:57 +0000 (16:05 +0200)
committerMichael Wallner <mike@php.net>
Tue, 4 Oct 2016 14:05:57 +0000 (16:05 +0200)
1  2 
src/php_http_client_curl_event.c

index 38e1a56b3d6d62a5146512a22bc80a7cb9470d1b,21f5c9cfe20596b30325cf6254a8c1ac871210ce..b8db1b2d5e15c33429b1c6f6cdc7c06da70ff201
  */
  
  #include "php_http_api.h"
 -#include "php_http_client.h"
 -#include "php_http_client_curl.h"
 -
 -#if PHP_HTTP_HAVE_CURL
 -#if PHP_HTTP_HAVE_EVENT
 -#     if !PHP_HTTP_HAVE_EVENT2 && /* just be really sure */ !(LIBEVENT_VERSION_NUMBER >= 0x02000000)
 -#             include <event.h>
 -#             define event_base_new event_init
 -#             define event_assign(e, b, s, a, cb, d) do {\
 -                      event_set(e, s, a, cb, d); \
 -                      event_base_set(b, e); \
 -              } while(0)
 -#     else
 -#             if PHP_HTTP_HAVE_EVENT2
 -#                     include <event2/event.h>
 -#                     include <event2/event_struct.h>
 -#             else
 -#                     error "libevent presence is unknown"
 -#             endif
 -#     endif
 -#     ifndef DBG_EVENTS
 -#             define DBG_EVENTS 0
 -#     endif
 +
 +#if PHP_HTTP_HAVE_LIBCURL
 +#if PHP_HTTP_HAVE_LIBEVENT
 +
 +#include <event.h>
 +#ifndef DBG_EVENTS
 +#     define DBG_EVENTS 0
 +#endif
  
  typedef struct php_http_client_curl_event_context {
        php_http_client_t *client;
@@@ -52,6 -67,7 +52,6 @@@ static void php_http_client_curl_event_
        CURLMcode rc;
        php_http_client_curl_event_context_t *ctx = context;
        php_http_client_curl_t *curl = ctx->client->ctx;
 -      TSRMLS_FETCH_FROM_CTX(ctx->client->ts);
  
  #if DBG_EVENTS
        fprintf(stderr, "H");
@@@ -62,7 -78,7 +62,7 @@@
        } while (CURLM_CALL_MULTI_PERFORM == rc);
  
        if (CURLM_OK != rc) {
 -              php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s",  curl_multi_strerror(rc));
 +              php_error_docref(NULL, E_WARNING, "%s",  curl_multi_strerror(rc));
        }
  
        php_http_client_curl_responsehandler(ctx->client);
@@@ -84,16 -100,22 +84,22 @@@ static void php_http_client_curl_event_
  static void php_http_client_curl_event_timer(CURLM *multi, long timeout_ms, void *timer_data)
  {
        php_http_client_curl_event_context_t *context = timer_data;
+       struct timeval timeout;
  
  #if DBG_EVENTS
-       fprintf(stderr, "\ntimer <- timeout_ms: %ld\n", timeout_ms);
+       fprintf(stderr, "(%ld)", timeout_ms);
  #endif
  
-       if (timeout_ms < 0) {
+       switch (timeout_ms) {
+       case -1:
+               if (event_initialized(context->timeout) && event_pending(context->timeout, EV_TIMEOUT, NULL)) {
+                       event_del(context->timeout);
+               }
+               break;
+       case 0:
                php_http_client_curl_event_handler(context, CURL_SOCKET_TIMEOUT, 0);
-       } else if (timeout_ms > 0 || !event_initialized(context->timeout) || !event_pending(context->timeout, EV_TIMEOUT, NULL)) {
-               struct timeval timeout;
+               break;
+       default:
                if (!event_initialized(context->timeout)) {
                        event_assign(context->timeout, context->evbase, CURL_SOCKET_TIMEOUT, 0, php_http_client_curl_event_timeout_callback, context);
                }
                timeout.tv_sec = timeout_ms / 1000;
                timeout.tv_usec = (timeout_ms % 1000) * 1000;
  
-               event_add(context->timeout, &timeout);
+               if (!event_pending(context->timeout, EV_TIMEOUT, &timeout)) {
+                       event_add(context->timeout, &timeout);
+               }
+               break;
        }
  }
  
@@@ -128,6 -153,7 +137,6 @@@ static int php_http_client_curl_event_s
        php_http_client_curl_t *curl = ctx->client->ctx;
        int events = EV_PERSIST;
        php_http_client_curl_event_ev_t *ev = assign_data;
 -      TSRMLS_FETCH_FROM_CTX(ctx->client->ts);
  
  #if DBG_EVENTS
        fprintf(stderr, "S");
                        return 0;
  
                default:
 -                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown socket action %d", action);
 +                      php_error_docref(NULL, E_WARNING, "Unknown socket action %d", action);
                        return -1;
        }
  
@@@ -218,6 -244,7 +227,6 @@@ static ZEND_RESULT_CODE php_http_client
  {
        php_http_client_curl_event_context_t *ctx = context;
        php_http_client_curl_t *curl = ctx->client->ctx;
 -      TSRMLS_FETCH_FROM_CTX(ctx->client->ts);
  
  #if DBG_EVENTS
        fprintf(stderr, "E");
@@@ -301,8 -328,8 +310,8 @@@ php_http_client_curl_ops_t *php_http_cl
        return &php_http_client_curl_event_ops;
  }
  
 -#endif /* PHP_HTTP_HAVE_EVENT */
 -#endif /* PHP_HTTP_HAVE_CURL */
 +#endif /* PHP_HTTP_HAVE_LIBEVENT */
 +#endif /* PHP_HTTP_HAVE_LIBCURL */
  
  /*
   * Local variables: