読者です 読者をやめる 読者になる 読者になる

万年素人からHackerへの道

万年素人がHackerになれるまで殴り書きするぜ。

Rustでcurl

How do I make an HTTP request from Rust? - Stack Overflow

ここの回答がいい。

githubhttps://github.com/carllerche/curl-rustだ。

Cargo.toml

[dependencies.curl]
git = "https://github.com/carllerche/curl-rust"

を追加する。

先ほどのStackOverflowのはおかしいのでgithubのサンプルは変。

println!("body={:?}", resp.get_body());

これだと

body=[60, 33, 100, 111, 99, 116, 121, 112, 101, 32, 104, 116, 109, 108, 62, 10, 60, 104, 116, 109, 108, 62, 10, 60, 104, 101, 97, 100, 62, 10, 32, 32, 32, 32, 60, 116, 105, 116, 108, 101, 62, 69, 120, 97, 109, 112, 108, 101, 32, 68, 111, 109, 97, 105, 110, 60, 47, 116, 105, 116, 108, 101, 62, 10, 10, 32, 32, 32, 32, 60, 109, 101, 116, 97, 32, 99, 104, 97, 114, 115, 101, 116, 61, 34, 117, 116, 102, 45, 56, 34, 32, 47, 62, 10, 32, 32, 32, 32, 60, 109, 101, 116, 97, 32, 104, 116, 116, 112, 45, 101, 113, 117, 105, 118, 61, 34, 67, 111, 110, 116, 101, 110, 116, 45, 116, 121, 112, 101, 34, 32, 99, 111, 110, 116, 101, 110, 116, 61, 34, 116, 101, 120, 116, 47, 104, 116, 109, 108, 59, 32, 99, 104, 97, 114, 115, 101, 116, 61, 117, 116, 102, 45, 56, 34, 32, 47, 62, 10, 32, 32, 32, 32, 60, 109, 101, 116, 97, 32, 110, 97, 109, 101, 61, 34, 118, 105, 101, 119, 112, 111, 114, 116, 34, 32, 99, 111, 110, 116, 101, 110, 116, 61, 34, 119, 105, 100, 116, 104, 61, 100, 101, 118, 105, 99, 101, 45, 119, 105, 100, 116, 104, 44, 32, 105, 110, 105, 116, 105, 97, 108, 45, 115, 99, 97, 108, 101, 61, 49, 34, 32, 47, 62, 10, 32, 32, 32, 32, 60, 115, 116, 121, 108, 101, 32, 116, 121, 112, 101, 61, 34, 116, 101, 120, 116, 47, 99, 115, 115, 34, 62, 10, 32, 32, 32, 32, 98, 111, 100, 121, 32, 123, 10, 32, 32, 32, 32, 32, 32, 32, 32, 98, 97, 99, 107, 103, 114, 111, 117, 110, 100, 45, 99, 111, 108, 111, 114, 58, 32, 35, 102, 48, 102, 48, 102, 50, 59, 10, 32, 32, 32, 32, 32, 32, 32, 32, 109, 97, 114, 103, 105, 110, 58, 32, 48, 59, 10, 32, 32, 32, 32, 32, 32, 32, 32, 112, 97, 100, 100, 105, 110, 103, 58, 32, 48, 59, 10, 32, 32, 32, 32, 32, 32, 32, 32, 102, 111, 110, 116, 45, 102, 97, 109, 105, 108, 121, 58, 32, 34, 79, 112, 101, 110, 32, 83, 97, 110, 115, 34, 44, 32, 34, 72, 101, 108, 118, 101, 116, 105, 99, 97, 32, 78, 101, 117, 101, 34, 44, 32, 72, 101, 108, 118, 101, 116, 105, 99, 97, 44, 32, 65, 114, 105, 97, 108, 44, 32, 115, 97, 110, 115, 45, 115, 101, 114, 105, 102, 59, 10, 32, 32, 32, 32, 32, 32, 32, 32, 10, 32, 32, 32, 32, 125, 10, 32, 32, 32, 32, 100, 105, 118, 32, 123, 10, 32, 32, 32, 32, 32, 32, 32, 32, 119, 105, 100, 116, 104, 58, 32, 54, 48, 48, 112, 120, 59, 10, 32, 32, 32, 32, 32, 32, 32, 32, 109, 97, 114, 103, 105, 110, 58, 32, 53, 101, 109, 32, 97, 117, 116, 111, 59, 10, 32, 32, 32, 32, 32, 32, 32, 32, 112, 97, 100, 100, 105, 110, 103, 58, 32, 53, 48, 112, 120, 59, 10, 32, 32, 32, 32, 32, 32, 32, 32, 98, 97, 99, 107, 103, 114, 111, 117, 110, 100, 45, 99, 111, 108, 111, 114, 58, 32, 35, 102, 102, 102, 59, 10, 32, 32, 32, 32, 32, 32, 32, 32, 98, 111, 114, 100, 101, 114, 45, 114, 97, 100, 105, 117, 115, 58, 32, 49, 101, 109, 59, 10, 32, 32, 32, 32, 125, 10, 32, 32, 32, 32, 97, 58, 108, 105, 110, 107, 44, 32, 97, 58, 118, 105, 115, 105, 116, 101, 100, 32, 123, 10, 32, 32, 32, 32, 32, 32, 32, 32, 99, 111, 108, 111, 114, 58, 32, 35, 51, 56, 52, 56, 56, 102, 59, 10, 32, 32, 32, 32, 32, 32, 32, 32, 116, 101, 120, 116, 45, 100, 101, 99, 111, 114, 97, 116, 105, 111, 110, 58, 32, 110, 111, 110, 101, 59, 10, 32, 32, 32, 32, 125, 10, 32, 32, 32, 32, 64, 109, 101, 100, 105, 97, 32, 40, 109, 97, 120, 45, 119, 105, 100, 116, 104, 58, 32, 55, 48, 48, 112, 120, 41, 32, 123, 10, 32, 32, 32, 32, 32, 32, 32, 32, 98, 111, 100, 121, 32, 123, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 98, 97, 99, 107, 103, 114, 111, 117, 110, 100, 45, 99, 111, 108, 111, 114, 58, 32, 35, 102, 102, 102, 59, 10, 32, 32, 32, 32, 32, 32, 32, 32, 125, 10, 32, 32, 32, 32, 32, 32, 32, 32, 100, 105, 118, 32, 123, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 119, 105, 100, 116, 104, 58, 32, 97, 117, 116, 111, 59, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 109, 97, 114, 103, 105, 110, 58, 32, 48, 32, 97, 117, 116, 111, 59, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 98, 111, 114, 100, 101, 114, 45, 114, 97, 100, 105, 117, 115, 58, 32, 48, 59, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 112, 97, 100, 100, 105, 110, 103, 58, 32, 49, 101, 109, 59, 10, 32, 32, 32, 32, 32, 32, 32, 32, 125, 10, 32, 32, 32, 32, 125, 10, 32, 32, 32, 32, 60, 47, 115, 116, 121, 108, 101, 62, 32, 32, 32, 32, 10, 60, 47, 104, 101, 97, 100, 62, 10, 10, 60, 98, 111, 100, 121, 62, 10, 60, 100, 105, 118, 62, 10, 32, 32, 32, 32, 60, 104, 49, 62, 69, 120, 97, 109, 112, 108, 101, 32, 68, 111, 109, 97, 105, 110, 60, 47, 104, 49, 62, 10, 32, 32, 32, 32, 60, 112, 62, 84, 104, 105, 115, 32, 100, 111, 109, 97, 105, 110, 32, 105, 115, 32, 101, 115, 116, 97, 98, 108, 105, 115, 104, 101, 100, 32, 116, 111, 32, 98, 101, 32, 117, 115, 101, 100, 32, 102, 111, 114, 32, 105, 108, 108, 117, 115, 116, 114, 97, 116, 105, 118, 101, 32, 101, 120, 97, 109, 112, 108, 101, 115, 32, 105, 110, 32, 100, 111, 99, 117, 109, 101, 110, 116, 115, 46, 32, 89, 111, 117, 32, 109, 97, 121, 32, 117, 115, 101, 32, 116, 104, 105, 115, 10, 32, 32, 32, 32, 100, 111, 109, 97, 105, 110, 32, 105, 110, 32, 101, 120, 97, 109, 112, 108, 101, 115, 32, 119, 105, 116, 104, 111, 117, 116, 32, 112, 114, 105, 111, 114, 32, 99, 111, 111, 114, 100, 105, 110, 97, 116, 105, 111, 110, 32, 111, 114, 32, 97, 115, 107, 105, 110, 103, 32, 102, 111, 114, 32, 112, 101, 114, 109, 105, 115, 115, 105, 111, 110, 46, 60, 47, 112, 62, 10, 32, 32, 32, 32, 60, 112, 62, 60, 97, 32, 104, 114, 101, 102, 61, 34, 104, 116, 116, 112, 58, 47, 47, 119, 119, 119, 46, 105, 97, 110, 97, 46, 111, 114, 103, 47, 100, 111, 109, 97, 105, 110, 115, 47, 101, 120, 97, 109, 112, 108, 101, 34, 62, 77, 111, 114, 101, 32, 105, 110, 102, 111, 114, 109, 97, 116, 105, 111, 110, 46, 46, 46, 60, 47, 97, 62, 60, 47, 112, 62, 10, 60, 47, 100, 105, 118, 62, 10, 60, 47, 98, 111, 100, 121, 62, 10, 60, 47, 104, 116, 109, 108, 62, 10]

