• Stars
    star
    982
  • Rank 44,739 (Top 1.0 %)
  • Language
    Rust
  • License
    MIT License
  • Created almost 10 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Rust bindings to libcurl

curl-rust

libcurl bindings for Rust

Latest Version Documentation License Build

Quick Start

use std::io::{stdout, Write};

use curl::easy::Easy;

// Print a web page onto stdout
fn main() {
    let mut easy = Easy::new();
    easy.url("https://www.rust-lang.org/").unwrap();
    easy.write_function(|data| {
        stdout().write_all(data).unwrap();
        Ok(data.len())
    }).unwrap();
    easy.perform().unwrap();

    println!("{}", easy.response_code().unwrap());
}
use curl::easy::Easy;

// Capture output into a local `Vec`.
fn main() {
    let mut dst = Vec::new();
    let mut easy = Easy::new();
    easy.url("https://www.rust-lang.org/").unwrap();

    let mut transfer = easy.transfer();
    transfer.write_function(|data| {
        dst.extend_from_slice(data);
        Ok(data.len())
    }).unwrap();
    transfer.perform().unwrap();
}

Post / Put requests

The put and post methods on Easy can configure the method of the HTTP request, and then read_function can be used to specify how data is filled in. This interface works particularly well with types that implement Read.

use std::io::Read;
use curl::easy::Easy;

fn main() {
    let mut data = "this is the body".as_bytes();

    let mut easy = Easy::new();
    easy.url("http://www.example.com/upload").unwrap();
    easy.post(true).unwrap();
    easy.post_field_size(data.len() as u64).unwrap();

    let mut transfer = easy.transfer();
    transfer.read_function(|buf| {
        Ok(data.read(buf).unwrap_or(0))
    }).unwrap();
    transfer.perform().unwrap();
}

Custom headers

Custom headers can be specified as part of the request:

use curl::easy::{Easy, List};

fn main() {
    let mut easy = Easy::new();
    easy.url("http://www.example.com").unwrap();

    let mut list = List::new();
    list.append("Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==").unwrap();
    easy.http_headers(list).unwrap();
    easy.perform().unwrap();
}

Keep alive

The handle can be re-used across multiple requests. Curl will attempt to keep the connections alive.

use curl::easy::Easy;

fn main() {
    let mut handle = Easy::new();

    handle.url("http://www.example.com/foo").unwrap();
    handle.perform().unwrap();

    handle.url("http://www.example.com/bar").unwrap();
    handle.perform().unwrap();
}

Multiple requests

The libcurl library provides support for sending multiple requests simultaneously through the "multi" interface. This is currently bound in the multi module of this crate and provides the ability to execute multiple transfers simultaneously. For more information, see that module.

Building

By default, this crate will attempt to dynamically link to the system-wide libcurl and the system-wide SSL library. Some of this behavior can be customized with various Cargo features:

  • ssl: Enable SSL/TLS support using the platform-default TLS backend. On Windows this is Schannel, on macOS Secure Transport, and OpenSSL (or equivalent) on all other platforms. Enabled by default.

  • rustls Enable SSL/TLS support via Rustls, a well-received alternative TLS backend written in Rust. Rustls is always statically linked. Disabled by default.

    Note that Rustls support is experimental within Curl itself and may have significant bugs, so we don't offer any sort of stability guarantee with this feature.

  • http2: Enable HTTP/2 support via libnghttp2. Disabled by default.

  • static-curl: Use a bundled libcurl version and statically link to it. Disabled by default.

  • static-ssl: Use a bundled OpenSSL version and statically link to it. Only applies on platforms that use OpenSSL. Disabled by default.

  • spnego: Enable SPNEGO support. Disabled by default.

  • upkeep_7_62_0: Enable curl_easy_upkeep() support, introduced in curl 7.62.0. Disabled by default.

  • poll_7_68_0: Enable curl_multi_poll()/curl_multi_wakeup() support, requires curl 7.68.0 or later. Disabled by default.

  • ntlm: Enable NTLM support in curl. Disabled by default.

  • windows-static-ssl: Enable Openssl support on Windows via the static build provided by vcpkg. Incompatible with ssl (use --no-default-features). Disabled by default.

    Note that to install openssl on windows via vcpkg the following commands needs to be ran:

    git clone https://github.com/microsoft/vcpkg
    cd vcpkg
    ./bootstrap-vcpkg.bat -disableMetrics
    ./vcpkg.exe integrate install
    ./vcpkg.exe install openssl:x64-windows-static-md

