xref: /curl/tests/libtest/lib556.c (revision ac6349b4)
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at https://curl.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 #include "test.h"
25 
26 #include "warnless.h"
27 #include "memdebug.h"
28 
29 /* For Windows, mainly (may be moved in a config file?) */
30 #ifndef STDIN_FILENO
31   #define STDIN_FILENO 0
32 #endif
33 #ifndef STDOUT_FILENO
34   #define STDOUT_FILENO 1
35 #endif
36 #ifndef STDERR_FILENO
37   #define STDERR_FILENO 2
38 #endif
39 
test(char * URL)40 CURLcode test(char *URL)
41 {
42   CURLcode res;
43   CURL *curl;
44 
45   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
46     fprintf(stderr, "curl_global_init() failed\n");
47     return TEST_ERR_MAJOR_BAD;
48   }
49 
50   curl = curl_easy_init();
51   if(!curl) {
52     fprintf(stderr, "curl_easy_init() failed\n");
53     curl_global_cleanup();
54     return TEST_ERR_MAJOR_BAD;
55   }
56 
57   test_setopt(curl, CURLOPT_URL, URL);
58   test_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
59   test_setopt(curl, CURLOPT_VERBOSE, 1L);
60 
61   res = curl_easy_perform(curl);
62 
63   if(!res) {
64     /* we are connected, now get an HTTP document the raw way */
65     const char *request =
66       "GET /556 HTTP/1.1\r\n"
67       "Host: ninja\r\n\r\n";
68     const char *sbuf = request;
69     size_t sblen = strlen(request);
70     size_t nwritten = 0, nread = 0;
71 
72     do {
73       char buf[1024];
74 
75       if(sblen) {
76         res = curl_easy_send(curl, sbuf, sblen, &nwritten);
77         if(res && res != CURLE_AGAIN)
78           break;
79         if(nwritten > 0) {
80           sbuf += nwritten;
81           sblen -= nwritten;
82         }
83       }
84 
85       /* busy-read like crazy */
86       res = curl_easy_recv(curl, buf, sizeof(buf), &nread);
87 
88       if(nread) {
89         /* send received stuff to stdout */
90         if(!write(STDOUT_FILENO, buf, nread))
91           break;
92       }
93 
94     } while((res == CURLE_OK && nread) || (res == CURLE_AGAIN));
95 
96     if(res && res != CURLE_AGAIN)
97       res = TEST_ERR_FAILURE;
98   }
99 
100 test_cleanup:
101 
102   curl_easy_cleanup(curl);
103   curl_global_cleanup();
104 
105   return res;
106 }
107