libcurl 的简单使用

使用方法

#include 

int main(int,char**)
{
	curl::easy easy;

    if (!easy) {
        std::cerr << "curl::easy::init failed" << std::endl;
        return 1;
    }

    easy.set_opt(curl::option::Url("https://cn.bing.com"));
    easy.set_opt(curl::option::Verbose(true));
    easy.set_opt(curl::option::Version(CURL_HTTP_VERSION_1_1));


    easy.perform();

    return 0;
}

以下为简易包装

#include 

namespace curl {
    template <typename T, size_t (T::*func)(const char *, size_t)>
    size_t write_func_thunk(void *data, size_t size, size_t bytesize, void *obj) {
        return (reinterpret_cast<T *>(obj)->*func)(reinterpret_cast<const char *>(data), size * bytesize);
    }
    
    struct easy {

        easy() : m_handle(curl_easy_init()) {

        }
        ~easy() {
            curl_easy_cleanup(m_handle);
        }

        template<typename Opt>
        int set_opt(Opt&& opt) {
           return curl_easy_setopt(m_handle, opt.option, opt.data());
        }

        operator bool() const {
            return m_handle;
        }

        CURL *handle() {
            return m_handle;
        }

        const CURL* handle() const {
            return m_handle;
        }

        int perform() {
            auto func = write_func_thunk<easy, &easy::write>;
            curl_easy_setopt(m_handle, CURLOPT_WRITEFUNCTION, func);
            curl_easy_setopt(m_handle, CURLOPT_WRITEDATA, this);
            return curl_easy_perform(m_handle);
        }

    private:
        size_t write(const char *data, size_t size) {
            return size;
        }

    private:
        CURL *m_handle;
    };

    namespace option {

        struct WriteFunction
        {
            static constexpr CURLoption option = CURLOPT_WRITEFUNCTION;


            template<typename Fn>
            WriteFunction(Fn func): m_data(static_cast<void *>(func)) {

            }

            void *data() {
                return m_data;
            }

            const void *data() const {
                return m_data;
            }

        private:
            void *m_data{nullptr};
        };

        struct Url {
            static constexpr CURLoption option = CURLOPT_URL;

            Url(const char *url):m_data(url) {
            }

            Url(std::string url): m_data(std::move(url)) {

            }

            char *data() {
                return &m_data[0];
            }

            const char *data() const {
                return m_data.data();
            }

        private:
            std::string m_data;
        };

        struct Verbose{

            static constexpr CURLoption option = CURLOPT_VERBOSE;

            Verbose(bool enable = true): m_data(enable) {}
            Verbose(long data): m_data(data) {}

            long data() const {
                return m_data;
            }
        private:
            long m_data{0};
        };

        struct Version {

            static constexpr CURLoption option = CURLOPT_HTTP_VERSION;

            Version(unsigned version = CURL_HTTP_VERSION_1_1)
                : m_data(version) {

            }

            unsigned data() const {
                return m_data;
            }

        private:
            unsigned m_data;
        };
    }

    namespace info {

        struct ContentType {
            ContentType(easy& handle)
                : m_code(0), m_data(32, 0) {
                char *contentType;
                m_code = curl_easy_getinfo(handle.handle(), CURLINFO_CONTENT_TYPE, &contentType);
                m_data = std::string(contentType);
            }

            const char *value() const {
                return m_data.c_str();
            }

        private:
            int m_code;
            std::string m_data;
        };
    }
}

你可能感兴趣的:(C++,网络,C++-三方库接入)