[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] [gnurl] 46/150: formdata: use the mime-content type functio
From: |
gnunet |
Subject: |
[GNUnet-SVN] [gnurl] 46/150: formdata: use the mime-content type function |
Date: |
Fri, 30 Mar 2018 16:48:20 +0200 |
This is an automated email from the git hooks/post-receive script.
ng0 pushed a commit to branch master
in repository gnurl.
commit 84ad1fd3047815f9c6e78728bb351b828eac10b1
Author: Daniel Stenberg <address@hidden>
AuthorDate: Sun Feb 4 09:23:11 2018 +0100
formdata: use the mime-content type function
Reduce code duplication by making Curl_mime_contenttype available and
used by the formdata function. This also makes the formdata function
recognize a set of more file extensions by default.
PR #2280 brought this to my attention.
Closes #2282
---
lib/formdata.c | 64 ++++++++--------------------------------------------------
lib/mime.c | 13 ++++--------
lib/mime.h | 5 +++++
3 files changed, 17 insertions(+), 65 deletions(-)
diff --git a/lib/formdata.c b/lib/formdata.c
index d0579c52f..f91241052 100644
--- a/lib/formdata.c
+++ b/lib/formdata.c
@@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
- * Copyright (C) 1998 - 2017, Daniel Stenberg, <address@hidden>, et al.
+ * Copyright (C) 1998 - 2018, Daniel Stenberg, <address@hidden>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@@ -155,60 +155,6 @@ static FormInfo * AddFormInfo(char *value,
/***************************************************************************
*
- * ContentTypeForFilename()
- *
- * Provides content type for filename if one of the known types (else
- * (either the prevtype or the default is returned).
- *
- * Returns some valid contenttype for filename.
- *
- ***************************************************************************/
-static const char *ContentTypeForFilename(const char *filename,
- const char *prevtype)
-{
- const char *contenttype = NULL;
- unsigned int i;
- /*
- * No type was specified, we scan through a few well-known
- * extensions and pick the first we match!
- */
- struct ContentType {
- const char *extension;
- const char *type;
- };
- static const struct ContentType ctts[]={
- {".gif", "image/gif"},
- {".jpg", "image/jpeg"},
- {".jpeg", "image/jpeg"},
- {".txt", "text/plain"},
- {".html", "text/html"},
- {".xml", "application/xml"}
- };
-
- if(prevtype)
- /* default to the previously set/used! */
- contenttype = prevtype;
- else
- contenttype = HTTPPOST_CONTENTTYPE_DEFAULT;
-
- if(filename) { /* in case a NULL was passed in */
- for(i = 0; i<sizeof(ctts)/sizeof(ctts[0]); i++) {
- if(strlen(filename) >= strlen(ctts[i].extension)) {
- if(strcasecompare(filename +
- strlen(filename) - strlen(ctts[i].extension),
- ctts[i].extension)) {
- contenttype = ctts[i].type;
- break;
- }
- }
- }
- }
- /* we have a contenttype by now */
- return contenttype;
-}
-
-/***************************************************************************
- *
* FormAdd()
*
* Stores a formpost parameter and builds the appropriate linked list.
@@ -627,9 +573,15 @@ CURLFORMcode FormAdd(struct curl_httppost **httppost,
!form->contenttype) {
char *f = form->flags & HTTPPOST_BUFFER?
form->showfilename : form->value;
+ char const *type;
+ type = Curl_mime_contenttype(f);
+ if(!type)
+ type = prevtype;
+ if(!type)
+ type = FILE_CONTENTTYPE_DEFAULT;
/* our contenttype is missing */
- form->contenttype = strdup(ContentTypeForFilename(f, prevtype));
+ form->contenttype = strdup(type);
if(!form->contenttype) {
return_value = CURL_FORMADD_MEMORY;
break;
diff --git a/lib/mime.c b/lib/mime.c
index e0853a9ed..0ccb346ee 100644
--- a/lib/mime.c
+++ b/lib/mime.c
@@ -51,10 +51,6 @@
#endif
-#define FILE_CONTENTTYPE_DEFAULT "application/octet-stream"
-#define MULTIPART_CONTENTTYPE_DEFAULT "multipart/mixed"
-#define DISPOSITION_DEFAULT "attachment"
-
#define READ_ERROR ((size_t) -1)
/* Encoders. */
@@ -1642,8 +1638,7 @@ static CURLcode add_content_type(struct curl_slist **slp,
boundary? boundary: "");
}
-
-static const char *ContentTypeForFilename(const char *filename)
+const char *Curl_mime_contenttype(const char *filename)
{
unsigned int i;
@@ -1715,14 +1710,14 @@ CURLcode Curl_mime_prepare_headers(curl_mimepart *part,
contenttype = MULTIPART_CONTENTTYPE_DEFAULT;
break;
case MIMEKIND_FILE:
- contenttype = ContentTypeForFilename(part->filename);
+ contenttype = Curl_mime_contenttype(part->filename);
if(!contenttype)
- contenttype = ContentTypeForFilename(part->data);
+ contenttype = Curl_mime_contenttype(part->data);
if(!contenttype && part->filename)
contenttype = FILE_CONTENTTYPE_DEFAULT;
break;
default:
- contenttype = ContentTypeForFilename(part->filename);
+ contenttype = Curl_mime_contenttype(part->filename);
break;
}
}
diff --git a/lib/mime.h b/lib/mime.h
index 920a8a77a..4d5c70404 100644
--- a/lib/mime.h
+++ b/lib/mime.h
@@ -30,6 +30,10 @@
#define MIME_USERHEADERS_OWNER (1 << 0)
#define MIME_BODY_ONLY (1 << 1)
+#define FILE_CONTENTTYPE_DEFAULT "application/octet-stream"
+#define MULTIPART_CONTENTTYPE_DEFAULT "multipart/mixed"
+#define DISPOSITION_DEFAULT "attachment"
+
/* Part source kinds. */
enum mimekind {
MIMEKIND_NONE = 0, /* Part not set. */
@@ -134,5 +138,6 @@ size_t Curl_mime_read(char *buffer, size_t size, size_t
nitems,
void *instream);
CURLcode Curl_mime_rewind(curl_mimepart *part);
CURLcode Curl_mime_add_header(struct curl_slist **slp, const char *fmt, ...);
+const char *Curl_mime_contenttype(const char *filename);
#endif /* HEADER_CURL_MIME_H */
--
To stop receiving notification emails like this one, please contact
address@hidden
- [GNUnet-SVN] [gnurl] 108/150: projects/README: remove reference to dead IDN link/package, (continued)
- [GNUnet-SVN] [gnurl] 108/150: projects/README: remove reference to dead IDN link/package, gnunet, 2018/03/30
- [GNUnet-SVN] [gnurl] 136/150: readwrite: make sure excess reads don't go beyond buffer end, gnunet, 2018/03/30
- [GNUnet-SVN] [gnurl] 139/150: openldap: white space changes, fixed up the copyright years, gnunet, 2018/03/30
- [GNUnet-SVN] [gnurl] 112/150: docs/MANUAL: formfind.pl is not accessible on the site anymore, gnunet, 2018/03/30
- [GNUnet-SVN] [gnurl] 147/150: CURLOPT_COOKIEFILE.3: "-" as file name means stdin, gnunet, 2018/03/30
- [GNUnet-SVN] [gnurl] 141/150: http2: mark the connection for close on GOAWAY, gnunet, 2018/03/30
- [GNUnet-SVN] [gnurl] 33/150: Curl_range: add check to ensure "from <= to", gnunet, 2018/03/30
- [GNUnet-SVN] [gnurl] 36/150: progress-bar.d: update to match implementation, gnunet, 2018/03/30
- [GNUnet-SVN] [gnurl] 41/150: parsedate: s/#if/#ifdef, gnunet, 2018/03/30
- [GNUnet-SVN] [gnurl] 53/150: curl_addrinfo.c: Allow Unix Domain Sockets to compile under Windows, gnunet, 2018/03/30
- [GNUnet-SVN] [gnurl] 46/150: formdata: use the mime-content type function,
gnunet <=
- [GNUnet-SVN] [gnurl] 32/150: Curl_range: commonize FTP and FILE range handling, gnunet, 2018/03/30
- [GNUnet-SVN] [gnurl] 92/150: curl --version: show PSL if the run-time lib has it enabled, gnunet, 2018/03/30
- [GNUnet-SVN] [gnurl] 11/150: parsedate: fix date parsing for systems with 32 bit long, gnunet, 2018/03/30
- [GNUnet-SVN] [gnurl] 102/150: form.d: rephrased somewhat, added two example command lines, gnunet, 2018/03/30
- [GNUnet-SVN] [gnurl] 60/150: http_chunks: don't write chunks twice with CURLOPT_HTTP_TRANSFER_DECODING on, gnunet, 2018/03/30
- [GNUnet-SVN] [gnurl] 62/150: sha256: build with OpenSSL < 0.9.8 too, gnunet, 2018/03/30
- [GNUnet-SVN] [gnurl] 72/150: curl_gssapi: make sure this file too uses our *printf(), gnunet, 2018/03/30
- [GNUnet-SVN] [gnurl] 61/150: README: language fix, gnunet, 2018/03/30
- [GNUnet-SVN] [gnurl] 119/150: CURLOPT_NOPROXY.3: mention how to list numerical IPv6 addresses, gnunet, 2018/03/30
- [GNUnet-SVN] [gnurl] 80/150: Revert "KNOWN_BUGS: 2.5 curl should not offer "ALPN: h2" when using https-proxy", gnunet, 2018/03/30