とかだ。

extern crate curl;
use std::fs;

use curl::http;

pub fn main() {
    let resp = http::handle()
    .get("http://www.example.com")
    .exec().unwrap();

    // println!("body={:?}", resp.get_body());
    let str_body = String::from_utf8(resp.get_body()).unwrap();
    println!("{}", str_body );
}

とやってみたが、 get_body()で返却されるのは&[u8]のようだ。

37       pub fn get_body<'a>(&'a self) -> &'a [u8] {
38          &self.body
39      }

from_ftp8とか言いつつ想定してるのはcollections::vec::Vec<u8>っぽい。

 error: mismatched types:
 expected `collections::vec::Vec<u8>`,
    found `&[u8]`
(expected struct `collections::vec::Vec`,
    found &-ptr) [E0308]

String::from_utf8(resp.get_body().to_vec()).unwrap();にしよう。

書き込みも

extern crate curl;
use std::fs;
use std::io::prelude::*;
use std::fs::File;
use curl::http;

pub fn main() {
    let resp = http::handle()
    .get("https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js")
    .exec().unwrap();

    let str_body = String::from_utf8(resp.get_body().to_vec()).unwrap();

    // 開始
    // フォルダ生成
    let path = "app/assets/lib";
    match fs::create_dir_all(path) {
        Err(why) => println!("! {:?}", why.kind()),
        Ok(_) => {},
    }

    let mut rust_f = File::create("app/assets/lib/angular.min.js").unwrap();
    rust_f.write_all(str_body.as_bytes());
}