tests: merge clients into libtests, drop duplicate code

libtests and clients were built the same way after recent overhauls.
libtests are used by runtests, clients by pytests.

Merge clients into libtests, aligning their entry function signature,
dropping common utility functions, and simplifying the build.

Note: After this patch `CURLDEBUG` applies to cli tests, when enabled.

Also:
- lib552: drop local copy-paste debug callback in favor of testtrace.
- lib552: drop local copy-paste dump function in favor of testtrace.
- clients: use `long` for HTTP version, drop casts.
- clients: replace local dump function in favor of testrace clone.
- sync cli test entry function prototype with libtests'.
- h2_serverpush: replace local trace callback with testtrace.
- de-duplicate 3 websocket close, ping, ping, functions. Kept the pong
  iteration from `ws_pingpong`. Note: the pong clone in `lib2304` was
  returning an error when `curl_ws_recv()` returned non-zero and
  the payload matched the expected one anyway. After this patch, this
  case returns success, as it does in `ws_pingpong`.
  `lib2304` keeps passing, but I'm not sure if the previous behavior
  was intentional.
- display full value in websocket close, ping, pong, drop casts.

Closes #18079
This commit is contained in:
Viktor Szakats 2025-07-29 09:53:14 +02:00
parent 952c929bdf
commit 00887aee8c
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
48 changed files with 541 additions and 1041 deletions

View file

@ -44,6 +44,16 @@ CURLX_C = \
# All libtest programs
TESTS_C = \
cli_h2_pausing.c \
cli_h2_serverpush.c \
cli_h2_upgrade_extreme.c \
cli_hx_download.c \
cli_hx_upload.c \
cli_tls_session_reuse.c \
cli_upload_pausing.c \
cli_ws_data.c \
cli_ws_pingpong.c \
\
lib500.c lib501.c lib502.c lib503.c lib504.c lib505.c lib506.c lib507.c \
lib508.c lib509.c lib510.c lib511.c lib512.c lib513.c lib514.c lib515.c \
lib516.c lib517.c lib518.c lib519.c lib520.c lib521.c lib523.c lib524.c \

View file

@ -0,0 +1,294 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* This is based on the PoC client of issue #11982
*/
#include "first.h"
#include "testtrace.h"
#include "memdebug.h"
static void usage_h2_pausing(const char *msg)
{
if(msg)
curl_mfprintf(stderr, "%s\n", msg);
curl_mfprintf(stderr,
"usage: [options] url\n"
" pause downloads with following options:\n"
" -V http_version (http/1.1, h2, h3) http version to use\n"
);
}
struct handle
{
size_t idx;
int paused;
int resumed;
int errored;
int fail_write;
CURL *h;
};
static size_t cb(char *data, size_t size, size_t nmemb, void *clientp)
{
size_t realsize = size * nmemb;
struct handle *handle = (struct handle *) clientp;
curl_off_t totalsize;
(void)data;
if(curl_easy_getinfo(handle->h, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T,
&totalsize) == CURLE_OK)
curl_mfprintf(stderr, "INFO: [%d] write, "
"Content-Length %" CURL_FORMAT_CURL_OFF_T "\n",
(int)handle->idx, totalsize);
if(!handle->resumed) {
++handle->paused;
curl_mfprintf(stderr, "INFO: [%d] write, PAUSING %d time on %lu bytes\n",
(int)handle->idx, handle->paused, (long)realsize);
assert(handle->paused == 1);
return CURL_WRITEFUNC_PAUSE;
}
if(handle->fail_write) {
++handle->errored;
curl_mfprintf(stderr, "INFO: [%d] FAIL write of %lu bytes, %d time\n",
(int)handle->idx, (long)realsize, handle->errored);
return CURL_WRITEFUNC_ERROR;
}
curl_mfprintf(stderr, "INFO: [%d] write, accepting %lu bytes\n",
(int)handle->idx, (long)realsize);
return realsize;
}
#define CLI_ERR() \
do { \
curl_mfprintf(stderr, "something unexpected went wrong - bailing out!\n");\
return (CURLcode)2; \
} while(0)
static CURLcode test_cli_h2_pausing(const char *URL)
{
struct handle handles[2];
CURLM *multi_handle;
int still_running = 1, msgs_left, numfds;
size_t i;
CURLMsg *msg;
int rounds = 0;
CURLcode rc = CURLE_OK;
CURLU *cu;
struct curl_slist *resolve = NULL;
char resolve_buf[1024];
const char *url;
char *host = NULL, *port = NULL;
int all_paused = 0;
int resume_round = -1;
long http_version = CURL_HTTP_VERSION_2_0;
int ch;
(void)URL;
while((ch = cgetopt(test_argc, test_argv, "hV:")) != -1) {
switch(ch) {
case 'h':
usage_h2_pausing(NULL);
return (CURLcode)2;
case 'V': {
if(!strcmp("http/1.1", coptarg))
http_version = CURL_HTTP_VERSION_1_1;
else if(!strcmp("h2", coptarg))
http_version = CURL_HTTP_VERSION_2_0;
else if(!strcmp("h3", coptarg))
http_version = CURL_HTTP_VERSION_3ONLY;
else {
usage_h2_pausing("invalid http version");
return (CURLcode)1;
}
break;
}
default:
usage_h2_pausing("invalid option");
return (CURLcode)1;
}
}
test_argc -= coptind;
test_argv += coptind;
if(test_argc != 1) {
curl_mfprintf(stderr, "ERROR: need URL as argument\n");
return (CURLcode)2;
}
url = test_argv[0];
curl_global_init(CURL_GLOBAL_DEFAULT);
curl_global_trace("ids,time,http/2,http/3");
cu = curl_url();
if(!cu) {
curl_mfprintf(stderr, "out of memory\n");
return (CURLcode)1;
}
if(curl_url_set(cu, CURLUPART_URL, url, 0)) {
curl_mfprintf(stderr, "not a URL: '%s'\n", url);
return (CURLcode)1;
}
if(curl_url_get(cu, CURLUPART_HOST, &host, 0)) {
curl_mfprintf(stderr, "could not get host of '%s'\n", url);
return (CURLcode)1;
}
if(curl_url_get(cu, CURLUPART_PORT, &port, 0)) {
curl_mfprintf(stderr, "could not get port of '%s'\n", url);
return (CURLcode)1;
}
memset(&resolve, 0, sizeof(resolve));
curl_msnprintf(resolve_buf, sizeof(resolve_buf)-1, "%s:%s:127.0.0.1",
host, port);
resolve = curl_slist_append(resolve, resolve_buf);
for(i = 0; i < CURL_ARRAYSIZE(handles); i++) {
handles[i].idx = i;
handles[i].paused = 0;
handles[i].resumed = 0;
handles[i].errored = 0;
handles[i].fail_write = 1;
handles[i].h = curl_easy_init();
if(!handles[i].h ||
curl_easy_setopt(handles[i].h, CURLOPT_WRITEFUNCTION, cb) != CURLE_OK ||
curl_easy_setopt(handles[i].h, CURLOPT_WRITEDATA, &handles[i])
!= CURLE_OK ||
curl_easy_setopt(handles[i].h, CURLOPT_FOLLOWLOCATION, 1L) != CURLE_OK ||
curl_easy_setopt(handles[i].h, CURLOPT_VERBOSE, 1L) != CURLE_OK ||
curl_easy_setopt(handles[i].h, CURLOPT_DEBUGFUNCTION, cli_debug_cb)
!= CURLE_OK ||
curl_easy_setopt(handles[i].h, CURLOPT_SSL_VERIFYPEER, 0L) != CURLE_OK ||
curl_easy_setopt(handles[i].h, CURLOPT_RESOLVE, resolve) != CURLE_OK ||
curl_easy_setopt(handles[i].h, CURLOPT_PIPEWAIT, 1L) ||
curl_easy_setopt(handles[i].h, CURLOPT_URL, url) != CURLE_OK) {
CLI_ERR();
}
curl_easy_setopt(handles[i].h, CURLOPT_HTTP_VERSION, http_version);
}
multi_handle = curl_multi_init();
if(!multi_handle)
CLI_ERR();
for(i = 0; i < CURL_ARRAYSIZE(handles); i++) {
if(curl_multi_add_handle(multi_handle, handles[i].h) != CURLM_OK)
CLI_ERR();
}
for(rounds = 0;; rounds++) {
curl_mfprintf(stderr, "INFO: multi_perform round %d\n", rounds);
if(curl_multi_perform(multi_handle, &still_running) != CURLM_OK)
CLI_ERR();
if(!still_running) {
int as_expected = 1;
curl_mfprintf(stderr, "INFO: no more handles running\n");
for(i = 0; i < CURL_ARRAYSIZE(handles); i++) {
if(!handles[i].paused) {
curl_mfprintf(stderr, "ERROR: [%d] NOT PAUSED\n", (int)i);
as_expected = 0;
}
else if(handles[i].paused != 1) {
curl_mfprintf(stderr, "ERROR: [%d] PAUSED %d times!\n",
(int)i, handles[i].paused);
as_expected = 0;
}
else if(!handles[i].resumed) {
curl_mfprintf(stderr, "ERROR: [%d] NOT resumed!\n", (int)i);
as_expected = 0;
}
else if(handles[i].errored != 1) {
curl_mfprintf(stderr, "ERROR: [%d] NOT errored once, %d instead!\n",
(int)i, handles[i].errored);
as_expected = 0;
}
}
if(!as_expected) {
curl_mfprintf(stderr, "ERROR: handles not in expected state "
"after %d rounds\n", rounds);
rc = (CURLcode)1;
}
break;
}
if(curl_multi_poll(multi_handle, NULL, 0, 100, &numfds) != CURLM_OK)
CLI_ERR();
/* !checksrc! disable EQUALSNULL 1 */
while((msg = curl_multi_info_read(multi_handle, &msgs_left)) != NULL) {
if(msg->msg == CURLMSG_DONE) {
for(i = 0; i < CURL_ARRAYSIZE(handles); i++) {
if(msg->easy_handle == handles[i].h) {
if(handles[i].paused != 1 || !handles[i].resumed) {
curl_mfprintf(stderr, "ERROR: [%d] done, pauses=%d, resumed=%d, "
"result %d - wtf?\n", (int)i, handles[i].paused,
handles[i].resumed, msg->data.result);
rc = (CURLcode)1;
goto out;
}
}
}
}
}
/* Successfully paused? */
if(!all_paused) {
for(i = 0; i < CURL_ARRAYSIZE(handles); i++) {
if(!handles[i].paused) {
break;
}
}
all_paused = (i == CURL_ARRAYSIZE(handles));
if(all_paused) {
curl_mfprintf(stderr, "INFO: all transfers paused\n");
/* give transfer some rounds to mess things up */
resume_round = rounds + 2;
}
}
if(resume_round > 0 && rounds == resume_round) {
/* time to resume */
for(i = 0; i < CURL_ARRAYSIZE(handles); i++) {
curl_mfprintf(stderr, "INFO: [%d] resumed\n", (int)i);
handles[i].resumed = 1;
curl_easy_pause(handles[i].h, CURLPAUSE_CONT);
}
}
}
out:
for(i = 0; i < CURL_ARRAYSIZE(handles); i++) {
curl_multi_remove_handle(multi_handle, handles[i].h);
curl_easy_cleanup(handles[i].h);
}
curl_slist_free_all(resolve);
curl_free(host);
curl_free(port);
curl_url_cleanup(cu);
curl_multi_cleanup(multi_handle);
curl_global_cleanup();
return rc;
}

View file

