php / php/php-src

Infinite waiting for CURLOPT_WRITEFUNCTION in case of no response

Abierto
#13,711 7 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Bug Extension: curl Status: Needs Triage
Lenguaje dominante
C
Estrellas
40.4k
Forks
8.1k
Merge medio
2 d 13 h
PR fusionados (30 d)
96

Descripción

Description

To reach an address that responds every 5 seconds, I use CURLOPT_WRITEFUNCTION. Since I want to maintain an infinite connection, I have CURLOPT_TIMEOUT => 0.

function writer($ch, $data) {
  handler($data);
  return strlen($data);
}

$ch = curl_init("$urlcam/cgi-bin/eventManager.cgi?action=attach&codes=[NewFile]&heartbeat=5");
curl_setopt_array($ch, [
        CURLOPT_CONNECTTIMEOUT => 20,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_WRITEFUNCTION => 'writer'
    ]);

$result = curl_exec($ch);
curl_close($ch);

The response looks like this:

--myboundary
Content-Type: text/plain
Content-Length: 9
Heartbeat
--myboundary
Content-Type: text/plain
Content-Length: 9
Heartbeat
...

Everything works as long as there is a response from the url. However, if the url stops responding, nothing happens. No error or warning. The PHP script will stay inside the "writer" function and never leave it.

For example, if you implement this functionality using fopen:

$resource = fopen("http://".$host.$uri, 'r', false, $context);
while (!feof($resource)) {
    print_r(date("d-m-Y H:i:s"));
    $line = fgets($resource);
    if (str_contains($line,"--myboundary")) {
        handler($response);
        $response = '';
    } else {
        $response.=$line;
    }
}

Then the print_r function will be called every minute. Yes, if the url is restored, the fopen connection will not be restored, but at least we can handle the error/state to restart the script. In the case of CURL, this cannot be done as nothing happens.

We need to add functionality to leave the function specified in CURLOPT_WRITEFUNCTION. I suppose you could take CURLOPT_CONNECTTIMEOUT and display an error if the WRITEFUNCTION has not received a response within that time.

PHP Version

8.1.23

Operating System

Ubuntu 20.04

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

El issue proporciona una reproducción en PHP con curl_exec que usa CURLOPT_WRITEFUNCTION y una URL con streaming indefinido; empieza reproduciendo el caso de respuesta bloqueada y siguiendo el manejo de la opción en php-src. Compara el comportamiento observado con la semántica solicitada de timeout y errores, y considera terminado el trabajo cuando se pueda detectar y gestionar un periodo sin respuesta como se describe.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
php
Área
networking
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.