UID2-Client-XS

 view release on metacpan or  search on metacpan

ext/uid2-client-cpp11/lib/external/httplib.h  view on Meta::CPAN

        void set_default_headers(Headers headers);

        void set_tcp_nodelay(bool on);
        void set_socket_options(SocketOptions socket_options);

        void set_connection_timeout(time_t sec, time_t usec = 0);
        void set_read_timeout(time_t sec, time_t usec = 0);
        void set_write_timeout(time_t sec, time_t usec = 0);

        void set_basic_auth(const char* username, const char* password);
        void set_bearer_token_auth(const char* token);
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
        void set_digest_auth(const char* username, const char* password);
#endif

        void set_keep_alive(bool on);
        void set_follow_location(bool on);

        void set_compress(bool on);

        void set_decompress(bool on);

        void set_interface(const char* intf);

        void set_proxy(const char* host, int port);
        void set_proxy_basic_auth(const char* username, const char* password);
        void set_proxy_bearer_token_auth(const char* token);
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
        void set_proxy_digest_auth(const char* username, const char* password);
#endif

#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
        void enable_server_certificate_verification(bool enabled);
#endif

        void set_logger(Logger logger);

        // SSL
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
        void set_ca_cert_path(const char* ca_cert_file_path,
            const char* ca_cert_dir_path = nullptr);

        void set_ca_cert_store(X509_STORE* ca_cert_store);

        long get_openssl_verify_result() const;

        SSL_CTX* ssl_context() const;
#endif

    private:
        std::unique_ptr<ClientImpl> cli_;

#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
        bool is_ssl_ = false;
#endif
    };

