Skip to content

Commit 1667367

Browse files
committed
fix request
1 parent 10a9598 commit 1667367

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "webparse"
3-
version = "0.3.0"
3+
version = "0.3.1"
44
edition = "2021"
55
authors = [ "tickbh <[email protected]>" ]
66
description = "http1.1/http2 parse http解析库"
@@ -13,4 +13,4 @@ log="0.4.22"
1313
bitflags="2.6.0"
1414
lazy_static = "1.5.0"
1515
base64 = "0.22.1"
16-
algorithm = "^0.1.17"
16+
algorithm = "^0.1.18"

src/http/request.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ pub struct Parts {
3838
pub header: HeaderMap,
3939
pub version: Version,
4040
pub url: Url,
41-
pub path: String,
4241
pub extensions: Extensions,
4342
}
4443

@@ -79,7 +78,7 @@ impl Builder {
7978
}
8079
if req.path() != &Url::DEFAULT_PATH {
8180
let _ = build.inner.as_mut().map(|head| {
82-
head.path = req.path().clone();
81+
head.url.path = req.path().clone();
8382
});
8483
}
8584

@@ -301,9 +300,6 @@ impl Builder {
301300
T: Serialize,
302301
{
303302
self.inner.map(move |mut head| {
304-
if head.path.len() == 0 {
305-
head.path = head.url.path.clone();
306-
}
307303
let server = HeaderName::from_static("User-Agent");
308304
if !head.header.contains(&server) {
309305
head.header.insert(server, "wenmeng");
@@ -328,9 +324,6 @@ impl Builder {
328324

329325
pub fn upgrade_http2(self, settings: Settings) -> Self {
330326
self.and_then(move |mut head| {
331-
if head.path.len() == 0 {
332-
head.path = head.url.path.clone();
333-
}
334327
head.header.insert("Connection", "Upgrade, HTTP2-Settings");
335328
head.header.insert("Upgrade", "h2c");
336329
head.header
@@ -390,7 +383,12 @@ where
390383
}
391384

392385
pub fn set_url(&mut self, url: Url) {
393-
self.parts.path = url.path.clone();
386+
if let Some(connect) = url.get_connect_url() {
387+
if !self.headers().contains(&"Host") {
388+
self.headers_mut().insert("Host", connect.clone());
389+
}
390+
}
391+
394392
self.parts.url = url;
395393
}
396394

@@ -425,12 +423,12 @@ where
425423

426424
#[inline]
427425
pub fn path(&self) -> &String {
428-
&self.parts.path
426+
&self.parts.url.path
429427
}
430428

431429
#[inline]
432430
pub fn set_path(&mut self, path: String) {
433-
self.parts.path = path;
431+
self.parts.url.path = path;
434432
}
435433

436434
pub fn scheme(&self) -> &Scheme {
@@ -557,7 +555,7 @@ where
557555
Helper::skip_empty_lines(buffer)?;
558556
self.parts.method = Helper::parse_method(buffer)?;
559557
Helper::skip_spaces(buffer)?;
560-
self.parts.path = Helper::parse_token(buffer)?.to_string();
558+
let path = Helper::parse_token(buffer)?.to_string();
561559
Helper::skip_spaces(buffer)?;
562560
self.parts.version = Helper::parse_version(buffer)?;
563561
Helper::skip_new_line(buffer)?;
@@ -567,11 +565,11 @@ where
567565
// Connect 协议, Path则为连接地址,
568566
Method::Connect => {
569567
let mut url = Url::new();
570-
Self::parse_connect_by_host(&mut url, &self.parts.path)?;
568+
Self::parse_connect_by_host(&mut url, &path)?;
571569
url
572570
}
573571
_ => {
574-
let mut url = Url::try_from(self.parts.path.to_string())?;
572+
let mut url = Url::try_from(path)?;
575573
if url.domain.is_none() {
576574
match self.parts.header.get_host() {
577575
Some(h) => {
@@ -643,7 +641,7 @@ where
643641
let mut size = 0;
644642
size += self.parts.method.encode(buffer)?;
645643
size += buffer.put_u8(b' ');
646-
size += self.parts.path.serialize(buffer)?;
644+
size += self.parts.url.path.serialize(buffer)?;
647645
size += buffer.put_u8(b' ');
648646
size += self.parts.version.encode(buffer)?;
649647
size += buffer.put_slice("\r\n".as_bytes());
@@ -710,7 +708,7 @@ where
710708
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
711709
self.parts.method.fmt(f)?;
712710
f.write_str(" ")?;
713-
self.parts.path.fmt(f)?;
711+
self.parts.url.path.fmt(f)?;
714712
f.write_str(" ")?;
715713
self.parts.version.fmt(f)?;
716714
f.write_str("\r\n")?;
@@ -726,7 +724,6 @@ impl Default for Parts {
726724
header: HeaderMap::new(),
727725
version: Version::Http11,
728726
url: Url::new(),
729-
path: String::new(),
730727
extensions: Extensions::new(),
731728
}
732729
}
@@ -739,7 +736,6 @@ impl Clone for Parts {
739736
header: self.header.clone(),
740737
version: self.version.clone(),
741738
url: self.url.clone(),
742-
path: self.path.clone(),
743739
extensions: Extensions::new(),
744740
};
745741

0 commit comments

Comments
 (0)