hyper
Rust is a shiny new systems language that the lovely folks at Mozilla are building. It focuses on complete memory-safety, and being very fast. Its speed is equivalent to C++ code, but you don’t have to manage pointers and the like; the language does that for you. It also catches a lot of irritating runtime errors at compile time, thanks to its fantastic type system. That should mean less crashes.
All of this sounds fantastic, let’s use it to make server software! It will be faster, and crash less. One speed bump: there’s no real rust http library.
rust-http and Teepee
There were 2 prior attempts at HTTP libraries, but the former (rust-http) has been ditched by its creator, and isn’t very “rust-like”. The latter, Teepee, started in an excellent direction, but life has gotten in the way for the author.1
For the client-side only, there exists curl-rust, which are just bindings to libcurl. Ideally, we’d like to have the all of the code written in Rust, so we don’t have to trust that the curl developers have written perfectly memory-safe code.
So I started a new one. I called it hyper, cause, y’know, hyper-text transfer protocol.
embracing types
The type system in Rust is quite phenomenal. Wait, what? Did I just say that? Huh, I guess I did. I know, I know, we hate wrestling with type systems. I can’t touch any Java code without cursing the type system. Thanks to Rust’s type inference, though, it’s not irritating at all.
In contrast, I’ve gotten tired of stringly-typed languages; chief among them is JavaScript. Everything is a string. Even property lookups. document.onlood = onload;
is perfectly valid, since it just treats onlood
as a string. You know a big problem with strings? Typos. If you write JavaScript, you will write typos that aren’t caught until your code is in production, and you see that an event handler is never triggered, or undefined is not a function
.
I’m done with that. But if you still want to be able to use strings in your rust code, you certainly can. Just use something else besides hyper.
Now then, how about some examples. It’s most noticeable when using headers. In JavaScript, you’d likely do something like:
req.headers['content-type'] = 'application/json';
Here’s how to do the same using hyper:
req.headers.set(ContentType(Mime(Application, Json, vec![])));
Huh, interesting. Looks like more code. Yes, yes it is. But it’s also code that has been checked by the compiler. It has made sure there are no typos. It also has made sure you didn’t try to see the wrong format to a header. To get the header back out:
match req.headers.get() {
Some(&ContentType(Mime(Application, Json, _))) => "it's json!",
Some(&ContentType(Mime(top, sub, _))) => "we can handle top and sub",
None => "le sad"
}
Here’s an example that makes sure the format is correct:
req.headers.set(Date(time::utc_now()));
// ...
match req.headers.get() {
Some(&Date(ref tm)) => {
// tm is a Tm instance, without you dealing with
// the various allowed formats in the HTTP spec.
}
// ...
}
Yea, yea, there is a stringly-typed API, for those rare cases you might need it, but it’s purposefully not easy to use. You shouldn’t use it. Maybe you think of a reason you might maybe have a good reason; no you don’t. Don’t use it. Let the compiler check for errors before you hit production.
Let’s look at status codes. Can you tell me what exactly this response means, without looking it up?
res.status = 307;
How about this instead:
res.status = StatusCode::MovedTemporarily;
Clearly better. You’ve seen code like this:
if res.status / 100 == 4 {}
What if we could make it better:
if res.status.is_client_error() {}
Message WriteStatus
I’ve been bitten by this before, I can only bet you have also: trying to write headers after they’ve already been sent. Hyper makes this a compile-time check. If you have a Request<Fresh>
, then there exists header_mut()
methods to get a mutable reference to the headers, so you can add some. You can’t accidently write to a Request<Fresh>
, since it doesn’t implement Writer
. When you are ready to start writing the body, you must specifically convert to a Request<Streaming>
using req.start()
.
Likewise, a Request<Streaming>
does not contain a headers_mut()
accessor. You cannot change the headers once streaming has started. You can still inspect them, if that’s needed, but no setting! The compiler will make sure you don’t have that mistake in your code.
NetworkStreams
Both the Server
and the Client
are generic over NetworkStream
s. The default is to use HttpStream
, which can handle HTTP over TCP, and HTTPS using openssl. This design also allows something like Servo to implement a ServoStream
or something, which could handle HTTPS using NSS instead.
Goals
These are some high level goals for the library, so you can see the direction:
- Be fast!
- The benchmarks preach that we’re already faster than both rust-http and libcurl. And we all know science doesn’t lie.
- Embrace types.
- See the above post for how we’re doing this.
- Provide an excellent http server library for rust webdev.
- Currently used by Iron, Rustless, Sserve, and others …
- Provide an excellent http client that can be used in place of curl.
- It has landed in Servo!
The first step for hyper was get the streams and types working correctly and quickly. With the factory working underneath, it allows others to write specific implementations without re-doing all of HTTP, such as implementing the XHR2 spec in Servo. Work now has been on providing ergonomic Client
and Server
implementations.
It looks increasingly likely that hyper will be available to use on Rust-1.0-day.3 There will be an HTTP library for Rust 1.0!
-
Teepee provided excellent inspiration in some of the design, and all that credit should go to its creator, Chris Morgan. He’s continued to provide insight in the development of hyper, so <3! ↩
-
Yes, it differs. It’s been a delight to see that developers are never content with an existing spec. ↩
-
Rust 1.0 will ship with only
stable
APIs and features. Some features will only be accessible by the nightlies, and not likely to be stabilized for 1.0. Hyper doesn’t depend on these, and so should be compilable usingrustc v1.0.0
. ↩