Skip to content
This repository has been archived by the owner on Jul 31, 2019. It is now read-only.

Commit

Permalink
Request parsing works
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Oltmanns committed Nov 13, 2017
1 parent 4f659a8 commit 62e3738
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 16 deletions.
8 changes: 4 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ zap = "0.0.3"

## Speed

So `zap` is not only fast, it is wapping **2.9** times faster than [iron](https://github.com/iron/iron), which is based on [hyper](https://github.com/hyperium/hyper). Benchmarks below:
So `zap` is not only fast, it is wapping **2.96** times faster than [iron](https://github.com/iron/iron), which is based on [hyper](https://github.com/hyperium/hyper). Benchmarks below:

### Benchmark Code

Expand Down Expand Up @@ -109,15 +109,15 @@ Transfer/sec: 33.44MB

```
[...]
Requests/sec: 900745.44
Transfer/sec: 43.81MB
Requests/sec: 912832.31
Transfer/sec: 40.90MB
```

## Todo

Some things we still need to make:

- [ ] Make it faster
- [X] Make it faster
- [ ] URL and body parsing
- [ ] Request key storing

Expand Down
6 changes: 3 additions & 3 deletions examples/simple-routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ impl Handler for HelloWorld {
fn call(&self, req: Request) -> ZapResult {
// Create new Response
let mut resp = Response::new();
let head = req.data();
let head = req.first();

// Different content, depending on route
if head.starts_with(b"GET / HTTP/1.1\r\n") {
if head.starts_with(b"GET / HTTP/1.1\r") {
resp.body_raw(b"Hello World");
} else if head.starts_with(b"GET /bye HTTP/1.1\r\n") {
} else if head.starts_with(b"GET /bye HTTP/1.1\r") {
resp.body_raw(b"Bye Bye");
} else {
resp.body_raw(b"Not Found");
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@
//!

// Load all crates and modules
#![feature(slice_rotate)]
extern crate bytes;
extern crate futures;
extern crate httparse;
extern crate tokio_io;
extern crate tokio_proto;
extern crate tokio_service;
Expand Down
82 changes: 75 additions & 7 deletions src/request.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
// Use the stuff we need to
use std::{io, str};
use std::io;

use bytes::BytesMut;

/// The result of an incoming request
pub struct Request {
raw: BytesMut,
first: BytesMut,
head: BytesMut,
body: BytesMut,
}

impl Request {
/// Get raw body
pub fn data(&self) -> &BytesMut {
&self.raw
pub fn body(&self) -> &BytesMut {
&self.body
}

/// Get first line
pub fn first(&self) -> &BytesMut {
&self.first
}

/// Get head
pub fn head(&self) -> &BytesMut {
&self.head
}
}

Expand All @@ -21,13 +33,69 @@ pub fn decode(buf: &mut BytesMut) -> io::Result<Option<Request>> {
return Ok(None);
}

let request = Request {
raw: buf.clone(),
};
// Clone buffer for iter
let iter = buf.clone();

// Loop over bytes, to find line endings
let mut it = iter.iter();
let mut firstc : u16 = 0_u16;
let mut headc : u16 = 0_u16;

loop {
// Check if end is reached
match it.next() {
Some(&b'\n') => break,
None => break,
_ => {
firstc += 1;
}
};
}

// Cache headers line length
let mut linec : u16 = 0_u16;

loop {
// Check if end of headers reached
match it.next() {
// On line end
Some(&b'\n') => {
// Headers end reached
if linec == 1 {
break;
}

// Increment total length
headc += 1;

// Reset line length
linec = 0;
},
// Buffer end
None => break,
_ => {
// Else increment length
linec += 1;
headc += 1;
}
};
}

// Split buffers into parts
let first = buf.split_to(firstc as usize);
let head = buf.split_to(headc as usize);
let body = buf.clone();

// Clear buffer for next request
buf.clear();

// Build request object
let request = Request {
first: first,
head: head,
body: body,
};

// Create a new Request object
Ok(request.into())
}

0 comments on commit 62e3738

Please sign in to comment.