Coder Social home page Coder Social logo

rust-http-helper's Introduction

rust-http-helper

FIXME: crate name use http.

A http client lib.

  • Working in progress.
  • Proof of concept.
  • Code needs clean up.

Support following:

  • Fundamental HTTP/1.0 HTTP/1.1 (partial)
  • HTTP method: GET/POST/PUT ...
  • HTTP headers (set/get)
  • HTTP chunked transfer encoding
  • Simple HTTP CookieJar
  • HTTP gzip/deflate content encoding (partial, via zlib, working in progress)
  • Opener + Handler structure, extensible (like python urllib2)

What is missing

  • keep-alive support
  • multipart support
  • https
  • sdch support (no plan)
  • redirect support
  • cookie persistence
  • timing
  • logger
  • ... and so on

How to install

build lib

$ rustc src/http/lib.rs

build test & run test

$ rustc -L. --test src/http/test.rs
$ rustc --test src/http/lib.rs
$ ./test
$ ./http
$ ./http --bench

build sample program & run

$ rustc -L. src/http/main.rs
$ ./main

Simple Usage

#[feature(globs)];

extern crate http;
use http::*;

fn dump_result(req: &Request, resp: &Response) {
    println!("\n======================= request result =======================");
    for (k, vs) in req.headers.iter() {
        println!("H {:?} => {:?}", k, vs)
    }
    println!("======================= response result =======================");
    println!("status = {} reason = {}", resp.status, resp.reason);
    for (k, vs) in resp.headers.iter() {
        println!("H {:?} => {:?}", k, vs)
    }
}

fn main() {
    let url : Url = from_str("http://www.google.com").unwrap();
    let mut req = Request::new_with_url(&url);

    // // Support Following:
    //req.add_header("Accept-Encoding", "gzip,deflate");
    //req.method = POST;
    //req.add_header("user-agent", "Mozilla/5.0");

    let mut opener = build_opener();
    let mut resp = opener.open(&mut req).unwrap();

    dump_result(&req, &resp);

    println!("got cookies = {:?}", resp.get_headers("set-cookie"));
    match resp.get_headers("Content-Encoding").head() {
        Some(&~"gzip") => {
            let mut gzreader = compress::GzipReader::new(resp);
            println!("unzip => {:?}",  gzreader.read_to_str());
        }
        Some(&~"deflate") => {
            let mut gzreader = compress::GzipReader::new(resp);
            println!("unzip => {:?}",  gzreader.read_to_str());
        }
        None => {
            println!("content => {:?}", resp.read_to_str());
        }
        _ => unreachable!()
    }
}

rust-http-helper's People

Contributors

andelf avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.