/***************************************************************************1* _ _ ____ _2* Project ___| | | | _ \| |3* / __| | | | |_) | |4* | (__| |_| | _ <| |___5* \___|\___/|_| \_\_____|6*7* Copyright (C) Daniel Stenberg, <[email protected]>, et al.8*9* This software is licensed as described in the file COPYING, which10* you should have received as part of this distribution. The terms11* are also available at https://curl.se/docs/copyright.html.12*13* You may opt to use, copy, modify, merge, publish, distribute and/or sell14* copies of the Software, and permit persons to whom the Software is15* furnished to do so, under the terms of the COPYING file.16*17* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY18* KIND, either express or implied.19*20* SPDX-License-Identifier: curl21*22***************************************************************************/23#include "test.h"2425#include "memdebug.h"2627static char testdata[]="this is what we post to the silly web server\n";2829struct WriteThis {30char *readptr;31size_t sizeleft;32};3334static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userp)35{36struct WriteThis *pooh = (struct WriteThis *)userp;37size_t tocopy = size * nmemb;3839/* Wait one second before return POST data *40* so libcurl will wait before sending request body */41wait_ms(1000);4243if(tocopy < 1 || !pooh->sizeleft)44return 0;4546if(pooh->sizeleft < tocopy)47tocopy = pooh->sizeleft;4849memcpy(ptr, pooh->readptr, tocopy);/* copy requested data */50pooh->readptr += tocopy; /* advance pointer */51pooh->sizeleft -= tocopy; /* less data left */52return tocopy;53}5455CURLcode test(char *URL)56{57CURL *curl;58CURLcode res = CURLE_OK;5960struct WriteThis pooh;6162if(!strcmp(URL, "check")) {63#if (defined(_WIN32) || defined(__CYGWIN__))64curl_mprintf("Windows TCP does not deliver response data but reports "65"CONNABORTED\n");66return TEST_ERR_FAILURE; /* skip since it fails on Windows without67workaround */68#else69return CURLE_OK; /* sure, run this! */70#endif71}7273pooh.readptr = testdata;74pooh.sizeleft = strlen(testdata);7576if(curl_global_init(CURL_GLOBAL_ALL)) {77curl_mfprintf(stderr, "curl_global_init() failed\n");78return TEST_ERR_MAJOR_BAD;79}8081curl = curl_easy_init();82if(!curl) {83curl_mfprintf(stderr, "curl_easy_init() failed\n");84curl_global_cleanup();85return TEST_ERR_MAJOR_BAD;86}8788/* First set the URL that is about to receive our POST. */89test_setopt(curl, CURLOPT_URL, URL);9091/* Now specify we want to POST data */92test_setopt(curl, CURLOPT_POST, 1L);9394/* Set the expected POST size */95test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);9697/* we want to use our own read function */98test_setopt(curl, CURLOPT_READFUNCTION, read_callback);99100/* pointer to pass to our read function */101test_setopt(curl, CURLOPT_READDATA, &pooh);102103/* get verbose debug output please */104test_setopt(curl, CURLOPT_VERBOSE, 1L);105106/* include headers in the output */107test_setopt(curl, CURLOPT_HEADER, 1L);108109/* detect HTTP error codes >= 400 */110/* test_setopt(curl, CURLOPT_FAILONERROR, 1L); */111112113/* Perform the request, res will get the return code */114res = curl_easy_perform(curl);115116test_cleanup:117118/* always cleanup */119curl_easy_cleanup(curl);120curl_global_cleanup();121122return res;123}124125126