mirror of
https://github.com/lifegpc/pixiv_downloader.git
synced 2026-07-08 01:32:41 +08:00
Update /proxy/pixiv
This commit is contained in:
@@ -938,7 +938,7 @@ pub fn http_error(item: TokenStream) -> TokenStream {
|
||||
Ok(re) => re,
|
||||
Err(e) => {
|
||||
builder = builder.status(#code);
|
||||
return Ok(builder.body::<Pin<Box<HttpBodyType>>>(Box::pin(Body::from(format!("{}", e))))?);
|
||||
return Ok(builder.body::<Pin<Box<HttpBodyType>>>(Box::pin(HyperBody::from(format!("{}", e))))?);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
@@ -28,8 +28,11 @@ pub enum PixivDownloaderError {
|
||||
#[cfg(feature = "openssl")]
|
||||
OpenSSLError(openssl::error::ErrorStack),
|
||||
ParseIntError(std::num::ParseIntError),
|
||||
ReqwestError(reqwest::Error),
|
||||
}
|
||||
|
||||
impl std::error::Error for PixivDownloaderError {}
|
||||
|
||||
impl From<&str> for PixivDownloaderError {
|
||||
fn from(p: &str) -> Self {
|
||||
Self::String(String::from(p))
|
||||
|
||||
51
src/server/body/hyper.rs
Normal file
51
src/server/body/hyper.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
use crate::server::preclude::PixivDownloaderError;
|
||||
use hyper::body::HttpBody;
|
||||
use hyper::Body as _Body;
|
||||
use hyper::HeaderMap;
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
pub struct HyperBody {
|
||||
_body: _Body,
|
||||
}
|
||||
|
||||
impl HyperBody {
|
||||
pub fn empty() -> Self {
|
||||
Self {
|
||||
_body: _Body::empty(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Into<_Body>> From<T> for HyperBody {
|
||||
fn from(body: T) -> Self {
|
||||
Self { _body: body.into() }
|
||||
}
|
||||
}
|
||||
|
||||
impl HttpBody for HyperBody {
|
||||
type Data = bytes::Bytes;
|
||||
type Error = PixivDownloaderError;
|
||||
|
||||
fn poll_data(
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<Option<Result<Self::Data, Self::Error>>> {
|
||||
Pin::new(&mut self._body)
|
||||
.poll_data(cx)
|
||||
.map_err(PixivDownloaderError::from)
|
||||
}
|
||||
|
||||
fn poll_trailers(
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<Result<Option<HeaderMap>, Self::Error>> {
|
||||
Pin::new(&mut self._body)
|
||||
.poll_trailers(cx)
|
||||
.map_err(PixivDownloaderError::from)
|
||||
}
|
||||
|
||||
fn is_end_stream(&self) -> bool {
|
||||
self._body.is_end_stream()
|
||||
}
|
||||
}
|
||||
2
src/server/body/mod.rs
Normal file
2
src/server/body/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod hyper;
|
||||
pub mod response;
|
||||
42
src/server/body/response.rs
Normal file
42
src/server/body/response.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use crate::error::PixivDownloaderError;
|
||||
use hyper::body::HttpBody;
|
||||
use reqwest::Response;
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
pub struct ResponseBody {
|
||||
res: Response,
|
||||
}
|
||||
|
||||
impl ResponseBody {
|
||||
pub fn new(res: Response) -> Self {
|
||||
Self { res }
|
||||
}
|
||||
}
|
||||
|
||||
impl HttpBody for ResponseBody {
|
||||
type Data = hyper::body::Bytes;
|
||||
type Error = PixivDownloaderError;
|
||||
|
||||
fn poll_data(
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<Option<Result<Self::Data, Self::Error>>> {
|
||||
match Pin::new(&mut Box::pin(self.res.chunk())).poll(cx) {
|
||||
Poll::Ready(f) => match f {
|
||||
Ok(Some(data)) => Poll::Ready(Some(Ok(data))),
|
||||
Ok(None) => Poll::Ready(None),
|
||||
Err(e) => Poll::Ready(Some(Err(PixivDownloaderError::from(e)))),
|
||||
},
|
||||
Poll::Pending => Poll::Pending,
|
||||
}
|
||||
}
|
||||
|
||||
fn poll_trailers(
|
||||
self: Pin<&mut Self>,
|
||||
_cx: &mut Context<'_>,
|
||||
) -> Poll<Result<Option<hyper::HeaderMap>, Self::Error>> {
|
||||
Poll::Ready(Ok(None))
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
/// Routes about authentication
|
||||
pub mod auth;
|
||||
/// Body types which implements [hyper::body::HttpBody]
|
||||
pub mod body;
|
||||
pub mod context;
|
||||
/// CORS Handle
|
||||
pub mod cors;
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
pub use super::body::hyper::HyperBody;
|
||||
pub use super::body::response::ResponseBody;
|
||||
pub use super::context::ServerContext;
|
||||
pub use super::params::RequestParams;
|
||||
pub use super::result::JSONResult;
|
||||
@@ -15,4 +17,5 @@ pub use regex::Regex;
|
||||
pub use std::pin::Pin;
|
||||
pub use std::sync::Arc;
|
||||
|
||||
pub type HttpBodyType = dyn HttpBody<Data = hyper::body::Bytes, Error = hyper::Error> + Send;
|
||||
pub type HttpBodyType =
|
||||
dyn HttpBody<Data = hyper::body::Bytes, Error = PixivDownloaderError> + Send;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use super::super::preclude::*;
|
||||
use crate::webclient::WebClient;
|
||||
use http::Uri;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub struct ProxyPixivContext {
|
||||
ctx: Arc<ServerContext>,
|
||||
@@ -19,7 +21,7 @@ impl ResponseFor<Body, Pin<Box<HttpBodyType>>> for ProxyPixivContext {
|
||||
) -> Result<Response<Pin<Box<HttpBodyType>>>, PixivDownloaderError> {
|
||||
filter_http_methods!(
|
||||
req,
|
||||
Box::pin(Body::empty()),
|
||||
Box::pin(HyperBody::empty()),
|
||||
true,
|
||||
self.ctx,
|
||||
allow_headers = [X_SIGN, X_TOKEN_ID],
|
||||
@@ -35,7 +37,14 @@ impl ResponseFor<Body, Pin<Box<HttpBodyType>>> for ProxyPixivContext {
|
||||
if !host.ends_with(".pximg.net") {
|
||||
http_error!(403, Err("Host is not allowed."));
|
||||
}
|
||||
return Ok(builder.body::<Pin<Box<HttpBodyType>>>(Box::pin(Body::empty()))?);
|
||||
let client = WebClient::default();
|
||||
client.set_header("referer", "https://www.pixiv.net/");
|
||||
let headers = HashMap::new();
|
||||
let re = http_error!(
|
||||
502,
|
||||
client.get(url, headers).await.ok_or("Failed to get image.")
|
||||
);
|
||||
return Ok(builder.body::<Pin<Box<HttpBodyType>>>(Box::pin(ResponseBody::new(re)))?);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ impl PixivDownloaderSvc {
|
||||
|
||||
impl Service<Request<Body>> for PixivDownloaderSvc {
|
||||
type Response = Response<Pin<Box<HttpBodyType>>>;
|
||||
type Error = hyper::Error;
|
||||
type Error = PixivDownloaderError;
|
||||
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
|
||||
|
||||
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||
@@ -43,7 +43,7 @@ impl Service<Request<Body>> for PixivDownloaderSvc {
|
||||
println!("{}", e);
|
||||
Ok(Response::builder()
|
||||
.status(500)
|
||||
.body::<Pin<Box<HttpBodyType>>>(Box::pin(Body::from(
|
||||
.body::<Pin<Box<HttpBodyType>>>(Box::pin(HyperBody::from(
|
||||
"Internal server error",
|
||||
)))
|
||||
.unwrap())
|
||||
@@ -53,7 +53,7 @@ impl Service<Request<Body>> for PixivDownloaderSvc {
|
||||
None => Box::pin(async {
|
||||
Ok(Response::builder()
|
||||
.status(404)
|
||||
.body::<Pin<Box<HttpBodyType>>>(Box::pin(Body::from("404 Not Found")))
|
||||
.body::<Pin<Box<HttpBodyType>>>(Box::pin(HyperBody::from("404 Not Found")))
|
||||
.unwrap())
|
||||
}),
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use super::body::hyper::HyperBody;
|
||||
use super::context::ServerContext;
|
||||
use super::params::RequestParams;
|
||||
use super::preclude::HttpBodyType;
|
||||
use crate::error::PixivDownloaderError;
|
||||
use hyper::Body;
|
||||
use hyper::Request;
|
||||
use hyper::Response;
|
||||
use json::JsonValue;
|
||||
@@ -55,7 +55,7 @@ where
|
||||
);
|
||||
Ok(Response::from_parts(
|
||||
parts,
|
||||
Box::pin(Body::from(body.to_string())),
|
||||
Box::pin(HyperBody::from(body.to_string())),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -266,6 +266,7 @@ impl PartialEq for UgoiraZipError2 {
|
||||
}
|
||||
|
||||
unsafe impl Send for UgoiraZipError2 {}
|
||||
unsafe impl Sync for UgoiraZipError2 {}
|
||||
|
||||
impl ToRawHandle<_ugoira::zip_error_t> for UgoiraZipError2 {
|
||||
unsafe fn to_raw_handle(&self) -> *mut _ugoira::zip_error_t {
|
||||
|
||||
Reference in New Issue
Block a user