mirror of
https://git.proxmox.com/git/proxmox
synced 2025-05-30 21:51:38 +00:00
rest-server: make handle_request a method of ApiConfig
This is what actually defines the API server after all. The ApiService trait in between is a hyper impl detail. Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
parent
5fe0777318
commit
93c027f5cc
@ -241,7 +241,7 @@ impl Service<Request<Body>> for ApiService {
|
|||||||
None => self.peer,
|
None => self.peer,
|
||||||
};
|
};
|
||||||
async move {
|
async move {
|
||||||
let response = match handle_request(Arc::clone(&config), req, &peer).await {
|
let response = match Arc::clone(&config).handle_request(req, &peer).await {
|
||||||
Ok(response) => response,
|
Ok(response) => response,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
let (err, code) = match err.downcast_ref::<HttpError>() {
|
let (err, code) = match err.downcast_ref::<HttpError>() {
|
||||||
@ -601,11 +601,12 @@ fn extract_compression_method(headers: &http::HeaderMap) -> Option<CompressionMe
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_request(
|
impl ApiConfig {
|
||||||
api: Arc<ApiConfig>,
|
pub async fn handle_request(
|
||||||
|
self: Arc<ApiConfig>,
|
||||||
req: Request<Body>,
|
req: Request<Body>,
|
||||||
peer: &std::net::SocketAddr,
|
peer: &std::net::SocketAddr,
|
||||||
) -> Result<Response<Body>, Error> {
|
) -> Result<Response<Body>, Error> {
|
||||||
let (parts, body) = req.into_parts();
|
let (parts, body) = req.into_parts();
|
||||||
let method = parts.method.clone();
|
let method = parts.method.clone();
|
||||||
let (path, components) = normalize_uri_path(parts.uri.path())?;
|
let (path, components) = normalize_uri_path(parts.uri.path())?;
|
||||||
@ -620,13 +621,14 @@ async fn handle_request(
|
|||||||
.unwrap());
|
.unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
let env_type = api.env_type();
|
let env_type = self.env_type();
|
||||||
let mut rpcenv = RestEnvironment::new(env_type, Arc::clone(&api));
|
let mut rpcenv = RestEnvironment::new(env_type, Arc::clone(&self));
|
||||||
|
|
||||||
rpcenv.set_client_ip(Some(*peer));
|
rpcenv.set_client_ip(Some(*peer));
|
||||||
|
|
||||||
let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000);
|
let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000);
|
||||||
let access_forbidden_time = std::time::Instant::now() + std::time::Duration::from_millis(500);
|
let access_forbidden_time =
|
||||||
|
std::time::Instant::now() + std::time::Duration::from_millis(500);
|
||||||
|
|
||||||
if comp_len >= 1 && components[0] == "api2" {
|
if comp_len >= 1 && components[0] == "api2" {
|
||||||
if comp_len >= 2 {
|
if comp_len >= 2 {
|
||||||
@ -639,7 +641,7 @@ async fn handle_request(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let mut uri_param = HashMap::new();
|
let mut uri_param = HashMap::new();
|
||||||
let api_method = api.find_method(&components[2..], method.clone(), &mut uri_param);
|
let api_method = self.find_method(&components[2..], method.clone(), &mut uri_param);
|
||||||
|
|
||||||
let mut auth_required = true;
|
let mut auth_required = true;
|
||||||
if let Some(api_method) = api_method {
|
if let Some(api_method) = api_method {
|
||||||
@ -652,7 +654,7 @@ async fn handle_request(
|
|||||||
Box::new(EmptyUserInformation {});
|
Box::new(EmptyUserInformation {});
|
||||||
|
|
||||||
if auth_required {
|
if auth_required {
|
||||||
match api.check_auth(&parts.headers, &method).await {
|
match self.check_auth(&parts.headers, &method).await {
|
||||||
Ok((authid, info)) => {
|
Ok((authid, info)) => {
|
||||||
rpcenv.set_auth_id(Some(authid));
|
rpcenv.set_auth_id(Some(authid));
|
||||||
user_info = info;
|
user_info = info;
|
||||||
@ -691,14 +693,18 @@ async fn handle_request(
|
|||||||
user_info.as_ref(),
|
user_info.as_ref(),
|
||||||
) {
|
) {
|
||||||
let err = http_err!(FORBIDDEN, "permission check failed");
|
let err = http_err!(FORBIDDEN, "permission check failed");
|
||||||
tokio::time::sleep_until(Instant::from_std(access_forbidden_time)).await;
|
tokio::time::sleep_until(Instant::from_std(access_forbidden_time))
|
||||||
|
.await;
|
||||||
return Ok(formatter.format_error(err));
|
return Ok(formatter.format_error(err));
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = if api_method.protected && env_type == RpcEnvironmentType::PUBLIC {
|
let result =
|
||||||
|
if api_method.protected && env_type == RpcEnvironmentType::PUBLIC {
|
||||||
proxy_protected_request(api_method, parts, body, peer).await
|
proxy_protected_request(api_method, parts, body, peer).await
|
||||||
} else {
|
} else {
|
||||||
handle_api_request(rpcenv, api_method, formatter, parts, body, uri_param)
|
handle_api_request(
|
||||||
|
rpcenv, api_method, formatter, parts, body, uri_param,
|
||||||
|
)
|
||||||
.await
|
.await
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -725,23 +731,24 @@ async fn handle_request(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if comp_len == 0 {
|
if comp_len == 0 {
|
||||||
match api.check_auth(&parts.headers, &method).await {
|
match self.check_auth(&parts.headers, &method).await {
|
||||||
Ok((auth_id, _user_info)) => {
|
Ok((auth_id, _user_info)) => {
|
||||||
rpcenv.set_auth_id(Some(auth_id));
|
rpcenv.set_auth_id(Some(auth_id));
|
||||||
return Ok(api.get_index(rpcenv, parts).await);
|
return Ok(self.get_index(rpcenv, parts).await);
|
||||||
}
|
}
|
||||||
Err(AuthError::Generic(_)) => {
|
Err(AuthError::Generic(_)) => {
|
||||||
tokio::time::sleep_until(Instant::from_std(delay_unauth_time)).await;
|
tokio::time::sleep_until(Instant::from_std(delay_unauth_time)).await;
|
||||||
}
|
}
|
||||||
Err(AuthError::NoData) => {}
|
Err(AuthError::NoData) => {}
|
||||||
}
|
}
|
||||||
return Ok(api.get_index(rpcenv, parts).await);
|
return Ok(self.get_index(rpcenv, parts).await);
|
||||||
} else {
|
} else {
|
||||||
let filename = api.find_alias(&components);
|
let filename = self.find_alias(&components);
|
||||||
let compression = extract_compression_method(&parts.headers);
|
let compression = extract_compression_method(&parts.headers);
|
||||||
return handle_static_file_download(filename, compression).await;
|
return handle_static_file_download(filename, compression).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Err(http_err!(NOT_FOUND, "Path '{}' not found.", path))
|
Err(http_err!(NOT_FOUND, "Path '{}' not found.", path))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user