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