1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/// when ever incoming request has body of type multipartForm-data
#[derive(Debug)]
pub struct MultiPartFormDataField<'a> {
pub headers:KeyValueList<'a,12>,
/// determining the main length of field headers
pub field_header_length:usize
}
impl <'a> MultiPartFormDataField<'a> {
/// checking if incoming field is a file or not
/// by checking file name property
/// you could check also content-type manually by using [self.content_type]
pub fn is_file(&self)->bool{
if let Some(v) = self.headers.get_as_header_value("Content-Disposition") {
return v.get_from_values_as_bytes("filename").is_some();
}
false
}
/// getting content_disposition name
pub fn content_disposition_name(&self)->Option<Cow<'_,str>>{
if let Some(ref v) = self.headers.get_as_header_value("Content-Disposition") {
if let Some(v) = v.get_from_values_as_str("name") {
return Some(Cow::Owned(v.to_string()))
}
}
None
}
/// for getting content type of field [MultiPartFormDataField]
pub fn content_type(&self)->Option<&'a [u8]>{
self.headers.get_as_bytes("Content-Type")
}
pub (crate) fn new(payload:&'a[u8])->Option<MultiPartFormDataField<'a> >{
let key_list = KeyValueList::<12>::try_parse(payload);
if let Some((list,length)) = key_list {
return Some(MultiPartFormDataField{
field_header_length:length,
headers:list,
})
}
None
}
// #[allow(unused_assignments)]
// pub (crate) fn new(payload:&'a[u8])->Option<MultiPartFormDataField<'a>>{
// let mut key:Option<&'a[u8]> = None;
// let mut disposition_key:Option<&'a[u8]> = None;
// let mut name = None;
// let mut disposition_type = ContentDispositionType::FormData;
// let mut file_name = None;
// let mut content_type = None;
// let mut end_counter:usize = 0;
// let mut start:usize = 0;
// let mut disposition_indexes_start = false;
// let payload_length = payload.len();
// for (index,bytes) in payload.iter().enumerate() {
//
// match bytes {
// b'\r' => {
// end_counter+=1;
// if disposition_indexes_start {
// disposition_indexes_start = false;
// if let Some(key) = disposition_key {
// match key {
// b"name"=>{ name = Some(&payload[start..index]);
// inc_start_pointer!(start,index,payload_length);
// disposition_key = None;
// }
// b"filename"=>{
// file_name = Some(&payload[start..index]);
// inc_start_pointer!(start,index,payload_length);
// disposition_key = None;
// }
// _=>{disposition_key = None;}
// }
// }
// }
// else if let Some(key) = key {
// if key == b"Content-Type" {
// content_type = Some(&payload[start..index]);
// }
// }
// inc_start_pointer!(start,index,payload_length);
// }
// b'\n' => {
// end_counter+=1;
// if end_counter >=3 {
// start = index+1;
// break;
// }
// inc_start_pointer!(start,index,payload_length);
// }
// b' '=> {
// end_counter = 0 ;
// if key.is_some() && start == index {
// inc_start_pointer!(start,index,payload_length);
// }
// }
// b';'=> {
// end_counter = 0 ;
// if disposition_indexes_start {
// if let Some(key) = disposition_key {
// match key {
// b"name"=>{ name = Some(&payload[start..index]);
// inc_start_pointer!(start,index,payload_length);
// },
// b"filename"=>{
// file_name = Some(&payload[start..index]);
// inc_start_pointer!(start,index,payload_length);
// },
// _ => {}
// }
// }
// else {
// let data = &payload[start..index];
// match data {
// b"inline"=>{
// disposition_type = ContentDispositionType::Inline;
// },
//
// b"attachment"=>{
// disposition_type = ContentDispositionType::Attachment;
// },
// _ => {}
// }
// inc_start_pointer!(start,index,payload_length);
// }
// }
// }
// b'='=> {
// end_counter = 0 ;
// if disposition_indexes_start {
// disposition_key = Some(&payload[start..index]);
// inc_start_pointer!(start,index,payload_length);
// }
// }
// b':'=>{
// end_counter = 0 ;
// let kd = &payload[start..index];
// if !disposition_indexes_start && kd == b"Content-Disposition" {
// disposition_indexes_start = true;
// }
// key = Some(kd);
// inc_start_pointer!(start,index,payload_length);
// }
// _ =>{
// end_counter = 0 ;
// }
// };
// }
// if let Some(name) = name {
// return Some(MultiPartFormDataField {
// content_disposition: disposition_type,
// name,
// file_name,
// field_header_length:start,
// content_type
// });
// }
// None
// }
}
use std::borrow::Cow;
use crate::http::request::KeyValueList;