mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-25 00:00:23 +02:00
Use pin-project-lite instead of pin-project (#95)
This commit is contained in:
+99
-97
@@ -277,110 +277,112 @@ where
|
||||
let this = self.clone();
|
||||
let protocols = self.protocols.clone();
|
||||
|
||||
ResponseFuture(Box::pin(async move {
|
||||
if req.method() != http::Method::GET {
|
||||
return response(StatusCode::NOT_FOUND, "Request method must be `GET`");
|
||||
}
|
||||
ResponseFuture {
|
||||
future: Box::pin(async move {
|
||||
if req.method() != http::Method::GET {
|
||||
return response(StatusCode::NOT_FOUND, "Request method must be `GET`");
|
||||
}
|
||||
|
||||
if !header_contains(&req, header::CONNECTION, "upgrade") {
|
||||
return response(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"Connection header did not include 'upgrade'",
|
||||
);
|
||||
}
|
||||
if !header_contains(&req, header::CONNECTION, "upgrade") {
|
||||
return response(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"Connection header did not include 'upgrade'",
|
||||
);
|
||||
}
|
||||
|
||||
if !header_eq(&req, header::UPGRADE, "websocket") {
|
||||
return response(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"`Upgrade` header did not include 'websocket'",
|
||||
);
|
||||
}
|
||||
if !header_eq(&req, header::UPGRADE, "websocket") {
|
||||
return response(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"`Upgrade` header did not include 'websocket'",
|
||||
);
|
||||
}
|
||||
|
||||
if !header_eq(&req, header::SEC_WEBSOCKET_VERSION, "13") {
|
||||
return response(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"`Sec-Websocket-Version` header did not include '13'",
|
||||
);
|
||||
}
|
||||
if !header_eq(&req, header::SEC_WEBSOCKET_VERSION, "13") {
|
||||
return response(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"`Sec-Websocket-Version` header did not include '13'",
|
||||
);
|
||||
}
|
||||
|
||||
// check requested protocols
|
||||
let protocol =
|
||||
req.headers()
|
||||
.get(&header::SEC_WEBSOCKET_PROTOCOL)
|
||||
.and_then(|req_protocols| {
|
||||
let req_protocols = req_protocols.to_str().ok()?;
|
||||
req_protocols
|
||||
.split(',')
|
||||
.map(|req_p| req_p.trim())
|
||||
.find(|req_p| protocols.iter().any(|p| p == req_p))
|
||||
});
|
||||
let protocol = match protocol {
|
||||
Some(protocol) => {
|
||||
if let Ok(protocol) = HeaderValue::from_str(protocol) {
|
||||
Some(protocol)
|
||||
} else {
|
||||
return response(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"`Sec-Websocket-Protocol` header is invalid",
|
||||
);
|
||||
// check requested protocols
|
||||
let protocol =
|
||||
req.headers()
|
||||
.get(&header::SEC_WEBSOCKET_PROTOCOL)
|
||||
.and_then(|req_protocols| {
|
||||
let req_protocols = req_protocols.to_str().ok()?;
|
||||
req_protocols
|
||||
.split(',')
|
||||
.map(|req_p| req_p.trim())
|
||||
.find(|req_p| protocols.iter().any(|p| p == req_p))
|
||||
});
|
||||
let protocol = match protocol {
|
||||
Some(protocol) => {
|
||||
if let Ok(protocol) = HeaderValue::from_str(protocol) {
|
||||
Some(protocol)
|
||||
} else {
|
||||
return response(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"`Sec-Websocket-Protocol` header is invalid",
|
||||
);
|
||||
}
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
|
||||
let key = if let Some(key) = req.headers_mut().remove(header::SEC_WEBSOCKET_KEY) {
|
||||
key
|
||||
} else {
|
||||
return response(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"`Sec-Websocket-Key` header missing",
|
||||
);
|
||||
};
|
||||
|
||||
let on_upgrade = req.extensions_mut().remove::<OnUpgrade>().unwrap();
|
||||
|
||||
let config = this.config;
|
||||
let callback = this.callback.clone();
|
||||
|
||||
let mut req = RequestParts::new(req);
|
||||
let input = match T::from_request(&mut req).await {
|
||||
Ok(input) => input,
|
||||
Err(rejection) => {
|
||||
let res = rejection.into_response().map(box_body);
|
||||
return Ok(res);
|
||||
}
|
||||
};
|
||||
|
||||
tokio::spawn(async move {
|
||||
let upgraded = on_upgrade.await.unwrap();
|
||||
let socket = WebSocketStream::from_raw_socket(
|
||||
upgraded,
|
||||
protocol::Role::Server,
|
||||
Some(config),
|
||||
)
|
||||
.await;
|
||||
let socket = WebSocket { inner: socket };
|
||||
callback.call(socket, input).await;
|
||||
});
|
||||
|
||||
let mut builder = Response::builder()
|
||||
.status(StatusCode::SWITCHING_PROTOCOLS)
|
||||
.header(
|
||||
http::header::CONNECTION,
|
||||
HeaderValue::from_str("upgrade").unwrap(),
|
||||
)
|
||||
.header(
|
||||
http::header::UPGRADE,
|
||||
HeaderValue::from_str("websocket").unwrap(),
|
||||
)
|
||||
.header(http::header::SEC_WEBSOCKET_ACCEPT, sign(key.as_bytes()));
|
||||
if let Some(protocol) = protocol {
|
||||
builder = builder.header(http::header::SEC_WEBSOCKET_PROTOCOL, protocol);
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
let res = builder.body(box_body(Full::new(Bytes::new()))).unwrap();
|
||||
|
||||
let key = if let Some(key) = req.headers_mut().remove(header::SEC_WEBSOCKET_KEY) {
|
||||
key
|
||||
} else {
|
||||
return response(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"`Sec-Websocket-Key` header missing",
|
||||
);
|
||||
};
|
||||
|
||||
let on_upgrade = req.extensions_mut().remove::<OnUpgrade>().unwrap();
|
||||
|
||||
let config = this.config;
|
||||
let callback = this.callback.clone();
|
||||
|
||||
let mut req = RequestParts::new(req);
|
||||
let input = match T::from_request(&mut req).await {
|
||||
Ok(input) => input,
|
||||
Err(rejection) => {
|
||||
let res = rejection.into_response().map(box_body);
|
||||
return Ok(res);
|
||||
}
|
||||
};
|
||||
|
||||
tokio::spawn(async move {
|
||||
let upgraded = on_upgrade.await.unwrap();
|
||||
let socket = WebSocketStream::from_raw_socket(
|
||||
upgraded,
|
||||
protocol::Role::Server,
|
||||
Some(config),
|
||||
)
|
||||
.await;
|
||||
let socket = WebSocket { inner: socket };
|
||||
callback.call(socket, input).await;
|
||||
});
|
||||
|
||||
let mut builder = Response::builder()
|
||||
.status(StatusCode::SWITCHING_PROTOCOLS)
|
||||
.header(
|
||||
http::header::CONNECTION,
|
||||
HeaderValue::from_str("upgrade").unwrap(),
|
||||
)
|
||||
.header(
|
||||
http::header::UPGRADE,
|
||||
HeaderValue::from_str("websocket").unwrap(),
|
||||
)
|
||||
.header(http::header::SEC_WEBSOCKET_ACCEPT, sign(key.as_bytes()));
|
||||
if let Some(protocol) = protocol {
|
||||
builder = builder.header(http::header::SEC_WEBSOCKET_PROTOCOL, protocol);
|
||||
}
|
||||
let res = builder.body(box_body(Full::new(Bytes::new()))).unwrap();
|
||||
|
||||
Ok(res)
|
||||
}))
|
||||
Ok(res)
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user