From 0176e45156bac50c36aca505730ce7451a8aa795 Mon Sep 17 00:00:00 2001 From: Nugine Date: Sat, 9 Dec 2023 22:47:06 +0800 Subject: [PATCH] codegen: GetObject range returns 206 (#119) --- codegen/src/ops.rs | 38 ++++++++++++++++++++------------- crates/s3s/src/ops/generated.rs | 5 ++++- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/codegen/src/ops.rs b/codegen/src/ops.rs index c2a69ecd..ee64a3e7 100644 --- a/codegen/src/ops.rs +++ b/codegen/src/ops.rs @@ -218,26 +218,27 @@ fn codegen_header_value(ops: &Operations, rust_types: &RustTypes) { } } +fn codegen_op_http_ser_unit(op: &Operation) { + g!("pub fn serialize_http() -> http::Response {{"); + + if op.http_code == 200 { + g!("http::Response::default()"); + } else { + g!("let mut res = http::Response::default();"); + g!("res.status = http::StatusCode::{};", status_code_name(op.http_code)); + g!("res"); + } + + g!("}}"); +} + fn codegen_op_http_ser(op: &Operation, rust_types: &RustTypes) { let output = op.output.as_str(); let rust_type = &rust_types[output]; match rust_type { rust::Type::Provided(ty) => { assert_eq!(ty.name, "Unit"); - g!("pub fn serialize_http() -> http::Response {{"); - - if op.http_code == 200 { - g!("http::Response::default()"); - } else { - g!("let mut res = http::Response::default();"); - - let code_name = status_code_name(op.http_code); - g!("res.status = http::StatusCode::{code_name};"); - - g!("res"); - } - - g!("}}"); + codegen_op_http_ser_unit(op); } rust::Type::Struct(ty) => { if ty.fields.is_empty() { @@ -255,7 +256,14 @@ fn codegen_op_http_ser(op: &Operation, rust_types: &RustTypes) { assert!(["header", "metadata", "xml", "payload"].contains(&field.position.as_str()),); } - { + if op.name == "GetObject" { + assert_eq!(op.http_code, 200); + g!("let mut res = http::Response::default();"); + // https://github.com/Nugine/s3s/issues/118 + g!("if x.content_range.is_some() {{"); + g!(" res.status = http::StatusCode::PARTIAL_CONTENT;"); + g!("}}"); + } else { let code_name = status_code_name(op.http_code); g!("let mut res = http::Response::with_status(http::StatusCode::{code_name});"); } diff --git a/crates/s3s/src/ops/generated.rs b/crates/s3s/src/ops/generated.rs index 37ca351e..c5674dff 100644 --- a/crates/s3s/src/ops/generated.rs +++ b/crates/s3s/src/ops/generated.rs @@ -2515,7 +2515,10 @@ impl GetObject { } pub fn serialize_http(x: GetObjectOutput) -> S3Result { - let mut res = http::Response::with_status(http::StatusCode::OK); + let mut res = http::Response::default(); + if x.content_range.is_some() { + res.status = http::StatusCode::PARTIAL_CONTENT; + } if let Some(val) = x.body { http::set_stream_body(&mut res, val); }