@ -0,0 +1,174 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include "first.h"
#include "testtrace.h"
#include "memdebug.h"
static FILE *out_download;
static int setup_h2_serverpush(CURL *hnd, const char *url)
{
out_download = fopen("download_0.data", "wb");
if(!out_download)
return 1; /* failed */
curl_easy_setopt(hnd, CURLOPT_URL, url);
curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(hnd, CURLOPT_WRITEDATA, out_download);
/* please be verbose */
curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, libtest_debug_cb);
curl_easy_setopt(hnd, CURLOPT_DEBUGDATA, &debug_config);
/* wait for pipe connection to confirm */
curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
return 0; /* all is good */
}
static FILE *out_push;
/* called when there's an incoming push */
static int server_push_callback(CURL *parent,
CURL *easy,
size_t num_headers,
struct curl_pushheaders *headers,
void *userp)
{
char *headp;
size_t i;
int *transfers = (int *)userp;
char filename[128];
static unsigned int count = 0;
int rv;
(void)parent; /* we have no use for this */
curl_msnprintf(filename, sizeof(filename) - 1, "push%u", count++);
/* here's a new stream, save it in a new file for each new push */
out_push = fopen(filename, "wb");
if(!out_push) {
/* if we cannot save it, deny it */
curl_mfprintf(stderr, "Failed to create output file for push\n");
rv = CURL_PUSH_DENY;
goto out;
}
/* write to this file */
curl_easy_setopt(easy, CURLOPT_WRITEDATA, out_push);
curl_mfprintf(stderr, "**** push callback approves stream %u, "
"got %lu headers!\n", count, (unsigned long)num_headers);
for(i = 0; i < num_headers; i++) {
headp = curl_pushheader_bynum(headers, i);
curl_mfprintf(stderr, "**** header %lu: %s\n", (unsigned long)i, headp);
}
headp = curl_pushheader_byname(headers, ":path");
if(headp) {
curl_mfprintf(stderr, "**** The PATH is %s\n",
headp /* skip :path + colon */);
}
(*transfers)++; /* one more */
rv = CURL_PUSH_OK;
out:
return rv;
}
/*
* Download a file over HTTP/2, take care of server push.
*/
static CURLcode test_cli_h2_serverpush(const char *URL)
{
CURL *easy;
CURLM *multi_handle;
int transfers = 1; /* we start with one */
struct CURLMsg *m;
debug_config.nohex = 1;
debug_config.tracetime = 0;
if(!URL) {
curl_mfprintf(stderr, "need URL as argument\n");
return (CURLcode)2;
}
multi_handle = curl_multi_init();
curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
curl_multi_setopt(multi_handle, CURLMOPT_PUSHFUNCTION, server_push_callback);
curl_multi_setopt(multi_handle, CURLMOPT_PUSHDATA, &transfers);
easy = curl_easy_init();
if(setup_h2_serverpush(easy, URL)) {
fclose(out_download);
curl_mfprintf(stderr, "failed\n");
return (CURLcode)1;
}
curl_multi_add_handle(multi_handle, easy);
do {
int still_running; /* keep number of running handles */
CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
if(still_running)
/* wait for activity, timeout or "nothing" */
mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
if(mc)
break;
/*
* A little caution when doing server push is that libcurl itself has
* created and added one or more easy handles but we need to clean them up
* when we are done.
*/
do {
int msgq = 0;
m = curl_multi_info_read(multi_handle, &msgq);
if(m && (m->msg == CURLMSG_DONE)) {
CURL *e = m->easy_handle;
transfers--;
curl_multi_remove_handle(multi_handle, e);
curl_easy_cleanup(e);
}
} while(m);
} while(transfers); /* as long as we have transfers going */
curl_multi_cleanup(multi_handle);
fclose(out_download);
if(out_push)
fclose(out_push);
return CURLE_OK;
}

View file

@ -0,0 +1,165 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include "first.h"
#include "testtrace.h"
#include "memdebug.h"
static size_t write_h2_upg_extreme_cb(char *ptr, size_t size, size_t nmemb,
void *opaque)
{
(void)ptr;
(void)opaque;
return size * nmemb;
}
static CURLcode test_cli_h2_upgrade_extreme(const char *URL)
{
CURLM *multi = NULL;
CURL *easy;
CURLMcode mc;
int running_handles = 0, start_count, numfds;
CURLMsg *msg;
int msgs_in_queue;
char range[128];
CURLcode exitcode = (CURLcode)1;
if(!URL) {
curl_mfprintf(stderr, "need URL as argument\n");
return (CURLcode)2;
}
multi = curl_multi_init();
if(!multi) {
curl_mfprintf(stderr, "curl_multi_init failed\n");
goto cleanup;
}
start_count = 200;
do {
if(start_count) {
easy = curl_easy_init();
if(!easy) {
curl_mfprintf(stderr, "curl_easy_init failed\n");
goto cleanup;
}
curl_easy_setopt(easy, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(easy, CURLOPT_DEBUGFUNCTION, cli_debug_cb);
curl_easy_setopt(easy, CURLOPT_URL, URL);
curl_easy_setopt(easy, CURLOPT_NOSIGNAL, 1L);
curl_easy_setopt(easy, CURLOPT_AUTOREFERER, 1L);
curl_easy_setopt(easy, CURLOPT_FAILONERROR, 1L);
curl_easy_setopt(easy, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
curl_easy_setopt(easy, CURLOPT_WRITEFUNCTION, write_h2_upg_extreme_cb);
curl_easy_setopt(easy, CURLOPT_WRITEDATA, NULL);
curl_easy_setopt(easy, CURLOPT_HTTPGET, 1L);
curl_msnprintf(range, sizeof(range),
"%" CURL_FORMAT_CURL_OFF_TU "-"
"%" CURL_FORMAT_CURL_OFF_TU,
(curl_off_t)0,
(curl_off_t)16384);
curl_easy_setopt(easy, CURLOPT_RANGE, range);
mc = curl_multi_add_handle(multi, easy);
if(mc != CURLM_OK) {
curl_mfprintf(stderr, "curl_multi_add_handle: %s\n",
curl_multi_strerror(mc));
curl_easy_cleanup(easy);
goto cleanup;
}
--start_count;
}
mc = curl_multi_perform(multi, &running_handles);
if(mc != CURLM_OK) {
curl_mfprintf(stderr, "curl_multi_perform: %s\n",
curl_multi_strerror(mc));
goto cleanup;
}
if(running_handles) {
mc = curl_multi_poll(multi, NULL, 0, 1000000, &numfds);
if(mc != CURLM_OK) {
curl_mfprintf(stderr, "curl_multi_poll: %s\n",
curl_multi_strerror(mc));
goto cleanup;
}
}
/* Check for finished handles and remove. */
/* !checksrc! disable EQUALSNULL 1 */
while((msg = curl_multi_info_read(multi, &msgs_in_queue)) != NULL) {
if(msg->msg == CURLMSG_DONE) {
long status = 0;
curl_off_t xfer_id;
curl_easy_getinfo(msg->easy_handle, CURLINFO_XFER_ID, &xfer_id);
curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &status);
if(msg->data.result == CURLE_SEND_ERROR ||
msg->data.result == CURLE_RECV_ERROR) {
/* We get these if the server had a GOAWAY in transit on
* reusing a connection */
}
else if(msg->data.result) {
curl_mfprintf(stderr, "transfer #%" CURL_FORMAT_CURL_OFF_T
": failed with %d\n", xfer_id, msg->data.result);
goto cleanup;
}
else if(status != 206) {
curl_mfprintf(stderr, "transfer #%" CURL_FORMAT_CURL_OFF_T
": wrong http status %ld (expected 206)\n", xfer_id,
status);
goto cleanup;
}
curl_multi_remove_handle(multi, msg->easy_handle);
curl_easy_cleanup(msg->easy_handle);
curl_mfprintf(stderr, "transfer #%" CURL_FORMAT_CURL_OFF_T" retiring "
"(%d now running)\n", xfer_id, running_handles);
}
}
curl_mfprintf(stderr, "running_handles=%d, yet_to_start=%d\n",
running_handles, start_count);
} while(running_handles > 0 || start_count);
curl_mfprintf(stderr, "exiting\n");
exitcode = CURLE_OK;
cleanup:
if(multi) {
CURL **list = curl_multi_get_handles(multi);
if(list) {
int i;
for(i = 0; list[i]; i++) {
curl_multi_remove_handle(multi, list[i]);
curl_easy_cleanup(list[i]);
}
curl_free(list);
}
curl_multi_cleanup(multi);
}
return exitcode;
}

View file

@ -0,0 +1,453 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include "first.h"
#include "testtrace.h"
#include "memdebug.h"
static int verbose_d = 1;
struct transfer_d {
int idx;
CURL *easy;
char filename[128];
FILE *out;
curl_off_t recv_size;
curl_off_t fail_at;
curl_off_t pause_at;
curl_off_t abort_at;
int started;
int paused;
int resumed;
int done;
CURLcode result;
};
static size_t transfer_count_d = 1;
static struct transfer_d *transfer_d;
static int forbid_reuse_d = 0;
static struct transfer_d *get_transfer_for_easy_d(CURL *easy)
{
size_t i;
for(i = 0; i < transfer_count_d; ++i) {
if(easy == transfer_d[i].easy)
return &transfer_d[i];
}
return NULL;
}
static size_t my_write_d_cb(char *buf, size_t nitems, size_t buflen,
void *userdata)
{
struct transfer_d *t = userdata;
size_t blen = (nitems * buflen);
size_t nwritten;
curl_mfprintf(stderr, "[t-%d] RECV %ld bytes, total=%ld, pause_at=%ld\n",
t->idx, (long)blen, (long)t->recv_size, (long)t->pause_at);
if(!t->out) {
curl_msnprintf(t->filename, sizeof(t->filename)-1, "download_%u.data",
t->idx);
t->out = fopen(t->filename, "wb");
if(!t->out)
return 0;
}
if(!t->resumed &&
t->recv_size < t->pause_at &&
((t->recv_size + (curl_off_t)blen) >= t->pause_at)) {
curl_mfprintf(stderr, "[t-%d] PAUSE\n", t->idx);
t->paused = 1;
return CURL_WRITEFUNC_PAUSE;
}
nwritten = fwrite(buf, nitems, buflen, t->out);
if(nwritten < blen) {
curl_mfprintf(stderr, "[t-%d] write failure\n", t->idx);
return 0;
}
t->recv_size += (curl_off_t)nwritten;
if(t->fail_at > 0 && t->recv_size >= t->fail_at) {
curl_mfprintf(stderr, "[t-%d] FAIL by write callback at %ld bytes\n",
t->idx, (long)t->recv_size);
return CURL_WRITEFUNC_ERROR;
}
return (size_t)nwritten;
}
static int my_progress_d_cb(void *userdata,
curl_off_t dltotal, curl_off_t dlnow,
curl_off_t ultotal, curl_off_t ulnow)
{
struct transfer_d *t = userdata;
(void)ultotal;
(void)ulnow;
(void)dltotal;
if(t->abort_at > 0 && dlnow >= t->abort_at) {
curl_mfprintf(stderr, "[t-%d] ABORT by progress_cb at %ld bytes\n",
t->idx, (long)dlnow);
return 1;
}
return 0;
}
static int setup_hx_download(CURL *hnd, const char *url, struct transfer_d *t,
long http_version, struct curl_slist *host,
CURLSH *share, int use_earlydata,
int fresh_connect)
{
curl_easy_setopt(hnd, CURLOPT_SHARE, share);
curl_easy_setopt(hnd, CURLOPT_URL, url);
curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, http_version);
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(hnd, CURLOPT_ACCEPT_ENCODING, "");
curl_easy_setopt(hnd, CURLOPT_BUFFERSIZE, (long)(128 * 1024));
curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, my_write_d_cb);
curl_easy_setopt(hnd, CURLOPT_WRITEDATA, t);
curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(hnd, CURLOPT_XFERINFOFUNCTION, my_progress_d_cb);
curl_easy_setopt(hnd, CURLOPT_XFERINFODATA, t);
if(use_earlydata)
curl_easy_setopt(hnd, CURLOPT_SSL_OPTIONS, CURLSSLOPT_EARLYDATA);
if(forbid_reuse_d)
curl_easy_setopt(hnd, CURLOPT_FORBID_REUSE, 1L);
if(host)
curl_easy_setopt(hnd, CURLOPT_RESOLVE, host);
if(fresh_connect)
curl_easy_setopt(hnd, CURLOPT_FRESH_CONNECT, 1L);
/* please be verbose */
if(verbose_d) {
curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, cli_debug_cb);
}
/* wait for pipe connection to confirm */
curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
return 0; /* all is good */
}
static void usage_hx_download(const char *msg)
{
if(msg)
curl_mfprintf(stderr, "%s\n", msg);
curl_mfprintf(stderr,
"usage: [options] url\n"
" download a url with following options:\n"
" -a abort paused transfer\n"
" -m number max parallel downloads\n"
" -e use TLS early data when possible\n"
" -f forbid connection reuse\n"
" -n number total downloads\n");
curl_mfprintf(stderr,
" -A number abort transfer after `number` response bytes\n"
" -F number fail writing response after `number` response bytes\n"
" -M number max concurrent connections to a host\n"
" -P number pause transfer after `number` response bytes\n"
" -r <host>:<port>:<addr> resolve information\n"
" -T number max concurrent connections total\n"
" -V http_version (http/1.1, h2, h3) http version to use\n"
);
}
/*
* Download a file over HTTP/2, take care of server push.
*/
static CURLcode test_cli_hx_download(const char *URL)
{
CURLM *multi_handle;
struct CURLMsg *m;
CURLSH *share;
const char *url;
size_t i, n, max_parallel = 1;
size_t active_transfers;
size_t pause_offset = 0;
size_t abort_offset = 0;
size_t fail_offset = 0;
int abort_paused = 0, use_earlydata = 0;
struct transfer_d *t;
long http_version = CURL_HTTP_VERSION_2_0;
int ch;
struct curl_slist *host = NULL;
char *resolve = NULL;
size_t max_host_conns = 0;
size_t max_total_conns = 0;
int fresh_connect = 0;
CURLcode result = CURLE_OK;
(void)URL;
while((ch = cgetopt(test_argc, test_argv, "aefhm:n:xA:F:M:P:r:T:V:"))
!= -1) {
switch(ch) {
case 'h':
usage_hx_download(NULL);
result = (CURLcode)2;
goto cleanup;
case 'a':
abort_paused = 1;
break;
case 'e':
use_earlydata = 1;
break;
case 'f':
forbid_reuse_d = 1;
break;
case 'm':
max_parallel = (size_t)strtol(coptarg, NULL, 10);
break;
case 'n':
transfer_count_d = (size_t)strtol(coptarg, NULL, 10);
break;
case 'x':
fresh_connect = 1;
break;
case 'A':
abort_offset = (size_t)strtol(coptarg, NULL, 10);
break;
case 'F':
fail_offset = (size_t)strtol(coptarg, NULL, 10);
break;
case 'M':
max_host_conns = (size_t)strtol(coptarg, NULL, 10);
break;
case 'P':
pause_offset = (size_t)strtol(coptarg, NULL, 10);
break;
case 'r':
free(resolve);
resolve = strdup(coptarg);
break;
case 'T':
max_total_conns = (size_t)strtol(coptarg, NULL, 10);
break;
case 'V': {
if(!strcmp("http/1.1", coptarg))
http_version = CURL_HTTP_VERSION_1_1;
else if(!strcmp("h2", coptarg))
http_version = CURL_HTTP_VERSION_2_0;
else if(!strcmp("h3", coptarg))
http_version = CURL_HTTP_VERSION_3ONLY;
else {
usage_hx_download("invalid http version");
result = (CURLcode)1;
goto cleanup;
}
break;
}
default:
usage_hx_download("invalid option");
result = (CURLcode)1;
goto cleanup;
}
}
test_argc -= coptind;
test_argv += coptind;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl_global_trace("ids,time,http/2,http/3");
if(test_argc != 1) {
usage_hx_download("not enough arguments");
result = (CURLcode)2;
goto cleanup;
}
url = test_argv[0];
if(resolve)
host = curl_slist_append(NULL, resolve);
share = curl_share_init();
if(!share) {
curl_mfprintf(stderr, "error allocating share\n");
result = (CURLcode)1;
goto cleanup;
}
curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
/* curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT); */
curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_PSL);
curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_HSTS);
transfer_d = calloc(transfer_count_d, sizeof(*transfer_d));
if(!transfer_d) {
curl_mfprintf(stderr, "error allocating transfer structs\n");
result = (CURLcode)1;
goto cleanup;
}
multi_handle = curl_multi_init();
curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
curl_multi_setopt(multi_handle, CURLMOPT_MAX_TOTAL_CONNECTIONS,
(long)max_total_conns);
curl_multi_setopt(multi_handle, CURLMOPT_MAX_HOST_CONNECTIONS,
(long)max_host_conns);
active_transfers = 0;
for(i = 0; i < transfer_count_d; ++i) {
t = &transfer_d[i];
t->idx = (int)i;
t->abort_at = (curl_off_t)abort_offset;
t->fail_at = (curl_off_t)fail_offset;
t->pause_at = (curl_off_t)pause_offset;
}
n = (max_parallel < transfer_count_d) ? max_parallel : transfer_count_d;
for(i = 0; i < n; ++i) {
t = &transfer_d[i];
t->easy = curl_easy_init();
if(!t->easy ||
setup_hx_download(t->easy, url, t, http_version, host, share,
use_earlydata, fresh_connect)) {
curl_mfprintf(stderr, "[t-%d] FAILED setup\n", (int)i);
result = (CURLcode)1;
goto cleanup;
}
curl_multi_add_handle(multi_handle, t->easy);
t->started = 1;
++active_transfers;
curl_mfprintf(stderr, "[t-%d] STARTED\n", t->idx);
}
do {
int still_running; /* keep number of running handles */
CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
if(still_running) {
/* wait for activity, timeout or "nothing" */
mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
}
if(mc)
break;
do {
int msgq = 0;
m = curl_multi_info_read(multi_handle, &msgq);
if(m && (m->msg == CURLMSG_DONE)) {
CURL *e = m->easy_handle;
--active_transfers;
curl_multi_remove_handle(multi_handle, e);
t = get_transfer_for_easy_d(e);
if(t) {
t->done = 1;
t->result = m->data.result;
curl_mfprintf(stderr, "[t-%d] FINISHED with result %d\n",
t->idx, t->result);
if(use_earlydata) {
curl_off_t sent;
curl_easy_getinfo(e, CURLINFO_EARLYDATA_SENT_T, &sent);
curl_mfprintf(stderr, "[t-%d] EarlyData: %ld\n", t->idx,
(long)sent);
}
}
else {
curl_easy_cleanup(e);
curl_mfprintf(stderr, "unknown FINISHED???\n");
}
}
/* nothing happening, maintenance */
if(abort_paused) {
/* abort paused transfers */
for(i = 0; i < transfer_count_d; ++i) {
t = &transfer_d[i];
if(!t->done && t->paused && t->easy) {
curl_multi_remove_handle(multi_handle, t->easy);
t->done = 1;
active_transfers--;
curl_mfprintf(stderr, "[t-%d] ABORTED\n", t->idx);
}
}
}
else {
/* resume one paused transfer */
for(i = 0; i < transfer_count_d; ++i) {
t = &transfer_d[i];
if(!t->done && t->paused) {
t->resumed = 1;
t->paused = 0;
curl_easy_pause(t->easy, CURLPAUSE_CONT);
curl_mfprintf(stderr, "[t-%d] RESUMED\n", t->idx);
break;
}
}
}
while(active_transfers < max_parallel) {
for(i = 0; i < transfer_count_d; ++i) {
t = &transfer_d[i];
if(!t->started) {
t->easy = curl_easy_init();
if(!t->easy ||
setup_hx_download(t->easy, url, t, http_version, host, share,
use_earlydata, fresh_connect)) {
curl_mfprintf(stderr, "[t-%d] FAILED setup\n", (int)i);
result = (CURLcode)1;
goto cleanup;
}
curl_multi_add_handle(multi_handle, t->easy);
t->started = 1;
++active_transfers;
curl_mfprintf(stderr, "[t-%d] STARTED\n", t->idx);
break;
}
}
/* all started */
if(i == transfer_count_d)
break;
}
} while(m);
} while(active_transfers); /* as long as we have transfers going */
curl_multi_cleanup(multi_handle);
for(i = 0; i < transfer_count_d; ++i) {
t = &transfer_d[i];
if(t->out) {
fclose(t->out);
t->out = NULL;
}
if(t->easy) {
curl_easy_cleanup(t->easy);
t->easy = NULL;
}
if(t->result)
result = t->result;
}
free(transfer_d);
curl_share_cleanup(share);
curl_slist_free_all(host);
cleanup:
free(resolve);
return result;
}

