lib1517.c (3647B)
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 "first.h" 25 26 #include "memdebug.h" 27 28 struct t1517_WriteThis { 29 const char *readptr; 30 size_t sizeleft; 31 }; 32 33 static size_t t1517_read_cb(char *ptr, size_t size, size_t nmemb, void *userp) 34 { 35 struct t1517_WriteThis *pooh = (struct t1517_WriteThis *)userp; 36 size_t tocopy = size * nmemb; 37 38 /* Wait one second before return POST data * 39 * so libcurl will wait before sending request body */ 40 curlx_wait_ms(1000); 41 42 if(tocopy < 1 || !pooh->sizeleft) 43 return 0; 44 45 if(pooh->sizeleft < tocopy) 46 tocopy = pooh->sizeleft; 47 48 memcpy(ptr, pooh->readptr, tocopy);/* copy requested data */ 49 pooh->readptr += tocopy; /* advance pointer */ 50 pooh->sizeleft -= tocopy; /* less data left */ 51 return tocopy; 52 } 53 54 static CURLcode test_lib1517(char *URL) 55 { 56 static const char testdata[] = 57 "this is what we post to the silly web server\n"; 58 59 CURL *curl; 60 CURLcode res = CURLE_OK; 61 62 struct t1517_WriteThis pooh; 63 64 if(!strcmp(URL, "check")) { 65 #if (defined(_WIN32) || defined(__CYGWIN__)) 66 curl_mprintf("Windows TCP does not deliver response data but reports " 67 "CONNABORTED\n"); 68 return TEST_ERR_FAILURE; /* skip since it fails on Windows without 69 workaround */ 70 #else 71 return CURLE_OK; /* sure, run this! */ 72 #endif 73 } 74 75 pooh.readptr = testdata; 76 pooh.sizeleft = strlen(testdata); 77 78 if(curl_global_init(CURL_GLOBAL_ALL)) { 79 curl_mfprintf(stderr, "curl_global_init() failed\n"); 80 return TEST_ERR_MAJOR_BAD; 81 } 82 83 curl = curl_easy_init(); 84 if(!curl) { 85 curl_mfprintf(stderr, "curl_easy_init() failed\n"); 86 curl_global_cleanup(); 87 return TEST_ERR_MAJOR_BAD; 88 } 89 90 /* First set the URL that is about to receive our POST. */ 91 test_setopt(curl, CURLOPT_URL, URL); 92 93 /* Now specify we want to POST data */ 94 test_setopt(curl, CURLOPT_POST, 1L); 95 96 /* Set the expected POST size */ 97 test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft); 98 99 /* we want to use our own read function */ 100 test_setopt(curl, CURLOPT_READFUNCTION, t1517_read_cb); 101 102 /* pointer to pass to our read function */ 103 test_setopt(curl, CURLOPT_READDATA, &pooh); 104 105 /* get verbose debug output please */ 106 test_setopt(curl, CURLOPT_VERBOSE, 1L); 107 108 /* include headers in the output */ 109 test_setopt(curl, CURLOPT_HEADER, 1L); 110 111 /* detect HTTP error codes >= 400 */ 112 /* test_setopt(curl, CURLOPT_FAILONERROR, 1L); */ 113 114 115 /* Perform the request, res will get the return code */ 116 res = curl_easy_perform(curl); 117 118 test_cleanup: 119 120 /* always cleanup */ 121 curl_easy_cleanup(curl); 122 curl_global_cleanup(); 123 124 return res; 125 }