This commit is contained in:
2022-09-19 14:18:13 +00:00
committed by GitHub
parent 530a91b680
commit 18637ddb07
7 changed files with 111 additions and 10 deletions

View File

@@ -19,6 +19,8 @@ pub enum PixivDownloaderError {
AVDict(crate::avdict::AVDictError),
#[cfg(feature = "db")]
DbError(crate::db::PixivDownloaderDbError),
#[cfg(feature = "server")]
FromUtf8Error(std::string::FromUtf8Error),
}
impl From<&str> for PixivDownloaderError {

View File

@@ -1,4 +1,5 @@
use super::super::preclude::*;
use crate::ext::json::ToJson2;
#[derive(Clone, Debug)]
/// Action to perform on a user.
@@ -21,6 +22,21 @@ impl AuthUserContext {
is_restful,
}
}
async fn handle(&self, mut req: Request<Body>) -> JSONResult {
let root_user = self.ctx.db.get_user(0).await?;
let params = req.get_params().await?;
if root_user.is_some() {
// # TODO auth
}
match &self.action {
Some(act) => {}
None => {
panic!("No action specified for AuthUserContext.");
}
}
Ok(json::object! {})
}
}
#[async_trait]
@@ -53,13 +69,7 @@ impl ResponseJsonFor<Body> for AuthUserContext {
);
builder
};
match &self.action {
Some(act) => {}
None => {
panic!("No action specified for AuthUserContext.");
}
}
Ok(builder.body(json::object! {})?)
Ok(builder.body(self.handle(req).await.to_json2())?)
}
}

View File

@@ -3,6 +3,8 @@ pub mod auth;
pub mod context;
/// CORS Handle
pub mod cors;
/// Get params from request
pub mod params;
/// Predefined includes
pub mod preclude;
/// Base result type for JSON response

59
src/server/params.rs Normal file
View File

@@ -0,0 +1,59 @@
use super::traits::GetRequestParams;
use crate::error::PixivDownloaderError;
use bytes::BytesMut;
use hyper::{body::HttpBody, Body, Request};
use std::collections::HashMap;
pub struct RequestParams {
pub params: HashMap<String, Vec<String>>,
}
impl RequestParams {
pub fn get<S: AsRef<str> + ?Sized>(&self, name: &S) -> Option<&str> {
match self.params.get(name.as_ref()) {
Some(v) => {
if v.len() > 0 {
Some(&v[0])
} else {
None
}
}
None => None,
}
}
}
#[async_trait]
impl GetRequestParams for Request<Body> {
async fn get_params(&mut self) -> Result<RequestParams, PixivDownloaderError> {
let mut params = HashMap::new();
if let Some(query) = self.uri().query() {
params = urlparse::parse_qs(query);
}
if let Some(ct) = self.headers().get(hyper::header::CONTENT_TYPE) {
if ct == "application/x-www-form-urlencoded" {
let mut body = BytesMut::new();
loop {
if let Some(d) = self.body_mut().data().await {
body.extend_from_slice(&d?);
} else {
break;
}
}
let body = String::from_utf8(body.to_vec())?;
let params2 = urlparse::parse_qs(&body);
for (k, v) in params2 {
match params.get_mut(&k) {
Some(l) => {
l.extend(v);
}
None => {
params.insert(k, v);
}
}
}
}
}
Ok(RequestParams { params })
}
}

View File

@@ -1,7 +1,7 @@
pub use super::context::ServerContext;
pub use super::result::JSONResult;
pub use super::route::ResponseForType;
pub use super::traits::{MatchRoute, ResponseFor, ResponseJsonFor};
pub use super::traits::{GetRequestParams, MatchRoute, ResponseFor, ResponseJsonFor};
pub use crate::error::PixivDownloaderError;
pub use hyper::Body;
pub use hyper::Method;

View File

@@ -1,6 +1,6 @@
use json::JsonValue;
use crate::ext::json::ToJson2;
use crate::gettext;
use json::JsonValue;
#[derive(Clone, Debug)]
/// Error information of a request
@@ -33,6 +33,26 @@ impl From<(i32, String, Option<JsonValue>)> for JSONError {
}
}
impl From<crate::db::PixivDownloaderDbError> for JSONError {
fn from(e: crate::db::PixivDownloaderDbError) -> Self {
Self {
code: -1001,
msg: format!("{} {}", gettext("Failed to operate the database:"), e),
debug_msg: Some(format!("{:?}", e).to_json2()),
}
}
}
impl From<crate::error::PixivDownloaderError> for JSONError {
fn from(e: crate::error::PixivDownloaderError) -> Self {
Self {
code: -500,
msg: format!("{}", e),
debug_msg: Some(format!("{:?}", e).to_json2()),
}
}
}
pub type JSONResult = Result<JsonValue, JSONError>;
impl ToJson2 for JSONResult {

View File

@@ -1,4 +1,5 @@
use super::context::ServerContext;
use super::params::RequestParams;
use crate::error::PixivDownloaderError;
use hyper::Body;
use hyper::Request;
@@ -27,6 +28,13 @@ pub trait ResponseJsonFor<T> {
) -> Result<Response<JsonValue>, PixivDownloaderError>;
}
#[async_trait]
/// Get params from request
pub trait GetRequestParams {
/// Get params from request
async fn get_params(&mut self) -> Result<RequestParams, PixivDownloaderError>;
}
#[async_trait]
impl<T, U> ResponseFor<T, Body> for U
where