lib: separate scheme info from protocol implementation

This allows builds know about all schemes - but only have the protocol
implementations for those actually built-in.

It further allows multiple protocols to reuse the same protocol setup
and functions for both TLS and non-TLS implementations instead of
needing two (or more) structs.

The scheme information is now in 'struct Curl_scheme' and all the
function pointers for each scheme/protocol implementation are in struct
Curl_protocol.

The URL API now always work with all known protocols.

Closes #20351
This commit is contained in:
Daniel Stenberg 2026-01-19 00:15:41 +01:00
parent 63baa10951
commit 8edc0338f3
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
59 changed files with 831 additions and 1040 deletions

View file

@ -1034,7 +1034,7 @@ static int myssh_in_AUTH_DONE(struct Curl_easy *data,
conn->recv_idx = FIRSTSOCKET;
conn->send_idx = -1;
if(conn->handler->protocol == CURLPROTO_SFTP) {
if(conn->scheme->protocol == CURLPROTO_SFTP) {
myssh_to(data, sshc, SSH_SFTP_INIT);
return SSH_NO_ERROR;
}
@ -2501,7 +2501,7 @@ static CURLcode myssh_connect(struct Curl_easy *data, bool *done)
return CURLE_FAILED_INIT;
CURL_TRC_SSH(data, "myssh_connect");
if(conn->handler->protocol & CURLPROTO_SCP) {
if(conn->scheme->protocol & CURLPROTO_SCP) {
conn->recv[FIRSTSOCKET] = scp_recv;
conn->send[FIRSTSOCKET] = scp_send;
}
@ -3034,7 +3034,7 @@ static CURLcode myssh_do_it(struct Curl_easy *data, bool *done)
Curl_pgrsReset(data);
if(conn->handler->protocol & CURLPROTO_SCP)
if(conn->scheme->protocol & CURLPROTO_SCP)
result = scp_perform(data, &connected, done);
else
result = sftp_perform(data, &connected, done);
@ -3062,10 +3062,9 @@ void Curl_ssh_version(char *buffer, size_t buflen)
}
/*
* SCP protocol handler.
* SCP.
*/
const struct Curl_handler Curl_handler_scp = {
"SCP", /* scheme */
const struct Curl_protocol Curl_protocol_scp = {
myssh_setup_connection, /* setup_connection */
myssh_do_it, /* do_it */
scp_done, /* done */
@ -3083,18 +3082,12 @@ const struct Curl_handler Curl_handler_scp = {
ZERO_NULL, /* connection_check */
ZERO_NULL, /* attach connection */
ZERO_NULL, /* follow */
PORT_SSH, /* defport */
CURLPROTO_SCP, /* protocol */
CURLPROTO_SCP, /* family */
PROTOPT_DIRLOCK | PROTOPT_CLOSEACTION | /* flags */
PROTOPT_NOURLQUERY | PROTOPT_CONN_REUSE
};
/*
* SFTP protocol handler.
* SFTP.
*/
const struct Curl_handler Curl_handler_sftp = {
"SFTP", /* scheme */
const struct Curl_protocol Curl_protocol_sftp = {
myssh_setup_connection, /* setup_connection */
myssh_do_it, /* do_it */
sftp_done, /* done */
@ -3112,11 +3105,6 @@ const struct Curl_handler Curl_handler_sftp = {
ZERO_NULL, /* connection_check */
ZERO_NULL, /* attach connection */
ZERO_NULL, /* follow */
PORT_SSH, /* defport */
CURLPROTO_SFTP, /* protocol */
CURLPROTO_SFTP, /* family */
PROTOPT_DIRLOCK | PROTOPT_CLOSEACTION | /* flags */
PROTOPT_NOURLQUERY | PROTOPT_CONN_REUSE
};
#endif /* USE_LIBSSH */

View file

@ -1858,7 +1858,7 @@ static CURLcode ssh_state_auth_done(struct Curl_easy *data,
data->conn->recv_idx = FIRSTSOCKET;
conn->send_idx = -1;
if(conn->handler->protocol == CURLPROTO_SFTP) {
if(conn->scheme->protocol == CURLPROTO_SFTP) {
myssh_state(data, sshc, SSH_SFTP_INIT);
return CURLE_OK;
}
@ -3478,7 +3478,7 @@ static CURLcode ssh_connect(struct Curl_easy *data, bool *done)
}
#endif /* CURL_DISABLE_PROXY */
if(conn->handler->protocol & CURLPROTO_SCP) {
if(conn->scheme->protocol & CURLPROTO_SCP) {
conn->recv[FIRSTSOCKET] = scp_recv;
conn->send[FIRSTSOCKET] = scp_send;
}
@ -3867,7 +3867,7 @@ static CURLcode ssh_do(struct Curl_easy *data, bool *done)
Curl_pgrsReset(data);
if(conn->handler->protocol & CURLPROTO_SCP)
if(conn->scheme->protocol & CURLPROTO_SCP)
result = scp_perform(data, &connected, done);
else
result = sftp_perform(data, &connected, done);
@ -3902,7 +3902,7 @@ static void ssh_attach(struct Curl_easy *data, struct connectdata *conn)
{
DEBUGASSERT(data);
DEBUGASSERT(conn);
if(conn->handler->protocol & PROTO_FAMILY_SSH) {
if(conn->scheme->protocol & PROTO_FAMILY_SSH) {
struct ssh_conn *sshc = Curl_conn_meta_get(conn, CURL_META_SSH_CONN);
if(sshc && sshc->ssh_session) {
/* only re-attach if the session already exists */
@ -3915,8 +3915,7 @@ static void ssh_attach(struct Curl_easy *data, struct connectdata *conn)
/*
* SCP protocol handler.
*/
const struct Curl_handler Curl_handler_scp = {
"SCP", /* scheme */
const struct Curl_protocol Curl_protocol_scp = {
ssh_setup_connection, /* setup_connection */
ssh_do, /* do_it */
scp_done, /* done */
@ -3934,18 +3933,12 @@ const struct Curl_handler Curl_handler_scp = {
ZERO_NULL, /* connection_check */
ssh_attach, /* attach */
ZERO_NULL, /* follow */
PORT_SSH, /* defport */
CURLPROTO_SCP, /* protocol */
CURLPROTO_SCP, /* family */
PROTOPT_DIRLOCK | PROTOPT_CLOSEACTION | /* flags */
PROTOPT_NOURLQUERY | PROTOPT_CONN_REUSE
};
/*
* SFTP protocol handler.
*/
const struct Curl_handler Curl_handler_sftp = {
"SFTP", /* scheme */
const struct Curl_protocol Curl_protocol_sftp = {
ssh_setup_connection, /* setup_connection */
ssh_do, /* do_it */
sftp_done, /* done */
@ -3963,11 +3956,6 @@ const struct Curl_handler Curl_handler_sftp = {
ZERO_NULL, /* connection_check */
ssh_attach, /* attach */
ZERO_NULL, /* follow */
PORT_SSH, /* defport */
CURLPROTO_SFTP, /* protocol */
CURLPROTO_SFTP, /* family */
PROTOPT_DIRLOCK | PROTOPT_CLOSEACTION | /* flags */
PROTOPT_NOURLQUERY | PROTOPT_CONN_REUSE
};
#endif /* USE_LIBSSH2 */

View file

@ -1,5 +1,5 @@
#ifndef HEADER_CURL_SSH_H
#define HEADER_CURL_SSH_H
#ifndef HEADER_CURL_VSSH_SSH_H
#define HEADER_CURL_VSSH_SSH_H
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
@ -25,6 +25,12 @@
***************************************************************************/
#include "../curl_setup.h"
extern const struct Curl_protocol Curl_protocol_sftp;
extern const struct Curl_protocol Curl_protocol_scp;
extern const struct Curl_scheme Curl_scheme_sftp;
extern const struct Curl_scheme Curl_scheme_scp;
#ifdef USE_LIBSSH2
#include <libssh2.h>
#include <libssh2_sftp.h>
@ -232,9 +238,6 @@ struct ssh_conn {
#ifdef USE_SSH
extern const struct Curl_handler Curl_handler_scp;
extern const struct Curl_handler Curl_handler_sftp;
/* generic SSH backend functions */
CURLcode Curl_ssh_init(void);
void Curl_ssh_cleanup(void);

View file

@ -23,9 +23,9 @@
***************************************************************************/
#include "../curl_setup.h"
#ifdef USE_SSH
#include "vssh.h"
#include "ssh.h"
#ifdef USE_SSH
#include "../curlx/strparse.h"
#include "../curl_trc.h"
#include "../escape.h"
@ -51,7 +51,7 @@ CURLcode Curl_getworkingpath(struct Curl_easy *data,
curlx_dyn_init(&npath, MAX_SSHPATH_LEN);
/* Check for /~/, indicating relative to the user's home directory */
if((data->conn->handler->protocol & CURLPROTO_SCP) &&
if((data->conn->scheme->protocol & CURLPROTO_SCP) &&
(working_path_len > 3) && (!memcmp(working_path, "/~/", 3))) {
/* It is referenced to the home directory, so strip the leading '/~/' */
if(curlx_dyn_addn(&npath, &working_path[3], working_path_len - 3)) {
@ -59,7 +59,7 @@ CURLcode Curl_getworkingpath(struct Curl_easy *data,
return CURLE_OUT_OF_MEMORY;
}
}
else if((data->conn->handler->protocol & CURLPROTO_SFTP) &&
else if((data->conn->scheme->protocol & CURLPROTO_SFTP) &&
(!strcmp("/~", working_path) ||
((working_path_len > 2) && !memcmp(working_path, "/~/", 3)))) {
if(curlx_dyn_add(&npath, homedir)) {
@ -239,3 +239,34 @@ CURLcode Curl_ssh_range(struct Curl_easy *data,
}
#endif /* USE_SSH */
/*
* SFTP protocol handler.
*/
const struct Curl_scheme Curl_scheme_sftp = {
"SFTP", /* scheme */
#ifndef USE_SSH
NULL,
#else
&Curl_protocol_sftp,
#endif
CURLPROTO_SFTP, /* protocol */
CURLPROTO_SFTP, /* family */
PROTOPT_DIRLOCK | PROTOPT_CLOSEACTION | /* flags */
PROTOPT_NOURLQUERY | PROTOPT_CONN_REUSE,
PORT_SSH /* defport */
};
const struct Curl_scheme Curl_scheme_scp = {
"SCP", /* scheme */
#ifndef USE_SSH
NULL,
#else
&Curl_protocol_scp,
#endif
CURLPROTO_SCP, /* protocol */
CURLPROTO_SCP, /* family */
PROTOPT_DIRLOCK | PROTOPT_CLOSEACTION | /* flags */
PROTOPT_NOURLQUERY | PROTOPT_CONN_REUSE,
PORT_SSH, /* defport */
};

View file

@ -1,5 +1,5 @@
#ifndef HEADER_CURL_PATH_H
#define HEADER_CURL_PATH_H
#ifndef HEADER_CURL_VSSH_VSSH_H
#define HEADER_CURL_VSSH_VSSH_H
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
@ -37,4 +37,4 @@ CURLcode Curl_ssh_range(struct Curl_easy *data,
const char *range, curl_off_t filesize,
curl_off_t *startp, curl_off_t *sizep);
#endif /* HEADER_CURL_PATH_H */
#endif /* HEADER_CURL_VSSH_VSSH_H */