Revert "Update to 7.40.1"
[platform/upstream/curl.git] / src / tool_setopt.c
index d387af1..62d94a6 100644 (file)
@@ -5,7 +5,7 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
  *
  * This software is licensed as described in the file COPYING, which
  * you should have received as part of this distribution. The terms
@@ -23,8 +23,6 @@
 
 #ifndef CURL_DISABLE_LIBCURL_OPTION
 
-#include <curl/curl.h>
-
 #define ENABLE_CURLX_PRINTF
 /* use our own printf() functions */
 #include "curlx.h"
@@ -41,6 +39,7 @@
 /* and finally any "NONE" value. */
 
 #define NV(e) {#e, e}
+#define NV1(e, v) {#e, (v)}
 #define NVEND {NULL, 0}         /* sentinel to mark end of list */
 
 const NameValue setopt_nv_CURLPROXY[] = {
@@ -53,7 +52,7 @@ const NameValue setopt_nv_CURLPROXY[] = {
   NVEND,
 };
 
-const NameValue setopt_nv_CURLAUTH[] = {
+const NameValueUnsigned setopt_nv_CURLAUTH[] = {
   NV(CURLAUTH_ANY),             /* combination */
   NV(CURLAUTH_ANYSAFE),         /* combination */
   NV(CURLAUTH_BASIC),
@@ -79,6 +78,9 @@ const NameValue setopt_nv_CURL_SSLVERSION[] = {
   NV(CURL_SSLVERSION_TLSv1),
   NV(CURL_SSLVERSION_SSLv2),
   NV(CURL_SSLVERSION_SSLv3),
+  NV(CURL_SSLVERSION_TLSv1_0),
+  NV(CURL_SSLVERSION_TLSv1_1),
+  NV(CURL_SSLVERSION_TLSv1_2),
   NVEND,
 };
 
@@ -97,6 +99,21 @@ const NameValue setopt_nv_CURLFTPSSL_CCC[] = {
   NVEND,
 };
 
+const NameValue setopt_nv_CURLUSESSL[] = {
+  NV(CURLUSESSL_NONE),
+  NV(CURLUSESSL_TRY),
+  NV(CURLUSESSL_CONTROL),
+  NV(CURLUSESSL_ALL),
+  NVEND,
+};
+
+const NameValue setopt_nv_CURL_NETRC[] = {
+  NV(CURL_NETRC_IGNORED),
+  NV(CURL_NETRC_OPTIONAL),
+  NV(CURL_NETRC_REQUIRED),
+  NVEND,
+};
+
 /* These mappings essentially triplicated - see
  * tool_libinfo.c and tool_paramhlp.c */
 const NameValue setopt_nv_CURLPROTO[] = {
@@ -124,6 +141,15 @@ const NameValue setopt_nv_CURLPROTO[] = {
   NVEND,
 };
 
+/* These options have non-zero default values. */
+static const NameValue setopt_nv_CURLNONZERODEFAULTS[] = {
+  NV1(CURLOPT_SSL_VERIFYPEER, 1),
+  NV1(CURLOPT_SSL_VERIFYHOST, 1),
+  NV1(CURLOPT_SSL_ENABLE_NPN, 1),
+  NV1(CURLOPT_SSL_ENABLE_ALPN, 1),
+  NVEND
+};
+
 /* Format and add code; jump to nomem on malloc error */
 #define ADD(args) do { \
   ret = easysrc_add args; \
@@ -193,7 +219,7 @@ static char *c_escape(const char *str)
       e += 2;
     }
     else if(! isprint(c)) {
-      sprintf(e, "\\%03o", c);
+      snprintf(e, 4, "\\%03o", c);
       e += 4;
     }
     else
@@ -204,7 +230,7 @@ static char *c_escape(const char *str)
 }
 
 /* setopt wrapper for enum types */
-CURLcode tool_setopt_enum(CURL *curl, struct Configurable *config,
+CURLcode tool_setopt_enum(CURL *curl, struct GlobalConfig *config,
                           const char *name, CURLoption tag,
                           const NameValue *nvlist, long lval)
 {
@@ -236,8 +262,8 @@ CURLcode tool_setopt_enum(CURL *curl, struct Configurable *config,
   return ret;
 }
 
-/* setopt wrapper for bit mask */
-CURLcode tool_setopt_flags(CURL *curl, struct Configurable *config,
+/* setopt wrapper for flags */
+CURLcode tool_setopt_flags(CURL *curl, struct GlobalConfig *config,
                            const char *name, CURLoption tag,
                            const NameValue *nvlist, long lval)
 {
@@ -264,7 +290,7 @@ CURLcode tool_setopt_flags(CURL *curl, struct Configurable *config,
         if(!rest)
           break;                /* handled them all */
         /* replace with all spaces for continuation line */
-        sprintf(preamble, "%*s", strlen(preamble), "");
+        snprintf(preamble, sizeof(preamble), "%*s", strlen(preamble), "");
       }
     }
     /* If any bits have no definition, output an explicit value.
@@ -278,8 +304,51 @@ CURLcode tool_setopt_flags(CURL *curl, struct Configurable *config,
   return ret;
 }
 
+/* setopt wrapper for bitmasks */
+CURLcode tool_setopt_bitmask(CURL *curl, struct GlobalConfig *config,
+                             const char *name, CURLoption tag,
+                             const NameValueUnsigned *nvlist,
+                             long lval)
+{
+  CURLcode ret = CURLE_OK;
+  bool skip = FALSE;
+
+  ret = curl_easy_setopt(curl, tag, lval);
+  if(!lval)
+    skip = TRUE;
+
+  if(config->libcurl && !skip && !ret) {
+    /* we only use this for real if --libcurl was used */
+    char preamble[80];
+    unsigned long rest = (unsigned long)lval;
+    const NameValueUnsigned *nv = NULL;
+    snprintf(preamble, sizeof(preamble),
+             "curl_easy_setopt(hnd, %s, ", name);
+    for(nv=nvlist; nv->name; nv++) {
+      if((nv->value & ~ rest) == 0) {
+        /* all value flags contained in rest */
+        rest &= ~ nv->value;    /* remove bits handled here */
+        CODE3("%s(long)%s%s",
+              preamble, nv->name, rest ? " |" : ");");
+        if(!rest)
+          break;                /* handled them all */
+        /* replace with all spaces for continuation line */
+        snprintf(preamble, sizeof(preamble), "%*s", strlen(preamble), "");
+      }
+    }
+    /* If any bits have no definition, output an explicit value.
+     * This could happen if new bits are defined and used
+     * but the NameValue list is not updated. */
+    if(rest)
+      CODE2("%s%luUL);", preamble, rest);
+  }
+
+ nomem:
+  return ret;
+}
+
 /* setopt wrapper for CURLOPT_HTTPPOST */
-CURLcode tool_setopt_httppost(CURL *curl, struct Configurable *config,
+CURLcode tool_setopt_httppost(CURL *curl, struct GlobalConfig *config,
                               const char *name, CURLoption tag,
                               struct curl_httppost *post)
 {
@@ -355,7 +424,7 @@ CURLcode tool_setopt_httppost(CURL *curl, struct Configurable *config,
 }
 
 /* setopt wrapper for curl_slist options */
-CURLcode tool_setopt_slist(CURL *curl, struct Configurable *config,
+CURLcode tool_setopt_slist(CURL *curl, struct GlobalConfig *config,
                            const char *name, CURLoption tag,
                            struct curl_slist *list)
 {
@@ -395,7 +464,7 @@ CURLcode tool_setopt_slist(CURL *curl, struct Configurable *config,
 
 /* generic setopt wrapper for all other options.
  * Some type information is encoded in the tag value. */
-CURLcode tool_setopt(CURL *curl, bool str, struct Configurable *config,
+CURLcode tool_setopt(CURL *curl, bool str, struct GlobalConfig *config,
                      const char *name, CURLoption tag, ...)
 {
   va_list arg;
@@ -412,10 +481,19 @@ CURLcode tool_setopt(CURL *curl, bool str, struct Configurable *config,
   if(tag < CURLOPTTYPE_OBJECTPOINT) {
     /* Value is expected to be a long */
     long lval = va_arg(arg, long);
+    long defval = 0L;
+    const NameValue *nv = NULL;
+    for(nv=setopt_nv_CURLNONZERODEFAULTS; nv->name; nv++) {
+      if(!strcmp(name, nv->name)) {
+        defval = nv->value;
+        break; /* found it */
+      }
+    }
+
     snprintf(buf, sizeof(buf), "%ldL", lval);
     value = buf;
     ret = curl_easy_setopt(curl, tag, lval);
-    if(!lval)
+    if(lval == defval)
       skip = TRUE;
   }
   else if(tag < CURLOPTTYPE_OFF_T) {