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