example24.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c) <2002-2005> <Jean-Philippe Barrette-LaPierre>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (curlpp), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <curlpp/cURLpp.hpp>
  24. #include <curlpp/Easy.hpp>
  25. #include <curlpp/Options.hpp>
  26. #include <curlpp/Exception.hpp>
  27. #include <curlpp/Infos.hpp>
  28. #include <cstdlib>
  29. struct MethodClass
  30. {
  31. private:
  32. MethodClass();
  33. public:
  34. MethodClass(std::ostream * stream)
  35. : mStream(stream)
  36. , writeRound(0)
  37. {}
  38. // Helper Class for reading result from remote host
  39. size_t debug(curlpp::Easy *handle, curl_infotype type, char* ptr, size_t size)
  40. {
  41. ++writeRound;
  42. curlpp::options::Url url;
  43. handle->getOpt(url);
  44. // Calculate the real size of the incoming buffer
  45. std::cerr << "write round: " << writeRound << ", url: " << url.getValue() << ", type: " << type << std::endl;
  46. mStream->write(ptr, size);
  47. // return the real size of the buffer...
  48. return size;
  49. };
  50. // Public member vars
  51. std::ostream * mStream;
  52. unsigned writeRound;
  53. };
  54. int main(int argc, char *argv[])
  55. {
  56. if(argc != 2) {
  57. std::cerr << argv[0] << ": Wrong number of arguments" << std::endl
  58. << argv[0] << ": Usage: " << " url "
  59. << std::endl;
  60. return EXIT_FAILURE;
  61. }
  62. char *url = argv[1];
  63. try {
  64. curlpp::Cleanup cleaner;
  65. curlpp::Easy request;
  66. MethodClass mObject(&std::cerr);
  67. // Set the debug callback to enable cURL
  68. // to write result in a stream
  69. using namespace std::placeholders;
  70. curlpp::options::DebugFunction * test = new curlpp::options::DebugFunction(std::bind(&MethodClass::debug, &mObject, &request, _1, _2, _3));
  71. request.setOpt(test);
  72. // Setting the URL to retrive.
  73. request.setOpt(new curlpp::options::Url(url));
  74. request.setOpt(new curlpp::options::Verbose(true));
  75. request.perform();
  76. return EXIT_SUCCESS;
  77. }
  78. catch ( curlpp::LogicError & e ) {
  79. std::cout << e.what() << std::endl;
  80. }
  81. catch ( curlpp::RuntimeError & e ) {
  82. std::cout << e.what() << std::endl;
  83. }
  84. return EXIT_FAILURE;
  85. }