View file

@ -0,0 +1,504 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include "first.h"
#include "testtrace.h"
#include "memdebug.h"
static int verbose_u = 1;
struct transfer_u {
int idx;
CURL *easy;
const char *method;
char filename[128];
FILE *out;
curl_off_t send_total;
curl_off_t recv_size;
curl_off_t send_size;
curl_off_t fail_at;
curl_off_t pause_at;
curl_off_t abort_at;
int started;
int paused;
int resumed;
int done;
};
static size_t transfer_count_u = 1;
static struct transfer_u *transfer_u;
static int forbid_reuse_u = 0;
static struct transfer_u *get_transfer_for_easy_u(CURL *easy)
{
size_t i;
for(i = 0; i < transfer_count_u; ++i) {
if(easy == transfer_u[i].easy)
return &transfer_u[i];
}
return NULL;
}
static size_t my_write_u_cb(char *buf, size_t nitems, size_t buflen,
void *userdata)
{
struct transfer_u *t = userdata;
size_t blen = (nitems * buflen);
size_t nwritten;
curl_mfprintf(stderr, "[t-%d] RECV %ld bytes, total=%ld, pause_at=%ld\n",
t->idx, (long)blen, (long)t->recv_size, (long)t->pause_at);
if(!t->out) {
curl_msnprintf(t->filename, sizeof(t->filename)-1, "download_%u.data",
t->idx);
t->out = fopen(t->filename, "wb");
if(!t->out)
return 0;
}
nwritten = fwrite(buf, nitems, buflen, t->out);
if(nwritten < blen) {
curl_mfprintf(stderr, "[t-%d] write failure\n", t->idx);
return 0;
}
t->recv_size += (curl_off_t)nwritten;
return (size_t)nwritten;
}
static size_t my_read_cb(char *buf, size_t nitems, size_t buflen,
void *userdata)
{
struct transfer_u *t = userdata;
size_t blen = (nitems * buflen);
size_t nread;
if(t->send_total <= t->send_size)
nread = 0;
else if((t->send_total - t->send_size) < (curl_off_t)blen)
nread = (size_t)(t->send_total - t->send_size);
else
nread = blen;
curl_mfprintf(stderr, "[t-%d] SEND %ld bytes, total=%ld, pause_at=%ld\n",
t->idx, (long)nread, (long)t->send_total, (long)t->pause_at);
if(!t->resumed &&
t->send_size < t->pause_at &&
((t->send_size + (curl_off_t)blen) >= t->pause_at)) {
curl_mfprintf(stderr, "[t-%d] PAUSE\n", t->idx);
t->paused = 1;
return CURL_READFUNC_PAUSE;
}
memset(buf, 'x', nread);
t->send_size += (curl_off_t)nread;
if(t->fail_at > 0 && t->send_size >= t->fail_at) {
curl_mfprintf(stderr, "[t-%d] ABORT by read callback at %ld bytes\n",
t->idx, (long)t->send_size);
return CURL_READFUNC_ABORT;
}
return (size_t)nread;
}
static int my_progress_u_cb(void *userdata,
curl_off_t dltotal, curl_off_t dlnow,
curl_off_t ultotal, curl_off_t ulnow)
{
struct transfer_u *t = userdata;
(void)ultotal;
(void)dlnow;
(void)dltotal;
if(t->abort_at > 0 && ulnow >= t->abort_at) {
curl_mfprintf(stderr, "[t-%d] ABORT by progress_cb at %ld bytes sent\n",
t->idx, (long)ulnow);
return 1;
}
return 0;
}
static int setup_hx_upload(CURL *hnd, const char *url, struct transfer_u *t,
long http_version, struct curl_slist *host,
CURLSH *share, int use_earlydata,
int announce_length)
{
curl_easy_setopt(hnd, CURLOPT_SHARE, share);
curl_easy_setopt(hnd, CURLOPT_URL, url);
curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, http_version);
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(hnd, CURLOPT_BUFFERSIZE, (long)(128 * 1024));
curl_easy_setopt(hnd, CURLOPT_FOLLOWLOCATION, CURLFOLLOW_OBEYCODE);
curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, my_write_u_cb);
curl_easy_setopt(hnd, CURLOPT_WRITEDATA, t);
if(use_earlydata)
curl_easy_setopt(hnd, CURLOPT_SSL_OPTIONS, CURLSSLOPT_EARLYDATA);
if(!t->method || !strcmp("PUT", t->method))
curl_easy_setopt(hnd, CURLOPT_UPLOAD, 1L);
else if(!strcmp("POST", t->method))
curl_easy_setopt(hnd, CURLOPT_POST, 1L);
else {
curl_mfprintf(stderr, "unsupported method '%s'\n", t->method);
return 1;
}
curl_easy_setopt(hnd, CURLOPT_READFUNCTION, my_read_cb);
curl_easy_setopt(hnd, CURLOPT_READDATA, t);
if(announce_length)
curl_easy_setopt(hnd, CURLOPT_INFILESIZE_LARGE, t->send_total);
curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(hnd, CURLOPT_XFERINFOFUNCTION, my_progress_u_cb);
curl_easy_setopt(hnd, CURLOPT_XFERINFODATA, t);
if(forbid_reuse_u)
curl_easy_setopt(hnd, CURLOPT_FORBID_REUSE, 1L);
if(host)
curl_easy_setopt(hnd, CURLOPT_RESOLVE, host);
/* please be verbose */
if(verbose_u) {
curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, cli_debug_cb);
}
/* wait for pipe connection to confirm */
curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
return 0; /* all is good */
}
static void usage_hx_upload(const char *msg)
{
if(msg)
curl_mfprintf(stderr, "%s\n", msg);
curl_mfprintf(stderr,
"usage: [options] url\n"
" upload to a url with following options:\n"
" -a abort paused transfer\n"
" -e use TLS earlydata\n"
" -m number max parallel uploads\n"
" -n number total uploads\n"
" -A number abort transfer after `number` request body bytes\n"
" -F number fail reading request body after `number` of bytes\n"
" -P number pause transfer after `number` request body bytes\n"
" -r <host>:<port>:<addr> resolve information\n"
" -S number size to upload\n"
" -V http_version (http/1.1, h2, h3) http version to use\n"
);
}
/*
* Download a file over HTTP/2, take care of server push.
*/
static CURLcode test_cli_hx_upload(const char *URL)
{
CURLM *multi_handle;
CURLSH *share;
const char *url;
const char *method = "PUT";
size_t i, n, max_parallel = 1;
size_t active_transfers;
size_t pause_offset = 0;
size_t abort_offset = 0;
size_t fail_offset = 0;
size_t send_total = (128 * 1024);
int abort_paused = 0;
int reuse_easy = 0;
int use_earlydata = 0;
int announce_length = 0;
struct transfer_u *t;
long http_version = CURL_HTTP_VERSION_2_0;
struct curl_slist *host = NULL;
const char *resolve = NULL;
int ch;
(void)URL;
while((ch = cgetopt(test_argc, test_argv, "aefhlm:n:A:F:M:P:r:RS:V:"))
!= -1) {
switch(ch) {
case 'h':
usage_hx_upload(NULL);
return (CURLcode)2;
case 'a':
abort_paused = 1;
break;
case 'e':
use_earlydata = 1;
break;
case 'f':
forbid_reuse_u = 1;
break;
case 'l':
announce_length = 1;
break;
case 'm':
max_parallel = (size_t)strtol(coptarg, NULL, 10);
break;
case 'n':
transfer_count_u = (size_t)strtol(coptarg, NULL, 10);
break;
case 'A':
abort_offset = (size_t)strtol(coptarg, NULL, 10);
break;
case 'F':
fail_offset = (size_t)strtol(coptarg, NULL, 10);
break;
case 'M':
method = coptarg;
break;
case 'P':
pause_offset = (size_t)strtol(coptarg, NULL, 10);
break;
case 'r':
resolve = coptarg;
break;
case 'R':
reuse_easy = 1;
break;
case 'S':
send_total = (size_t)strtol(coptarg, NULL, 10);
break;
case 'V': {
if(!strcmp("http/1.1", coptarg))
http_version = CURL_HTTP_VERSION_1_1;
else if(!strcmp("h2", coptarg))
http_version = CURL_HTTP_VERSION_2_0;
else if(!strcmp("h3", coptarg))
http_version = CURL_HTTP_VERSION_3ONLY;
else {
usage_hx_upload("invalid http version");
return (CURLcode)1;
}
break;
}
default:
usage_hx_upload("invalid option");
return (CURLcode)1;
}
}
test_argc -= coptind;
test_argv += coptind;
if(max_parallel > 1 && reuse_easy) {
usage_hx_upload("cannot mix -R and -P");
return (CURLcode)2;
}
curl_global_init(CURL_GLOBAL_DEFAULT);
curl_global_trace("ids,time,http/2,http/3");
if(test_argc != 1) {
usage_hx_upload("not enough arguments");
return (CURLcode)2;
}
url = test_argv[0];
if(resolve)
host = curl_slist_append(NULL, resolve);
share = curl_share_init();
if(!share) {
curl_mfprintf(stderr, "error allocating share\n");
return (CURLcode)1;
}
curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_PSL);
curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_HSTS);
transfer_u = calloc(transfer_count_u, sizeof(*transfer_u));
if(!transfer_u) {
curl_mfprintf(stderr, "error allocating transfer structs\n");
return (CURLcode)1;
}
active_transfers = 0;
for(i = 0; i < transfer_count_u; ++i) {
t = &transfer_u[i];
t->idx = (int)i;
t->method = method;
t->send_total = (curl_off_t)send_total;
t->abort_at = (curl_off_t)abort_offset;
t->fail_at = (curl_off_t)fail_offset;
t->pause_at = (curl_off_t)pause_offset;
}
if(reuse_easy) {
CURL *easy = curl_easy_init();
CURLcode rc = CURLE_OK;
if(!easy) {
curl_mfprintf(stderr, "failed to init easy handle\n");
return (CURLcode)1;
}
for(i = 0; i < transfer_count_u; ++i) {
t = &transfer_u[i];
t->easy = easy;
if(setup_hx_upload(t->easy, url, t, http_version, host, share,
use_earlydata, announce_length)) {
curl_mfprintf(stderr, "[t-%d] FAILED setup\n", (int)i);
return (CURLcode)1;
}
curl_mfprintf(stderr, "[t-%d] STARTING\n", t->idx);
rc = curl_easy_perform(easy);
curl_mfprintf(stderr, "[t-%d] DONE -> %d\n", t->idx, rc);
t->easy = NULL;
curl_easy_reset(easy);
}
curl_easy_cleanup(easy);
}
else {
multi_handle = curl_multi_init();
curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
n = (max_parallel < transfer_count_u) ? max_parallel : transfer_count_u;
for(i = 0; i < n; ++i) {
t = &transfer_u[i];
t->easy = curl_easy_init();
if(!t->easy || setup_hx_upload(t->easy, url, t, http_version, host,
share, use_earlydata, announce_length)) {
curl_mfprintf(stderr, "[t-%d] FAILED setup\n", (int)i);
return (CURLcode)1;
}
curl_multi_add_handle(multi_handle, t->easy);
t->started = 1;
++active_transfers;
curl_mfprintf(stderr, "[t-%d] STARTED\n", t->idx);
}
do {
int still_running; /* keep number of running handles */
CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
struct CURLMsg *m;
if(still_running) {
/* wait for activity, timeout or "nothing" */
mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
}
if(mc)
break;
do {
int msgq = 0;
m = curl_multi_info_read(multi_handle, &msgq);
if(m && (m->msg == CURLMSG_DONE)) {
CURL *e = m->easy_handle;
--active_transfers;
curl_multi_remove_handle(multi_handle, e);
t = get_transfer_for_easy_u(e);
if(t) {
long res_status;
curl_easy_getinfo(e, CURLINFO_RESPONSE_CODE, &res_status);
t->done = 1;
curl_mfprintf(stderr, "[t-%d] FINISHED, result=%d, response=%ld\n",
t->idx, m->data.result, res_status);
if(use_earlydata) {
curl_off_t sent;
curl_easy_getinfo(e, CURLINFO_EARLYDATA_SENT_T, &sent);
curl_mfprintf(stderr, "[t-%d] EarlyData: %ld\n", t->idx,
(long)sent);
}
}
else {
curl_easy_cleanup(e);
curl_mfprintf(stderr, "unknown FINISHED???\n");
}
}
/* nothing happening, maintenance */
if(abort_paused) {
/* abort paused transfers */
for(i = 0; i < transfer_count_u; ++i) {
t = &transfer_u[i];
if(!t->done && t->paused && t->easy) {
curl_multi_remove_handle(multi_handle, t->easy);
t->done = 1;
active_transfers--;
curl_mfprintf(stderr, "[t-%d] ABORTED\n", t->idx);
}
}
}
else {
/* resume one paused transfer */
for(i = 0; i < transfer_count_u; ++i) {
t = &transfer_u[i];
if(!t->done && t->paused) {
t->resumed = 1;
t->paused = 0;
curl_easy_pause(t->easy, CURLPAUSE_CONT);
curl_mfprintf(stderr, "[t-%d] RESUMED\n", t->idx);
break;
}
}
}
while(active_transfers < max_parallel) {
for(i = 0; i < transfer_count_u; ++i) {
t = &transfer_u[i];
if(!t->started) {
t->easy = curl_easy_init();
if(!t->easy || setup_hx_upload(t->easy, url, t, http_version,
host, share, use_earlydata,
announce_length)) {
curl_mfprintf(stderr, "[t-%d] FAILED setup\n", (int)i);
return (CURLcode)1;
}
curl_multi_add_handle(multi_handle, t->easy);
t->started = 1;
++active_transfers;
curl_mfprintf(stderr, "[t-%d] STARTED\n", t->idx);
break;
}
}
/* all started */
if(i == transfer_count_u)
break;
}
} while(m);
} while(active_transfers); /* as long as we have transfers going */
curl_multi_cleanup(multi_handle);
}
for(i = 0; i < transfer_count_u; ++i) {
t = &transfer_u[i];
if(t->out) {
fclose(t->out);
t->out = NULL;
}
if(t->easy) {
curl_easy_cleanup(t->easy);
t->easy = NULL;
}
}
free(transfer_u);
curl_share_cleanup(share);
return CURLE_OK;
}

