Hi, I attach a patch with some changes and a test case. In the patch: -- I copy my 'trailer_headers_buf' into the original buffer and I free mine at the end. (no leaks from 'trailer_headers_buf' anymore) -- I added a bool variable 'done' so as to update 'data->req.upload_done' at its original place.
0003-CURLOPT_TRAILERFUNCTION-support-chunked-encoding-req.patch
Description: Binary data
/*************************************************************************** * _ _ ____ _ * Project ___| | | | _ \| | * / __| | | | |_) | | * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * * Copyright (C) 1998 - 2013, Daniel Stenberg, <[email protected]>, et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms * are also available at http://curl.haxx.se/docs/copyright.html. * * You may opt to use, copy, modify, merge, publish, distribute and/or sell * copies of the Software, and permit persons to whom the Software is * furnished to do so, under the terms of the COPYING file. * * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY * KIND, either express or implied. * ***************************************************************************/ #include "test.h"
#include "memdebug.h"
/*
*
* This example shows an HTTP PUT (chuncked), with a trailer header sent at
* the end of the last chunk(zero-chunk).
*
*/
size_t filecode;
/* Trailer header callback function */
struct curl_slist * trailerheader_callback(CURL *handle, void *userdata) {
struct curl_slist *trailer_headers = NULL;
int *code = (int *)userdata;
const char *buf_eof = "mytrailer: EOF";
const char *buf_error = "mytrailer: error";
if(!(*code)) {
if((trailer_headers = curl_slist_append(trailer_headers,
buf_eof)) == NULL) {
fprintf(stderr, "curl_slist_append() failed\n");
curl_easy_cleanup(handle);
curl_global_cleanup();
return TEST_ERR_MAJOR_BAD;
}
}
else {
if((trailer_headers = curl_slist_append(trailer_headers,
buf_error)) == NULL) {
fprintf(stderr, "curl_slist_append() failed\n");
curl_easy_cleanup(handle);
curl_global_cleanup();
return TEST_ERR_MAJOR_BAD;
}
}
return trailer_headers;
}
/* Read callback function */
size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream) {
size_t retcode;
retcode = fread(ptr, size, nmemb, stream);
if(!retcode) {
if(ferror(stream))
filecode = 1;
if(feof(stream))
filecode = 0;
}
return retcode;
}
int test(char *URL)
{
CURL *curl;
CURLcode res = CURLE_OK;
FILE *hd_src;
int error;
struct curl_slist *hl = NULL;
struct curl_slist *headerlist=NULL;
const char *buf_1 = "Transfer-Encoding: chunked";
const char *buf_2 = "Trailer: mytrailer";
if (!libtest_arg2) {
fprintf(stderr, "Usage: <url> <file-to-upload>\n");
return -1;
}
hd_src = fopen(libtest_arg2, "rb");
if(NULL == hd_src) {
error = ERRNO;
fprintf(stderr, "fopen() failed with error: %d %s\n",
error, strerror(error));
fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
return -2; /* if this happens things are major weird */
}
if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
fprintf(stderr, "curl_global_init() failed\n");
fclose(hd_src);
return TEST_ERR_MAJOR_BAD;
}
/* get a curl handle */
if ((curl = curl_easy_init()) == NULL) {
fprintf(stderr, "curl_easy_init() failed\n");
curl_global_cleanup();
fclose(hd_src);
return TEST_ERR_MAJOR_BAD;
}
/* build a list of commands to pass to libcurl */
if ((hl = curl_slist_append(headerlist, buf_1)) == NULL) {
fprintf(stderr, "curl_slist_append() failed\n");
curl_easy_cleanup(curl);
curl_global_cleanup();
fclose(hd_src);
return TEST_ERR_MAJOR_BAD;
}
if ((headerlist = curl_slist_append(hl, buf_2)) == NULL) {
fprintf(stderr, "curl_slist_append() failed\n");
curl_slist_free_all(hl);
curl_easy_cleanup(curl);
curl_global_cleanup();
fclose(hd_src);
return TEST_ERR_MAJOR_BAD;
}
headerlist = hl;
/* enable uploading */
test_setopt(curl, CURLOPT_UPLOAD, 1L);
/* enable verbose */
test_setopt(curl, CURLOPT_VERBOSE, 1L);
/* specify target */
test_setopt(curl,CURLOPT_URL, URL);
/* pointer to pass to the file read function */
test_setopt(curl, CURLOPT_READDATA, hd_src);
/* read callback function */
test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
/* trailer header callback function */
curl_easy_setopt(curl, CURLOPT_TRAILERFUNCTION, trailerheader_callback);
/* pointer to pass to the trailer header function */
curl_easy_setopt(curl, CURLOPT_TRAILERDATA, &filecode);
/* Now run off and do what you've been told! */
res = curl_easy_perform(curl);
test_cleanup:
/* clean up the FTP commands list */
curl_slist_free_all(headerlist);
/* close the local file */
fclose(hd_src);
curl_easy_cleanup(curl);
curl_global_cleanup();
return res;
}
------------------------------------------------------------------- List admin: http://cool.haxx.se/list/listinfo/curl-library Etiquette: http://curl.haxx.se/mail/etiquette.html
