Replace unsafe code by ByteStr (fixes #440)
This commit is contained in:
committed by
Sean McArthur
parent
a6b414458f
commit
50d6297d23
@@ -5,7 +5,7 @@ use crate::hpack::{self, BytesStr};
|
|||||||
use http::header::{self, HeaderName, HeaderValue};
|
use http::header::{self, HeaderName, HeaderValue};
|
||||||
use http::{uri, HeaderMap, Method, Request, StatusCode, Uri};
|
use http::{uri, HeaderMap, Method, Request, StatusCode, Uri};
|
||||||
|
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::BytesMut;
|
||||||
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::io::Cursor;
|
use std::io::Cursor;
|
||||||
@@ -549,13 +549,13 @@ impl Pseudo {
|
|||||||
|
|
||||||
let mut path = parts
|
let mut path = parts
|
||||||
.path_and_query
|
.path_and_query
|
||||||
.map(|v| Bytes::copy_from_slice(v.as_str().as_bytes()))
|
.map(|v| BytesStr::from(v.as_str()))
|
||||||
.unwrap_or_else(Bytes::new);
|
.unwrap_or(BytesStr::from_static(""));
|
||||||
|
|
||||||
match method {
|
match method {
|
||||||
Method::OPTIONS | Method::CONNECT => {}
|
Method::OPTIONS | Method::CONNECT => {}
|
||||||
_ if path.is_empty() => {
|
_ if path.is_empty() => {
|
||||||
path = Bytes::from_static(b"/");
|
path = BytesStr::from_static("/");
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
@@ -564,7 +564,7 @@ impl Pseudo {
|
|||||||
method: Some(method),
|
method: Some(method),
|
||||||
scheme: None,
|
scheme: None,
|
||||||
authority: None,
|
authority: None,
|
||||||
path: Some(unsafe { BytesStr::from_utf8_unchecked(path) }).filter(|p| !p.is_empty()),
|
path: Some(path).filter(|p| !p.is_empty()),
|
||||||
status: None,
|
status: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -578,9 +578,7 @@ impl Pseudo {
|
|||||||
// If the URI includes an authority component, add it to the pseudo
|
// If the URI includes an authority component, add it to the pseudo
|
||||||
// headers
|
// headers
|
||||||
if let Some(authority) = parts.authority {
|
if let Some(authority) = parts.authority {
|
||||||
pseudo.set_authority(unsafe {
|
pseudo.set_authority(BytesStr::from(authority.as_str()));
|
||||||
BytesStr::from_utf8_unchecked(Bytes::copy_from_slice(authority.as_str().as_bytes()))
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pseudo
|
pseudo
|
||||||
@@ -597,12 +595,12 @@ impl Pseudo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_scheme(&mut self, scheme: uri::Scheme) {
|
pub fn set_scheme(&mut self, scheme: uri::Scheme) {
|
||||||
let bytes = match scheme.as_str() {
|
let bytes_str = match scheme.as_str() {
|
||||||
"http" => Bytes::from_static(b"http"),
|
"http" => BytesStr::from_static("http"),
|
||||||
"https" => Bytes::from_static(b"https"),
|
"https" => BytesStr::from_static("https"),
|
||||||
s => Bytes::copy_from_slice(s.as_bytes()),
|
s => BytesStr::from(s),
|
||||||
};
|
};
|
||||||
self.scheme = Some(unsafe { BytesStr::from_utf8_unchecked(bytes) });
|
self.scheme = Some(bytes_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_authority(&mut self, authority: BytesStr) {
|
pub fn set_authority(&mut self, authority: BytesStr) {
|
||||||
|
|||||||
@@ -577,13 +577,13 @@ pub fn get_static(idx: usize) -> Header {
|
|||||||
use http::header::HeaderValue;
|
use http::header::HeaderValue;
|
||||||
|
|
||||||
match idx {
|
match idx {
|
||||||
1 => Header::Authority(from_static("")),
|
1 => Header::Authority(BytesStr::from_static("")),
|
||||||
2 => Header::Method(Method::GET),
|
2 => Header::Method(Method::GET),
|
||||||
3 => Header::Method(Method::POST),
|
3 => Header::Method(Method::POST),
|
||||||
4 => Header::Path(from_static("/")),
|
4 => Header::Path(BytesStr::from_static("/")),
|
||||||
5 => Header::Path(from_static("/index.html")),
|
5 => Header::Path(BytesStr::from_static("/index.html")),
|
||||||
6 => Header::Scheme(from_static("http")),
|
6 => Header::Scheme(BytesStr::from_static("http")),
|
||||||
7 => Header::Scheme(from_static("https")),
|
7 => Header::Scheme(BytesStr::from_static("https")),
|
||||||
8 => Header::Status(StatusCode::OK),
|
8 => Header::Status(StatusCode::OK),
|
||||||
9 => Header::Status(StatusCode::NO_CONTENT),
|
9 => Header::Status(StatusCode::NO_CONTENT),
|
||||||
10 => Header::Status(StatusCode::PARTIAL_CONTENT),
|
10 => Header::Status(StatusCode::PARTIAL_CONTENT),
|
||||||
@@ -783,10 +783,6 @@ pub fn get_static(idx: usize) -> Header {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_static(s: &'static str) -> BytesStr {
|
|
||||||
unsafe { BytesStr::from_utf8_unchecked(Bytes::from_static(s.as_bytes())) }
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|||||||
@@ -246,8 +246,12 @@ impl<'a> Name<'a> {
|
|||||||
// ===== impl BytesStr =====
|
// ===== impl BytesStr =====
|
||||||
|
|
||||||
impl BytesStr {
|
impl BytesStr {
|
||||||
pub(crate) unsafe fn from_utf8_unchecked(bytes: Bytes) -> Self {
|
pub(crate) const fn from_static(value: &'static str) -> Self {
|
||||||
BytesStr(bytes)
|
BytesStr(Bytes::from_static(value.as_bytes()))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn from(value: &str) -> Self {
|
||||||
|
BytesStr(Bytes::copy_from_slice(value.as_bytes()))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use crate::hpack::{Decoder, Encode, Encoder, Header};
|
|||||||
|
|
||||||
use http::header::{HeaderName, HeaderValue};
|
use http::header::{HeaderName, HeaderValue};
|
||||||
|
|
||||||
use bytes::{buf::BufMut, Bytes, BytesMut};
|
use bytes::{buf::BufMut, BytesMut};
|
||||||
use quickcheck::{Arbitrary, Gen, QuickCheck, TestResult};
|
use quickcheck::{Arbitrary, Gen, QuickCheck, TestResult};
|
||||||
use rand::{Rng, SeedableRng, StdRng};
|
use rand::{Rng, SeedableRng, StdRng};
|
||||||
|
|
||||||
@@ -404,6 +404,5 @@ fn gen_string(g: &mut StdRng, min: usize, max: usize) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn to_shared(src: String) -> crate::hpack::BytesStr {
|
fn to_shared(src: String) -> crate::hpack::BytesStr {
|
||||||
let b: Bytes = src.into();
|
crate::hpack::BytesStr::from(src.as_str())
|
||||||
unsafe { crate::hpack::BytesStr::from_utf8_unchecked(b) }
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user