From 35d94e14f6f710937ab7918bb085e163b3673d90 Mon Sep 17 00:00:00 2001 From: shenjack-5600u <3695888@qq.com> Date: Sun, 28 Jan 2024 11:25:48 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=20enumerate=20=E6=94=B9=E8=BF=9B?= =?UTF-8?q?=E4=BA=86=E4=B8=80=E4=B8=8B=20meausre=20=E7=9A=84=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/serve.rs | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/src/serve.rs b/src/serve.rs index d98e818..cd20f5f 100644 --- a/src/serve.rs +++ b/src/serve.rs @@ -1,10 +1,28 @@ +use std::collections::HashMap; + use axum::{ - extract::Path, + extract::{Path, Query}, http::header::HeaderMap, http::StatusCode, response::{IntoResponse, Response}, }; +pub enum MeasureRes { + Forbidden, + BadResquest, + Data(Vec) +} + +impl IntoResponse for MeasureRes { + fn into_response(self) -> Response { + match self { + Self::Forbidden => (StatusCode::FORBIDDEN).into_response(), + Self::BadResquest => (StatusCode::BAD_REQUEST).into_response(), + Self::Data(data) => (StatusCode::OK, data).into_response() + } + } +} + /// ```ts /// import express from 'express' /// @@ -27,40 +45,28 @@ use axum::{ /// /// export default MeasureRoute /// ``` -pub async fn measure(header: HeaderMap, Path(size): Path) -> Response> { +pub async fn measure(header: HeaderMap, Path(size): Path) -> MeasureRes { let mut data: Vec = Vec::new(); match header.get("x-openbmclapi-secret") { Some(secret) => { if secret != "secret" { - return Response::builder() - .status(StatusCode::FORBIDDEN) - .body(data) - .unwrap(); + return MeasureRes::Forbidden; } if size > 200 { - return Response::builder() - .status(StatusCode::BAD_REQUEST) - .body(data) - .unwrap(); + return MeasureRes::BadResquest; } let buffer: Vec = vec![0x00, 0x66, 0xcc, 0xff]; for _ in 0..size { data.extend(&buffer); } - // return (StatusCode::OK, response); - return Response::builder() - .status(StatusCode::OK) - .body(data) - .unwrap(); + return MeasureRes::Data(data); } - None => Response::builder() - .status(StatusCode::FORBIDDEN) - .body(data) - .unwrap(), + None => MeasureRes::Forbidden } } /// 返回文件的请求函数 -pub async fn get_file() -> impl IntoResponse { +/// app.get('/download/:hash(\\w+)', async (req: Request, res: Response, next: NextFunction) => { +pub async fn res_donwload(header: HeaderMap, Query(param): Query>, Path(hash): Path) -> impl IntoResponse { todo!(); }