tool_helpers.c 4.1 KB

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