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