Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
WebServer
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
codecraft
WebServer
Commits
374dcc3e
Commit
374dcc3e
authored
1 year ago
by
codecraft
Browse files
Options
Downloads
Patches
Plain Diff
Update `get_post_form_text` to allow for an array of keys to search
parent
fe8dba58
No related branches found
No related tags found
1 merge request
!1
Initial feature merge
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
core/http/src/handling/request.rs
+48
-22
48 additions, 22 deletions
core/http/src/handling/request.rs
with
48 additions
and
22 deletions
core/http/src/handling/request.rs
+
48
−
22
View file @
374dcc3e
...
@@ -72,9 +72,9 @@ impl Request<'_> {
...
@@ -72,9 +72,9 @@ impl Request<'_> {
}
}
}
}
// pub fn get_post_form_key<T: FromRequest>(&self, data: Data) -> T {}
// pub fn get_post_form_key<T: FromRequest>(&self, data: Data) -> T {}
pub
fn
get_get_form_keys
(
pub
fn
get_get_form_keys
<
'a
>
(
&
self
,
&
'a
self
,
keys
:
Vec
<
&
str
>
,
keys
:
&
'a
[
&
str
]
,
)
->
Result
<
HashMap
<&
str
,
&
str
>
,
ParseFormError
>
{
)
->
Result
<
HashMap
<&
str
,
&
str
>
,
ParseFormError
>
{
let
data
=
if
let
Some
(
val
)
=
self
.uri
.split_once
(
"?"
)
{
let
data
=
if
let
Some
(
val
)
=
self
.uri
.split_once
(
"?"
)
{
val
val
...
@@ -111,7 +111,11 @@ impl Request<'_> {
...
@@ -111,7 +111,11 @@ impl Request<'_> {
}
}
Ok
(
response
)
Ok
(
response
)
}
}
pub
fn
get_post_text_form_key
(
&
self
,
key
:
&
str
,
data
:
&
Data
)
->
Result
<
String
,
()
>
{
pub
fn
get_post_text_form_key
(
&
self
,
keys
:
&
[
&
str
],
data
:
&
Data
,
)
->
Result
<
Vec
<
String
>
,
ParseFormError
>
{
let
mut
post_type
=
self
let
mut
post_type
=
self
.headers
.headers
.iter
()
.iter
()
...
@@ -134,11 +138,18 @@ impl Request<'_> {
...
@@ -134,11 +138,18 @@ impl Request<'_> {
.split
(
"&"
)
.split
(
"&"
)
.map
(|
kvp
|
kvp
.split_once
(
"="
)
.unwrap
())
.map
(|
kvp
|
kvp
.split_once
(
"="
)
.unwrap
())
.collect
::
<
HashMap
<&
str
,
&
str
>>
();
.collect
::
<
HashMap
<&
str
,
&
str
>>
();
if
let
Some
(
val
)
=
kvps
.get
(
key
)
{
Ok
(
val
.to_string
())
let
mut
result
:
Vec
<
String
>
=
Vec
::
with_capacity
(
keys
.len
());
}
else
{
for
key
in
keys
{
Err
(())
if
let
Some
(
val
)
=
kvps
.get
(
key
)
{
result
.push
(
val
.to_string
());
}
else
{
return
Err
(
ParseFormError
{
error
:
ParseErrors
::
NoData
,
});
}
}
}
Ok
(
result
)
}
}
Mime
::
MultipartFormData
=>
{
Mime
::
MultipartFormData
=>
{
let
from_req
=
post_type
[
1
..
]
let
from_req
=
post_type
[
1
..
]
...
@@ -158,16 +169,17 @@ impl Request<'_> {
...
@@ -158,16 +169,17 @@ impl Request<'_> {
.collect
::
<
Vec
<&
[
u8
]
>>
();
.collect
::
<
Vec
<&
[
u8
]
>>
();
let
mut
boundary_found
=
false
;
let
mut
boundary_found
=
false
;
let
mut
key_found
=
fals
e
;
let
mut
current_key
:
Option
<
usize
>
=
Non
e
;
let
mut
value
=
vec!
[
];
let
mut
result
:
Vec
<
Vec
<
u8
>>
=
vec!
[
""
.into
();
keys
.len
()
];
for
part
in
parts
{
for
part
in
parts
{
if
part
==
[]
{
if
part
==
[]
{
continue
;
continue
;
}
}
if
(
key_found
&&
part
==
boundary
)
||
part
==
end_boundary
{
if
part
==
end_boundary
{
break
;
break
;
}
}
if
!
boundary_found
&&
part
==
boundary
{
if
!
boundary_found
&&
part
==
boundary
{
current_key
=
None
;
boundary_found
=
true
;
boundary_found
=
true
;
continue
;
continue
;
}
}
...
@@ -188,18 +200,27 @@ impl Request<'_> {
...
@@ -188,18 +200,27 @@ impl Request<'_> {
.trim_end
()
.trim_end
()
.trim_matches
(
'"'
)
.trim_matches
(
'"'
)
.to_owned
();
.to_owned
();
key_found
=
key
==
mkey
;
let
mut
index
=
0
;
}
else
if
key_found
==
true
{
for
i
in
keys
{
value
.extend_from_slice
(
part
);
if
*
i
==
mkey
{
value
.extend_from_slice
(
&
[
b'\n'
]);
current_key
=
Some
(
index
);
}
index
+=
1
;
}
boundary_found
=
false
;
}
else
if
let
Some
(
key
)
=
current_key
{
result
[
key
]
.extend_from_slice
(
part
);
result
[
key
]
.extend_from_slice
(
&
[
b'\n'
]);
}
}
}
}
Ok
(
String
::
from_utf8_lossy
(
&
value
)
Ok
(
result
.
to_owned
()
.
iter
()
.
trim
(
)
.
map
(|
arr
|
String
::
from_utf8
(
arr
.to_vec
())
.unwrap
()
.trim
()
.into
()
)
.
to_string
())
.
collect
())
}
}
_
=>
Err
(()),
_
=>
Err
(
ParseFormError
{
error
:
ParseErrors
::
BadData
,
}),
}
}
}
}
}
}
...
@@ -232,11 +253,16 @@ value2\r
...
@@ -232,11 +253,16 @@ value2\r
};
};
assert_eq!
(
assert_eq!
(
"value1"
,
"value1"
,
req
.get_post_text_form_key
(
"field1"
,
&
data
)
.unwrap
()
req
.get_post_text_form_key
(
&
[
"field1"
]
,
&
data
)
.unwrap
()
[
0
]
);
);
assert_eq!
(
assert_eq!
(
"value2"
,
"value2"
,
req
.get_post_text_form_key
(
"field2"
,
&
data
)
.unwrap
()
req
.get_post_text_form_key
(
&
[
"field2"
]
,
&
data
)
.unwrap
()
[
0
]
);
);
assert_eq!
(
vec!
[
"value1"
,
"value2"
],
req
.get_post_text_form_key
(
&
[
"field1"
,
"field2"
],
&
data
)
.unwrap
()
)
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment