Repository navigation
Description
I did this
I'm trying to receive Server-Sent Events using libcurl. I've found CURLOPT_CONNECT_ONLY option which
can be set to '2' and if HTTP or WebSocket are used, libcurl performs the request and reads all response headers before handing over control to the application
but curl_easy_perform() call blocks while connection alive.
For demonstration, I have attached a simple server and client applications.
demo.zip
The server (Kotlin) sends 5 messages every 3 seconds and terminates the connection.
sse {
repeat(5) {
delay(3.seconds)
send(LocalTime.now().toString())
}
}
The client (C) connects to the server, exchange HTTP headers, report the execution time and should exit without waiting events.
CURL *curl = curl_easy_init();
if (!curl)
goto cleanup;
if (curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8080"))
goto cleanup;
if (curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 2L))
goto cleanup;
time_t start_time = time(NULL);
if (curl_easy_perform(curl))
goto cleanup;
time_t end_time = time(NULL);
double duration = difftime(end_time, start_time);
printf("perform takes %.2f seconds\n", duration);
but outputs
perform takes 15.00 seconds
How-to build & run server:
cd server
gradle build
cd build/distributions
tar -xf server.tar
./server/bin/server
curl -v http://localhost:8080
* Host localhost:8080 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
* Trying [::1]:8080...
* Connected to localhost (::1) port 8080
* using HTTP/1.x
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/8.12.1
> Accept: */*
>
* Request completely sent off
< HTTP/1.1 200 OK
< Content-Type: text/event-stream
< Cache-Control: no-store
< Connection: keep-alive
< X-Accel-Buffering: no
< Transfer-Encoding: chunked
<
data: 22:22:18.489580323
data: 22:22:21.490710433
data: 22:22:24.491684059
data: 22:22:27.492930039
data: 22:22:30.494157034
* Connection #0 to host localhost left intact
I expected the following
No response
curl/libcurl version
curl 8.12.1 (x86_64-pc-linux-gnu) libcurl/8.12.1 OpenSSL/3.4.1 zlib/1.3.1 brotli/1.1.0 zstd/1.5.6 libidn2/2.3.8 libpsl/0.21.2 libssh2/1.11.1 nghttp2/1.64.0 librtmp/2.3 OpenLDAP/2.6.9
Release-Date: 2025-02-13, security patched: 8.12.1-3ubuntu1
Protocols: dict file ftp ftps gopher gophers http https imap imaps ipfs ipns ldap ldaps mqtt pop3 pop3s rtmp rtsp scp sftp smb smbs smtp smtps telnet tftp ws wss
Features: alt-svc AsynchDNS brotli GSS-API HSTS HTTP2 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM PSL SPNEGO SSL threadsafe TLS-SRP UnixSockets zstd
operating system
Linux lpc 6.14.0-15-generic #15-Ubuntu SMP PREEMPT_DYNAMIC Sun Apr 6 15:05:05 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
Activity
dfandrich commentedon Jun 14, 2025
KirillObukhov commentedon Jun 14, 2025
When the option parameter is 1, then curl performs connection setup and returns control to the application.
When the option parameter is 2, then curl performs connection setup, makes the request, reads all response headers and returns control to the application.
In my case the option parameter is 2, but curl continues reading the response after the headers have been received.
dfandrich commentedon Jun 14, 2025
Does the URL support WebSockets? Does the example program docs/examples/websocket.c work against that site? Does it work with curl 8.14.1? What do the logs show in the WebSockets case? Are you using a wss: scheme or http: scheme (it should be the former)?
KirillObukhov commentedon Jun 15, 2025
It's Server-Sent Events, not WebSockets. It is unidirectional messages stream over HTTP connection.
CURL woks nice with it in blocking mode:
outputs:
But I need the
curl_easy_perform()call to return control to the application after receiving HTTP headers.dfandrich commentedon Jun 15, 2025
For some reason I thought you were trying to do WebSockets. Can you show the trace output for the connection?
KirillObukhov commentedon Jun 15, 2025
curl_global_trace("all")+curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L)bagder commentedon Jun 15, 2025
Setting it to 2 was only ever thought of and designed for WebSocket. As long as you speak HTTP, we don't recommend it. If you don't want blocking, I propose you use the multi interface instead.
KirillObukhov commentedon Jun 15, 2025
So, confusion is in the documentation?
It seems that in my case it is easier to use the value 1 and write custom HTTP headers processing.
Thanks for the help!
docs: fix documentation of connect_only 2
icing commentedon Jun 16, 2025
Propose to update the documentation in #17635
docs: fix documentation of connect_only 2
docs: fix documentation of connect_only 2