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
844c4389
Commit
844c4389
authored
1 year ago
by
codecraft
Browse files
Options
Downloads
Patches
Plain Diff
Add Get request form data function
parent
677eb8a0
No related branches found
No related tags found
1 merge request
!1
Initial feature merge
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
core/http/src/handling/request.rs
+75
-1
75 additions, 1 deletion
core/http/src/handling/request.rs
core/http/src/handling/routes.rs
+3
-1
3 additions, 1 deletion
core/http/src/handling/routes.rs
site/src/main.rs
+21
-7
21 additions, 7 deletions
site/src/main.rs
with
99 additions
and
9 deletions
core/http/src/handling/request.rs
+
75
−
1
View file @
844c4389
// use std::net::SocketAddr;
// use std::net::SocketAddr;
use
super
::{
methods
::
Method
,
routes
::
Uri
};
use
std
::{
collections
::
HashMap
,
error
::
Error
,
fmt
::
Display
};
use
super
::{
methods
::
Method
,
routes
::{
Data
,
Uri
},
};
type
HeaderMap
=
Vec
<
String
>
;
type
HeaderMap
=
Vec
<
String
>
;
#[derive(Clone)]
#[derive(Clone)]
...
@@ -23,6 +28,34 @@ pub enum MediaType {
...
@@ -23,6 +28,34 @@ pub enum MediaType {
Html
,
Html
,
}
}
#[derive(Debug)]
pub
enum
ParseErrors
{
NoData
,
BadData
,
}
#[derive(Debug)]
pub
struct
ParseFormError
{
pub
error
:
ParseErrors
,
}
impl
Display
for
ParseFormError
{
fn
fmt
(
&
self
,
f
:
&
mut
std
::
fmt
::
Formatter
<
'_
>
)
->
std
::
fmt
::
Result
{
write!
(
f
,
"{}"
,
self
.error
)
}
}
impl
Display
for
ParseErrors
{
fn
fmt
(
&
self
,
f
:
&
mut
std
::
fmt
::
Formatter
<
'_
>
)
->
std
::
fmt
::
Result
{
match
self
{
ParseErrors
::
NoData
=>
write!
(
f
,
"No Data at key"
),
ParseErrors
::
BadData
=>
write!
(
f
,
"Bad Data at key"
),
}
}
}
impl
Error
for
ParseFormError
{}
impl
Request
<
'_
>
{
impl
Request
<
'_
>
{
pub
fn
can_have_body
(
&
self
)
->
bool
{
pub
fn
can_have_body
(
&
self
)
->
bool
{
match
self
.method
{
match
self
.method
{
...
@@ -30,4 +63,45 @@ impl Request<'_> {
...
@@ -30,4 +63,45 @@ impl Request<'_> {
_
=>
false
,
_
=>
false
,
}
}
}
}
pub
fn
get_form_keys
(
&
self
,
keys
:
Vec
<&
str
>
)
->
Result
<
HashMap
<&
str
,
&
str
>
,
ParseFormError
>
{
let
data
=
self
.uri
.split_once
(
"?"
);
if
data
==
None
{
return
Err
(
ParseFormError
{
error
:
ParseErrors
::
NoData
,
});
}
let
data
=
data
.unwrap
()
.1
.split
(
"&"
)
.map
(|
kvp
|
kvp
.split_once
(
"="
))
.collect
::
<
Vec
<
Option
<
(
&
str
,
&
str
)
>>>
();
let
mut
values
:
HashMap
<&
str
,
&
str
>
=
HashMap
::
new
();
for
kvp
in
data
{
if
kvp
==
None
{
continue
;
}
let
kvp
=
kvp
.unwrap
();
values
.insert
(
kvp
.0
,
kvp
.1
);
}
let
mut
response
=
HashMap
::
new
();
for
key
in
keys
{
let
entry
=
values
.get_key_value
(
key
);
if
entry
==
None
{
return
Err
(
ParseFormError
{
error
:
ParseErrors
::
NoData
,
});
}
response
.insert
(
*
entry
.unwrap
()
.0
,
*
entry
.unwrap
()
.1
);
}
Ok
(
response
)
}
pub
fn
get_form_keys_with_data
(
&
self
,
_keys
:
Vec
<&
str
>
,
_data_buffer
:
Data
,
)
->
Result
<
HashMap
<&
str
,
&
str
>
,
ParseFormError
>
{
todo!
()
}
}
}
This diff is collapsed.
Click to expand it.
core/http/src/handling/routes.rs
+
3
−
1
View file @
844c4389
...
@@ -43,7 +43,9 @@ impl Route<'_> {
...
@@ -43,7 +43,9 @@ impl Route<'_> {
}
else
{
}
else
{
return
false
;
return
false
;
};
};
if
true_str
.starts_with
(
"<"
)
&&
true_str
.ends_with
(
"..>"
)
{
if
(
true_str
.starts_with
(
"<"
)
&&
true_str
.ends_with
(
"..>"
))
||
(
comp_str
.starts_with
(
true_str
)
&&
comp_str
.contains
(
"?"
))
{
return
true
;
return
true
;
}
}
if
true_str
.starts_with
(
"<"
)
&&
true_str
.ends_with
(
">"
)
{
if
true_str
.starts_with
(
"<"
)
&&
true_str
.ends_with
(
">"
)
{
...
...
This diff is collapsed.
Click to expand it.
site/src/main.rs
+
21
−
7
View file @
844c4389
use
std
::
path
::
PathBuf
;
use
std
::
{
path
::
PathBuf
,
collections
::
HashMap
}
;
use
http
::
handling
::{
use
http
::
handling
::{
file_handlers
::
NamedFile
,
file_handlers
::
NamedFile
,
...
@@ -8,12 +8,26 @@ use http::handling::{
...
@@ -8,12 +8,26 @@ use http::handling::{
routes
::{
Data
,
Route
},
routes
::{
Data
,
Route
},
};
};
fn
handle_static_hi
(
request
:
Request
<
'_
>
,
data
:
Data
)
->
Outcome
<
Response
,
Status
,
Data
>
{
fn
hashmap_to_string
(
map
:
&
HashMap
<&
str
,
&
str
>
)
->
String
{
// Outcome::Success(Response { headers: vec![
let
mut
result
=
String
::
new
();
// format!("Content-Length: 4"),
for
(
key
,
value
)
in
map
{
// format!("Content-Type: text/plain")
result
.push_str
(
key
);
// ], status: Some(Status::Ok), body: Box::new("jkl;") })
result
.push
(
'='
);
Outcome
::
Forward
(
data
)
result
.push_str
(
value
);
result
.push
(
';'
);
}
result
.pop
();
// Remove the trailing semicolon if desired
result
}
fn
handle_static_hi
(
request
:
Request
<
'_
>
,
_data
:
Data
)
->
Outcome
<
Response
,
Status
,
Data
>
{
let
response
=
hashmap_to_string
(
&
request
.get_form_keys
(
vec!
[
"asdf"
,
"jkj"
])
.unwrap
());
Outcome
::
Success
(
Response
{
headers
:
vec!
[
format!
(
"Content-Length: {}"
,
response
.len
()),
format!
(
"Content-Type: text/plain"
)
],
status
:
Some
(
Status
::
Ok
),
body
:
Box
::
new
(
response
)
})
// Outcome::Forward(data)
}
}
...
...
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