/***************************************************************************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 "first.h"2425#include "memdebug.h"2627struct t1517_WriteThis {28const char *readptr;29size_t sizeleft;30};3132static size_t t1517_read_cb(char *ptr, size_t size, size_t nmemb, void *userp)33{34struct t1517_WriteThis *pooh = (struct t1517_WriteThis *)userp;35size_t tocopy = size * nmemb;3637/* Wait one second before return POST data *38* so libcurl will wait before sending request body */39curlx_wait_ms(1000);4041if(tocopy < 1 || !pooh->sizeleft)42return 0;4344if(pooh->sizeleft < tocopy)45tocopy = pooh->sizeleft;4647memcpy(ptr, pooh->readptr, tocopy);/* copy requested data */48pooh->readptr += tocopy; /* advance pointer */49pooh->sizeleft -= tocopy; /* less data left */50return tocopy;51}5253static CURLcode test_lib1517(const char *URL)54{55static const char testdata[] =56"this is what we post to the silly web server\n";5758CURL *curl;59CURLcode res = CURLE_OK;6061struct t1517_WriteThis pooh;6263pooh.readptr = testdata;64pooh.sizeleft = strlen(testdata);6566if(curl_global_init(CURL_GLOBAL_ALL)) {67curl_mfprintf(stderr, "curl_global_init() failed\n");68return TEST_ERR_MAJOR_BAD;69}7071curl = curl_easy_init();72if(!curl) {73curl_mfprintf(stderr, "curl_easy_init() failed\n");74curl_global_cleanup();75return TEST_ERR_MAJOR_BAD;76}7778/* First set the URL that is about to receive our POST. */79test_setopt(curl, CURLOPT_URL, URL);8081/* Now specify we want to POST data */82test_setopt(curl, CURLOPT_POST, 1L);8384/* Set the expected POST size */85test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);8687/* we want to use our own read function */88test_setopt(curl, CURLOPT_READFUNCTION, t1517_read_cb);8990/* pointer to pass to our read function */91test_setopt(curl, CURLOPT_READDATA, &pooh);9293/* get verbose debug output please */94test_setopt(curl, CURLOPT_VERBOSE, 1L);9596/* include headers in the output */97test_setopt(curl, CURLOPT_HEADER, 1L);9899/* detect HTTP error codes >= 400 */100/* test_setopt(curl, CURLOPT_FAILONERROR, 1L); */101102103/* Perform the request, res will get the return code */104res = curl_easy_perform(curl);105106test_cleanup:107108/* always cleanup */109curl_easy_cleanup(curl);110curl_global_cleanup();111112return res;113}114115116