Skip to main content

nv_redfish_core/
upload.rs

1// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use core::pin::Pin;
17use std::error::Error as StdError;
18use std::fmt;
19use std::time::Duration;
20
21use futures_io::AsyncRead;
22
23const OEM_PREFIX: &str = "Oem";
24
25/// Async reader accepted by upload methods.
26pub trait UploadReader: AsyncRead + Send + 'static {}
27
28impl<T> UploadReader for T where T: AsyncRead + Send + 'static {}
29
30/// Named data stream accepted by multipart upload methods.
31pub struct DataStream<R> {
32    /// Multipart filename for this stream.
33    pub name: String,
34
35    /// Streamed upload data.
36    pub reader: R,
37
38    /// Known stream length, when available.
39    pub content_length: Option<u64>,
40}
41
42impl<R> DataStream<R> {
43    /// Create a named data stream without a known content length.
44    #[must_use]
45    pub fn new(name: impl Into<String>, reader: R) -> Self {
46        Self {
47            name: name.into(),
48            reader,
49            content_length: None,
50        }
51    }
52
53    /// Attach a known content length.
54    #[must_use]
55    pub const fn with_content_length(mut self, content_length: u64) -> Self {
56        self.content_length = Some(content_length);
57        self
58    }
59}
60
61/// Streamed body accepted by deprecated raw upload methods.
62pub struct UploadStream<R> {
63    /// Streamed upload data.
64    pub reader: R,
65
66    /// Known stream length, when available.
67    pub content_length: Option<u64>,
68}
69
70impl<R> UploadStream<R> {
71    /// Create a streamed upload body without a known content length.
72    #[must_use]
73    pub const fn new(reader: R) -> Self {
74        Self {
75            reader,
76            content_length: None,
77        }
78    }
79
80    /// Attach a known content length.
81    #[must_use]
82    pub const fn with_content_length(mut self, content_length: u64) -> Self {
83        self.content_length = Some(content_length);
84        self
85    }
86}
87
88/// Error returned when an OEM multipart part name is invalid.
89#[derive(Debug, Clone, PartialEq, Eq)]
90pub struct OemMultipartPartNameError {
91    /// Invalid multipart part name.
92    pub name: String,
93}
94
95impl fmt::Display for OemMultipartPartNameError {
96    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97        write!(
98            f,
99            "OEM multipart part name must start with Oem: {}",
100            self.name
101        )
102    }
103}
104
105impl StdError for OemMultipartPartNameError {}
106
107/// Reader type used for OEM multipart form parts.
108pub type OemMultipartPartReader = Pin<Box<dyn AsyncRead + Send + 'static>>;
109
110/// OEM multipart form part.
111pub struct OemMultipartPart {
112    /// Multipart part name.
113    pub name: String,
114
115    /// Streamed part data.
116    pub reader: OemMultipartPartReader,
117
118    /// Optional part content type.
119    pub content_type: Option<String>,
120
121    /// Known part length, when available.
122    pub content_length: Option<u64>,
123}
124
125impl OemMultipartPart {
126    /// Checks if the OEM part name is valid per the spec.
127    #[must_use]
128    pub fn is_name_valid(&self) -> bool {
129        self.name.starts_with(OEM_PREFIX)
130    }
131}
132
133impl OemMultipartPart {
134    /// Create an OEM multipart part.
135    ///
136    /// # Errors
137    ///
138    /// Returns an error if `name` does not start with `Oem`.
139    pub fn new(
140        name: impl Into<String>,
141        reader: impl UploadReader,
142    ) -> Result<Self, OemMultipartPartNameError> {
143        let name = name.into();
144
145        if !name.starts_with(OEM_PREFIX) {
146            return Err(OemMultipartPartNameError { name });
147        }
148
149        Ok(Self {
150            name,
151            reader: Box::pin(reader),
152            content_type: None,
153            content_length: None,
154        })
155    }
156
157    /// Attach a content type.
158    #[must_use]
159    pub fn with_content_type(mut self, content_type: impl Into<String>) -> Self {
160        self.content_type = Some(content_type.into());
161        self
162    }
163
164    /// Attach a known content length.
165    #[must_use]
166    pub const fn with_content_length(mut self, content_length: u64) -> Self {
167        self.content_length = Some(content_length);
168        self
169    }
170}
171
172/// Multipart `UpdateService` upload request data.
173pub struct MultipartUpdateRequest<'a, U, V> {
174    /// Redfish `UpdateParameters` JSON part.
175    pub update_parameters: &'a V,
176
177    /// Named stream sent as the Redfish `UpdateFile` part.
178    pub update_stream: DataStream<U>,
179
180    /// Optional OEM-defined multipart parts.
181    pub oem_parts: Vec<OemMultipartPart>,
182
183    /// Timeout used only for this upload request.
184    pub upload_timeout: Duration,
185}
186
187/// `UpdateService` raw `HttpPushUri` upload request data.
188pub struct HttpPushUriUpdateRequest<U> {
189    /// Streamed update image data.
190    pub update_stream: UploadStream<U>,
191
192    /// Timeout used only for this upload request.
193    pub upload_timeout: Duration,
194}