seanmonstar / httparse

A push parser for the HTTP 1.x protocol in Rust.
https://docs.rs/httparse
Apache License 2.0
573 stars 113 forks source link

use with ringbuffer #44

Closed jkarneges closed 5 years ago

jkarneges commented 6 years ago

Hi,

First, thanks for this great library!

Is it possible to parse around the edge of a ring buffer boundary? E.g by providing two buffers as input to the parser? Currently I'm shifting all remaining bytes down after every successful parse but it would be nice to not have to do that. Any advice?

seanmonstar commented 6 years ago

I think it could be nice to have an API that takes a Buf. That'd allow ring buffers, and any other instance where you don't have the bytes in contiguous memory.

However, there'd definitely be a performance cost to that, so I don't think it could be the default API... Hm, it might actually be annoying to figure out how to get that to all work nicely...

jkarneges commented 6 years ago

The main times it would be useful are probably:

I believe pipelining and chunked request bodies are uncommon in practice, so maybe a single buf parser plus memmove (when needed) as we have today is ideal in the general case.

An optional multi-buf parser would be interesting though, that the app could invoke in the right situations, provided that the performance hit of such a parser would not be worse than a memmove.

seanmonstar commented 6 years ago

You don't necessarily need to memmove with pipelined requests. If you're using some sort of cursor, you can just pass the rest of the buffer to be parsed after having finished with the previous request. If you get a partial result back, you can then decide whether to consolidate or just keep reading into the end.

jkarneges commented 6 years ago

Good point. Consolidating when data has already wrapped around can be tricky though, which is why I was shifting after each read to ensure that couldn't happen. But perhaps I can optimize this.