]> granicus.if.org Git - curl/commitdiff
mime: remove support "-" stdin pseudo-file name in curl_mime_filedata().
authorPatrick Monnerat <patrick@monnerat.net>
Sun, 3 Sep 2017 13:45:43 +0000 (14:45 +0100)
committerPatrick Monnerat <patrick@monnerat.net>
Sun, 3 Sep 2017 13:45:43 +0000 (14:45 +0100)
This feature is badly supported in Windows: as a replacement, a caller has
to use curl_mime_data_cb() with fread, fseek and possibly fclose
callbacks to process opened files.

The cli tool and documentation are updated accordingly.

The feature is however kept internally for form API compatibility, with
the known caveats it always had.

As a side effect, stdin size is not determined by the cli tool even if
possible and this results in a chunked transfer encoding. Test 173 is
updated accordingly.

docs/examples/smtp-mime.c
docs/libcurl/curl_mime_filedata.3
lib/formdata.c
lib/mime.c
src/tool_formparse.c
src/tool_setopt.c
tests/data/test173

index a467535a9c75ab8e28a4db87123313911ecb180e..2895dbc31b97d3ddba2278eb1814a501736417f6 100644 (file)
@@ -153,6 +153,9 @@ int main(void)
      * clean up in the end.
      */
     curl_easy_cleanup(curl);
+
+    /* Free multipart message. */
+    curl_mime_free(mime);
   }
 
   return (int)res;
index fcd86cc18dfb7fadbe1054972d8ef123121a8503..71562edd0368392e440ae79b575fbc0e527b79e6 100644 (file)
@@ -35,7 +35,6 @@ contents.
 \fIpart\fP is the part's to assign contents to.
 \fIfilename\fP points to the nul-terminated file's path name. The pointer can
 be NULL to detach previous part contents settings.
-If \fIfilename\fP is "-", part contents data will be read from stdin.
 Filename storage can be safely be reused after this call.
 
 As a side effect, the part's remote file name is set to the base name of the
index 129086deee7223d59113df8e76cb27f4ec939faa..b7ea21c8c55c13f57ec7a4ea59cdba0d730d13c4 100644 (file)
@@ -897,7 +897,10 @@ CURLcode Curl_getformdata(struct Curl_easy *data,
           clen = -1;
 
         if(post->flags & (HTTPPOST_FILENAME | HTTPPOST_READFILE)) {
-          result = curl_mime_filedata(part, file->contents);
+          if(!strcmp(file->contents, "-"))
+            result = Curl_mime_file(part, stdin, 0);
+          else
+            result = curl_mime_filedata(part, file->contents);
           if(!result && (post->flags & HTTPPOST_READFILE))
             result = curl_mime_filename(part, NULL);
         }
index 11d387d62135adb294eebd50dd740b51f754588d..9e1336475bd1e4271f3a0a3522b9d908e5335beb 100644 (file)
@@ -634,8 +634,18 @@ static int mime_part_rewind(struct Curl_mimepart *part)
     targetstate = MIMESTATE_BODY;
   if(part->state.state > targetstate) {
     res = CURL_SEEKFUNC_CANTSEEK;
-    if(part->seekfunc)
+    if(part->seekfunc) {
       res = part->seekfunc(part->arg, part->origin, SEEK_SET);
+      switch(res) {
+      case CURL_SEEKFUNC_OK:
+      case CURL_SEEKFUNC_FAIL:
+      case CURL_SEEKFUNC_CANTSEEK:
+        break;
+      default:
+        res = CURL_SEEKFUNC_FAIL;
+        break;
+      }
+    }
   }
 
   if(res == CURL_SEEKFUNC_OK)
@@ -907,9 +917,6 @@ CURLcode curl_mime_filedata(struct Curl_mimepart *part, const char *filename)
   if(!part || !filename)
     return CURLE_BAD_FUNCTION_ARGUMENT;
 
-  if(!strcmp(filename, "-"))
-    return Curl_mime_file(part, stdin, 0);
-
   if(stat(filename, &sbuf) || access(filename, R_OK))
     result = CURLE_READ_ERROR;
 
index cf19c05088a4051d36a77d299f753cdb06884095..fc5162eed1a6f169989c86dbaf315e3cfd72aa73 100644 (file)
@@ -347,6 +347,21 @@ static int get_param_part(struct OperationConfig *config, char **str,
   return sep & 0xFF;
 }
 
+/* Check if file is "-". If so, use a callback to read OUR stdin (to
+ * workaround Windows DLL file handle caveat).
+ * Else use curl_mime_filedata(). */
+static CURLcode file_or_stdin(curl_mimepart *part, const char *file)
+{
+  CURLcode ret = CURLE_OK;
+
+  if(strcmp(file, "-"))
+    return curl_mime_filedata(part, file);
+
+  return curl_mime_data_cb(part, -1, (curl_read_callback) fread,
+                           (curl_seek_callback) fseek, NULL, stdin);
+}
+
+
 /***************************************************************************
  *
  * formparse()
@@ -547,9 +562,9 @@ int formparse(struct OperationConfig *config,
         }
 
         /* Setup file in part. */
-        res = curl_mime_filedata(part, data);
+        res = file_or_stdin(part, data);
         if(res) {
-          warnf(config->global, "curl_mime_filedata failed!\n");
+          warnf(config->global, "setting file %s  failed!\n", data);
           if(res != CURLE_READ_ERROR) {
             if(subparts != *mimecurrent)
               curl_mime_free(subparts);
@@ -619,9 +634,9 @@ int formparse(struct OperationConfig *config,
         }
 
         /* Setup file in part. */
-        res = curl_mime_filedata(part, data);
+        res = file_or_stdin(part, data);
         if(res) {
-          warnf(config->global, "curl_mime_filedata failed!\n");
+          warnf(config->global, "setting file %s failed!\n", data);
           if(res != CURLE_READ_ERROR) {
             Curl_safefree(contents);
             return 22;
index 4e25e9e12511689af11c30972587c7cc6ecd2e2a..19646ea696b449efd9b7ed979fb03aceff714624 100644 (file)
@@ -450,9 +450,11 @@ static CURLcode libcurl_generate_mime(curl_mime *mime, int *mimeno)
             filename = part->filename;
         }
         break;
-      case MIMEKIND_FILE:
-        /* Can only be stdin in the current context. */
-        CODE1("curl_mime_file(part%d, \"-\");", *mimeno);
+      case MIMEKIND_CALLBACK:
+        /* Can only be reading stdin in the current context. */
+        CODE1("curl_mime_data_cb(part%d, -1, (curl_read_callback) fread, \\",
+              *mimeno);
+        CODE0("                  (curl_seek_callback) fseek, NULL, stdin);");
         break;
       case MIMEKIND_DATA:
 #ifdef CURL_DOES_CONVERSIONS
index 865ef7ba239148efa6ae9a22557bfc771db3512e..754950105eb37a1e98cdb890b6c89605816ca181 100644 (file)
@@ -53,9 +53,11 @@ POST /we/want/173 HTTP/1.1
 User-Agent: curl/7.12.1-CVS (i686-pc-linux-gnu) libcurl/7.12.1-CVS OpenSSL/0.9.6b ipv6 zlib/1.1.4 GSS libidn/0.4.6\r
 Host: %HOSTIP:%HTTPPORT\r
 Accept: */*\r
-Content-Length: 360\r
+Transfer-Encoding: chunked\r
+Expect: 100-continue\r
 Content-Type: multipart/form-data; boundary=----------------------------5dbea401cd8c\r
 \r
+168\r
 ------------------------------5dbea401cd8c\r
 Content-Disposition: form-data; name="field1"\r
 \r
@@ -74,6 +76,9 @@ line7
 line8
 \r
 ------------------------------5dbea401cd8c--\r
+\r
+0\r
+\r
 </protocol>
 </verify>
 </testcase>