rumtk_web/utils/form_data.rs
1/*
2 * rumtk attempts to implement HL7 and medical protocols for interoperability in medicine.
3 * This toolkit aims to be reliable, simple, performant, and standards compliant.
4 * Copyright (C) 2025 Luis M. Santos, M.D. <lsantos@medicalmasses.com>
5 * Copyright (C) 2025 Ethan Dixon
6 * Copyright (C) 2025 MedicalMasses L.L.C. <contact@medicalmasses.com>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21use rumtk_core::core::RUMResult;
22use rumtk_core::strings::{
23 rumtk_format, RUMArrayConversions, RUMString, RUMStringConversions, ToCompactString,
24};
25use rumtk_core::types::{RUMBuffer, RUMHashMap, RUMID};
26
27use crate::utils::defaults::*;
28use crate::{RUMWebData, RouterForm};
29
30pub type FormBuffer = RUMBuffer;
31
32#[derive(Default, Debug, PartialEq, Clone)]
33pub struct FormData {
34 pub form: RUMWebData,
35 pub files: RUMHashMap<RUMString, FormBuffer>,
36}
37
38impl FormData {
39 pub fn len(&self) -> usize {
40 self.form.len()
41 }
42
43 pub fn is_empty(&self) -> bool {
44 self.form.is_empty()
45 }
46}
47
48pub type FormResult = RUMResult<FormData>;
49
50pub async fn get_type(content_type: &str) -> &'static str {
51 match content_type {
52 CONTENT_TYPE_PDF => FORM_DATA_TYPE_PDF,
53 _ => FORM_DATA_TYPE_DEFAULT,
54 }
55}
56
57///
58/// Converts the incoming form data with type [RouterForm] to [FormData] which is the preferred
59/// type in the library.
60///
61/// ## Examples
62///
63/// ### Plaintext only
64/// ```
65/// use axum::body::Body;
66/// use rumtk_core::{rumtk_spawn_task, rumtk_resolve_task};
67/// use rumtk_web::utils::testdata::data::TESTDATA_FORMDATA_REQUEST;
68/// use rumtk_web::utils::RouterForm;
69/// use rumtk_web::utils::form_data::compile_form_data;
70/// use rumtk_web::FormData;
71/// use axum::extract::{Request, FromRequest};
72/// use rumtk_core::core::RUMResult;
73/// use rumtk_core::types::RUMBuffer;
74/// use rumtk_web::form_data::FormResult;
75///
76/// let expected_form = FormData::default();
77///
78/// async fn create_form() -> FormResult {
79/// let mut raw_form = RouterForm::from_request(TESTDATA_FORMDATA_REQUEST(), &()).await.expect("Multipart form expected.");
80/// compile_form_data(&mut raw_form).await
81/// }
82///
83/// rumtk_resolve_task!(create_form());
84///
85/// ```
86///
87/// ## Note
88/// ```text
89/// Because anything that axum does not like could trigger a truncation of the incoming form, I
90/// could not even test this function without silencing the parsing error and returning any successful
91/// results so far. Axum would complain about an error parsing the multipart form when using a "mocked" Body buffer.
92/// Turns out, you can still properly parse a buffer. Also, for testing purposes, you cannot use byte
93/// literals as the input to a mocked Body but you can use a Vec<u8> and write!() to it then call
94/// into() on that buffer and everything then works despite still complaining about the error.
95/// Since we are ignoring anything past this point, I think this is technically safe while still
96/// allowing us to test this logic.
97/// ```
98///
99pub async fn compile_form_data(form: &mut RouterForm) -> FormResult {
100 let mut form_data = FormData::default();
101
102 while let field_result = form.next_field().await {
103 match field_result {
104 Ok(field_option) => match field_option {
105 Some(mut field) => {
106 let typ = match field.content_type() {
107 Some(content_type) => get_type(content_type).await,
108 None => FORM_DATA_TYPE_DEFAULT,
109 };
110 let name = field.name().unwrap_or_default().to_rumstring();
111
112 // If we got an empty field name, discard.
113 if name.is_empty() {
114 continue;
115 }
116
117 let data = match field.bytes().await {
118 Ok(bytes) => bytes,
119 Err(e) => {
120 return Err(rumtk_format!("Field data transfer failed because {}!", e))
121 }
122 };
123
124 if typ == FORM_DATA_TYPE_DEFAULT {
125 form_data.form.insert(name, data.to_vec().to_rumstring());
126 } else {
127 let file_id = RUMID::new_v4().to_compact_string();
128 &form_data.files.insert(file_id.clone(), data);
129 &form_data.form.insert(name, file_id);
130 }
131 }
132 None => {
133 // Ok so this one is important to be careful with.
134 // During some testing, I was able to pass a form with no fields.
135 // This form is not 0 bytes as it does contain one boundary line and then the
136 // new line. However, unlike during the browser test, it was not possible to
137 // craft a unit test that replicates the issue perhaps because the unit test has
138 // a fixed buffer? Not sure, it is strange. Without a unit test, it is not clear
139 // how to report issue to axum. At any rate, we should be handling this bit
140 // anyways. So if we encounter a None for the next field, let's assume the form
141 // is complete.
142 break;
143 }
144 },
145 Err(e) => {
146 // Just return what you got. This is tricky, because anything that axum does not like could
147 // trigger a truncation of the incoming form, but I could not even test this function without
148 // doing this because it would complain about an error parsing the multipart form. Turns out,
149 // you can still properly parse a buffer. Also, for testing purposes, you cannot use byte
150 // literals as the input to a mocked Body but you can use a Vec<u8> and write!() to it then
151 // call into() on that buffer and everything then works despite still complaining about the error.
152 // Since we are ignoring anything past this point, I think this is technically safe while still
153 // allowing us to test this logic.
154 return Ok(form_data);
155 }
156 }
157 }
158
159 Ok(form_data)
160}