+static int url_bio_create(BIO *b)
+{
+#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
+ BIO_set_init(b, 1);
+ BIO_set_data(b, NULL);
+ BIO_set_flags(b, 0);
+#else
+ b->init = 1;
+ b->ptr = NULL;
+ b->flags = 0;
+#endif
+ return 1;
+}
+
+static int url_bio_destroy(BIO *b)
+{
+ return 1;
+}
+
+#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
+#define GET_BIO_DATA(x) BIO_get_data(x);
+#else
+#define GET_BIO_DATA(x) (x)->ptr;
+#endif
+
+static int url_bio_bread(BIO *b, char *buf, int len)
+{
+ URLContext *h;
+ int ret;
+ h = GET_BIO_DATA(b);
+ ret = ffurl_read(h, buf, len);
+ if (ret >= 0)
+ return ret;
+ BIO_clear_retry_flags(b);
+ if (ret == AVERROR_EXIT)
+ return 0;
+ return -1;
+}
+
+static int url_bio_bwrite(BIO *b, const char *buf, int len)
+{
+ URLContext *h;
+ int ret;
+ h = GET_BIO_DATA(b);
+ ret = ffurl_write(h, buf, len);
+ if (ret >= 0)
+ return ret;
+ BIO_clear_retry_flags(b);
+ if (ret == AVERROR_EXIT)
+ return 0;
+ return -1;
+}
+
+static long url_bio_ctrl(BIO *b, int cmd, long num, void *ptr)
+{
+ if (cmd == BIO_CTRL_FLUSH) {
+ BIO_clear_retry_flags(b);
+ return 1;
+ }
+ return 0;
+}
+
+static int url_bio_bputs(BIO *b, const char *str)
+{
+ return url_bio_bwrite(b, str, strlen(str));
+}
+
+#if OPENSSL_VERSION_NUMBER < 0x1010000fL
+static BIO_METHOD url_bio_method = {
+ .type = BIO_TYPE_SOURCE_SINK,
+ .name = "urlprotocol bio",
+ .bwrite = url_bio_bwrite,
+ .bread = url_bio_bread,
+ .bputs = url_bio_bputs,
+ .bgets = NULL,
+ .ctrl = url_bio_ctrl,
+ .create = url_bio_create,
+ .destroy = url_bio_destroy,
+};
+#endif
+