You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.6 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015 Mark Roszko <mark.roszko@gmail.com>
  5. * Copyright (C) 2015 KiCad Developers, see CHANGELOG.TXT for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <kicad_curl/kicad_curl_easy.h>
  25. #include <cstddef>
  26. #include <exception>
  27. #include <stdarg.h>
  28. #include <sstream>
  29. #include <richio.h>
  30. static size_t write_callback( void* contents, size_t size, size_t nmemb, void* userp )
  31. {
  32. size_t realsize = size * nmemb;
  33. std::string* p = (std::string*) userp;
  34. p->append( (const char*) contents, realsize );
  35. return realsize;
  36. }
  37. KICAD_CURL_EASY::KICAD_CURL_EASY() :
  38. m_headers( NULL )
  39. {
  40. // Call KICAD_CURL::Init() from in here everytime, but only the first time
  41. // will incur any overhead. This strategy ensures that libcurl is never loaded
  42. // unless it is needed.
  43. KICAD_CURL::Init();
  44. m_CURL = curl_easy_init();
  45. if( !m_CURL )
  46. {
  47. THROW_IO_ERROR( "Unable to initialize CURL session" );
  48. }
  49. curl_easy_setopt( m_CURL, CURLOPT_WRITEFUNCTION, write_callback );
  50. curl_easy_setopt( m_CURL, CURLOPT_WRITEDATA, (void*) &m_buffer );
  51. }
  52. KICAD_CURL_EASY::~KICAD_CURL_EASY()
  53. {
  54. if( m_headers )
  55. curl_slist_free_all( m_headers );
  56. curl_easy_cleanup( m_CURL );
  57. }
  58. void KICAD_CURL_EASY::Perform()
  59. {
  60. if( m_headers )
  61. {
  62. curl_easy_setopt( m_CURL, CURLOPT_HTTPHEADER, m_headers );
  63. }
  64. // bonus: retain worst case memory allocation, should re-use occur
  65. m_buffer.clear();
  66. CURLcode res = curl_easy_perform( m_CURL );
  67. if( res != CURLE_OK )
  68. {
  69. std::string msg = StrPrintf( "curl_easy_perform()=%d: %s",
  70. res, GetErrorText( res ).c_str() );
  71. THROW_IO_ERROR( msg );
  72. }
  73. }