Git init
[external/curl.git] / tests / libtest / lib556.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  */
9
10 #include "test.h"
11
12 #include "memdebug.h"
13
14 /* For Windows, mainly (may be moved in a config file?) */
15 #ifndef STDIN_FILENO
16   #define STDIN_FILENO 0
17 #endif
18 #ifndef STDOUT_FILENO
19   #define STDOUT_FILENO 1
20 #endif
21 #ifndef STDERR_FILENO
22   #define STDERR_FILENO 2
23 #endif
24
25 int test(char *URL)
26 {
27   CURLcode res;
28   CURL *curl;
29
30   if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
31     fprintf(stderr, "curl_global_init() failed\n");
32     return TEST_ERR_MAJOR_BAD;
33   }
34
35   if ((curl = curl_easy_init()) == NULL) {
36     fprintf(stderr, "curl_easy_init() failed\n");
37     curl_global_cleanup();
38     return TEST_ERR_MAJOR_BAD;
39   }
40
41   test_setopt(curl, CURLOPT_URL, URL);
42   test_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
43
44   res = curl_easy_perform(curl);
45
46   if(!res) {
47     /* we are connected, now get a HTTP document the raw way */
48     const char *request =
49 #ifdef CURL_DOES_CONVERSIONS
50       /* ASCII representation with escape sequences for non-ASCII platforms */
51       "\x47\x45\x54\x20\x2f\x35\x35\x36\x20\x48\x54\x54\x50\x2f\x31\x2e"
52       "\x32\x0d\x0a\x48\x6f\x73\x74\x3a\x20\x6e\x69\x6e\x6a\x61\x0d\x0a"
53       "\x0d\x0a";
54 #else
55       "GET /556 HTTP/1.2\r\n"
56       "Host: ninja\r\n\r\n";
57 #endif
58     size_t iolen;
59     char buf[1024];
60
61     res = curl_easy_send(curl, request, strlen(request), &iolen);
62
63     if(!res) {
64       /* we assume that sending always work */
65       size_t total=0;
66
67       do {
68         /* busy-read like crazy */
69         res = curl_easy_recv(curl, buf, 1024, &iolen);
70
71 #ifdef TPF
72         sleep(1); /* avoid ctl-10 dump */
73 #endif
74
75         if(iolen)
76           /* send received stuff to stdout */
77           write(STDOUT_FILENO, buf, iolen);
78         total += iolen;
79
80       } while(((res == CURLE_OK) || (res == CURLE_AGAIN)) && (total < 129));
81     }
82   }
83
84 test_cleanup:
85
86   curl_easy_cleanup(curl);
87   curl_global_cleanup();
88
89   return (int)res;
90 }
91