xref: /curl/src/tool_helpers.c (revision 07bcae89)
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at https://curl.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 #include "tool_setup.h"
25 
26 #include "strcase.h"
27 
28 #define ENABLE_CURLX_PRINTF
29 /* use our own printf() functions */
30 #include "curlx.h"
31 
32 #include "tool_cfgable.h"
33 #include "tool_msgs.h"
34 #include "tool_getparam.h"
35 #include "tool_helpers.h"
36 
37 #include "memdebug.h" /* keep this as LAST include */
38 
39 /*
40 ** Helper functions that are used from more than one source file.
41 */
42 
param2text(int res)43 const char *param2text(int res)
44 {
45   ParameterError error = (ParameterError)res;
46   switch(error) {
47   case PARAM_GOT_EXTRA_PARAMETER:
48     return "had unsupported trailing garbage";
49   case PARAM_OPTION_UNKNOWN:
50     return "is unknown";
51   case PARAM_OPTION_AMBIGUOUS:
52     return "is ambiguous";
53   case PARAM_REQUIRES_PARAMETER:
54     return "requires parameter";
55   case PARAM_BAD_USE:
56     return "is badly used here";
57   case PARAM_BAD_NUMERIC:
58     return "expected a proper numerical parameter";
59   case PARAM_NEGATIVE_NUMERIC:
60     return "expected a positive numerical parameter";
61   case PARAM_LIBCURL_DOESNT_SUPPORT:
62     return "the installed libcurl version doesn't support this";
63   case PARAM_LIBCURL_UNSUPPORTED_PROTOCOL:
64     return "a specified protocol is unsupported by libcurl";
65   case PARAM_NO_MEM:
66     return "out of memory";
67   case PARAM_NO_PREFIX:
68     return "the given option can't be reversed with a --no- prefix";
69   case PARAM_NUMBER_TOO_LARGE:
70     return "too large number";
71   case PARAM_NO_NOT_BOOLEAN:
72     return "used '--no-' for option that isn't a boolean";
73   case PARAM_CONTDISP_SHOW_HEADER:
74     return "showing headers and --remote-header-name cannot be combined";
75   case PARAM_CONTDISP_RESUME_FROM:
76     return "--continue-at and --remote-header-name cannot be combined";
77   case PARAM_READ_ERROR:
78     return "error encountered when reading a file";
79   case PARAM_EXPAND_ERROR:
80     return "variable expansion failure";
81   case PARAM_BLANK_STRING:
82     return "blank argument where content is expected";
83   default:
84     return "unknown error";
85   }
86 }
87 
SetHTTPrequest(struct OperationConfig * config,HttpReq req,HttpReq * store)88 int SetHTTPrequest(struct OperationConfig *config, HttpReq req, HttpReq *store)
89 {
90   /* this mirrors the HttpReq enum in tool_sdecls.h */
91   const char *reqname[]= {
92     "", /* unspec */
93     "GET (-G, --get)",
94     "HEAD (-I, --head)",
95     "multipart formpost (-F, --form)",
96     "POST (-d, --data)",
97     "PUT (-T, --upload-file)"
98   };
99 
100   if((*store == HTTPREQ_UNSPEC) ||
101      (*store == req)) {
102     *store = req;
103     return 0;
104   }
105   warnf(config->global, "You can only select one HTTP request method! "
106         "You asked for both %s and %s.",
107         reqname[req], reqname[*store]);
108 
109   return 1;
110 }
111 
customrequest_helper(struct OperationConfig * config,HttpReq req,char * method)112 void customrequest_helper(struct OperationConfig *config, HttpReq req,
113                           char *method)
114 {
115   /* this mirrors the HttpReq enum in tool_sdecls.h */
116   const char *dflt[]= {
117     "GET",
118     "GET",
119     "HEAD",
120     "POST",
121     "POST",
122     "PUT"
123   };
124 
125   if(!method)
126     ;
127   else if(curl_strequal(method, dflt[req])) {
128     notef(config->global, "Unnecessary use of -X or --request, %s is already "
129           "inferred.", dflt[req]);
130   }
131   else if(curl_strequal(method, "head")) {
132     warnf(config->global,
133           "Setting custom HTTP method to HEAD with -X/--request may not work "
134           "the way you want. Consider using -I/--head instead.");
135   }
136 }
137