xref: /curl/src/tool_helpers.c (revision b0c82239)
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 #include "curlx.h"
29 
30 #include "tool_cfgable.h"
31 #include "tool_msgs.h"
32 #include "tool_getparam.h"
33 #include "tool_helpers.h"
34 
35 #include "memdebug.h" /* keep this as LAST include */
36 
37 /*
38 ** Helper functions that are used from more than one source file.
39 */
40 
param2text(ParameterError error)41 const char *param2text(ParameterError error)
42 {
43   switch(error) {
44   case PARAM_GOT_EXTRA_PARAMETER:
45     return "had unsupported trailing garbage";
46   case PARAM_OPTION_UNKNOWN:
47     return "is unknown";
48   case PARAM_OPTION_AMBIGUOUS:
49     return "is ambiguous";
50   case PARAM_REQUIRES_PARAMETER:
51     return "requires parameter";
52   case PARAM_BAD_USE:
53     return "is badly used here";
54   case PARAM_BAD_NUMERIC:
55     return "expected a proper numerical parameter";
56   case PARAM_NEGATIVE_NUMERIC:
57     return "expected a positive numerical parameter";
58   case PARAM_LIBCURL_DOESNT_SUPPORT:
59     return "the installed libcurl version does not support this";
60   case PARAM_LIBCURL_UNSUPPORTED_PROTOCOL:
61     return "a specified protocol is unsupported by libcurl";
62   case PARAM_NO_MEM:
63     return "out of memory";
64   case PARAM_NO_PREFIX:
65     return "the given option cannot be reversed with a --no- prefix";
66   case PARAM_NUMBER_TOO_LARGE:
67     return "too large number";
68   case PARAM_NO_NOT_BOOLEAN:
69     return "used '--no-' for option that is not a boolean";
70   case PARAM_CONTDISP_RESUME_FROM:
71     return "--continue-at and --remote-header-name cannot be combined";
72   case PARAM_READ_ERROR:
73     return "error encountered when reading a file";
74   case PARAM_EXPAND_ERROR:
75     return "variable expansion failure";
76   case PARAM_BLANK_STRING:
77     return "blank argument where content is expected";
78   default:
79     return "unknown error";
80   }
81 }
82 
SetHTTPrequest(struct OperationConfig * config,HttpReq req,HttpReq * store)83 int SetHTTPrequest(struct OperationConfig *config, HttpReq req, HttpReq *store)
84 {
85   /* this mirrors the HttpReq enum in tool_sdecls.h */
86   const char *reqname[]= {
87     "", /* unspec */
88     "GET (-G, --get)",
89     "HEAD (-I, --head)",
90     "multipart formpost (-F, --form)",
91     "POST (-d, --data)",
92     "PUT (-T, --upload-file)"
93   };
94 
95   if((*store == TOOL_HTTPREQ_UNSPEC) ||
96      (*store == req)) {
97     *store = req;
98     return 0;
99   }
100   warnf(config->global, "You can only select one HTTP request method! "
101         "You asked for both %s and %s.",
102         reqname[req], reqname[*store]);
103 
104   return 1;
105 }
106 
customrequest_helper(struct OperationConfig * config,HttpReq req,char * method)107 void customrequest_helper(struct OperationConfig *config, HttpReq req,
108                           char *method)
109 {
110   /* this mirrors the HttpReq enum in tool_sdecls.h */
111   const char *dflt[]= {
112     "GET",
113     "GET",
114     "HEAD",
115     "POST",
116     "POST",
117     "PUT"
118   };
119 
120   if(!method)
121     ;
122   else if(curl_strequal(method, dflt[req])) {
123     notef(config->global, "Unnecessary use of -X or --request, %s is already "
124           "inferred.", dflt[req]);
125   }
126   else if(curl_strequal(method, "head")) {
127     warnf(config->global,
128           "Setting custom HTTP method to HEAD with -X/--request may not work "
129           "the way you want. Consider using -I/--head instead.");
130   }
131 }
132