xref: /third_party/curl/tests/libtest/lib556.c (revision 13498266)
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
40int 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    size_t iolen = 0;
69
70    res = curl_easy_send(curl, request, strlen(request), &iolen);
71
72    if(!res) {
73      /* we assume that sending always work */
74
75      do {
76        char buf[1024];
77        /* busy-read like crazy */
78        res = curl_easy_recv(curl, buf, sizeof(buf), &iolen);
79
80        if(iolen) {
81          /* send received stuff to stdout */
82          if(!write(STDOUT_FILENO, buf, iolen))
83            break;
84        }
85
86      } while((res == CURLE_OK && iolen) || (res == CURLE_AGAIN));
87    }
88
89    if(iolen)
90      res = (CURLcode)TEST_ERR_FAILURE;
91  }
92
93test_cleanup:
94
95  curl_easy_cleanup(curl);
96  curl_global_cleanup();
97
98  return (int)res;
99}
100