113498266Sopenharmony_ci--- 213498266Sopenharmony_cic: Copyright (C) Daniel Stenberg, <daniel.se>, et al. 313498266Sopenharmony_ciSPDX-License-Identifier: curl 413498266Sopenharmony_ciTitle: curl_easy_recv 513498266Sopenharmony_ciSection: 3 613498266Sopenharmony_ciSource: libcurl 713498266Sopenharmony_ciSee-also: 813498266Sopenharmony_ci - curl_easy_getinfo (3) 913498266Sopenharmony_ci - curl_easy_perform (3) 1013498266Sopenharmony_ci - curl_easy_send (3) 1113498266Sopenharmony_ci - curl_easy_setopt (3) 1213498266Sopenharmony_ci--- 1313498266Sopenharmony_ci 1413498266Sopenharmony_ci# NAME 1513498266Sopenharmony_ci 1613498266Sopenharmony_cicurl_easy_recv - receives raw data on an "easy" connection 1713498266Sopenharmony_ci 1813498266Sopenharmony_ci# SYNOPSIS 1913498266Sopenharmony_ci 2013498266Sopenharmony_ci~~~c 2113498266Sopenharmony_ci#include <curl/curl.h> 2213498266Sopenharmony_ci 2313498266Sopenharmony_ciCURLcode curl_easy_recv(CURL *curl, void *buffer, size_t buflen, size_t *n); 2413498266Sopenharmony_ci~~~ 2513498266Sopenharmony_ci 2613498266Sopenharmony_ci# DESCRIPTION 2713498266Sopenharmony_ci 2813498266Sopenharmony_ciThis function receives raw data from the established connection. You may use 2913498266Sopenharmony_ciit together with curl_easy_send(3) to implement custom protocols using 3013498266Sopenharmony_cilibcurl. This functionality can be particularly useful if you use proxies 3113498266Sopenharmony_ciand/or SSL encryption: libcurl takes care of proxy negotiation and connection 3213498266Sopenharmony_cisetup. 3313498266Sopenharmony_ci 3413498266Sopenharmony_ci**buffer** is a pointer to your buffer memory that gets populated by the 3513498266Sopenharmony_cireceived data. **buflen** is the maximum amount of data you can get in that 3613498266Sopenharmony_cibuffer. The variable **n** points to receives the number of received bytes. 3713498266Sopenharmony_ci 3813498266Sopenharmony_ciTo establish the connection, set CURLOPT_CONNECT_ONLY(3) option before 3913498266Sopenharmony_cicalling curl_easy_perform(3) or curl_multi_perform(3). Note that 4013498266Sopenharmony_cicurl_easy_recv(3) does not work on connections that were created without 4113498266Sopenharmony_cithis option. 4213498266Sopenharmony_ci 4313498266Sopenharmony_ciThe call returns **CURLE_AGAIN** if there is no data to read - the socket is 4413498266Sopenharmony_ciused in non-blocking mode internally. When **CURLE_AGAIN** is returned, use 4513498266Sopenharmony_ciyour operating system facilities like *select(2)* to wait for data. The 4613498266Sopenharmony_cisocket may be obtained using curl_easy_getinfo(3) with 4713498266Sopenharmony_ciCURLINFO_ACTIVESOCKET(3). 4813498266Sopenharmony_ci 4913498266Sopenharmony_ciWait on the socket only if curl_easy_recv(3) returns **CURLE_AGAIN**. 5013498266Sopenharmony_ciThe reason for this is libcurl or the SSL library may internally cache some 5113498266Sopenharmony_cidata, therefore you should call curl_easy_recv(3) until all data is 5213498266Sopenharmony_ciread which would include any cached data. 5313498266Sopenharmony_ci 5413498266Sopenharmony_ciFurthermore if you wait on the socket and it tells you there is data to read, 5513498266Sopenharmony_cicurl_easy_recv(3) may return **CURLE_AGAIN** if the only data that was 5613498266Sopenharmony_ciread was for internal SSL processing, and no other data is available. 5713498266Sopenharmony_ci 5813498266Sopenharmony_ci# EXAMPLE 5913498266Sopenharmony_ci 6013498266Sopenharmony_ci~~~c 6113498266Sopenharmony_ciint main(void) 6213498266Sopenharmony_ci{ 6313498266Sopenharmony_ci CURL *curl = curl_easy_init(); 6413498266Sopenharmony_ci if(curl) { 6513498266Sopenharmony_ci CURLcode res; 6613498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_URL, "https://example.com"); 6713498266Sopenharmony_ci /* Do not do the transfer - only connect to host */ 6813498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L); 6913498266Sopenharmony_ci res = curl_easy_perform(curl); 7013498266Sopenharmony_ci 7113498266Sopenharmony_ci if(res == CURLE_OK) { 7213498266Sopenharmony_ci char buf[256]; 7313498266Sopenharmony_ci size_t nread; 7413498266Sopenharmony_ci long sockfd; 7513498266Sopenharmony_ci 7613498266Sopenharmony_ci /* Extract the socket from the curl handle - we need it for waiting. */ 7713498266Sopenharmony_ci res = curl_easy_getinfo(curl, CURLINFO_ACTIVESOCKET, &sockfd); 7813498266Sopenharmony_ci 7913498266Sopenharmony_ci /* read data */ 8013498266Sopenharmony_ci res = curl_easy_recv(curl, buf, sizeof(buf), &nread); 8113498266Sopenharmony_ci } 8213498266Sopenharmony_ci } 8313498266Sopenharmony_ci} 8413498266Sopenharmony_ci~~~ 8513498266Sopenharmony_ci 8613498266Sopenharmony_ci# AVAILABILITY 8713498266Sopenharmony_ci 8813498266Sopenharmony_ciAdded in 7.18.2. 8913498266Sopenharmony_ci 9013498266Sopenharmony_ci# RETURN VALUE 9113498266Sopenharmony_ci 9213498266Sopenharmony_ciOn success, returns **CURLE_OK**, stores the received data into 9313498266Sopenharmony_ci**buffer**, and the number of bytes it actually read into ***n**. 9413498266Sopenharmony_ci 9513498266Sopenharmony_ciOn failure, returns the appropriate error code. 9613498266Sopenharmony_ci 9713498266Sopenharmony_ciThe function may return **CURLE_AGAIN**. In this case, use your operating 9813498266Sopenharmony_cisystem facilities to wait until data can be read, and retry. 9913498266Sopenharmony_ci 10013498266Sopenharmony_ciReading exactly 0 bytes indicates a closed connection. 10113498266Sopenharmony_ci 10213498266Sopenharmony_ciIf there is no socket available to use from the previous transfer, this function 10313498266Sopenharmony_cireturns **CURLE_UNSUPPORTED_PROTOCOL**. 104