mirror of
https://github.com/moparisthebest/curl
synced 2024-08-13 17:03:50 -04:00
sendf: move Curl_sendf to dict.c and make it static
... as the only remaining user of that function. Also fix gopher.c to instead use Curl_write() Closes #6020
This commit is contained in:
parent
422b257fef
commit
a87cca7b1c
93
lib/dict.c
93
lib/dict.c
@ -57,6 +57,7 @@
|
|||||||
#include "escape.h"
|
#include "escape.h"
|
||||||
#include "progress.h"
|
#include "progress.h"
|
||||||
#include "dict.h"
|
#include "dict.h"
|
||||||
|
#include "curl_printf.h"
|
||||||
#include "strcase.h"
|
#include "strcase.h"
|
||||||
#include "curl_memory.h"
|
#include "curl_memory.h"
|
||||||
/* The last #include file should be: */
|
/* The last #include file should be: */
|
||||||
@ -127,6 +128,52 @@ static char *unescape_word(struct Curl_easy *data, const char *inputbuff)
|
|||||||
return dictp;
|
return dictp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* sendf() sends formatted data to the server */
|
||||||
|
static CURLcode sendf(curl_socket_t sockfd, struct connectdata *conn,
|
||||||
|
const char *fmt, ...)
|
||||||
|
{
|
||||||
|
struct Curl_easy *data = conn->data;
|
||||||
|
ssize_t bytes_written;
|
||||||
|
size_t write_len;
|
||||||
|
CURLcode result = CURLE_OK;
|
||||||
|
char *s;
|
||||||
|
char *sptr;
|
||||||
|
va_list ap;
|
||||||
|
va_start(ap, fmt);
|
||||||
|
s = vaprintf(fmt, ap); /* returns an allocated string */
|
||||||
|
va_end(ap);
|
||||||
|
if(!s)
|
||||||
|
return CURLE_OUT_OF_MEMORY; /* failure */
|
||||||
|
|
||||||
|
bytes_written = 0;
|
||||||
|
write_len = strlen(s);
|
||||||
|
sptr = s;
|
||||||
|
|
||||||
|
for(;;) {
|
||||||
|
/* Write the buffer to the socket */
|
||||||
|
result = Curl_write(conn, sockfd, sptr, write_len, &bytes_written);
|
||||||
|
|
||||||
|
if(result)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if(data->set.verbose)
|
||||||
|
Curl_debug(data, CURLINFO_DATA_OUT, sptr, (size_t)bytes_written);
|
||||||
|
|
||||||
|
if((size_t)bytes_written != write_len) {
|
||||||
|
/* if not all was written at once, we must advance the pointer, decrease
|
||||||
|
the size left and try again! */
|
||||||
|
write_len -= bytes_written;
|
||||||
|
sptr += bytes_written;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(s); /* free the output string */
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
static CURLcode dict_do(struct connectdata *conn, bool *done)
|
static CURLcode dict_do(struct connectdata *conn, bool *done)
|
||||||
{
|
{
|
||||||
char *word;
|
char *word;
|
||||||
@ -184,18 +231,16 @@ static CURLcode dict_do(struct connectdata *conn, bool *done)
|
|||||||
if(!eword)
|
if(!eword)
|
||||||
return CURLE_OUT_OF_MEMORY;
|
return CURLE_OUT_OF_MEMORY;
|
||||||
|
|
||||||
result = Curl_sendf(sockfd, conn,
|
result = sendf(sockfd, conn,
|
||||||
"CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
|
"CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
|
||||||
"MATCH "
|
"MATCH "
|
||||||
"%s " /* database */
|
"%s " /* database */
|
||||||
"%s " /* strategy */
|
"%s " /* strategy */
|
||||||
"%s\r\n" /* word */
|
"%s\r\n" /* word */
|
||||||
"QUIT\r\n",
|
"QUIT\r\n",
|
||||||
|
database,
|
||||||
database,
|
strategy,
|
||||||
strategy,
|
eword);
|
||||||
eword
|
|
||||||
);
|
|
||||||
|
|
||||||
free(eword);
|
free(eword);
|
||||||
|
|
||||||
@ -234,14 +279,14 @@ static CURLcode dict_do(struct connectdata *conn, bool *done)
|
|||||||
if(!eword)
|
if(!eword)
|
||||||
return CURLE_OUT_OF_MEMORY;
|
return CURLE_OUT_OF_MEMORY;
|
||||||
|
|
||||||
result = Curl_sendf(sockfd, conn,
|
result = sendf(sockfd, conn,
|
||||||
"CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
|
"CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
|
||||||
"DEFINE "
|
"DEFINE "
|
||||||
"%s " /* database */
|
"%s " /* database */
|
||||||
"%s\r\n" /* word */
|
"%s\r\n" /* word */
|
||||||
"QUIT\r\n",
|
"QUIT\r\n",
|
||||||
database,
|
database,
|
||||||
eword);
|
eword);
|
||||||
|
|
||||||
free(eword);
|
free(eword);
|
||||||
|
|
||||||
@ -262,10 +307,10 @@ static CURLcode dict_do(struct connectdata *conn, bool *done)
|
|||||||
if(ppath[i] == ':')
|
if(ppath[i] == ':')
|
||||||
ppath[i] = ' ';
|
ppath[i] = ' ';
|
||||||
}
|
}
|
||||||
result = Curl_sendf(sockfd, conn,
|
result = sendf(sockfd, conn,
|
||||||
"CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
|
"CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
|
||||||
"%s\r\n"
|
"%s\r\n"
|
||||||
"QUIT\r\n", ppath);
|
"QUIT\r\n", ppath);
|
||||||
if(result) {
|
if(result) {
|
||||||
failf(data, "Failed sending DICT request");
|
failf(data, "Failed sending DICT request");
|
||||||
return result;
|
return result;
|
||||||
|
@ -171,9 +171,7 @@ static CURLcode gopher_do(struct connectdata *conn, bool *done)
|
|||||||
free(sel_org);
|
free(sel_org);
|
||||||
|
|
||||||
if(!result)
|
if(!result)
|
||||||
/* We can use Curl_sendf to send the terminal \r\n relatively safely and
|
result = Curl_write(conn, sockfd, "\r\n", 2, &amount);
|
||||||
save allocing another string/doing another _write loop. */
|
|
||||||
result = Curl_sendf(sockfd, conn, "\r\n");
|
|
||||||
if(result) {
|
if(result) {
|
||||||
failf(data, "Failed sending Gopher request");
|
failf(data, "Failed sending Gopher request");
|
||||||
return result;
|
return result;
|
||||||
|
46
lib/sendf.c
46
lib/sendf.c
@ -286,52 +286,6 @@ void Curl_failf(struct Curl_easy *data, const char *fmt, ...)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Curl_sendf() sends formatted data to the server */
|
|
||||||
CURLcode Curl_sendf(curl_socket_t sockfd, struct connectdata *conn,
|
|
||||||
const char *fmt, ...)
|
|
||||||
{
|
|
||||||
struct Curl_easy *data = conn->data;
|
|
||||||
ssize_t bytes_written;
|
|
||||||
size_t write_len;
|
|
||||||
CURLcode result = CURLE_OK;
|
|
||||||
char *s;
|
|
||||||
char *sptr;
|
|
||||||
va_list ap;
|
|
||||||
va_start(ap, fmt);
|
|
||||||
s = vaprintf(fmt, ap); /* returns an allocated string */
|
|
||||||
va_end(ap);
|
|
||||||
if(!s)
|
|
||||||
return CURLE_OUT_OF_MEMORY; /* failure */
|
|
||||||
|
|
||||||
bytes_written = 0;
|
|
||||||
write_len = strlen(s);
|
|
||||||
sptr = s;
|
|
||||||
|
|
||||||
for(;;) {
|
|
||||||
/* Write the buffer to the socket */
|
|
||||||
result = Curl_write(conn, sockfd, sptr, write_len, &bytes_written);
|
|
||||||
|
|
||||||
if(result)
|
|
||||||
break;
|
|
||||||
|
|
||||||
if(data->set.verbose)
|
|
||||||
Curl_debug(data, CURLINFO_DATA_OUT, sptr, (size_t)bytes_written);
|
|
||||||
|
|
||||||
if((size_t)bytes_written != write_len) {
|
|
||||||
/* if not all was written at once, we must advance the pointer, decrease
|
|
||||||
the size left and try again! */
|
|
||||||
write_len -= bytes_written;
|
|
||||||
sptr += bytes_written;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
free(s); /* free the output string */
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Curl_write() is an internal write function that sends data to the
|
* Curl_write() is an internal write function that sends data to the
|
||||||
* server. Works with plain sockets, SCP, SSL or kerberos.
|
* server. Works with plain sockets, SCP, SSL or kerberos.
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
* | (__| |_| | _ <| |___
|
* | (__| |_| | _ <| |___
|
||||||
* \___|\___/|_| \_\_____|
|
* \___|\___/|_| \_\_____|
|
||||||
*
|
*
|
||||||
* Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
|
* Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
|
||||||
*
|
*
|
||||||
* This software is licensed as described in the file COPYING, which
|
* This software is licensed as described in the file COPYING, which
|
||||||
* you should have received as part of this distribution. The terms
|
* you should have received as part of this distribution. The terms
|
||||||
@ -24,8 +24,6 @@
|
|||||||
|
|
||||||
#include "curl_setup.h"
|
#include "curl_setup.h"
|
||||||
|
|
||||||
CURLcode Curl_sendf(curl_socket_t sockfd, struct connectdata *,
|
|
||||||
const char *fmt, ...);
|
|
||||||
void Curl_infof(struct Curl_easy *, const char *fmt, ...);
|
void Curl_infof(struct Curl_easy *, const char *fmt, ...);
|
||||||
void Curl_failf(struct Curl_easy *, const char *fmt, ...);
|
void Curl_failf(struct Curl_easy *, const char *fmt, ...);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user