cURLpp.hh 2.83 KB
Newer Older
1 2
// (C) Copyright 2015 by Autodesk, Inc.

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
//=============================================================================
//
//  namespace COMISO::cURLpp
//
//=============================================================================

//== COMPILE-TIME PACKAGE REQUIREMENTS ========================================
#ifndef COMISO_CURLPP_HH
#define COMISO_CURLPP_HH

#include <CoMISo/Config/config.hh>
#if COMISO_DOCLOUD_AVAILABLE

#include <string>
#include <stdio.h>

struct curl_slist; // cURL type forward declaration
typedef void CURL; // cURL type forward declaration

namespace COMISO {
namespace cURLpp { // some classes to wrap around the libcurl C data

struct Session
{
  Session();
  ~Session();
};

// TODO: This inheritance could be restrictive in the future for now is OK
class Request
{
public:
  Request();
  virtual ~Request();

  void set_url(const char* _url);

  void add_http_header(const char* _hdr);

  // TODO: REMOVE this security hole!!
  void disable_ssl_verification();

  void perform();

  const std::string& header() const { return hdr_; }
  const std::string& body() const { return bdy_; }

protected:
  static size_t write_func(const char* _ptr, const size_t _size, 
    const size_t _nmemb, void* _str);

protected:
  CURL* hnd_;

protected:
  //virtual functions to control the perform() behavior
  virtual void prepare() {}
  virtual void finalize() {}

private:
  curl_slist* http_hdr_;
  std::string hdr_;
  std::string bdy_;
};

class Post : public Request
{
public: 
  Post(const char* _post) : post_(_post) {}
  Post(const std::string& _post) : post_(_post) {}

protected:
  virtual void prepare();

private:
  std::string post_;
};

class Upload : public Request
{
protected:
  virtual void prepare();
  virtual void finalize();

  virtual size_t send_data() = 0;
};

class UploadFile : public Upload
{
public:
  UploadFile(const char* _filename) : filename_(_filename) {}
  UploadFile(const std::string& _filename) : filename_(_filename) {}

  virtual ~UploadFile();

protected:
  virtual size_t send_data();

private:
  std::string filename_;
  FILE* file_;
};

class UploadData : public Upload
{
public:
109
  explicit UploadData(const std::string& _buf) : buf_(_buf)  {}
110 111 112 113 114 115 116 117

protected:
  virtual size_t send_data();

private:
  class Buffer
  {
  public:
118
    explicit Buffer(const std::string& _data)
119 120 121 122 123 124 125
      : ptr_(_data.c_str()), len_(_data.size()), pos_(0) 
    {}

    static size_t copy(void* _target, const size_t _elem_size, 
      const size_t _n_elem, void* _from_buf);

    size_t length() const { return len_; }
126

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
  private:
    const char* ptr_;
    size_t len_;
    size_t pos_;
  };

  Buffer buf_;
};

class Get : public Request
{
protected:
  virtual void prepare();
};

class Delete : public Request
{
protected:
  virtual void prepare();
};

} // namespace cURLpp 
} // namespace COMISO 

#endif//COMISO_DOCLOUD_AVAILABLE
#endif//COMISO_CURLPP_HH