1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-09-25 22:29:45 +00:00

Update actix

This commit is contained in:
2022-03-09 19:58:46 +01:00
parent 4aa7657e28
commit c874d1c964
7 changed files with 404 additions and 1005 deletions

View File

@@ -2,9 +2,9 @@ use std::collections::HashMap;
use std::pin::Pin;
use actix_web::{App, FromRequest, http, HttpMessage, HttpRequest, HttpResponse, HttpServer, web};
use actix_web::dev::{Decompress, Payload, PayloadStream};
use actix_web::dev::{Decompress, Payload};
use actix_web::error::{ErrorBadRequest, ErrorInternalServerError, PayloadError};
use actix_web::web::{Buf, BufMut, Bytes, BytesMut};
use actix_web::web::{BufMut, Bytes, BytesMut};
use encoding_rs::UTF_8;
use futures::{FutureExt, Stream, StreamExt};
use futures::future::LocalBoxFuture;
@@ -54,7 +54,7 @@ impl<'a> Stream for LimitedStream
/// Custom request value
struct CustomRequest {
req: web::HttpRequest,
req: HttpRequest,
body: HashMap<String, RequestValue>,
}
@@ -62,9 +62,8 @@ struct CustomRequest {
impl FromRequest for CustomRequest {
type Error = actix_web::Error;
type Future = LocalBoxFuture<'static, Result<CustomRequest, actix_web::Error>>;
type Config = ();
fn from_request(req: &HttpRequest, payload: &mut Payload<PayloadStream>) -> Self::Future {
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
let req = req.clone();
let payload = Box::new(payload.take());
@@ -159,8 +158,7 @@ impl FromRequest for CustomRequest {
while let Some(el) = req.next().await {
let mut field = el?;
let content_type = field.content_disposition().ok_or(
ErrorInternalServerError("Missing content type"))?;
let content_type = field.content_disposition().clone();
let name = content_type.get_name().ok_or(
ErrorInternalServerError("Missing field name!"))?;
@@ -177,7 +175,7 @@ impl FromRequest for CustomRequest {
body_args.insert(name.to_string(),
RequestValue::File(PostFile {
name: filename.to_string(),
buff: buf.to_bytes(),
buff: buf.freeze(),
}));
}
@@ -187,7 +185,7 @@ impl FromRequest for CustomRequest {
// Get content
while let Some(chunk) = field.next().await {
content = format!("{}{}", content, String::from_utf8_lossy(chunk?.bytes()));
content = format!("{}{}", content, String::from_utf8_lossy(chunk?.as_ref()));
}
body_args.insert(name.to_string(), RequestValue::String(content));
@@ -342,14 +340,15 @@ async fn handle_options_request(r: HttpRequest) -> HttpResponse {
// Accept request
HttpResponse::NoContent()
.header("Access-Control-Allow-Origin", origin)
.header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
.append_header(("Access-Control-Allow-Origin", origin))
.append_header(("Access-Control-Allow-Methods", "POST, GET, OPTIONS"))
.finish()
}
/// Given the configuration, start the server
pub async fn start_server(conf: &Config) -> std::io::Result<()> {
pub fn start_server(conf: &Config) -> std::io::Result<()> {
let sys = actix::System::new();
// Initialize limit helper
requests_limit_helper::init();
@@ -362,7 +361,7 @@ pub async fn start_server(conf: &Config) -> std::io::Result<()> {
let serve_storage_file = conf.serve_storage_file;
HttpServer::new(move || {
let server = HttpServer::new(move || {
let mut app = App::new();
if serve_storage_file {
@@ -376,10 +375,14 @@ pub async fn start_server(conf: &Config) -> std::io::Result<()> {
.service(actix_web::web::resource("/rtc_proxy/ws").to(rtc_relay_controller::open_ws))
// Option
.route("**", web::method(http::Method::OPTIONS).to(handle_options_request))
.route("{tail:.*}", web::method(http::Method::OPTIONS).to(handle_options_request))
// API routes
.route("**", web::get().to(process_request))
.route("**", web::post().to(process_request))
}).bind(&addr)?.run().await
.route("{tail:.*}", web::get().to(process_request))
.route("{tail:.*}", web::post().to(process_request))
}).bind(&addr)?.run();
sys.block_on(server)?;
Ok(())
}