From 9632ff96e7e68ae88fcb35a2027f7d291434e7dd Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Fri, 26 Jan 2024 17:10:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=8Aserve=E6=94=B9=E5=A5=BD=E4=BA=86?= =?UTF-8?q?=EF=BC=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/serve.rs | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/serve.rs b/src/serve.rs index 18743da..156dfdd 100644 --- a/src/serve.rs +++ b/src/serve.rs @@ -1,4 +1,4 @@ -use axum::{extract::Path, http::header::HeaderMap, http::StatusCode, response::IntoResponse}; +use axum::{extract::Path, http::header::HeaderMap, http::StatusCode, response::Response}; /// ```ts /// import express from 'express' @@ -22,22 +22,35 @@ use axum::{extract::Path, http::header::HeaderMap, http::StatusCode, response::I /// /// export default MeasureRoute /// ``` -async fn measure(header: HeaderMap, Path(size): Path) -> impl IntoResponse { +async fn measure(header: HeaderMap, Path(size): Path) -> Response> { + let mut data: Vec = Vec::new(); match header.get("x-openbmclapi-secret") { Some(secret) => { if secret != "secret" { - return (StatusCode::FORBIDDEN, Vec::new()); + return Response::builder() + .status(StatusCode::FORBIDDEN) + .body(data) + .unwrap(); } if size > 200 { - return (StatusCode::BAD_REQUEST, Vec::new()); + return Response::builder() + .status(StatusCode::BAD_REQUEST) + .body(data) + .unwrap(); } let buffer: Vec = vec![0x00, 0x66, 0xcc, 0xff]; - let mut response: Vec = Vec::new(); for _ in 0..size { - response.extend(&buffer); + data.extend(&buffer); } - return (StatusCode::OK, response); + // return (StatusCode::OK, response); + return Response::builder() + .status(StatusCode::OK) + .body(data) + .unwrap(); } - None => return (StatusCode::FORBIDDEN, Vec::new()), + None => Response::builder() + .status(StatusCode::FORBIDDEN) + .body(data) + .unwrap(), } }