View file

@ -0,0 +1,261 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include "first.h"
#include "testtrace.h"
#include "memdebug.h"
static int tse_found_tls_session = FALSE;
static size_t write_tse_cb(char *ptr, size_t size, size_t nmemb, void *opaque)
{
CURL *easy = opaque;
(void)ptr;
if(!tse_found_tls_session) {
struct curl_tlssessioninfo *tlssession;
CURLcode rc;
rc = curl_easy_getinfo(easy, CURLINFO_TLS_SSL_PTR, &tlssession);
if(rc) {
curl_mfprintf(stderr, "curl_easy_getinfo(CURLINFO_TLS_SSL_PTR) "
"failed: %s\n", curl_easy_strerror(rc));
return rc;
}
if(tlssession->backend == CURLSSLBACKEND_NONE) {
curl_mfprintf(stderr, "curl_easy_getinfo(CURLINFO_TLS_SSL_PTR) "
"gave no backend\n");
return CURLE_FAILED_INIT;
}
if(!tlssession->internals) {
curl_mfprintf(stderr, "curl_easy_getinfo(CURLINFO_TLS_SSL_PTR) "
"missing\n");
return CURLE_FAILED_INIT;
}
tse_found_tls_session = TRUE;
}
return size * nmemb;
}
static CURL *tse_add_transfer(CURLM *multi, CURLSH *share,
struct curl_slist *resolve,
const char *url, long http_version)
{
CURL *easy;
CURLMcode mc;
easy = curl_easy_init();
if(!easy) {
curl_mfprintf(stderr, "curl_easy_init failed\n");
return NULL;
}
curl_easy_setopt(easy, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(easy, CURLOPT_DEBUGFUNCTION, cli_debug_cb);
curl_easy_setopt(easy, CURLOPT_URL, url);
curl_easy_setopt(easy, CURLOPT_SHARE, share);
curl_easy_setopt(easy, CURLOPT_NOSIGNAL, 1L);
curl_easy_setopt(easy, CURLOPT_AUTOREFERER, 1L);
curl_easy_setopt(easy, CURLOPT_FAILONERROR, 1L);
curl_easy_setopt(easy, CURLOPT_HTTP_VERSION, http_version);
curl_easy_setopt(easy, CURLOPT_WRITEFUNCTION, write_tse_cb);
curl_easy_setopt(easy, CURLOPT_WRITEDATA, easy);
curl_easy_setopt(easy, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(easy, CURLOPT_SSL_VERIFYPEER, 0L);
if(resolve)
curl_easy_setopt(easy, CURLOPT_RESOLVE, resolve);
mc = curl_multi_add_handle(multi, easy);
if(mc != CURLM_OK) {
curl_mfprintf(stderr, "curl_multi_add_handle: %s\n",
curl_multi_strerror(mc));
curl_easy_cleanup(easy);
return NULL;
}
return easy;
}
static CURLcode test_cli_tls_session_reuse(const char *URL)
{
CURLM *multi = NULL;
CURLMcode mc;
int running_handles = 0, numfds;
CURLMsg *msg;
CURLSH *share = NULL;
CURLU *cu;
struct curl_slist *resolve = NULL;
char resolve_buf[1024];
int msgs_in_queue;
int add_more, waits, ongoing = 0;
char *host = NULL, *port = NULL;
long http_version = CURL_HTTP_VERSION_1_1;
CURLcode exitcode = (CURLcode)1;
if(!URL || !libtest_arg2) {
curl_mfprintf(stderr, "need args: URL proto\n");
return (CURLcode)2;
}
if(!strcmp("h2", libtest_arg2))
http_version = CURL_HTTP_VERSION_2;
else if(!strcmp("h3", libtest_arg2))
http_version = CURL_HTTP_VERSION_3ONLY;
cu = curl_url();
if(!cu) {
curl_mfprintf(stderr, "out of memory\n");
return (CURLcode)1;
}
if(curl_url_set(cu, CURLUPART_URL, URL, 0)) {
curl_mfprintf(stderr, "not a URL: '%s'\n", URL);
goto cleanup;
}
if(curl_url_get(cu, CURLUPART_HOST, &host, 0)) {
curl_mfprintf(stderr, "could not get host of '%s'\n", URL);
goto cleanup;
}
if(curl_url_get(cu, CURLUPART_PORT, &port, 0)) {
curl_mfprintf(stderr, "could not get port of '%s'\n", URL);
goto cleanup;
}
curl_msnprintf(resolve_buf, sizeof(resolve_buf)-1, "%s:%s:127.0.0.1",
host, port);
resolve = curl_slist_append(resolve, resolve_buf);
multi = curl_multi_init();
if(!multi) {
curl_mfprintf(stderr, "curl_multi_init failed\n");
goto cleanup;
}
share = curl_share_init();
if(!share) {
curl_mfprintf(stderr, "curl_share_init failed\n");
goto cleanup;
}
curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
if(!tse_add_transfer(multi, share, resolve, URL, http_version))
goto cleanup;
++ongoing;
add_more = 6;
waits = 3;
do {
mc = curl_multi_perform(multi, &running_handles);
if(mc != CURLM_OK) {
curl_mfprintf(stderr, "curl_multi_perform: %s\n",
curl_multi_strerror(mc));
goto cleanup;
}
if(running_handles) {
mc = curl_multi_poll(multi, NULL, 0, 1000000, &numfds);
if(mc != CURLM_OK) {
curl_mfprintf(stderr, "curl_multi_poll: %s\n",
curl_multi_strerror(mc));
goto cleanup;
}
}
if(waits) {
--waits;
}
else {
while(add_more) {
if(!tse_add_transfer(multi, share, resolve, URL, http_version))
goto cleanup;
++ongoing;
--add_more;
}
}
/* Check for finished handles and remove. */
/* !checksrc! disable EQUALSNULL 1 */
while((msg = curl_multi_info_read(multi, &msgs_in_queue)) != NULL) {
if(msg->msg == CURLMSG_DONE) {
long status = 0;
curl_off_t xfer_id;
curl_easy_getinfo(msg->easy_handle, CURLINFO_XFER_ID, &xfer_id);
curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &status);
if(msg->data.result == CURLE_SEND_ERROR ||
msg->data.result == CURLE_RECV_ERROR) {
/* We get these if the server had a GOAWAY in transit on
* reusing a connection */
}
else if(msg->data.result) {
curl_mfprintf(stderr, "transfer #%" CURL_FORMAT_CURL_OFF_T
": failed with %d\n", xfer_id, msg->data.result);
goto cleanup;
}
else if(status != 200) {
curl_mfprintf(stderr, "transfer #%" CURL_FORMAT_CURL_OFF_T
": wrong http status %ld (expected 200)\n", xfer_id,
status);
goto cleanup;
}
curl_multi_remove_handle(multi, msg->easy_handle);
curl_easy_cleanup(msg->easy_handle);
--ongoing;
curl_mfprintf(stderr, "transfer #%" CURL_FORMAT_CURL_OFF_T" retiring "
"(%d now running)\n", xfer_id, running_handles);
}
}
curl_mfprintf(stderr, "running_handles=%d, yet_to_start=%d\n",
running_handles, add_more);
} while(ongoing || add_more);
if(!tse_found_tls_session) {
curl_mfprintf(stderr, "CURLINFO_TLS_SSL_PTR not found during run\n");
exitcode = CURLE_FAILED_INIT;
goto cleanup;
}
curl_mfprintf(stderr, "exiting\n");
exitcode = CURLE_OK;
cleanup:
if(multi) {
CURL **list = curl_multi_get_handles(multi);
if(list) {
int i;
for(i = 0; list[i]; i++) {
curl_multi_remove_handle(multi, list[i]);
curl_easy_cleanup(list[i]);
}
curl_free(list);
}
curl_multi_cleanup(multi);
}
curl_share_cleanup(share);
curl_slist_free_all(resolve);
curl_free(host);
curl_free(port);
curl_url_cleanup(cu);
return exitcode;
}

View file

@ -0,0 +1,203 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* This is based on the PoC client of issue #11769
*/
#include "first.h"
#include "testtrace.h"
#include "memdebug.h"
static size_t total_read = 0;
static size_t read_callback(char *ptr, size_t size, size_t nmemb,
void *userdata)
{
static const size_t PAUSE_READ_AFTER = 1;
(void)size;
(void)nmemb;
(void)userdata;
if(total_read >= PAUSE_READ_AFTER) {
curl_mfprintf(stderr, "read_callback, return PAUSE\n");
return CURL_READFUNC_PAUSE;
}
else {
ptr[0] = '\n';
++total_read;
curl_mfprintf(stderr, "read_callback, return 1 byte\n");
return 1;
}
}
static int progress_callback(void *clientp,
curl_off_t dltotal,
curl_off_t dlnow,
curl_off_t ultotal,
curl_off_t ulnow)
{
(void)dltotal;
(void)dlnow;
(void)ultotal;
(void)ulnow;
(void)clientp;
#if 0
/* Used to unpause on progress, but keeping for now. */
{
CURL *curl = (CURL *)clientp;
curl_easy_pause(curl, CURLPAUSE_CONT);
/* curl_easy_pause(curl, CURLPAUSE_RECV_CONT); */
}
#endif
return 0;
}
static void usage_upload_pausing(const char *msg)
{
if(msg)
curl_mfprintf(stderr, "%s\n", msg);
curl_mfprintf(stderr,
"usage: [options] url\n"
" upload and pause, options:\n"
" -V http_version (http/1.1, h2, h3) http version to use\n"
);
}
static CURLcode test_cli_upload_pausing(const char *URL)
{
CURL *curl;
CURLcode rc = CURLE_OK;
CURLU *cu;
struct curl_slist *resolve = NULL;
char resolve_buf[1024];
const char *url;
char *host = NULL, *port = NULL;
long http_version = CURL_HTTP_VERSION_1_1;
int ch;
(void)URL;
while((ch = cgetopt(test_argc, test_argv, "V:")) != -1) {
switch(ch) {
case 'V': {
if(!strcmp("http/1.1", coptarg))
http_version = CURL_HTTP_VERSION_1_1;
else if(!strcmp("h2", coptarg))
http_version = CURL_HTTP_VERSION_2_0;
else if(!strcmp("h3", coptarg))
http_version = CURL_HTTP_VERSION_3ONLY;
else {
usage_upload_pausing("invalid http version");
return (CURLcode)1;
}
break;
}
default:
usage_upload_pausing("invalid option");
return (CURLcode)1;
}
}
test_argc -= coptind;
test_argv += coptind;
if(test_argc != 1) {
usage_upload_pausing("not enough arguments");
return (CURLcode)2;
}
url = test_argv[0];
curl_global_init(CURL_GLOBAL_DEFAULT);
curl_global_trace("ids,time");
cu = curl_url();
if(!cu) {
curl_mfprintf(stderr, "out of memory\n");
return (CURLcode)1;
}
if(curl_url_set(cu, CURLUPART_URL, url, 0)) {
curl_mfprintf(stderr, "not a URL: '%s'\n", url);
return (CURLcode)1;
}
if(curl_url_get(cu, CURLUPART_HOST, &host, 0)) {
curl_mfprintf(stderr, "could not get host of '%s'\n", url);
return (CURLcode)1;
}
if(curl_url_get(cu, CURLUPART_PORT, &port, 0)) {
curl_mfprintf(stderr, "could not get port of '%s'\n", url);
return (CURLcode)1;
}
memset(&resolve, 0, sizeof(resolve));
curl_msnprintf(resolve_buf, sizeof(resolve_buf)-1, "%s:%s:127.0.0.1",
host, port);
resolve = curl_slist_append(resolve, resolve_buf);
curl = curl_easy_init();
if(!curl) {
curl_mfprintf(stderr, "out of memory\n");
return (CURLcode)1;
}
/* We want to use our own read function. */
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
/* It will help us to continue the read function. */
curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progress_callback);
curl_easy_setopt(curl, CURLOPT_XFERINFODATA, curl);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
/* It will help us to ensure that keepalive does not help. */
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
curl_easy_setopt(curl, CURLOPT_TCP_KEEPIDLE, 1L);
curl_easy_setopt(curl, CURLOPT_TCP_KEEPINTVL, 1L);
curl_easy_setopt(curl, CURLOPT_TCP_KEEPCNT, 1L);
/* Enable uploading. */
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
if(curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L) != CURLE_OK ||
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, cli_debug_cb) != CURLE_OK ||
curl_easy_setopt(curl, CURLOPT_RESOLVE, resolve) != CURLE_OK) {
curl_mfprintf(stderr, "something unexpected went wrong - bailing out!\n");
return (CURLcode)2;
}
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, http_version);
rc = curl_easy_perform(curl);
if(curl) {
curl_easy_cleanup(curl);
}
curl_slist_free_all(resolve);
curl_free(host);
curl_free(port);
curl_url_cleanup(cu);
curl_global_cleanup();
return rc;
}