Version Support

The bindings have been developed using curl version 7.24.0. They should work with any newer version of curl and possibly with older versions, but this has not been tested.

Troubleshooting

Curl built against the NSS SSL library

If you encounter the following error message:

  [77] Problem with the SSL CA cert (path? access rights?)

That means most likely, that curl was linked against libcurl-nss.so due to installed libcurl NSS development files, and that the required library libnsspem.so is missing. See also the curl man page: "If curl is built against the NSS SSL library, the NSS PEM PKCS#11 module (libnsspem.so) needs to be available for this option to work properly."

In order to avoid this failure you can either

  • install the missing library (e.g. Debian: nss-plugin-pem), or
  • remove the libcurl NSS development files (e.g. Debian: libcurl4-nss-dev) and rebuild curl-rust.

License

The curl-rust crate is licensed under the MIT license, see LICENSE for more details.

More Repositories

1

rust-ffi-examples

FFI examples written in Rust
Makefile
1,160
star
2

toml-rs

A TOML encoding/decoding library for Rust
Rust
994
star
3

futures-await

Rust
733
star
4

tar-rs

Tar file reading/writing for Rust
Rust
551
star
5

ssh2-rs

Rust bindings for libssh2
Rust
450
star
6

cargo-vendor

Archived as subcommand is now part of Cargo itself
Rust
260
star
7

wasm-gc

gc-sections for wasm
Rust
247
star
8

coz-rs

Rust support for the coz Causal profiler, code now lives upstream -- https://github.com/plasma-umass/coz
Rust
211
star
9

tokio-process

Asynchronous process management for tokio
Rust
146
star
10

tokio-curl

Asynchronous HTTP client built on libcurl
Rust
108
star
11

filetime

Accessing file timestamps in a platform-agnostic fashion in Rust
Rust
108
star
12

tokio-signal

Unix signal handling for tokio
Rust
87
star
13

rlibc

77
star
14

bzip2-rs

libbz2 (bzip2 compression) bindings for Rust
C
77
star
15

dlmalloc-rs

dlmalloc ported into Rust
C
75
star
16

openssl-src-rs

Source code and logic to build OpenSSL from source
Rust
66
star
17

xz2-rs

Bindings to liblzma in Rust (xz streams in Rust)
Rust
65
star
18

wait-timeout

Waiting on a child process with a timeout in Rust
Rust
57
star
19

green-rs

Green Tasks for Rust
Rust
54
star
20

openssl-probe

Rust
51
star
21

socks5-rs

Implementation of a socks5 proxy server in Rust
Rust
49
star
22

jba

Javascript Gameboy
JavaScript
42
star
23

bufstream

A buffered I/O stream for Rust
Rust
30
star
24

wasm-sodium

PoC of libsodium being used in Rust on wasm32-unknown-unknown
Rust
25
star
25

wasm-interface-types-polyfill

Polyfill for WebAssembly Interface Types
Rust
25
star
26

port-of-rust

Docker images for Rust projects
Shell
24
star
27

splay-rs

A splay tree implementation written in Rust
Rust
24
star
28

talks

Talks I've given at various conferences
18
star
29

debug-cell

Debug RefCell which keeps track of stack traces in debug mode
Rust
18
star
30

link-config

Rust
17
star
31

nghttp2-rs

Rust
17
star
32

rack-shibboleth

Shibboleth meets Ruby and Rack
Ruby
17
star
33