#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
    class SSLServer : public Server {
    public:
        SSLServer(const char* cert_path, const char* private_key_path,
            const char* client_ca_cert_file_path = nullptr,
            const char* client_ca_cert_dir_path = nullptr);

        SSLServer(X509* cert, EVP_PKEY* private_key,
            X509_STORE* client_ca_cert_store = nullptr);

        ~SSLServer() override;

        bool is_valid() const override;

    private:
        bool process_and_close_socket(socket_t sock) override;

        SSL_CTX* ctx_;
        std::mutex ctx_mutex_;
    };

    class SSLClient : public ClientImpl {
    public:
        explicit SSLClient(const std::string& host);

        explicit SSLClient(const std::string& host, int port);

        explicit SSLClient(const std::string& host, int port,
            const std::string& client_cert_path,
            const std::string& client_key_path);

        explicit SSLClient(const std::string& host, int port, X509* client_cert,
            EVP_PKEY* client_key);

        ~SSLClient() override;

        bool is_valid() const override;

        void set_ca_cert_path(const char* ca_cert_file_path,
            const char* ca_cert_dir_path = nullptr);

        void set_ca_cert_store(X509_STORE* ca_cert_store);

        long get_openssl_verify_result() const;

        SSL_CTX* ssl_context() const;

    private:
        bool create_and_connect_socket(Socket& socket, Error& error) override;
        void shutdown_ssl(Socket& socket, bool shutdown_gracefully) override;

        bool process_socket(const Socket& socket,
            std::function<bool(Stream& strm)> callback) override;
        bool is_ssl() const override;

        bool connect_with_proxy(Socket& sock, Response& res, bool& success,
            Error& error);
        bool initialize_ssl(Socket& socket, Error& error);

        bool load_certs();

        bool verify_host(X509* server_cert) const;
        bool verify_host_with_subject_alt_name(X509* server_cert) const;
        bool verify_host_with_common_name(X509* server_cert) const;
        bool check_host_name(const char* pattern, size_t pattern_len) const;

        SSL_CTX* ctx_;
        std::mutex ctx_mutex_;

ext/uid2-client-cpp11/lib/external/httplib.h  view on Meta::CPAN

            write_timeout_sec_(write_timeout_sec),
            write_timeout_usec_(write_timeout_usec) {
            SSL_clear_mode(ssl, SSL_MODE_AUTO_RETRY);
        }

        inline SSLSocketStream::~SSLSocketStream() {}

        inline bool SSLSocketStream::is_readable() const {
            return detail::select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0;
        }

        inline bool SSLSocketStream::is_writable() const {
            return detail::select_write(sock_, write_timeout_sec_, write_timeout_usec_) >
                0;
        }

        inline ssize_t SSLSocketStream::read(char* ptr, size_t size) {
            if (SSL_pending(ssl_) > 0) {
                return SSL_read(ssl_, ptr, static_cast<int>(size));
            }
            else if (is_readable()) {
                auto ret = SSL_read(ssl_, ptr, static_cast<int>(size));
                if (ret < 0) {
                    auto err = SSL_get_error(ssl_, ret);
                    while (err == SSL_ERROR_WANT_READ) {
                        if (SSL_pending(ssl_) > 0) {
                            return SSL_read(ssl_, ptr, static_cast<int>(size));
                        }
                        else if (is_readable()) {
                            ret = SSL_read(ssl_, ptr, static_cast<int>(size));
                            if (ret >= 0) { return ret; }
                            err = SSL_get_error(ssl_, ret);
                        }
                        else {
                            return -1;
                        }
                    }
                }
                return ret;
            }
            return -1;
        }

        inline ssize_t SSLSocketStream::write(const char* ptr, size_t size) {
            if (is_writable()) { return SSL_write(ssl_, ptr, static_cast<int>(size)); }
            return -1;
        }

        inline void SSLSocketStream::get_remote_ip_and_port(std::string& ip,
            int& port) const {
            detail::get_remote_ip_and_port(sock_, ip, port);
        }

        inline socket_t SSLSocketStream::socket() const { return sock_; }

        static SSLInit sslinit_;

    } // namespace detail

    // SSL HTTP server implementation
    inline SSLServer::SSLServer(const char* cert_path, const char* private_key_path,
        const char* client_ca_cert_file_path,
        const char* client_ca_cert_dir_path) {
        ctx_ = SSL_CTX_new(SSLv23_server_method());

        if (ctx_) {
            SSL_CTX_set_options(ctx_,
                SSL_OP_ALL | SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
                SSL_OP_NO_COMPRESSION |
                SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION);

            // auto ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
            // SSL_CTX_set_tmp_ecdh(ctx_, ecdh);
            // EC_KEY_free(ecdh);

            if (SSL_CTX_use_certificate_chain_file(ctx_, cert_path) != 1 ||
                SSL_CTX_use_PrivateKey_file(ctx_, private_key_path, SSL_FILETYPE_PEM) !=
                1) {
                SSL_CTX_free(ctx_);
                ctx_ = nullptr;
            }
            else if (client_ca_cert_file_path || client_ca_cert_dir_path) {
                // if (client_ca_cert_file_path) {
                //   auto list = SSL_load_client_CA_file(client_ca_cert_file_path);
                //   SSL_CTX_set_client_CA_list(ctx_, list);
                // }

                SSL_CTX_load_verify_locations(ctx_, client_ca_cert_file_path,
                    client_ca_cert_dir_path);

                SSL_CTX_set_verify(
                    ctx_,
                    SSL_VERIFY_PEER |
                    SSL_VERIFY_FAIL_IF_NO_PEER_CERT, // SSL_VERIFY_CLIENT_ONCE,
                    nullptr);
            }
        }
    }

    inline SSLServer::SSLServer(X509* cert, EVP_PKEY* private_key,
        X509_STORE* client_ca_cert_store) {
        ctx_ = SSL_CTX_new(SSLv23_server_method());

        if (ctx_) {
            SSL_CTX_set_options(ctx_,
                SSL_OP_ALL | SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
                SSL_OP_NO_COMPRESSION |
                SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION);

            if (SSL_CTX_use_certificate(ctx_, cert) != 1 ||
                SSL_CTX_use_PrivateKey(ctx_, private_key) != 1) {
                SSL_CTX_free(ctx_);
                ctx_ = nullptr;
            }
            else if (client_ca_cert_store) {

                SSL_CTX_set_cert_store(ctx_, client_ca_cert_store);

                SSL_CTX_set_verify(
                    ctx_,
                    SSL_VERIFY_PEER |
                    SSL_VERIFY_FAIL_IF_NO_PEER_CERT, // SSL_VERIFY_CLIENT_ONCE,
                    nullptr);
            }
        }
    }

    inline SSLServer::~SSLServer() {
        if (ctx_) { SSL_CTX_free(ctx_); }
    }

    inline bool SSLServer::is_valid() const { return ctx_; }

    inline bool SSLServer::process_and_close_socket(socket_t sock) {
        auto ssl = detail::ssl_new(
            sock, ctx_, ctx_mutex_,
            [&](SSL* ssl) {
                return detail::ssl_connect_or_accept_nonblocking(
                    sock, ssl, SSL_accept, read_timeout_sec_, read_timeout_usec_);
            },
            [](SSL* /*ssl*/) { return true; });

        bool ret = false;
        if (ssl) {
            ret = detail::process_server_socket_ssl(
                ssl, sock, keep_alive_max_count_, keep_alive_timeout_sec_,
                read_timeout_sec_, read_timeout_usec_, write_timeout_sec_,
                write_timeout_usec_,
                [this, ssl](Stream& strm, bool close_connection,
                    bool& connection_closed) {
                        return process_request(strm, close_connection, connection_closed,
                            [&](Request& req) { req.ssl = ssl; });
                });

            // Shutdown gracefully if the result seemed successful, non-gracefully if
            // the connection appeared to be closed.
            const bool shutdown_gracefully = ret;
            detail::ssl_delete(ctx_mutex_, ssl, shutdown_gracefully);
        }

        detail::shutdown_socket(sock);
        detail::close_socket(sock);
        return ret;
    }

    // SSL HTTP client implementation
    inline SSLClient::SSLClient(const std::string& host)
        : SSLClient(host, 443, std::string(), std::string()) {}

    inline SSLClient::SSLClient(const std::string& host, int port)
        : SSLClient(host, port, std::string(), std::string()) {}



( run in 0.278 second using v1.01-cache-2.11-cpan-eab888a1d7d )