diff options
author | Daniel Stenberg <daniel@haxx.se> | 2000-10-03 11:01:32 +0000 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2000-10-03 11:01:32 +0000 |
commit | d3f9b2a49051e512c0daa8fccdee7231fadff015 (patch) | |
tree | 5f08df676ffa28ba706c4ba17ab4fcece8353bec /lib/sendf.c | |
parent | 398d21696f8a5dcf98117be4c1f46f921cb2c77b (diff) |
introduced the new add_buffer() concept that makes the HTTP request to get
sent in only one shot
Diffstat (limited to 'lib/sendf.c')
-rw-r--r-- | lib/sendf.c | 90 |
1 files changed, 85 insertions, 5 deletions
diff --git a/lib/sendf.c b/lib/sendf.c index 77f403d70..8042ac8b9 100644 --- a/lib/sendf.c +++ b/lib/sendf.c @@ -53,6 +53,7 @@ #include <curl/curl.h> #include "urldata.h" +#include "sendf.h" #include <curl/mprintf.h> @@ -89,7 +90,7 @@ void failf(struct UrlData *data, char *fmt, ...) } /* sendf() sends the formated data to the server */ -int sendf(int fd, struct UrlData *data, char *fmt, ...) +size_t sendf(int fd, struct UrlData *data, char *fmt, ...) { size_t bytes_written; char *s; @@ -118,7 +119,7 @@ int sendf(int fd, struct UrlData *data, char *fmt, ...) /* * ftpsendf() sends the formated string as a ftp command to a ftp server */ -int ftpsendf(int fd, struct connectdata *conn, char *fmt, ...) +size_t ftpsendf(int fd, struct connectdata *conn, char *fmt, ...) { size_t bytes_written; char *s; @@ -154,9 +155,6 @@ size_t ssend(int fd, struct connectdata *conn, void *mem, size_t len) size_t bytes_written; struct UrlData *data=conn->data; /* conn knows data, not vice versa */ - if(data->bits.verbose) - fprintf(data->err, "> [binary output]\n"); - #ifdef USE_SSLEAY if (data->use_ssl) { bytes_written = SSL_write(data->ssl, mem, len); @@ -177,6 +175,88 @@ size_t ssend(int fd, struct connectdata *conn, void *mem, size_t len) return bytes_written; } +/* + * add_buffer_init() returns a fine buffer struct + */ +send_buffer *add_buffer_init(void) +{ + send_buffer *blonk; + blonk=(send_buffer *)malloc(sizeof(send_buffer)); + if(blonk) { + memset(blonk, 0, sizeof(send_buffer)); + return blonk; + } + return NULL; /* failed, go home */ +} + +/* + * add_buffer_send() sends a buffer and frees all associated memory. + */ +size_t add_buffer_send(int sockfd, struct connectdata *conn, send_buffer *in) +{ + if(in->buffer) + free(in->buffer); + free(in); + + if(conn->data->bits.verbose) { + fputs("> ", conn->data->err); + /* this data _may_ contain binary stuff */ + fwrite(in->buffer, in->size_used, 1, conn->data->err); + } + return ssend(sockfd, conn, in->buffer, in->size_used); +} +/* + * add_bufferf() builds a buffer from the formatted input + */ +CURLcode add_bufferf(send_buffer *in, char *fmt, ...) +{ + CURLcode result = CURLE_OUT_OF_MEMORY; + char *s; + va_list ap; + va_start(ap, fmt); + s = mvaprintf(fmt, ap); /* this allocs a new string to append */ + va_end(ap); + + if(s) { + result = add_buffer(in, s, strlen(s)); + free(s); + } + return result; +} + +/* + * add_buffer() appends a memory chunk to the existing one + */ +CURLcode add_buffer(send_buffer *in, void *inptr, size_t size) +{ + char *new_rb; + int new_size; + + if(size > 0) { + if(!in->buffer || + ((in->size_used + size) > (in->size_max - 1))) { + new_size = (in->size_used+size)*2; + if(in->buffer) + /* we have a buffer, enlarge the existing one */ + new_rb = (char *)realloc(in->buffer, new_size); + else + /* create a new buffer */ + new_rb = (char *)malloc(new_size); + + if(!new_rb) + return CURLE_OUT_OF_MEMORY; + + in->buffer = new_rb; + in->size_max = new_size; + } + memcpy(&in->buffer[in->size_used], inptr, size); + + in->size_used += size; + } + + return CURLE_OK; +} + |