xref: /curl/src/tool_helpers.c (revision 40c264db)
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   case PARAM_VAR_SYNTAX:
79     return "syntax error in --variable argument";
80   default:
81     return "unknown error";
82   }
83 }
84 
SetHTTPrequest(struct OperationConfig * config,HttpReq req,HttpReq * store)85 int SetHTTPrequest(struct OperationConfig *config, HttpReq req, HttpReq *store)
86 {
87   /* this mirrors the HttpReq enum in tool_sdecls.h */
88   const char *reqname[]= {
89     "", /* unspec */
90     "GET (-G, --get)",
91     "HEAD (-I, --head)",
92     "multipart formpost (-F, --form)",
93     "POST (-d, --data)",
94     "PUT (-T, --upload-file)"
95   };
96 
97   if((*store == TOOL_HTTPREQ_UNSPEC) ||
98      (*store == req)) {
99     *store = req;
100     return 0;
101   }
102   warnf(config->global, "You can only select one HTTP request method! "
103         "You asked for both %s and %s.",
104         reqname[req], reqname[*store]);
105 
106   return 1;
107 }
108 
customrequest_helper(struct OperationConfig * config,HttpReq req,char * method)109 void customrequest_helper(struct OperationConfig *config, HttpReq req,
110                           char *method)
111 {
112   /* this mirrors the HttpReq enum in tool_sdecls.h */
113   const char *dflt[]= {
114     "GET",
115     "GET",
116     "HEAD",
117     "POST",
118     "POST",
119     "PUT"
120   };
121 
122   if(!method)
123     ;
124   else if(curl_strequal(method, dflt[req])) {
125     notef(config->global, "Unnecessary use of -X or --request, %s is already "
126           "inferred.", dflt[req]);
127   }
128   else if(curl_strequal(method, "head")) {
129     warnf(config->global,
130           "Setting custom HTTP method to HEAD with -X/--request may not work "
131           "the way you want. Consider using -I/--head instead.");
132   }
133 }
134