tool_helpers.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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. ***************************************************************************/
  22. #include "tool_setup.h"
  23. #include "strcase.h"
  24. #define ENABLE_CURLX_PRINTF
  25. /* use our own printf() functions */
  26. #include "curlx.h"
  27. #include "tool_cfgable.h"
  28. #include "tool_msgs.h"
  29. #include "tool_getparam.h"
  30. #include "tool_helpers.h"
  31. #include "memdebug.h" /* keep this as LAST include */
  32. /*
  33. ** Helper functions that are used from more than one source file.
  34. */
  35. const char *param2text(int res)
  36. {
  37. ParameterError error = (ParameterError)res;
  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 doesn't 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 can't be reversed with a --no- prefix";
  61. case PARAM_NUMBER_TOO_LARGE:
  62. return "too large number";
  63. case PARAM_NO_NOT_BOOLEAN:
  64. return "used '--no-' for option that isn't a boolean";
  65. default:
  66. return "unknown error";
  67. }
  68. }
  69. int SetHTTPrequest(struct OperationConfig *config, HttpReq req, HttpReq *store)
  70. {
  71. /* this mirrors the HttpReq enum in tool_sdecls.h */
  72. const char *reqname[]= {
  73. "", /* unspec */
  74. "GET (-G, --get)",
  75. "HEAD (-I, --head)",
  76. "multipart formpost (-F, --form)",
  77. "POST (-d, --data)"
  78. };
  79. if((*store == HTTPREQ_UNSPEC) ||
  80. (*store == req)) {
  81. *store = req;
  82. return 0;
  83. }
  84. warnf(config->global, "You can only select one HTTP request method! "
  85. "You asked for both %s and %s.\n",
  86. reqname[req], reqname[*store]);
  87. return 1;
  88. }
  89. void customrequest_helper(struct OperationConfig *config, HttpReq req,
  90. char *method)
  91. {
  92. /* this mirrors the HttpReq enum in tool_sdecls.h */
  93. const char *dflt[]= {
  94. "GET",
  95. "GET",
  96. "HEAD",
  97. "POST",
  98. "POST"
  99. };
  100. if(!method)
  101. ;
  102. else if(curl_strequal(method, dflt[req])) {
  103. notef(config->global, "Unnecessary use of -X or --request, %s is already "
  104. "inferred.\n", dflt[req]);
  105. }
  106. else if(curl_strequal(method, "head")) {
  107. warnf(config->global,
  108. "Setting custom HTTP method to HEAD with -X/--request may not work "
  109. "the way you want. Consider using -I/--head instead.\n");
  110. }
  111. }