Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dynamic headers support #51

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

earthengine
Copy link

@earthengine earthengine commented Jan 26, 2019

This work is an experimential support for a better API in regards to #18 .

The design is not yet ideal: it requires the user to call get_request on the same buffer as the parse call. But it works.

Now we can write

let buf = b"GET /404 HTTP/1.1\r\nHost:";
let mut req = httparse::DynRequest::new(None);
let res = req.parse(buf).unwrap();
if res.is_partial() {
    // get a snapshot with statically allocated headers
    let req = req.get_request(buf);
    match req.path {
        Some(ref path) => {
            // check router for path.
            // /404 doesn't exist? we could stop parsing
        },
        None => {
            // must read more and parse again
        }
    }
}

without knowing or predict in advance how many headers there can be. The cost is a Vec allocation, and predicting capacity is possible (pass Some(capacity) rather than None to DynRequest::new.

Future improvements

Create a macro parse_request! to automatically run parse, and then get_request. The lifetime restrictions make it hard to write a function to call them both, but this is not an issue for a macro.

Limitations

This have to be built with std, as Vec involved. We can instead use core, but still it is not going to compile with Rust 1.10.

As this is just a proof-of-concept, we can improve it later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant