mirror of
https://github.com/curl/curl.git
synced 2026-04-14 00:51:42 +08:00
Before this patch curl used the C preprocessor to override standard memory allocation symbols: malloc, calloc, strdup, realloc, free. The goal of these is to replace them with curl's debug wrappers in `CURLDEBUG` builds, another was to replace them with the wrappers calling user-defined allocators in libcurl. This solution needed a bunch of workarounds to avoid breaking external headers: it relied on include order to do the overriding last. For "unity" builds it needed to reset overrides before external includes. Also in test apps, which are always built as single source files. It also needed the `(symbol)` trick to avoid overrides in some places. This would still not fix cases where the standard symbols were macros. It was also fragile and difficult to figure out which was the actual function behind an alloc or free call in a specific piece of code. This in turn caused bugs where the wrong allocator was accidentally called. To avoid these problems, this patch replaces this solution with `curlx_`-prefixed allocator macros, and mapping them _once_ to either the libcurl wrappers, the debug wrappers or the standard ones, matching the rest of the code in libtests. This concludes the long journey to avoid redefining standard functions in the curl codebase. Note: I did not update `packages/OS400/*.c` sources. They did not `#include` `curl_setup.h`, `curl_memory.h` or `memdebug.h`, meaning the overrides were never applied to them. This may or may not have been correct. For now I suppressed the direct use of standard allocators via a local `.checksrc`. Probably they (except for `curlcl.c`) should be updated to include `curl_setup.h` and use the `curlx_` macros. This patch changes mappings in two places: - `lib/curl_threads.c` in libtests: Before this patch it mapped to libcurl allocators. After, it maps to standard allocators, like the rest of libtests code. - `units`: before this patch it mapped to standard allocators. After, it maps to libcurl allocators. Also: - drop all position-dependent `curl_memory.h` and `memdebug.h` includes, and delete the now unnecessary headers. - rename `Curl_tcsdup` macro to `curlx_tcsdup` and define like the other allocators. - map `curlx_strdup()` to `_strdup()` on Windows (was: `strdup()`). To fix warnings silenced via `_CRT_NONSTDC_NO_DEPRECATE`. - multibyte: map `curlx_convert_*()` to `_strdup()` on Windows (was: `strdup()`). - src: do not reuse the `strdup` name for the local replacement. - lib509: call `_strdup()` on Windows (was: `strdup()`). - test1132: delete test obsoleted by this patch. - CHECKSRC.md: update text for `SNPRINTF`. - checksrc: ban standard allocator symbols. Follow-up tob12da22db1#18866 Follow-up todb98daab05#18844 Follow-up to4deea9396b#18814 Follow-up to9678ff5b1b#18776 Follow-up to10bac43b87#18774 Follow-up to20142f5d06#18634 Follow-up tobf7375ecc5#18503 Follow-up to9863599d69#18502 Follow-up to3bb5e58c10#17827 Closes #19626
239 lines
7.4 KiB
C
239 lines
7.4 KiB
C
/***************************************************************************
|
|
* _ _ ____ _
|
|
* 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 "curl_setup.h"
|
|
|
|
#ifndef CURL_DISABLE_GOPHER
|
|
|
|
#include "urldata.h"
|
|
#include <curl/curl.h>
|
|
#include "transfer.h"
|
|
#include "sendf.h"
|
|
#include "cfilters.h"
|
|
#include "connect.h"
|
|
#include "progress.h"
|
|
#include "gopher.h"
|
|
#include "select.h"
|
|
#include "vtls/vtls.h"
|
|
#include "url.h"
|
|
#include "escape.h"
|
|
#include "curlx/warnless.h"
|
|
|
|
/*
|
|
* Forward declarations.
|
|
*/
|
|
|
|
static CURLcode gopher_do(struct Curl_easy *data, bool *done);
|
|
#ifdef USE_SSL
|
|
static CURLcode gopher_connect(struct Curl_easy *data, bool *done);
|
|
static CURLcode gopher_connecting(struct Curl_easy *data, bool *done);
|
|
#endif
|
|
|
|
/*
|
|
* Gopher protocol handler.
|
|
* This is also a nice simple template to build off for simple
|
|
* connect-command-download protocols.
|
|
*/
|
|
|
|
const struct Curl_handler Curl_handler_gopher = {
|
|
"gopher", /* scheme */
|
|
ZERO_NULL, /* setup_connection */
|
|
gopher_do, /* do_it */
|
|
ZERO_NULL, /* done */
|
|
ZERO_NULL, /* do_more */
|
|
ZERO_NULL, /* connect_it */
|
|
ZERO_NULL, /* connecting */
|
|
ZERO_NULL, /* doing */
|
|
ZERO_NULL, /* proto_pollset */
|
|
ZERO_NULL, /* doing_pollset */
|
|
ZERO_NULL, /* domore_pollset */
|
|
ZERO_NULL, /* perform_pollset */
|
|
ZERO_NULL, /* disconnect */
|
|
ZERO_NULL, /* write_resp */
|
|
ZERO_NULL, /* write_resp_hd */
|
|
ZERO_NULL, /* connection_check */
|
|
ZERO_NULL, /* attach connection */
|
|
ZERO_NULL, /* follow */
|
|
PORT_GOPHER, /* defport */
|
|
CURLPROTO_GOPHER, /* protocol */
|
|
CURLPROTO_GOPHER, /* family */
|
|
PROTOPT_NONE /* flags */
|
|
};
|
|
|
|
#ifdef USE_SSL
|
|
const struct Curl_handler Curl_handler_gophers = {
|
|
"gophers", /* scheme */
|
|
ZERO_NULL, /* setup_connection */
|
|
gopher_do, /* do_it */
|
|
ZERO_NULL, /* done */
|
|
ZERO_NULL, /* do_more */
|
|
gopher_connect, /* connect_it */
|
|
gopher_connecting, /* connecting */
|
|
ZERO_NULL, /* doing */
|
|
ZERO_NULL, /* proto_pollset */
|
|
ZERO_NULL, /* doing_pollset */
|
|
ZERO_NULL, /* domore_pollset */
|
|
ZERO_NULL, /* perform_pollset */
|
|
ZERO_NULL, /* disconnect */
|
|
ZERO_NULL, /* write_resp */
|
|
ZERO_NULL, /* write_resp_hd */
|
|
ZERO_NULL, /* connection_check */
|
|
ZERO_NULL, /* attach connection */
|
|
ZERO_NULL, /* follow */
|
|
PORT_GOPHER, /* defport */
|
|
CURLPROTO_GOPHERS, /* protocol */
|
|
CURLPROTO_GOPHER, /* family */
|
|
PROTOPT_SSL /* flags */
|
|
};
|
|
|
|
static CURLcode gopher_connect(struct Curl_easy *data, bool *done)
|
|
{
|
|
(void)data;
|
|
(void)done;
|
|
return CURLE_OK;
|
|
}
|
|
|
|
static CURLcode gopher_connecting(struct Curl_easy *data, bool *done)
|
|
{
|
|
struct connectdata *conn = data->conn;
|
|
CURLcode result;
|
|
|
|
result = Curl_conn_connect(data, FIRSTSOCKET, TRUE, done);
|
|
if(result)
|
|
connclose(conn, "Failed TLS connection");
|
|
*done = TRUE;
|
|
return result;
|
|
}
|
|
#endif
|
|
|
|
static CURLcode gopher_do(struct Curl_easy *data, bool *done)
|
|
{
|
|
CURLcode result = CURLE_OK;
|
|
struct connectdata *conn = data->conn;
|
|
curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
|
|
char *gopherpath;
|
|
char *path = data->state.up.path;
|
|
char *query = data->state.up.query;
|
|
const char *buf = NULL;
|
|
char *buf_alloc = NULL;
|
|
size_t nwritten, buf_len;
|
|
timediff_t timeout_ms;
|
|
int what;
|
|
|
|
*done = TRUE; /* unconditionally */
|
|
|
|
/* path is guaranteed non-NULL */
|
|
DEBUGASSERT(path);
|
|
|
|
if(query)
|
|
gopherpath = curl_maprintf("%s?%s", path, query);
|
|
else
|
|
gopherpath = curlx_strdup(path);
|
|
|
|
if(!gopherpath)
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
/* Create selector. Degenerate cases: / and /1 => convert to "" */
|
|
if(strlen(gopherpath) <= 2) {
|
|
buf = "";
|
|
buf_len = 0;
|
|
curlx_free(gopherpath);
|
|
}
|
|
else {
|
|
char *newp;
|
|
|
|
/* Otherwise, drop / and the first character (i.e., item type) ... */
|
|
newp = gopherpath;
|
|
newp += 2;
|
|
|
|
/* ... and finally unescape */
|
|
result = Curl_urldecode(newp, 0, &buf_alloc, &buf_len, REJECT_ZERO);
|
|
curlx_free(gopherpath);
|
|
if(result)
|
|
return result;
|
|
buf = buf_alloc;
|
|
}
|
|
|
|
for(; buf_len;) {
|
|
|
|
result = Curl_xfer_send(data, buf, buf_len, FALSE, &nwritten);
|
|
if(!result) { /* Which may not have written it all! */
|
|
result = Curl_client_write(data, CLIENTWRITE_HEADER, buf, nwritten);
|
|
if(result)
|
|
break;
|
|
|
|
if(nwritten > buf_len) {
|
|
DEBUGASSERT(0);
|
|
break;
|
|
}
|
|
buf_len -= nwritten;
|
|
buf += nwritten;
|
|
if(!buf_len)
|
|
break; /* but it did write it all */
|
|
}
|
|
else
|
|
break;
|
|
|
|
timeout_ms = Curl_timeleft_ms(data, NULL, FALSE);
|
|
if(timeout_ms < 0) {
|
|
result = CURLE_OPERATION_TIMEDOUT;
|
|
break;
|
|
}
|
|
if(!timeout_ms)
|
|
timeout_ms = TIMEDIFF_T_MAX;
|
|
|
|
/* Do not busyloop. The entire loop thing is a work-around as it causes a
|
|
BLOCKING behavior which is a NO-NO. This function should rather be
|
|
split up in a do and a doing piece where the pieces that are not
|
|
possible to send now will be sent in the doing function repeatedly
|
|
until the entire request is sent.
|
|
*/
|
|
what = SOCKET_WRITABLE(sockfd, timeout_ms);
|
|
if(what < 0) {
|
|
result = CURLE_SEND_ERROR;
|
|
break;
|
|
}
|
|
else if(!what) {
|
|
result = CURLE_OPERATION_TIMEDOUT;
|
|
break;
|
|
}
|
|
}
|
|
|
|
curlx_free(buf_alloc);
|
|
|
|
if(!result)
|
|
result = Curl_xfer_send(data, "\r\n", 2, FALSE, &nwritten);
|
|
if(result) {
|
|
failf(data, "Failed sending Gopher request");
|
|
return result;
|
|
}
|
|
result = Curl_client_write(data, CLIENTWRITE_HEADER, "\r\n", 2);
|
|
if(result)
|
|
return result;
|
|
|
|
Curl_xfer_setup_recv(data, FIRSTSOCKET, -1);
|
|
return CURLE_OK;
|
|
}
|
|
#endif /* CURL_DISABLE_GOPHER */
|