260
tests/libtest/cli_ws_data.c Normal file
View file

@ -0,0 +1,260 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include "first.h"
#include "testtrace.h"
#include "memdebug.h"
#ifndef CURL_DISABLE_WEBSOCKETS
static CURLcode check_recv(const struct curl_ws_frame *frame,
size_t r_offset, size_t nread, size_t exp_len)
{
if(!frame)
return CURLE_OK;
if(frame->flags & CURLWS_CLOSE) {
curl_mfprintf(stderr, "recv_data: unexpected CLOSE frame from server, "
"got %ld bytes, offset=%ld, rflags %x\n",
(long)nread, (long)r_offset, frame->flags);
return CURLE_RECV_ERROR;
}
if(!r_offset && !(frame->flags & CURLWS_BINARY)) {
curl_mfprintf(stderr, "recv_data: wrong frame, got %ld bytes, offset=%ld, "
"rflags %x\n",
(long)nread, (long)r_offset, frame->flags);
return CURLE_RECV_ERROR;
}
if(frame->offset != (curl_off_t)r_offset) {
curl_mfprintf(stderr, "recv_data: frame offset, expected %ld, got %ld\n",
(long)r_offset, (long)frame->offset);
return CURLE_RECV_ERROR;
}
if(frame->bytesleft != (curl_off_t)(exp_len - r_offset - nread)) {
curl_mfprintf(stderr, "recv_data: frame bytesleft, "
"expected %ld, got %ld\n",
(long)(exp_len - r_offset - nread), (long)frame->bytesleft);
return CURLE_RECV_ERROR;
}
if(r_offset + nread > exp_len) {
curl_mfprintf(stderr, "recv_data: data length, expected %ld, now at %ld\n",
(long)exp_len, (long)(r_offset + nread));
return CURLE_RECV_ERROR;
}
return CURLE_OK;
}
static CURLcode data_echo(CURL *curl, size_t count,
size_t plen_min, size_t plen_max)
{
CURLcode r = CURLE_OK;
const struct curl_ws_frame *frame;
size_t len;
char *send_buf = NULL, *recv_buf = NULL;
size_t i, scount = count, rcount = count;
int rblock, sblock;
send_buf = calloc(1, plen_max + 1);
recv_buf = calloc(1, plen_max + 1);
if(!send_buf || !recv_buf) {
r = CURLE_OUT_OF_MEMORY;
goto out;
}
for(i = 0; i < plen_max; ++i) {
send_buf[i] = (char)('0' + ((int)i % 10));
}
for(len = plen_min; len <= plen_max; ++len) {
size_t nwritten, nread, slen = len, rlen = len;
char *sbuf = send_buf, *rbuf = recv_buf;
memset(recv_buf, 0, plen_max);
while(slen || rlen || scount || rcount) {
sblock = rblock = 1;
if(slen) {
r = curl_ws_send(curl, sbuf, slen, &nwritten, 0, CURLWS_BINARY);
sblock = (r == CURLE_AGAIN);
if(!r || (r == CURLE_AGAIN)) {
curl_mfprintf(stderr, "curl_ws_send(len=%ld) -> %d, %ld (%ld/%ld)\n",
(long)slen, r, (long)nwritten,
(long)(len - slen), (long)len);
sbuf += nwritten;
slen -= nwritten;
}
else
goto out;
}
if(!slen && scount) { /* go again? */
scount--;
sbuf = send_buf;
slen = len;
}
if(rlen) {
size_t max_recv = (64 * 1024);
r = curl_ws_recv(curl, rbuf, (rlen > max_recv) ? max_recv : rlen,
&nread, &frame);
if(!r || (r == CURLE_AGAIN)) {
rblock = (r == CURLE_AGAIN);
curl_mfprintf(stderr, "curl_ws_recv(len=%ld) -> %d, %ld (%ld/%ld) "
"\n",
(long)rlen, r, (long)nread, (long)(len - rlen),
(long)len);
if(!r) {
r = check_recv(frame, len - rlen, nread, len);
if(r)
goto out;
}
rbuf += nread;
rlen -= nread;
}
else
goto out;
}
if(!rlen && rcount) { /* go again? */
rcount--;
rbuf = recv_buf;
rlen = len;
}
if(rblock && sblock) {
curl_mfprintf(stderr, "EAGAIN, sleep, try again\n");
curlx_wait_ms(100);
}
}
if(memcmp(send_buf, recv_buf, len)) {
curl_mfprintf(stderr, "recv_data: data differs\n");
debug_dump("", "expected:", stderr,
(const unsigned char *)send_buf, len, 0);
debug_dump("", "received:", stderr,
(const unsigned char *)recv_buf, len, 0);
r = CURLE_RECV_ERROR;
goto out;
}
}
out:
if(!r)
ws_close(curl);
free(send_buf);
free(recv_buf);
return r;
}
static void usage_ws_data(const char *msg)
{
if(msg)
curl_mfprintf(stderr, "%s\n", msg);
curl_mfprintf(stderr,
"usage: [options] url\n"
" -m number minimum frame size\n"
" -M number maximum frame size\n"
);
}
#endif
static CURLcode test_cli_ws_data(const char *URL)
{
#ifndef CURL_DISABLE_WEBSOCKETS
CURL *curl;
CURLcode res = CURLE_OK;
const char *url;
size_t plen_min = 0, plen_max = 0, count = 1;
int ch;
(void)URL;
while((ch = cgetopt(test_argc, test_argv, "c:hm:M:")) != -1) {
switch(ch) {
case 'h':
usage_ws_data(NULL);
res = CURLE_BAD_FUNCTION_ARGUMENT;
goto cleanup;
case 'c':
count = (size_t)strtol(coptarg, NULL, 10);
break;
case 'm':
plen_min = (size_t)strtol(coptarg, NULL, 10);
break;
case 'M':
plen_max = (size_t)strtol(coptarg, NULL, 10);
break;
default:
usage_ws_data("invalid option");
res = CURLE_BAD_FUNCTION_ARGUMENT;
goto cleanup;
}
}
test_argc -= coptind;
test_argv += coptind;
if(!plen_max)
plen_max = plen_min;
if(plen_max < plen_min) {
curl_mfprintf(stderr, "maxlen must be >= minlen, got %ld-%ld\n",
(long)plen_min, (long)plen_max);
res = CURLE_BAD_FUNCTION_ARGUMENT;
goto cleanup;
}
if(test_argc != 1) {
usage_ws_data(NULL);
res = CURLE_BAD_FUNCTION_ARGUMENT;
goto cleanup;
}
url = test_argv[0];
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
/* use the callback style */
curl_easy_setopt(curl, CURLOPT_USERAGENT, "ws-data");
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 2L); /* websocket style */
res = curl_easy_perform(curl);
curl_mfprintf(stderr, "curl_easy_perform() returned %u\n", (int)res);
if(res == CURLE_OK)
res = data_echo(curl, count, plen_min, plen_max);
/* always cleanup */
curl_easy_cleanup(curl);
}
cleanup:
curl_global_cleanup();
return res;
#else /* !CURL_DISABLE_WEBSOCKETS */
(void)URL;
curl_mfprintf(stderr, "WebSockets not enabled in libcurl\n");
return (CURLcode)1;
#endif /* CURL_DISABLE_WEBSOCKETS */
}