rust-wasm-benchmark

JavaScript
14
star
34

cargo-fancy

Fancy output for Cargo
Rust
13
star
35

wbg-rand

Random numbers for wasm32-unknown-unknown in Rust
Rust
12
star
36

ipc-rs

IPC primitives for Rust
Rust
11
star
37

rustfmt-wasm

Dockerfile
9
star
38

oa-pubcookie

OmniAuth strategy when using Pubcookie or CMU's WebISO
Ruby
8
star
39

homebrew-formula

Ruby
7
star
40

paste

JS + CSS dependency managament for Rails 3
Ruby
7
star
41

rack-pubcookie

Pubcookie finally isn't tied to Apache, now it's in Ruby!
Ruby
7
star
42

cfg-specialize

Rust
7
star
43

rustc-auto-publish

Rust
6
star
44

tokio-named-pipes

Windows named pipes bindings for tokio
Rust
6
star
45

l4c

Rust
6
star
46

memset-nt

Non-temporal memset for RUst
Rust
6
star
47

wasm-cross-lang-lto-example

Shell
6
star
48

glimmer-experiments

Rust
6
star
49

xxhash2-rs

Rust bindings to libxxhash
Rust
5
star
50

witx-async-demo

Rust
5
star
51

hacku

hacku
JavaScript
4
star
52

socparse

Ruby
4
star
53

example-wasi-tools

Rust
4
star
54

differential-wasmtime-v8

Rust
4
star
55

golden-circle

The Golden Circle website registration and grading application
JavaScript
4
star
56

openssl-sys

Rust
4
star
57

futures-await-syn

futures-await temporary fork of dtolnay/syn
Rust
3
star
58

oddit

Oddit - Auditing application for CMU students
Ruby
3
star
59

rust-fannkuch-js-and-wasm

Rust
3
star
60

my-awesome-wasi-proposal

3
star
61

futures-spsc

Single-producer, single-consumer queues for Rust futures
Rust
3
star
62

rust-travis-deploy

Moved to https://github.com/rust-lang/simpleinfra
Rust
3
star
63

stamp-rs

Rust
2
star
64

twocan

Collaborative Crosswords!
Ruby
2
star
65

www

HTML
2
star
66

eeyore

Label tagging bot
Rust
2
star
67

io2

Rust
2
star
68

cpu-usage-over-time

Rust
2
star
69

complicated-linkage-example

Rust
2
star
70

fargo

A DC protocol implementation in Ruby
Ruby
2
star
71

rustjob

Rust
2
star
72

conduit-git-http-backend

Conduit handler for running `git http-backend`
Rust
2
star
73

tls-rs

Thread-Local-Storage (TLS) for Rust
2
star
74

rbindgen

Rust
2
star
75

movies

A proposed solution to the Movie Chain Runner problem
C
2
star
76

rustc-compile-time-analyze

Rust
1
star
77

dcfs

A FUSE filesystem over the DC protocol
Ruby
1
star
78

example-wasi-abi-up-to-date

JavaScript
1
star
79

wasmtime-unwinding-timings

Rust
1
star
80

futures-tools

1
star
81

longjmp-repro

Rust
1
star
82

ars

ar in Rust
Rust
1
star
83

weird-proc-macro-spans

Rust
1
star
84

recipe_box

Recipe Box as a web application
Ruby
1
star
85

bors2

WIP
Rust
1
star
86

crates.io-index-2018-09-20-squashed

Shell
1
star
87

webpush-server-proof-of-concept

Rust
1
star
88

wasm-component-ld

Command line linker for creating WebAssembly components
Rust
1
star
89

futures-await-quote

futures-await temporary fork of dtolnay/quote
Rust
1
star
90

cargo-apply

Rust
1
star
91

rustc-test

Extraction of libtest from rustc
Rust
1
star
92

cohort_radio

The best radio ever
Ruby
1
star
93

wasm64-timings

Rust
1
star