diff --git a/lib/formdata.c b/lib/formdata.c index 49e79545313c8c5f141a6fb883f92b9245049902..5419371deba839f72f5700e3af7b6f01c3491420 100644 --- a/lib/formdata.c +++ b/lib/formdata.c @@ -891,7 +891,7 @@ int curl_formget(struct curl_httppost *form, void *arg, Curl_formclean(&data); return -1; } - } while(nread == sizeof(buffer)); + } while(nread); } else { if(ptr->length != append(arg, ptr->line, ptr->length)) { @@ -1306,6 +1306,11 @@ static size_t readfromfile(struct Form *form, char *buffer, return 0; else nread = form->fread_func(buffer, 1, size, form->data->line); + + if(nread > size) + /* the read callback can return a value larger than the buffer but + treat any such as no data in this case */ + nread = 0; } else { if(!form->fp) { @@ -1316,9 +1321,9 @@ static size_t readfromfile(struct Form *form, char *buffer, } nread = fread(buffer, 1, size, form->fp); } - if(!nread || nread > size) { + if(!nread) { /* this is the last chunk from the file, move on */ - if(!callback) { + if(form->fp) { fclose(form->fp); form->fp = NULL; } diff --git a/tests/data/test1308 b/tests/data/test1308 index fe7509688c16583bfb35e273e208ff2216638968..88e9771ddf289594fea5779a0a5a02a39db6c68d 100644 --- a/tests/data/test1308 +++ b/tests/data/test1308 @@ -23,6 +23,9 @@ formpost unit tests unit1308 + +Piece of the file that is to uploaded as a formpost + diff --git a/tests/unit/unit1308.c b/tests/unit/unit1308.c index 6b2ab0da6cae7e57a6a0a4fcbca3453d74e2e316..80e6c57b0f0c8af1c905b5a821519ff9642eb352 100644 --- a/tests/unit/unit1308.c +++ b/tests/unit/unit1308.c @@ -75,4 +75,21 @@ UNITTEST_START curl_formfree(post); + /* start a new formpost with a file upload and formget */ + post = last = NULL; + + rc = curl_formadd(&post, &last, + CURLFORM_PTRNAME, "name of file field", + CURLFORM_FILE, "log/test-1308", + CURLFORM_FILENAME, "custom named file", + CURLFORM_END); + + fail_unless(rc == 0, "curl_formadd returned error"); + + rc = curl_formget(post, &total_size, print_httppost_callback); + fail_unless(rc == 0, "curl_formget returned error"); + fail_unless(total_size == 847, "curl_formget got wrong size back"); + + curl_formfree(post); + UNITTEST_STOP