mirror of
https://github.com/curl/curl.git
synced 2026-08-26 01:53:31 +03:00
introduced the new add_buffer() concept that makes the HTTP request to get
sent in only one shot
This commit is contained in:
parent
398d21696f
commit
d3f9b2a490
3 changed files with 174 additions and 70 deletions
90
lib/sendf.c
90
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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue