Skip to content

Add support for IO body (for websockets) #134

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

Closed
wants to merge 3 commits into from
Closed

Conversation

Yarn
Copy link

@Yarn Yarn commented Aug 21, 2020

@@ -16,6 +16,13 @@ const LF: u8 = b'\n';
/// The number returned from httparse when the request is HTTP 1.1
const HTTP_1_1_VERSION: u8 = 1;

#[derive(Debug, Clone)]
pub(crate) enum BodyType {
Copy link

@pickfire pickfire Sep 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we keep this to u32 rather than 2 usize since browsers only supports 2GB content length?

pub(crate) enum BodyType {
    FixedLength(NonZeroU32),
    Chunked,
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the assumption that the client is a mainstream browser should be baked into the library.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, but what if we make it NonZeroUsize? I think it is quite wasteful to use one usize for only one bit.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are three variants anyway. Even if there weren't it corresponds to the Content-Length header which can be 0.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I didn't know Content-Length can be zero. But I don't understand why BodyType have Close, if it is closed means there are no body right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, it means the end of the body is indicated by the connection being closed. in http terms if the content length isn't specified and the transfer encoding isn't chunked.

no body is a separate concern, either the connection is closed without sending more data, content-length is set to 0 or transfer encoding is chunked and a 0 size chunk is sent

Comment on lines +82 to +83
let mut buf: Vec<u8> = Vec::new();
buf.resize(8 * 1024, 0);
Copy link

@pickfire pickfire Sep 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use with_capacity? And tweak the logic below a bit.

Suggested change
let mut buf: Vec<u8> = Vec::new();
buf.resize(8 * 1024, 0);
let mut buf: Vec<u8> = Vec::with_capacity(8 * 1024);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with_capacity makes sense, I'm not sure what the tweak you're referring to is.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, with_capacity only reserve the capacity but it does not fill it up. I think you might want this instead, I am not sure if the _u8 can be elided.

Suggested change
let mut buf: Vec<u8> = Vec::new();
buf.resize(8 * 1024, 0);
let mut buf = [0_u8].repeat(8 * 1024);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like repeat might be more optimized here. That should work fine either way. It's worth noting that I haven't benchmarked this code at all so I have no idea if it even performs decently in the first place.

}

for idx in 3..n {
if idx >= 3 && &buf[idx - 3..=idx] == b"\r\n\r\n" {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this check?

Suggested change
if idx >= 3 && &buf[idx - 3..=idx] == b"\r\n\r\n" {
if &buf[idx - 3..=idx] == b"\r\n\r\n" {

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think so

}
}

if pos == buf.len() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if pos == buf.len() {
if pos == buf_len {


let mut read_failure = None;

// read more if no body has been recieved

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// read more if no body has been recieved
// read more if no body has been received

Comment on lines +161 to +163
Ok(n) => {
n
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Ok(n) => {
n
}
Ok(n) => n,

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in general, this needs rustfmt run on the whole pr. i've mentioned this before to @Yarn

}
};

match io_send.send(Ok((buf_ready.clone(), n))).await {
Copy link

@pickfire pickfire Sep 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't find the receiver end for this but do we need to send if n == 0? Or is it just to signal read is done?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbr
Copy link
Member

jbr commented Dec 4, 2020

closing this — upgrades are in async-h1 2.2.0

@jbr jbr closed this Dec 4, 2020
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.

3 participants