113498266Sopenharmony_ci/*************************************************************************** 213498266Sopenharmony_ci * _ _ ____ _ 313498266Sopenharmony_ci * Project ___| | | | _ \| | 413498266Sopenharmony_ci * / __| | | | |_) | | 513498266Sopenharmony_ci * | (__| |_| | _ <| |___ 613498266Sopenharmony_ci * \___|\___/|_| \_\_____| 713498266Sopenharmony_ci * 813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 913498266Sopenharmony_ci * 1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which 1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms 1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html. 1313498266Sopenharmony_ci * 1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell 1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is 1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file. 1713498266Sopenharmony_ci * 1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 1913498266Sopenharmony_ci * KIND, either express or implied. 2013498266Sopenharmony_ci * 2113498266Sopenharmony_ci * SPDX-License-Identifier: curl 2213498266Sopenharmony_ci * 2313498266Sopenharmony_ci ***************************************************************************/ 2413498266Sopenharmony_ci/* <DESC> 2513498266Sopenharmony_ci * Demonstrate curl_easy_send() and curl_easy_recv() usage. 2613498266Sopenharmony_ci * </DESC> 2713498266Sopenharmony_ci */ 2813498266Sopenharmony_ci 2913498266Sopenharmony_ci#include <stdio.h> 3013498266Sopenharmony_ci#include <string.h> 3113498266Sopenharmony_ci#include <curl/curl.h> 3213498266Sopenharmony_ci 3313498266Sopenharmony_ci/* Auxiliary function that waits on the socket. */ 3413498266Sopenharmony_cistatic int wait_on_socket(curl_socket_t sockfd, int for_recv, long timeout_ms) 3513498266Sopenharmony_ci{ 3613498266Sopenharmony_ci struct timeval tv; 3713498266Sopenharmony_ci fd_set infd, outfd, errfd; 3813498266Sopenharmony_ci int res; 3913498266Sopenharmony_ci 4013498266Sopenharmony_ci tv.tv_sec = timeout_ms / 1000; 4113498266Sopenharmony_ci tv.tv_usec = (int)(timeout_ms % 1000) * 1000; 4213498266Sopenharmony_ci 4313498266Sopenharmony_ci FD_ZERO(&infd); 4413498266Sopenharmony_ci FD_ZERO(&outfd); 4513498266Sopenharmony_ci FD_ZERO(&errfd); 4613498266Sopenharmony_ci 4713498266Sopenharmony_ci/* Avoid this warning with pre-2020 Cygwin/MSYS releases: 4813498266Sopenharmony_ci * warning: conversion to 'long unsigned int' from 'curl_socket_t' {aka 'int'} 4913498266Sopenharmony_ci * may change the sign of the result [-Wsign-conversion] 5013498266Sopenharmony_ci */ 5113498266Sopenharmony_ci#if defined(__GNUC__) && defined(__CYGWIN__) 5213498266Sopenharmony_ci#pragma GCC diagnostic push 5313498266Sopenharmony_ci#pragma GCC diagnostic ignored "-Wsign-conversion" 5413498266Sopenharmony_ci#endif 5513498266Sopenharmony_ci FD_SET(sockfd, &errfd); /* always check for error */ 5613498266Sopenharmony_ci 5713498266Sopenharmony_ci if(for_recv) { 5813498266Sopenharmony_ci FD_SET(sockfd, &infd); 5913498266Sopenharmony_ci } 6013498266Sopenharmony_ci else { 6113498266Sopenharmony_ci FD_SET(sockfd, &outfd); 6213498266Sopenharmony_ci } 6313498266Sopenharmony_ci#if defined(__GNUC__) && defined(__CYGWIN__) 6413498266Sopenharmony_ci#pragma GCC diagnostic pop 6513498266Sopenharmony_ci#endif 6613498266Sopenharmony_ci 6713498266Sopenharmony_ci /* select() returns the number of signalled sockets or -1 */ 6813498266Sopenharmony_ci res = select((int)sockfd + 1, &infd, &outfd, &errfd, &tv); 6913498266Sopenharmony_ci return res; 7013498266Sopenharmony_ci} 7113498266Sopenharmony_ci 7213498266Sopenharmony_ciint main(void) 7313498266Sopenharmony_ci{ 7413498266Sopenharmony_ci CURL *curl; 7513498266Sopenharmony_ci /* Minimalistic http request */ 7613498266Sopenharmony_ci const char *request = "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n"; 7713498266Sopenharmony_ci size_t request_len = strlen(request); 7813498266Sopenharmony_ci 7913498266Sopenharmony_ci /* A general note of caution here: if you are using curl_easy_recv() or 8013498266Sopenharmony_ci curl_easy_send() to implement HTTP or _any_ other protocol libcurl 8113498266Sopenharmony_ci supports "natively", you are doing it wrong and you should stop. 8213498266Sopenharmony_ci 8313498266Sopenharmony_ci This example uses HTTP only to show how to use this API, it does not 8413498266Sopenharmony_ci suggest that writing an application doing this is sensible. 8513498266Sopenharmony_ci */ 8613498266Sopenharmony_ci 8713498266Sopenharmony_ci curl = curl_easy_init(); 8813498266Sopenharmony_ci if(curl) { 8913498266Sopenharmony_ci CURLcode res; 9013498266Sopenharmony_ci curl_socket_t sockfd; 9113498266Sopenharmony_ci size_t nsent_total = 0; 9213498266Sopenharmony_ci 9313498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_URL, "https://example.com"); 9413498266Sopenharmony_ci /* Do not do the transfer - only connect to host */ 9513498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L); 9613498266Sopenharmony_ci res = curl_easy_perform(curl); 9713498266Sopenharmony_ci 9813498266Sopenharmony_ci if(res != CURLE_OK) { 9913498266Sopenharmony_ci printf("Error: %s\n", curl_easy_strerror(res)); 10013498266Sopenharmony_ci return 1; 10113498266Sopenharmony_ci } 10213498266Sopenharmony_ci 10313498266Sopenharmony_ci /* Extract the socket from the curl handle - we will need it for 10413498266Sopenharmony_ci waiting. */ 10513498266Sopenharmony_ci res = curl_easy_getinfo(curl, CURLINFO_ACTIVESOCKET, &sockfd); 10613498266Sopenharmony_ci 10713498266Sopenharmony_ci if(res != CURLE_OK) { 10813498266Sopenharmony_ci printf("Error: %s\n", curl_easy_strerror(res)); 10913498266Sopenharmony_ci return 1; 11013498266Sopenharmony_ci } 11113498266Sopenharmony_ci 11213498266Sopenharmony_ci printf("Sending request.\n"); 11313498266Sopenharmony_ci 11413498266Sopenharmony_ci do { 11513498266Sopenharmony_ci /* Warning: This example program may loop indefinitely. 11613498266Sopenharmony_ci * A production-quality program must define a timeout and exit this loop 11713498266Sopenharmony_ci * as soon as the timeout has expired. */ 11813498266Sopenharmony_ci size_t nsent; 11913498266Sopenharmony_ci do { 12013498266Sopenharmony_ci nsent = 0; 12113498266Sopenharmony_ci res = curl_easy_send(curl, request + nsent_total, 12213498266Sopenharmony_ci request_len - nsent_total, &nsent); 12313498266Sopenharmony_ci nsent_total += nsent; 12413498266Sopenharmony_ci 12513498266Sopenharmony_ci if(res == CURLE_AGAIN && !wait_on_socket(sockfd, 0, 60000L)) { 12613498266Sopenharmony_ci printf("Error: timeout.\n"); 12713498266Sopenharmony_ci return 1; 12813498266Sopenharmony_ci } 12913498266Sopenharmony_ci } while(res == CURLE_AGAIN); 13013498266Sopenharmony_ci 13113498266Sopenharmony_ci if(res != CURLE_OK) { 13213498266Sopenharmony_ci printf("Error: %s\n", curl_easy_strerror(res)); 13313498266Sopenharmony_ci return 1; 13413498266Sopenharmony_ci } 13513498266Sopenharmony_ci 13613498266Sopenharmony_ci printf("Sent %lu bytes.\n", (unsigned long)nsent); 13713498266Sopenharmony_ci 13813498266Sopenharmony_ci } while(nsent_total < request_len); 13913498266Sopenharmony_ci 14013498266Sopenharmony_ci printf("Reading response.\n"); 14113498266Sopenharmony_ci 14213498266Sopenharmony_ci for(;;) { 14313498266Sopenharmony_ci /* Warning: This example program may loop indefinitely (see above). */ 14413498266Sopenharmony_ci char buf[1024]; 14513498266Sopenharmony_ci size_t nread; 14613498266Sopenharmony_ci do { 14713498266Sopenharmony_ci nread = 0; 14813498266Sopenharmony_ci res = curl_easy_recv(curl, buf, sizeof(buf), &nread); 14913498266Sopenharmony_ci 15013498266Sopenharmony_ci if(res == CURLE_AGAIN && !wait_on_socket(sockfd, 1, 60000L)) { 15113498266Sopenharmony_ci printf("Error: timeout.\n"); 15213498266Sopenharmony_ci return 1; 15313498266Sopenharmony_ci } 15413498266Sopenharmony_ci } while(res == CURLE_AGAIN); 15513498266Sopenharmony_ci 15613498266Sopenharmony_ci if(res != CURLE_OK) { 15713498266Sopenharmony_ci printf("Error: %s\n", curl_easy_strerror(res)); 15813498266Sopenharmony_ci break; 15913498266Sopenharmony_ci } 16013498266Sopenharmony_ci 16113498266Sopenharmony_ci if(nread == 0) { 16213498266Sopenharmony_ci /* end of the response */ 16313498266Sopenharmony_ci break; 16413498266Sopenharmony_ci } 16513498266Sopenharmony_ci 16613498266Sopenharmony_ci printf("Received %lu bytes.\n", (unsigned long)nread); 16713498266Sopenharmony_ci } 16813498266Sopenharmony_ci 16913498266Sopenharmony_ci /* always cleanup */ 17013498266Sopenharmony_ci curl_easy_cleanup(curl); 17113498266Sopenharmony_ci } 17213498266Sopenharmony_ci return 0; 17313498266Sopenharmony_ci} 174