From a87cca7b1cbec0b4206b2bb1cb074a8a4a5bd151 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 28 Sep 2020 14:14:25 +0200 Subject: 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 --- lib/dict.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 69 insertions(+), 24 deletions(-) (limited to 'lib/dict.c') diff --git a/lib/dict.c b/lib/dict.c index b6676aa84..8dd4a90f7 100644 --- a/lib/dict.c +++ b/lib/dict.c @@ -57,6 +57,7 @@ #include "escape.h" #include "progress.h" #include "dict.h" +#include "curl_printf.h" #include "strcase.h" #include "curl_memory.h" /* The last #include file should be: */ @@ -127,6 +128,52 @@ static char *unescape_word(struct Curl_easy *data, const char *inputbuff) 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) { char *word; @@ -184,18 +231,16 @@ static CURLcode dict_do(struct connectdata *conn, bool *done) if(!eword) return CURLE_OUT_OF_MEMORY; - result = Curl_sendf(sockfd, conn, - "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n" - "MATCH " - "%s " /* database */ - "%s " /* strategy */ - "%s\r\n" /* word */ - "QUIT\r\n", - - database, - strategy, - eword - ); + result = sendf(sockfd, conn, + "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n" + "MATCH " + "%s " /* database */ + "%s " /* strategy */ + "%s\r\n" /* word */ + "QUIT\r\n", + database, + strategy, + eword); free(eword); @@ -234,14 +279,14 @@ static CURLcode dict_do(struct connectdata *conn, bool *done) if(!eword) return CURLE_OUT_OF_MEMORY; - result = Curl_sendf(sockfd, conn, - "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n" - "DEFINE " - "%s " /* database */ - "%s\r\n" /* word */ - "QUIT\r\n", - database, - eword); + result = sendf(sockfd, conn, + "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n" + "DEFINE " + "%s " /* database */ + "%s\r\n" /* word */ + "QUIT\r\n", + database, + eword); free(eword); @@ -262,10 +307,10 @@ static CURLcode dict_do(struct connectdata *conn, bool *done) if(ppath[i] == ':') ppath[i] = ' '; } - result = Curl_sendf(sockfd, conn, - "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n" - "%s\r\n" - "QUIT\r\n", ppath); + result = sendf(sockfd, conn, + "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n" + "%s\r\n" + "QUIT\r\n", ppath); if(result) { failf(data, "Failed sending DICT request"); return result; -- cgit v1.2.1