View file

@ -0,0 +1,94 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include "first.h"
#include "testtrace.h"
#include "memdebug.h"
#ifndef CURL_DISABLE_WEBSOCKETS
static CURLcode pingpong(CURL *curl, const char *payload)
{
CURLcode res;
int i;
res = ws_send_ping(curl, payload);
if(res)
return res;
for(i = 0; i < 10; ++i) {
curl_mfprintf(stderr, "Receive pong\n");
res = ws_recv_pong(curl, payload);
if(res == CURLE_AGAIN) {
curlx_wait_ms(100);
continue;
}
ws_close(curl);
return res;
}
ws_close(curl);
return CURLE_RECV_ERROR;
}
#endif
static CURLcode test_cli_ws_pingpong(const char *URL)
{
#ifndef CURL_DISABLE_WEBSOCKETS
CURL *curl;
CURLcode res = CURLE_OK;
const char *payload;
if(!URL || !libtest_arg2) {
curl_mfprintf(stderr, "need args: URL payload\n");
return (CURLcode)2;
}
payload = libtest_arg2;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, URL);
/* use the callback style */
curl_easy_setopt(curl, CURLOPT_USERAGENT, "ws-pingpong");
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 2L); /* websocket style */
res = curl_easy_perform(curl);
curl_mfprintf(stderr, "curl_easy_perform() returned %u\n", (int)res);
if(res == CURLE_OK)
res = pingpong(curl, payload);
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return res;
#else /* !CURL_DISABLE_WEBSOCKETS */
(void)URL;
curl_mfprintf(stderr, "WebSockets not enabled in libcurl\n");
return (CURLcode)1;
#endif /* CURL_DISABLE_WEBSOCKETS */
}

View file

