xref: /aosp_15_r20/external/curl/docs/examples/https.c (revision 6236dae45794135f37c4eb022389c904c8b0090d)
1*6236dae4SAndroid Build Coastguard Worker /***************************************************************************
2*6236dae4SAndroid Build Coastguard Worker  *                                  _   _ ____  _
3*6236dae4SAndroid Build Coastguard Worker  *  Project                     ___| | | |  _ \| |
4*6236dae4SAndroid Build Coastguard Worker  *                             / __| | | | |_) | |
5*6236dae4SAndroid Build Coastguard Worker  *                            | (__| |_| |  _ <| |___
6*6236dae4SAndroid Build Coastguard Worker  *                             \___|\___/|_| \_\_____|
7*6236dae4SAndroid Build Coastguard Worker  *
8*6236dae4SAndroid Build Coastguard Worker  * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
9*6236dae4SAndroid Build Coastguard Worker  *
10*6236dae4SAndroid Build Coastguard Worker  * This software is licensed as described in the file COPYING, which
11*6236dae4SAndroid Build Coastguard Worker  * you should have received as part of this distribution. The terms
12*6236dae4SAndroid Build Coastguard Worker  * are also available at https://curl.se/docs/copyright.html.
13*6236dae4SAndroid Build Coastguard Worker  *
14*6236dae4SAndroid Build Coastguard Worker  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15*6236dae4SAndroid Build Coastguard Worker  * copies of the Software, and permit persons to whom the Software is
16*6236dae4SAndroid Build Coastguard Worker  * furnished to do so, under the terms of the COPYING file.
17*6236dae4SAndroid Build Coastguard Worker  *
18*6236dae4SAndroid Build Coastguard Worker  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19*6236dae4SAndroid Build Coastguard Worker  * KIND, either express or implied.
20*6236dae4SAndroid Build Coastguard Worker  *
21*6236dae4SAndroid Build Coastguard Worker  * SPDX-License-Identifier: curl
22*6236dae4SAndroid Build Coastguard Worker  *
23*6236dae4SAndroid Build Coastguard Worker  ***************************************************************************/
24*6236dae4SAndroid Build Coastguard Worker /* <DESC>
25*6236dae4SAndroid Build Coastguard Worker  * Simple HTTPS GET
26*6236dae4SAndroid Build Coastguard Worker  * </DESC>
27*6236dae4SAndroid Build Coastguard Worker  */
28*6236dae4SAndroid Build Coastguard Worker #include <stdio.h>
29*6236dae4SAndroid Build Coastguard Worker #include <curl/curl.h>
30*6236dae4SAndroid Build Coastguard Worker 
main(void)31*6236dae4SAndroid Build Coastguard Worker int main(void)
32*6236dae4SAndroid Build Coastguard Worker {
33*6236dae4SAndroid Build Coastguard Worker   CURL *curl;
34*6236dae4SAndroid Build Coastguard Worker   CURLcode res;
35*6236dae4SAndroid Build Coastguard Worker 
36*6236dae4SAndroid Build Coastguard Worker   curl_global_init(CURL_GLOBAL_DEFAULT);
37*6236dae4SAndroid Build Coastguard Worker 
38*6236dae4SAndroid Build Coastguard Worker   curl = curl_easy_init();
39*6236dae4SAndroid Build Coastguard Worker   if(curl) {
40*6236dae4SAndroid Build Coastguard Worker     curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
41*6236dae4SAndroid Build Coastguard Worker 
42*6236dae4SAndroid Build Coastguard Worker #ifdef SKIP_PEER_VERIFICATION
43*6236dae4SAndroid Build Coastguard Worker     /*
44*6236dae4SAndroid Build Coastguard Worker      * If you want to connect to a site who is not using a certificate that is
45*6236dae4SAndroid Build Coastguard Worker      * signed by one of the certs in the CA bundle you have, you can skip the
46*6236dae4SAndroid Build Coastguard Worker      * verification of the server's certificate. This makes the connection
47*6236dae4SAndroid Build Coastguard Worker      * A LOT LESS SECURE.
48*6236dae4SAndroid Build Coastguard Worker      *
49*6236dae4SAndroid Build Coastguard Worker      * If you have a CA cert for the server stored someplace else than in the
50*6236dae4SAndroid Build Coastguard Worker      * default bundle, then the CURLOPT_CAPATH option might come handy for
51*6236dae4SAndroid Build Coastguard Worker      * you.
52*6236dae4SAndroid Build Coastguard Worker      */
53*6236dae4SAndroid Build Coastguard Worker     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
54*6236dae4SAndroid Build Coastguard Worker #endif
55*6236dae4SAndroid Build Coastguard Worker 
56*6236dae4SAndroid Build Coastguard Worker #ifdef SKIP_HOSTNAME_VERIFICATION
57*6236dae4SAndroid Build Coastguard Worker     /*
58*6236dae4SAndroid Build Coastguard Worker      * If the site you are connecting to uses a different host name that what
59*6236dae4SAndroid Build Coastguard Worker      * they have mentioned in their server certificate's commonName (or
60*6236dae4SAndroid Build Coastguard Worker      * subjectAltName) fields, libcurl refuses to connect. You can skip this
61*6236dae4SAndroid Build Coastguard Worker      * check, but it makes the connection insecure.
62*6236dae4SAndroid Build Coastguard Worker      */
63*6236dae4SAndroid Build Coastguard Worker     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
64*6236dae4SAndroid Build Coastguard Worker #endif
65*6236dae4SAndroid Build Coastguard Worker 
66*6236dae4SAndroid Build Coastguard Worker     /* cache the CA cert bundle in memory for a week */
67*6236dae4SAndroid Build Coastguard Worker     curl_easy_setopt(curl, CURLOPT_CA_CACHE_TIMEOUT, 604800L);
68*6236dae4SAndroid Build Coastguard Worker 
69*6236dae4SAndroid Build Coastguard Worker     /* Perform the request, res gets the return code */
70*6236dae4SAndroid Build Coastguard Worker     res = curl_easy_perform(curl);
71*6236dae4SAndroid Build Coastguard Worker     /* Check for errors */
72*6236dae4SAndroid Build Coastguard Worker     if(res != CURLE_OK)
73*6236dae4SAndroid Build Coastguard Worker       fprintf(stderr, "curl_easy_perform() failed: %s\n",
74*6236dae4SAndroid Build Coastguard Worker               curl_easy_strerror(res));
75*6236dae4SAndroid Build Coastguard Worker 
76*6236dae4SAndroid Build Coastguard Worker     /* always cleanup */
77*6236dae4SAndroid Build Coastguard Worker     curl_easy_cleanup(curl);
78*6236dae4SAndroid Build Coastguard Worker   }
79*6236dae4SAndroid Build Coastguard Worker 
80*6236dae4SAndroid Build Coastguard Worker   curl_global_cleanup();
81*6236dae4SAndroid Build Coastguard Worker 
82*6236dae4SAndroid Build Coastguard Worker   return 0;
83*6236dae4SAndroid Build Coastguard Worker }
84