GithubHelp home page GithubHelp logo

justjavac / tiny-http Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tiny-http/tiny-http

1.0 2.0 0.0 5.19 MB

Low level HTTP server library in Rust

Home Page: https://crates.io/crates/tiny_http

License: Apache License 2.0

Shell 1.58% Rust 98.42%

tiny-http's Introduction

tiny-http

Build Status tiny_http on Crates.io

Documentation

Tiny but strong HTTP server in Rust. Its main objectives are to be 100% compliant with the HTTP standard and to provide an easy way to create an HTTP server.

What does tiny-http handle?

  • Accepting and managing connections to the clients
  • Parsing requests
  • Requests pipelining
  • Transfer-Encoding and Content-Encoding (not fully implemented yet)
  • Turning user input (eg. POST input) into a contiguous UTF-8 string (not implemented yet)
  • Ranges (not implemented yet)
  • HTTPS
  • Connection: upgrade (used by websockets)

Tiny-http handles everything that is related to client connections and data transfers and encoding.

Everything else (parsing the values of the headers, multipart data, routing, etags, cache-control, HTML templates, etc.) must be handled by your code. If you want to create a website in Rust, I strongly recommend using a framework instead of this library.

Installation

Add this to the Cargo.toml file of your project:

[dependencies]
tiny_http = "0.6"

Don't forget to add the external crate:

extern crate tiny_http;

Usage

use tiny_http::{Server, Response};

let server = Server::http("0.0.0.0:8000").unwrap();

for request in server.incoming_requests() {
    println!("received request! method: {:?}, url: {:?}, headers: {:?}",
        request.method(),
        request.url(),
        request.headers()
    );

    let response = Response::from_string("hello world");
    request.respond(response);
}

Speed

Tiny-http was designed with speed in mind:

  • Each client connection will be dispatched to a thread pool. Each thread will handle one client. If there is no thread available when a client connects, a new one is created. Threads that are idle for a long time (currently 5 seconds) will automatically die.
  • If multiple requests from the same client are being pipelined (ie. multiple requests are sent without waiting for the answer), tiny-http will read them all at once and they will all be available via server.recv(). Tiny-http will automatically rearrange the responses so that they are sent in the right order.
  • One exception to the previous statement exists when a request has a large body (currently > 1kB), in which case the request handler will read the body directly from the stream and tiny-http will wait for it to be read before processing the next request. Tiny-http will never wait for a request to be answered to read the next one.
  • When a client connection has sent its last request (by sending Connection: close header), the thread will immediately stop reading from this client and can be reclaimed, even when the request has not yet been answered. The reading part of the socket will also be immediately closed.
  • Decoding the client's request is done lazily. If you don't read the request's body, it will not be decoded.

Examples

Examples of tiny-http in use:

  • heroku-tiny-http-hello-world - A simple web application demonstrating how to deploy tiny-http to Heroku
  • crate-deps - A web service that generates images of dependency graphs for crates hosted on crates.io
  • rouille - Web framework built on tiny-http

License

This project is licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in tiny-http by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

tiny-http's People

Contributors

aaron1011 avatar algesten avatar ashleysommer avatar bors[bot] avatar computerdruid avatar djc avatar ebfe avatar eijebong avatar erickt avatar est31 avatar frewsxcv avatar ignatenkobrain avatar logandark avatar mb64 avatar moises-silva avatar nikolaplejic avatar ordepdev avatar penguinliong avatar phildawes avatar rawler avatar spk avatar tomaka avatar zaneli avatar

Stargazers

 avatar

Watchers

 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.