fix(http): separate transport and protocol limits
This commit is contained in:
@@ -5,15 +5,15 @@ use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::thread;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use http::header::ALLOW;
|
||||
use http::{Request, Response, StatusCode};
|
||||
use wip_host::{ExecutionContext, ObjectProvider};
|
||||
use wip_http::{
|
||||
BatchRequestKind, BatchResponse, BatchResponseItem, BatchSuccess, CodecError, EncodeHttpError,
|
||||
Endpoint, RequestMetadataError, ResourceLimitKind, Route, decode_batch_request,
|
||||
decode_call_operation_metadata, decode_fetch_interface_request, decode_observe_request,
|
||||
encode_batch_error_response, encode_batch_resource_limit_error_response, encode_batch_response,
|
||||
encode_call_operation_response, encode_fetch_interface_response, encode_observe_response,
|
||||
encode_protocol_error_response, encode_resource_limit_error_response, validate_http_request,
|
||||
Endpoint, RequestMetadataError, Route, decode_batch_request, decode_call_operation_metadata,
|
||||
decode_fetch_interface_request, decode_observe_request, encode_batch_error_response,
|
||||
encode_batch_response, encode_call_operation_response, encode_fetch_interface_response,
|
||||
encode_observe_response, encode_protocol_error_response, validate_http_request,
|
||||
};
|
||||
use wip_protocol::{ProtocolError, ProtocolErrorCode, ProtocolInteraction};
|
||||
|
||||
@@ -68,20 +68,25 @@ impl WipFsServer {
|
||||
request: Request<Vec<u8>>,
|
||||
deadline: Instant,
|
||||
) -> Response<Vec<u8>> {
|
||||
let Some(route) = self.route(request.uri().path()) else {
|
||||
let Some(route) = self.route(request.uri()) else {
|
||||
return empty_response(StatusCode::NOT_FOUND);
|
||||
};
|
||||
if let Err(error) = validate_http_request(&self.endpoint, route, &request) {
|
||||
return self.metadata_error(route, error);
|
||||
return self.metadata_error(error);
|
||||
}
|
||||
let context = ExecutionContext::new()
|
||||
.with_deadline(deadline)
|
||||
.with_resource_budget(u64::from(self.config.limits.max_observation_nodes));
|
||||
match route {
|
||||
let response = match route {
|
||||
Route::Observe => self.observe(request.body(), &context),
|
||||
Route::FetchInterface => self.fetch_interface(request.body()),
|
||||
Route::CallOperation => self.call_operation(request.body(), &context),
|
||||
Route::Batch => self.batch(request.body(), &context),
|
||||
};
|
||||
if Instant::now() >= deadline {
|
||||
empty_response(StatusCode::GATEWAY_TIMEOUT)
|
||||
} else {
|
||||
response
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,7 +111,10 @@ impl WipFsServer {
|
||||
})
|
||||
}
|
||||
|
||||
fn route(&self, path: &str) -> Option<Route> {
|
||||
fn route(&self, uri: &http::Uri) -> Option<Route> {
|
||||
if uri.query().is_some() {
|
||||
return None;
|
||||
}
|
||||
[
|
||||
Route::Observe,
|
||||
Route::FetchInterface,
|
||||
@@ -114,7 +122,7 @@ impl WipFsServer {
|
||||
Route::Batch,
|
||||
]
|
||||
.into_iter()
|
||||
.find(|route| self.endpoint.route(*route).path() == path)
|
||||
.find(|route| self.endpoint.route(*route).path() == uri.path())
|
||||
}
|
||||
|
||||
fn observe(&self, body: &[u8], context: &ExecutionContext) -> Response<Vec<u8>> {
|
||||
@@ -127,7 +135,7 @@ impl WipFsServer {
|
||||
.unwrap_or_else(|error| {
|
||||
self.read_encode_error(ProtocolInteraction::Observe, error)
|
||||
}),
|
||||
Err(error) => self.protocol_error(ProtocolInteraction::Observe, &error, None),
|
||||
Err(error) => self.protocol_error(ProtocolInteraction::Observe, &error),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,7 +151,7 @@ impl WipFsServer {
|
||||
self.read_encode_error(ProtocolInteraction::FetchInterface, error)
|
||||
})
|
||||
}
|
||||
Err(error) => self.protocol_error(ProtocolInteraction::FetchInterface, &error, None),
|
||||
Err(error) => self.protocol_error(ProtocolInteraction::FetchInterface, &error),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,7 +189,7 @@ impl WipFsServer {
|
||||
},
|
||||
message: "operation completed without response metadata".into(),
|
||||
};
|
||||
return self.protocol_error(ProtocolInteraction::CallOperation, &error, None);
|
||||
return self.protocol_error(ProtocolInteraction::CallOperation, &error);
|
||||
};
|
||||
encode_call_operation_response(
|
||||
&request,
|
||||
@@ -190,6 +198,12 @@ impl WipFsServer {
|
||||
self.config.limits.wire,
|
||||
)
|
||||
.unwrap_or_else(|encode_error| {
|
||||
if matches!(
|
||||
&encode_error,
|
||||
EncodeHttpError::Codec(codec) if is_transport_limit(codec)
|
||||
) {
|
||||
return empty_response(StatusCode::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
if !may_commit {
|
||||
return self
|
||||
.read_encode_error(ProtocolInteraction::CallOperation, encode_error);
|
||||
@@ -200,10 +214,10 @@ impl WipFsServer {
|
||||
code: ProtocolErrorCode::OperationOutcomeUnknown,
|
||||
message: "operation completed but its response could not be encoded".into(),
|
||||
};
|
||||
self.protocol_error(ProtocolInteraction::CallOperation, &error, None)
|
||||
self.protocol_error(ProtocolInteraction::CallOperation, &error)
|
||||
})
|
||||
}
|
||||
Err(error) => self.protocol_error(ProtocolInteraction::CallOperation, &error, None),
|
||||
Err(error) => self.protocol_error(ProtocolInteraction::CallOperation, &error),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,22 +250,20 @@ impl WipFsServer {
|
||||
let response = BatchResponse { responses };
|
||||
encode_batch_response(&request, &response, self.config.limits.wire).unwrap_or_else(
|
||||
|error| match error {
|
||||
EncodeHttpError::Codec(codec) => self.batch_codec_error(codec),
|
||||
EncodeHttpError::Http(_) => self.batch_top_error(&internal_error(), None),
|
||||
EncodeHttpError::Codec(codec) => self.batch_encode_error(&codec),
|
||||
EncodeHttpError::Http(_) => self.batch_top_error(&internal_error()),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn metadata_error(&self, route: Route, error: RequestMetadataError) -> Response<Vec<u8>> {
|
||||
fn metadata_error(&self, error: RequestMetadataError) -> Response<Vec<u8>> {
|
||||
match error {
|
||||
RequestMetadataError::InvalidRequest(_) => self.protocol_error(
|
||||
interaction(route),
|
||||
&ProtocolError {
|
||||
code: ProtocolErrorCode::InvalidRequest,
|
||||
message: "invalid HTTP request metadata".into(),
|
||||
},
|
||||
None,
|
||||
),
|
||||
RequestMetadataError::NotFound => empty_response(StatusCode::NOT_FOUND),
|
||||
RequestMetadataError::MethodNotAllowed => Response::builder()
|
||||
.status(StatusCode::METHOD_NOT_ALLOWED)
|
||||
.header(ALLOW, "POST")
|
||||
.body(Vec::new())
|
||||
.expect("static method response is valid"),
|
||||
RequestMetadataError::UnsupportedMediaType => {
|
||||
empty_response(StatusCode::UNSUPPORTED_MEDIA_TYPE)
|
||||
}
|
||||
@@ -264,8 +276,8 @@ impl WipFsServer {
|
||||
interaction: ProtocolInteraction,
|
||||
error: CodecError,
|
||||
) -> Response<Vec<u8>> {
|
||||
if let Some(kind) = error.resource_limit_kind() {
|
||||
self.protocol_error(interaction, &resource_error(), Some(kind))
|
||||
if is_transport_limit(&error) {
|
||||
empty_response(StatusCode::PAYLOAD_TOO_LARGE)
|
||||
} else {
|
||||
self.protocol_error(
|
||||
interaction,
|
||||
@@ -273,22 +285,18 @@ impl WipFsServer {
|
||||
code: ProtocolErrorCode::InvalidRequest,
|
||||
message: "malformed WIP request".into(),
|
||||
},
|
||||
None,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn batch_codec_error(&self, error: CodecError) -> Response<Vec<u8>> {
|
||||
if let Some(kind) = error.resource_limit_kind() {
|
||||
self.batch_top_error(&resource_error(), Some(kind))
|
||||
if is_transport_limit(&error) {
|
||||
empty_response(StatusCode::PAYLOAD_TOO_LARGE)
|
||||
} else {
|
||||
self.batch_top_error(
|
||||
&ProtocolError {
|
||||
code: ProtocolErrorCode::InvalidRequest,
|
||||
message: "malformed WIP batch request".into(),
|
||||
},
|
||||
None,
|
||||
)
|
||||
self.batch_top_error(&ProtocolError {
|
||||
code: ProtocolErrorCode::InvalidRequest,
|
||||
message: "malformed WIP batch request".into(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -298,10 +306,18 @@ impl WipFsServer {
|
||||
error: EncodeHttpError,
|
||||
) -> Response<Vec<u8>> {
|
||||
match error {
|
||||
EncodeHttpError::Codec(codec) if codec.resource_limit_kind().is_some() => {
|
||||
self.protocol_error(interaction, &resource_error(), codec.resource_limit_kind())
|
||||
EncodeHttpError::Codec(codec) if is_transport_limit(&codec) => {
|
||||
empty_response(StatusCode::INTERNAL_SERVER_ERROR)
|
||||
}
|
||||
_ => self.protocol_error(interaction, &internal_error(), None),
|
||||
_ => self.protocol_error(interaction, &internal_error()),
|
||||
}
|
||||
}
|
||||
|
||||
fn batch_encode_error(&self, error: &CodecError) -> Response<Vec<u8>> {
|
||||
if is_transport_limit(error) {
|
||||
empty_response(StatusCode::INTERNAL_SERVER_ERROR)
|
||||
} else {
|
||||
self.batch_top_error(&internal_error())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -309,32 +325,14 @@ impl WipFsServer {
|
||||
&self,
|
||||
interaction: ProtocolInteraction,
|
||||
error: &ProtocolError,
|
||||
resource_kind: Option<ResourceLimitKind>,
|
||||
) -> Response<Vec<u8>> {
|
||||
let encoded = match resource_kind {
|
||||
Some(kind) => encode_resource_limit_error_response(
|
||||
interaction,
|
||||
error,
|
||||
kind,
|
||||
self.config.limits.wire,
|
||||
),
|
||||
None => encode_protocol_error_response(interaction, error, self.config.limits.wire),
|
||||
};
|
||||
encoded.unwrap_or_else(|_| empty_response(StatusCode::INTERNAL_SERVER_ERROR))
|
||||
encode_protocol_error_response(interaction, error, self.config.limits.wire)
|
||||
.unwrap_or_else(|_| empty_response(StatusCode::INTERNAL_SERVER_ERROR))
|
||||
}
|
||||
|
||||
fn batch_top_error(
|
||||
&self,
|
||||
error: &ProtocolError,
|
||||
resource_kind: Option<ResourceLimitKind>,
|
||||
) -> Response<Vec<u8>> {
|
||||
let encoded = match resource_kind {
|
||||
Some(kind) => {
|
||||
encode_batch_resource_limit_error_response(error, kind, self.config.limits.wire)
|
||||
}
|
||||
None => encode_batch_error_response(error, self.config.limits.wire),
|
||||
};
|
||||
encoded.unwrap_or_else(|_| empty_response(StatusCode::INTERNAL_SERVER_ERROR))
|
||||
fn batch_top_error(&self, error: &ProtocolError) -> Response<Vec<u8>> {
|
||||
encode_batch_error_response(error, self.config.limits.wire)
|
||||
.unwrap_or_else(|_| empty_response(StatusCode::INTERNAL_SERVER_ERROR))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -404,7 +402,7 @@ struct ParsedRequestHead {
|
||||
enum IntakeError {
|
||||
Timeout,
|
||||
HeaderTooLarge,
|
||||
PayloadTooLarge { target: String },
|
||||
PayloadTooLarge,
|
||||
BadRequest,
|
||||
Socket,
|
||||
}
|
||||
@@ -425,9 +423,7 @@ fn serve_connection(service: &WipFsServer, mut stream: TcpStream) {
|
||||
Err(IntakeError::HeaderTooLarge) => {
|
||||
Some(empty_response(StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE))
|
||||
}
|
||||
Err(IntakeError::PayloadTooLarge { target }) => {
|
||||
Some(payload_too_large_response(service, &target))
|
||||
}
|
||||
Err(IntakeError::PayloadTooLarge) => Some(empty_response(StatusCode::PAYLOAD_TOO_LARGE)),
|
||||
Err(IntakeError::BadRequest) => Some(empty_response(StatusCode::BAD_REQUEST)),
|
||||
Err(IntakeError::Timeout | IntakeError::Socket) => None,
|
||||
};
|
||||
@@ -466,9 +462,7 @@ fn read_http_request(
|
||||
|
||||
let parsed = parse_request_head(&received[..header_end])?;
|
||||
if parsed.content_length > maximum_body {
|
||||
return Err(IntakeError::PayloadTooLarge {
|
||||
target: parsed.target,
|
||||
});
|
||||
return Err(IntakeError::PayloadTooLarge);
|
||||
}
|
||||
let mut body = Vec::with_capacity(parsed.content_length);
|
||||
let buffered = &received[header_end..];
|
||||
@@ -567,27 +561,6 @@ fn read_with_deadline(
|
||||
})
|
||||
}
|
||||
|
||||
fn payload_too_large_response(service: &WipFsServer, target: &str) -> Response<Vec<u8>> {
|
||||
let route = service.route(target.split('?').next().unwrap_or_default());
|
||||
route.map_or_else(
|
||||
|| empty_response(StatusCode::PAYLOAD_TOO_LARGE),
|
||||
|route| {
|
||||
if route == Route::Batch {
|
||||
service.batch_top_error(
|
||||
&resource_error(),
|
||||
Some(ResourceLimitKind::RepresentationBytes),
|
||||
)
|
||||
} else {
|
||||
service.protocol_error(
|
||||
interaction(route),
|
||||
&resource_error(),
|
||||
Some(ResourceLimitKind::RepresentationBytes),
|
||||
)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn write_http_response(
|
||||
stream: &mut TcpStream,
|
||||
response: &Response<Vec<u8>>,
|
||||
@@ -648,20 +621,13 @@ fn write_all_with_deadline(
|
||||
stream.flush()
|
||||
}
|
||||
|
||||
fn interaction(route: Route) -> ProtocolInteraction {
|
||||
match route {
|
||||
Route::Observe => ProtocolInteraction::Observe,
|
||||
Route::FetchInterface => ProtocolInteraction::FetchInterface,
|
||||
Route::CallOperation => ProtocolInteraction::CallOperation,
|
||||
Route::Batch => ProtocolInteraction::Observe,
|
||||
}
|
||||
}
|
||||
|
||||
fn resource_error() -> ProtocolError {
|
||||
ProtocolError {
|
||||
code: ProtocolErrorCode::ResourceLimitExceeded,
|
||||
message: "configured resource limit exceeded".into(),
|
||||
}
|
||||
fn is_transport_limit(error: &CodecError) -> bool {
|
||||
matches!(
|
||||
error,
|
||||
CodecError::BodyTooLarge { .. }
|
||||
| CodecError::NestingTooDeep { .. }
|
||||
| CodecError::TooManyBatchItems { .. }
|
||||
)
|
||||
}
|
||||
|
||||
fn internal_error() -> ProtocolError {
|
||||
|
||||
Reference in New Issue
Block a user