@ -61,6 +61,69 @@ struct curltime tv_test_start; /* for test timing */
int unitfail; /* for unittests */
int coptind;
const char *coptarg;
int cgetopt(int argc, const char * const argv[], const char *optstring)
{
static int optpos = 1;
int coptopt;
const char *arg;
if(coptind == 0) { /* Reset? */
coptind = !!argc;
optpos = 1;
}
arg = argv[coptind];
if(arg && strcmp(arg, "--") == 0) {
coptind++;
return -1;
}
else if(!arg || arg[0] != '-') {
return -1;
}
else {
const char *opt = strchr(optstring, arg[optpos]);
coptopt = arg[optpos];
if(!opt) {
if(!arg[++optpos]) {
coptind++;
optpos = 1;
}
return '?';
}
else if(opt[1] == ':') {
if(arg[optpos + 1]) {
coptarg = arg + optpos + 1;
coptind++;
optpos = 1;
return coptopt;
}
else if(argv[coptind + 1]) {
coptarg = argv[coptind + 1];
coptind += 2;
optpos = 1;
return coptopt;
}
else {
if(!arg[++optpos]) {
coptind++;
optpos = 1;
}
return *optstring == ':' ? ':' : '?';
}
}
else {
if(!arg[++optpos]) {
coptind++;
optpos = 1;
}
return coptopt;
}
}
}
#ifdef CURLDEBUG
static void memory_tracking_init(void)
{
@ -97,10 +160,59 @@ char *hexdump(const unsigned char *buf, size_t len)
return dump;
}
#ifndef CURL_DISABLE_WEBSOCKETS
CURLcode ws_send_ping(CURL *curl, const char *send_payload)
{
size_t sent;
CURLcode result = curl_ws_send(curl, send_payload, strlen(send_payload),
&sent, 0, CURLWS_PING);
curl_mfprintf(stderr, "ws: curl_ws_send returned %u, sent %zu\n",
result, sent);
return result;
}
CURLcode ws_recv_pong(CURL *curl, const char *expected_payload)
{
size_t rlen;
const struct curl_ws_frame *meta;
char buffer[256];
CURLcode result = curl_ws_recv(curl, buffer, sizeof(buffer), &rlen, &meta);
if(result) {
curl_mfprintf(stderr, "ws: curl_ws_recv returned %u, received %zd\n",
result, rlen);
return result;
}
if(!(meta->flags & CURLWS_PONG)) {
curl_mfprintf(stderr, "recv_pong: wrong frame, got %zd bytes rflags %x\n",
rlen, meta->flags);
return CURLE_RECV_ERROR;
}
curl_mfprintf(stderr, "ws: got PONG back\n");
if(rlen == strlen(expected_payload) &&
!memcmp(expected_payload, buffer, rlen)) {
curl_mfprintf(stderr, "ws: got the same payload back\n");
return CURLE_OK; /* lib2304 returned 'result' here. Intentional? */
}
curl_mfprintf(stderr, "ws: did NOT get the same payload back\n");
return CURLE_RECV_ERROR;
}
/* just close the connection */
void ws_close(CURL *curl)
{
size_t sent;
CURLcode result = curl_ws_send(curl, "", 0, &sent, 0, CURLWS_CLOSE);
curl_mfprintf(stderr, "ws: curl_ws_send returned %u, sent %zu\n",
result, sent);
}
#endif /* CURL_DISABLE_WEBSOCKETS */
int main(int argc, const char **argv)
{
const char *URL;
const char *URL = "";
CURLcode result;
entry_func_t entry_func;
const char *entry_name;
@ -126,8 +238,9 @@ int main(int argc, const char **argv)
test_argc = argc - 1;
test_argv = argv + 1;
if(argc < 3) {
curl_mfprintf(stderr, "Pass testname and URL as arguments please\n");
if(argc < 2) {
curl_mfprintf(stderr, "Pass testname "
"(and URL as argument for numbered tests) please\n");
return 1;
}
@ -145,6 +258,11 @@ int main(int argc, const char **argv)
return 1;
}
if(argc > 2) {
URL = argv[2];
curl_mfprintf(stderr, "URL: %s\n", URL);
}
if(argc > 3)
libtest_arg2 = argv[3];
@ -154,16 +272,12 @@ int main(int argc, const char **argv)
if(argc > 5)
libtest_arg4 = argv[5];
URL = argv[2]; /* provide this to the rest */
env = getenv("CURL_TESTNUM");
if(env)
testnum = atoi(env);
else
testnum = 0;
curl_mfprintf(stderr, "URL: %s\n", URL);
result = entry_func(URL);
curl_mfprintf(stderr, "Test ended with result %d\n", result);

View file

@ -79,11 +79,22 @@ extern const char **test_argv;
extern int testnum;
extern struct curltime tv_test_start; /* for test timing */
extern int coptind;
extern const char *coptarg;
int cgetopt(int argc, const char * const argv[], const char *optstring);
extern int select_wrapper(int nfds, fd_set *rd, fd_set *wr, fd_set *exc,
struct timeval *tv);
extern char *hexdump(const unsigned char *buffer, size_t len);
#ifndef CURL_DISABLE_WEBSOCKETS
CURLcode ws_send_ping(CURL *curl, const char *send_payload);
CURLcode ws_recv_pong(CURL *curl, const char *expected_payload);
/* just close the connection */
void ws_close(CURL *curl);
#endif
/*
** TEST_ERR_* values must within the CURLcode range to not cause compiler
** errors.

View file

@ -52,9 +52,9 @@ static CURLcode do_one_request(CURLM *m, const char *URL, const char *resolve)
easy_setopt(curls, CURLOPT_RESOLVE, resolve_list);
easy_setopt(curls, CURLOPT_DNS_CACHE_TIMEOUT, DNS_TIMEOUT);
libtest_debug_config.nohex = 1;
libtest_debug_config.tracetime = 1;
easy_setopt(curls, CURLOPT_DEBUGDATA, &libtest_debug_config);
debug_config.nohex = 1;
debug_config.tracetime = 1;
easy_setopt(curls, CURLOPT_DEBUGDATA, &debug_config);
easy_setopt(curls, CURLOPT_DEBUGFUNCTION, libtest_debug_cb);
easy_setopt(curls, CURLOPT_VERBOSE, 1L);

View file

@ -60,9 +60,9 @@ static CURLcode test_lib1522(const char *URL)
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, g_Data);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)sizeof(g_Data));
libtest_debug_config.nohex = 1;
libtest_debug_config.tracetime = 1;
test_setopt(curl, CURLOPT_DEBUGDATA, &libtest_debug_config);
debug_config.nohex = 1;
debug_config.tracetime = 1;
test_setopt(curl, CURLOPT_DEBUGDATA, &debug_config);
test_setopt(curl, CURLOPT_DEBUGFUNCTION, libtest_debug_cb);
test_setopt(curl, CURLOPT_VERBOSE, 1L);

View file

@ -106,9 +106,9 @@ static CURLcode test_lib1540(const char *URL)
easy_setopt(curls, CURLOPT_XFERINFODATA, &st);
easy_setopt(curls, CURLOPT_NOPROGRESS, 0L);
libtest_debug_config.nohex = 1;
libtest_debug_config.tracetime = 1;
test_setopt(curls, CURLOPT_DEBUGDATA, &libtest_debug_config);
debug_config.nohex = 1;
debug_config.tracetime = 1;
test_setopt(curls, CURLOPT_DEBUGDATA, &debug_config);
easy_setopt(curls, CURLOPT_DEBUGFUNCTION, libtest_debug_cb);
easy_setopt(curls, CURLOPT_VERBOSE, 1L);

View file

@ -46,9 +46,9 @@ static CURLcode test_lib1542(const char *URL)
easy_setopt(easy, CURLOPT_URL, URL);
libtest_debug_config.nohex = 1;
libtest_debug_config.tracetime = 0;
easy_setopt(easy, CURLOPT_DEBUGDATA, &libtest_debug_config);
debug_config.nohex = 1;
debug_config.tracetime = 0;
easy_setopt(easy, CURLOPT_DEBUGDATA, &debug_config);
easy_setopt(easy, CURLOPT_DEBUGFUNCTION, libtest_debug_cb);
easy_setopt(easy, CURLOPT_VERBOSE, 1L);

View file

@ -71,9 +71,9 @@ static CURLcode test_lib1553(const char *URL)
easy_setopt(curls, CURLOPT_XFERINFOFUNCTION, t1553_xferinfo);
easy_setopt(curls, CURLOPT_NOPROGRESS, 1L);
libtest_debug_config.nohex = 1;
libtest_debug_config.tracetime = 1;
test_setopt(curls, CURLOPT_DEBUGDATA, &libtest_debug_config);
debug_config.nohex = 1;
debug_config.tracetime = 1;
test_setopt(curls, CURLOPT_DEBUGDATA, &debug_config);
easy_setopt(curls, CURLOPT_DEBUGFUNCTION, libtest_debug_cb);
easy_setopt(curls, CURLOPT_VERBOSE, 1L);

View file

@ -103,8 +103,8 @@ static CURLcode test_lib1915(const char *URL)
global_init(CURL_GLOBAL_ALL);
libtest_debug_config.nohex = 1;
libtest_debug_config.tracetime = 1;
debug_config.nohex = 1;
debug_config.tracetime = 1;
easy_init(hnd);
easy_setopt(hnd, CURLOPT_URL, URL);
@ -114,7 +114,7 @@ static CURLcode test_lib1915(const char *URL)
easy_setopt(hnd, CURLOPT_HSTSWRITEFUNCTION, hstswrite);
easy_setopt(hnd, CURLOPT_HSTSWRITEDATA, &st);
easy_setopt(hnd, CURLOPT_HSTS_CTRL, CURLHSTS_ENABLE);
easy_setopt(hnd, CURLOPT_DEBUGDATA, &libtest_debug_config);
easy_setopt(hnd, CURLOPT_DEBUGDATA, &debug_config);
easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, libtest_debug_cb);
easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(hnd);
@ -133,7 +133,7 @@ static CURLcode test_lib1915(const char *URL)
easy_setopt(hnd, CURLOPT_HSTSWRITEFUNCTION, hstswrite);
easy_setopt(hnd, CURLOPT_HSTSWRITEDATA, &st);
easy_setopt(hnd, CURLOPT_HSTS_CTRL, CURLHSTS_ENABLE);
easy_setopt(hnd, CURLOPT_DEBUGDATA, &libtest_debug_config);
easy_setopt(hnd, CURLOPT_DEBUGDATA, &debug_config);
easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, libtest_debug_cb);
easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(hnd);

View file

@ -25,70 +25,19 @@
#ifndef CURL_DISABLE_WEBSOCKETS
#if 0
static CURLcode t2301_send_ping(CURL *curl, const char *send_payload)
{
size_t sent;
CURLcode result =
curl_ws_send(curl, send_payload, strlen(send_payload), &sent, CURLWS_PING);
curl_mfprintf(stderr,
"ws: curl_ws_send returned %d, sent %d\n", result, (int)sent);
return result;
}
static CURLcode t2301_recv_pong(CURL *curl, const char *expected_payload)
{
size_t rlen;
unsigned int rflags;
char buffer[256];
CURLcode result =
curl_ws_recv(curl, buffer, sizeof(buffer), &rlen, &rflags);
if(rflags & CURLWS_PONG) {
int same = 0;
curl_mfprintf(stderr, "ws: got PONG back\n");
if(rlen == strlen(expected_payload)) {
if(!memcmp(expected_payload, buffer, rlen)) {
curl_mfprintf(stderr, "ws: got the same payload back\n");
same = 1;
}
}
if(!same)
curl_mfprintf(stderr, "ws: did NOT get the same payload back\n");
}
else {
curl_mfprintf(stderr, "recv_pong: got %d bytes rflags %x\n",
(int)rlen, rflags);
}
curl_mfprintf(stderr, "ws: curl_ws_recv returned %d, received %d\n", result,
(int)rlen);
return result;
}
/* just close the connection */
static void t2301_websocket_close(CURL *curl)
{
size_t sent;
CURLcode result =
curl_ws_send(curl, "", 0, &sent, CURLWS_CLOSE);
curl_mfprintf(stderr,
"ws: curl_ws_send returned %d, sent %d\n", result, (int)sent);
}
static void t2301_websocket(CURL *curl)
{
int i = 0;
curl_mfprintf(stderr, "ws: websocket() starts\n");
do {
if(t2301_send_ping(curl, "foobar"))
if(ws_send_ping(curl, "foobar"))
return;
if(t2301_recv_pong(curl, "foobar"))
if(ws_recv_pong(curl, "foobar"))
return;
curlx_wait_ms(2000);
} while(i++ < 10);
t2301_websocket_close(curl);
ws_close(curl);
}
#endif
static size_t t2301_write_cb(char *b, size_t size, size_t nitems, void *p)

View file

@ -24,48 +24,6 @@
#include "first.h"
#ifndef CURL_DISABLE_WEBSOCKETS
static CURLcode t2304_send_ping(CURL *curl, const char *send_payload)
{
size_t sent;
CURLcode result =
curl_ws_send(curl, send_payload, strlen(send_payload), &sent, 0,
CURLWS_PING);
curl_mfprintf(stderr,
"ws: curl_ws_send returned %d, sent %d\n", result, (int)sent);
return result;
}
static CURLcode t2304_recv_pong(CURL *curl, const char *expected_payload)
{
size_t rlen;
const struct curl_ws_frame *meta;
char buffer[256];
CURLcode result = curl_ws_recv(curl, buffer, sizeof(buffer), &rlen, &meta);
if(!result) {
if(meta->flags & CURLWS_PONG) {
int same = 0;
curl_mfprintf(stderr, "ws: got PONG back\n");
if(rlen == strlen(expected_payload)) {
if(!memcmp(expected_payload, buffer, rlen)) {
curl_mfprintf(stderr, "ws: got the same payload back\n");
same = 1;
}
}
if(!same)
curl_mfprintf(stderr, "ws: did NOT get the same payload back\n");
}
else {
curl_mfprintf(stderr, "recv_pong: got %d bytes rflags %x\n", (int)rlen,
meta->flags);
}
}
curl_mfprintf(stderr, "ws: curl_ws_recv returned %d, received %d\n", result,
(int)rlen);
return result;
}
static CURLcode recv_any(CURL *curl)
{
size_t rlen;
@ -80,16 +38,6 @@ static CURLcode recv_any(CURL *curl)
return CURLE_OK;
}
/* just close the connection */
static void t2304_websocket_close(CURL *curl)
{
size_t sent;
CURLcode result =
curl_ws_send(curl, "", 0, &sent, 0, CURLWS_CLOSE);
curl_mfprintf(stderr,
"ws: curl_ws_send returned %d, sent %u\n", result, (int)sent);
}
static void t2304_websocket(CURL *curl)
{
int i = 0;
@ -97,16 +45,16 @@ static void t2304_websocket(CURL *curl)
do {
recv_any(curl);
curl_mfprintf(stderr, "Send ping\n");
if(t2304_send_ping(curl, "foobar"))
if(ws_send_ping(curl, "foobar"))
return;
curl_mfprintf(stderr, "Receive pong\n");
if(t2304_recv_pong(curl, "foobar")) {
if(ws_recv_pong(curl, "foobar")) {
curl_mprintf("Connection closed\n");
return;
}
curlx_wait_ms(2000);
} while(i++ < 10);
t2304_websocket_close(curl);
ws_close(curl);
}
#endif

View file

@ -75,9 +75,9 @@ static CURLcode test_lib2502(const char *URL)
/* wait for first connection established to see if we can share it */
easy_setopt(curl[i], CURLOPT_PIPEWAIT, 1L);
/* go verbose */
libtest_debug_config.nohex = 1;
libtest_debug_config.tracetime = 0;
test_setopt(curl[i], CURLOPT_DEBUGDATA, &libtest_debug_config);
debug_config.nohex = 1;
debug_config.tracetime = 0;
test_setopt(curl[i], CURLOPT_DEBUGDATA, &debug_config);
easy_setopt(curl[i], CURLOPT_DEBUGFUNCTION, libtest_debug_cb);
easy_setopt(curl[i], CURLOPT_VERBOSE, 1L);
/* include headers */

View file

@ -222,9 +222,9 @@ static CURLcode test_lib2700(const char *URL)
easy_setopt(curl, CURLOPT_URL, URL);
easy_setopt(curl, CURLOPT_USERAGENT, "client/test2700");
libtest_debug_config.nohex = 1;
libtest_debug_config.tracetime = 1;
easy_setopt(curl, CURLOPT_DEBUGDATA, &libtest_debug_config);
debug_config.nohex = 1;
debug_config.tracetime = 1;
easy_setopt(curl, CURLOPT_DEBUGDATA, &debug_config);
easy_setopt(curl, CURLOPT_DEBUGFUNCTION, libtest_debug_cb);
easy_setopt(curl, CURLOPT_VERBOSE, 1L);
easy_setopt(curl, CURLOPT_CONNECT_ONLY, 2L);

View file

@ -45,7 +45,7 @@ static CURLcode t3033_req_test(CURLM *multi, CURL *easy,
curl_easy_reset(easy);
curl_easy_setopt(easy, CURLOPT_URL, URL);
easy_setopt(easy, CURLOPT_DEBUGDATA, &libtest_debug_config);
easy_setopt(easy, CURLOPT_DEBUGDATA, &debug_config);
easy_setopt(easy, CURLOPT_DEBUGFUNCTION, libtest_debug_cb);
easy_setopt(easy, CURLOPT_VERBOSE, 1L);
@ -107,8 +107,8 @@ static CURLcode test_lib3033(const char *URL)
multi_init(multi);
easy_init(curl);
libtest_debug_config.nohex = 1;
libtest_debug_config.tracetime = 1;
debug_config.nohex = 1;
debug_config.tracetime = 1;
res = t3033_req_test(multi, curl, URL, 0);
if(res != CURLE_OK)

View file

@ -73,9 +73,9 @@ static CURLcode test_lib500(const char *URL)
test_setopt(curl, CURLOPT_URL, URL);
test_setopt(curl, CURLOPT_HEADER, 1L);
libtest_debug_config.nohex = 1;
libtest_debug_config.tracetime = 1;
test_setopt(curl, CURLOPT_DEBUGDATA, &libtest_debug_config);
debug_config.nohex = 1;
debug_config.tracetime = 1;
test_setopt(curl, CURLOPT_DEBUGDATA, &debug_config);
test_setopt(curl, CURLOPT_DEBUGFUNCTION, libtest_debug_cb);
test_setopt(curl, CURLOPT_VERBOSE, 1L);

View file

@ -27,99 +27,9 @@
#include "first.h"
#include "testtrace.h"
#include "memdebug.h"
struct t552_testdata {
char trace_ascii; /* 1 or 0 */
};
static void dump(const char *text,
FILE *stream, unsigned char *ptr, size_t size,
char nohex)
{
size_t i;
size_t c;
unsigned int width = 0x10;
if(nohex)
/* without the hex output, we can fit more on screen */
width = 0x40;
curl_mfprintf(stream, "%s, %zu bytes (0x%zx)\n", text, size, size);
for(i = 0; i < size; i += width) {
curl_mfprintf(stream, "%04zx: ", i);
if(!nohex) {
/* hex not disabled, show it */
for(c = 0; c < width; c++)
if(i + c < size)
curl_mfprintf(stream, "%02x ", ptr[i + c]);
else
fputs(" ", stream);
}
for(c = 0; (c < width) && (i + c < size); c++) {
/* check for 0D0A; if found, skip past and start a new line of output */
if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
ptr[i + c + 1] == 0x0A) {
i += (c + 2 - width);
break;
}
curl_mfprintf(stream, "%c",
(ptr[i + c] >= 0x20) && (ptr[i + c] < 0x80) ? ptr[i + c] : '.');
/* check again for 0D0A, to avoid an extra \n if it's at width */
if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
ptr[i + c + 2] == 0x0A) {
i += (c + 3 - width);
break;
}
}
fputc('\n', stream); /* newline */
}
fflush(stream);
}
static int my_trace(CURL *handle, curl_infotype type,
char *data, size_t size,
void *userp)
{
struct t552_testdata *config = (struct t552_testdata *)userp;
const char *text;
(void)handle; /* prevent compiler warning */
switch(type) {
case CURLINFO_TEXT:
curl_mfprintf(stderr, "== Info: %s", (char *)data);
return 0;
case CURLINFO_HEADER_OUT:
text = "=> Send header";
break;
case CURLINFO_DATA_OUT:
text = "=> Send data";
break;
case CURLINFO_SSL_DATA_OUT:
text = "=> Send SSL data";
break;
case CURLINFO_HEADER_IN:
text = "<= Recv header";
break;
case CURLINFO_DATA_IN:
text = "<= Recv data";
break;
case CURLINFO_SSL_DATA_IN:
text = "<= Recv SSL data";
break;
default: /* in case a new one is introduced to shock us */
return 0;
}
dump(text, stderr, (unsigned char *)data, size, config->trace_ascii);
return 0;
}
static size_t current_offset = 0;
static char databuf[70000]; /* MUST be more than 64k OR
MAX_INITIAL_POST_SIZE */
@ -161,17 +71,17 @@ static CURLcode test_lib552(const char *URL)
{
CURL *curl;
CURLcode res = CURLE_OK;
struct t552_testdata config;
size_t i;
static const char fill[] = "test data";
config.trace_ascii = 1; /* enable ASCII tracing */
debug_config.nohex = 1;
debug_config.tracetime = 0;
global_init(CURL_GLOBAL_ALL);
easy_init(curl);
test_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
test_setopt(curl, CURLOPT_DEBUGDATA, &config);
test_setopt(curl, CURLOPT_DEBUGFUNCTION, libtest_debug_cb);
test_setopt(curl, CURLOPT_DEBUGDATA, &debug_config);
/* the DEBUGFUNCTION has no effect until we enable VERBOSE */
test_setopt(curl, CURLOPT_VERBOSE, 1L);

View file

@ -53,9 +53,9 @@ static CURLcode test_lib573(const char *URL)
easy_setopt(c, CURLOPT_HEADER, 1L);
easy_setopt(c, CURLOPT_URL, URL);
libtest_debug_config.nohex = 1;
libtest_debug_config.tracetime = 1;
easy_setopt(c, CURLOPT_DEBUGDATA, &libtest_debug_config);
debug_config.nohex = 1;
debug_config.tracetime = 1;
easy_setopt(c, CURLOPT_DEBUGDATA, &debug_config);
easy_setopt(c, CURLOPT_DEBUGFUNCTION, libtest_debug_cb);
easy_setopt(c, CURLOPT_VERBOSE, 1L);

View file

@ -83,7 +83,7 @@ static bool t753_setup(const char *URL, const char *name,
easy_setopt(easy, CURLOPT_HEADERDATA, st);
easy_setopt(easy, CURLOPT_NOPROGRESS, 1L);
easy_setopt(easy, CURLOPT_DEBUGDATA, &libtest_debug_config);
easy_setopt(easy, CURLOPT_DEBUGDATA, &debug_config);
easy_setopt(easy, CURLOPT_DEBUGFUNCTION, libtest_debug_cb);
easy_setopt(easy, CURLOPT_VERBOSE, 1L);
@ -107,8 +107,8 @@ static CURLcode test_lib753(const char *URL)
start_test_timing();
libtest_debug_config.nohex = 1;
libtest_debug_config.tracetime = 1;
debug_config.nohex = 1;
debug_config.tracetime = 1;
curl_global_init(CURL_GLOBAL_DEFAULT);

View file

@ -25,14 +25,14 @@
#include "memdebug.h"
struct libtest_trace_cfg libtest_debug_config;
struct libtest_trace_cfg debug_config;
static time_t epoch_offset; /* for test time tracing */
static int known_offset; /* for test time tracing */
static void libtest_debug_dump(const char *timebuf, const char *text,
FILE *stream, const unsigned char *ptr,
size_t size, int nohex)
void debug_dump(const char *timebuf, const char *text,
FILE *stream, const unsigned char *ptr,
size_t size, int nohex)
{
size_t i;
size_t c;
@ -61,9 +61,8 @@ static void libtest_debug_dump(const char *timebuf, const char *text,
for(c = 0; (c < width) && (i + c < size); c++) {
/* check for 0D0A; if found, skip past and start a new line of output */
if(nohex &&
(i + c + 1 < size) && (ptr[i + c] == 0x0D) &&
(ptr[i + c + 1] == 0x0A)) {
if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
ptr[i + c + 1] == 0x0A) {
i += (c + 2 - width);
break;
}
@ -71,9 +70,8 @@ static void libtest_debug_dump(const char *timebuf, const char *text,
((ptr[i + c] >= 0x20) && (ptr[i + c] < 0x80)) ?
ptr[i + c] : '.');
/* check again for 0D0A, to avoid an extra \n if it's at width */
if(nohex &&
(i + c + 2 < size) && (ptr[i + c + 1] == 0x0D) &&
(ptr[i + c + 2] == 0x0A)) {
if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
ptr[i + c + 2] == 0x0A) {
i += (c + 3 - width);
break;
}
@ -114,7 +112,7 @@ int libtest_debug_cb(CURL *handle, curl_infotype type,
switch(type) {
case CURLINFO_TEXT:
curl_mfprintf(stderr, "%s== Info: %s", timestr, (char *)data);
curl_mfprintf(stderr, "%s== Info: %s", timestr, data);
return 0;
case CURLINFO_HEADER_OUT:
text = "=> Send header";
@ -138,7 +136,101 @@ int libtest_debug_cb(CURL *handle, curl_infotype type,
return 0;
}
libtest_debug_dump(timebuf, text, stderr, (unsigned char *)data, size,
trace_cfg->nohex);
debug_dump(timebuf, text, stderr, (const unsigned char *)data, size,
trace_cfg->nohex);
return 0;
}
static void log_line_start(FILE *log, const char *idsbuf, curl_infotype type)
{
/*
* This is the trace look that is similar to what libcurl makes on its
* own.
*/
static const char * const s_infotype[] = {
"* ", "< ", "> ", "{ ", "} ", "{ ", "} "
};
if(idsbuf && *idsbuf)
curl_mfprintf(log, "%s%s", idsbuf, s_infotype[type]);
else
fputs(s_infotype[type], log);
}
/* callback for CURLOPT_DEBUGFUNCTION (used in client tests) */
int cli_debug_cb(CURL *handle, curl_infotype type,
char *data, size_t size, void *userp)
{
FILE *output = stderr;
static int newl = 0;
static int traced_data = 0;
char idsbuf[60];
curl_off_t xfer_id, conn_id;
(void)handle; /* not used */
(void)userp;
if(!curl_easy_getinfo(handle, CURLINFO_XFER_ID, &xfer_id) && xfer_id >= 0) {
if(!curl_easy_getinfo(handle, CURLINFO_CONN_ID, &conn_id) &&
conn_id >= 0) {
curl_msnprintf(idsbuf, sizeof(idsbuf),
"[%" CURL_FORMAT_CURL_OFF_T "-"
"%" CURL_FORMAT_CURL_OFF_T "] ", xfer_id, conn_id);
}
else {
curl_msnprintf(idsbuf, sizeof(idsbuf),
"[%" CURL_FORMAT_CURL_OFF_T "-x] ", xfer_id);
}
}
else
idsbuf[0] = 0;
switch(type) {
case CURLINFO_HEADER_OUT:
if(size > 0) {
size_t st = 0;
size_t i;
for(i = 0; i < size - 1; i++) {
if(data[i] == '\n') { /* LF */
if(!newl) {
log_line_start(output, idsbuf, type);
}
(void)fwrite(data + st, i - st + 1, 1, output);
st = i + 1;
newl = 0;
}
}
if(!newl)
log_line_start(output, idsbuf, type);
(void)fwrite(data + st, i - st + 1, 1, output);
}
newl = (size && (data[size - 1] != '\n')) ? 1 : 0;
traced_data = 0;
break;
case CURLINFO_TEXT:
case CURLINFO_HEADER_IN:
if(!newl)
log_line_start(output, idsbuf, type);
(void)fwrite(data, size, 1, output);
newl = (size && (data[size - 1] != '\n')) ? 1 : 0;
traced_data = 0;
break;
case CURLINFO_DATA_OUT:
case CURLINFO_DATA_IN:
case CURLINFO_SSL_DATA_IN:
case CURLINFO_SSL_DATA_OUT:
if(!traced_data) {
if(!newl)
log_line_start(output, idsbuf, type);
curl_mfprintf(output, "[%ld bytes data]\n", (long)size);
newl = 0;
traced_data = 1;
}
break;
default: /* nada */
newl = 0;
traced_data = 1;
break;
}
return 0;
}

View file

@ -25,14 +25,22 @@
***************************************************************************/
#include "first.h"
void debug_dump(const char *timebuf, const char *text,
FILE *stream, const unsigned char *ptr,
size_t size, int nohex);
struct libtest_trace_cfg {
int tracetime; /* 0 represents FALSE, anything else TRUE */
int nohex; /* 0 represents FALSE, anything else TRUE */
};
extern struct libtest_trace_cfg libtest_debug_config;
extern struct libtest_trace_cfg debug_config;
int libtest_debug_cb(CURL *handle, curl_infotype type,
char *data, size_t size, void *userp);
/* callback for CURLOPT_DEBUGFUNCTION (client tests) */
int cli_debug_cb(CURL *handle, curl_infotype type,
char *data, size_t size, void *userp);
#endif /* HEADER_LIBTEST_TESTTRACE_H */