@@ -5,18 +5,18 @@ use tiny_http::{Method, Response, Server};
5
5
6
6
struct HttpServerOptions {
7
7
port : u16 ,
8
- host : String ,
8
+ host : Box < str > ,
9
9
}
10
10
11
11
struct ResponseSkeleton {
12
12
code : u8 ,
13
- headers : HashMap < String , String > ,
14
- body : String ,
13
+ headers : HashMap < Box < str > , Box < str > > ,
14
+ body : Box < str > ,
15
15
}
16
16
17
17
#[ no_mangle]
18
18
pub fn abi_version ( ) -> String {
19
- String :: from ( "0.0.1 " )
19
+ String :: from ( "0.0.5 " )
20
20
}
21
21
22
22
#[ no_mangle]
@@ -41,31 +41,25 @@ pub fn serve_http(
41
41
// );
42
42
43
43
let mut m: HashMap < Edn , Edn > = HashMap :: new ( ) ;
44
- m. insert (
45
- Edn :: Keyword ( String :: from ( "method" ) ) ,
46
- Edn :: Keyword ( request. method ( ) . to_string ( ) ) ,
47
- ) ;
48
- m. insert (
49
- Edn :: Keyword ( String :: from ( "url" ) ) ,
50
- Edn :: Str ( request. url ( ) . to_string ( ) ) ,
51
- ) ;
44
+ m. insert ( Edn :: kwd ( "method" ) , Edn :: kwd ( & request. method ( ) . to_string ( ) ) ) ;
45
+ m. insert ( Edn :: kwd ( "url" ) , Edn :: str ( request. url ( ) . to_string ( ) ) ) ;
52
46
53
47
let mut headers: HashMap < Edn , Edn > = HashMap :: new ( ) ;
54
48
55
49
for pair in request. headers ( ) {
56
50
headers. insert (
57
- Edn :: Keyword ( pair. field . to_string ( ) ) ,
58
- Edn :: Str ( pair. value . to_string ( ) ) ,
51
+ Edn :: kwd ( & pair. field . to_string ( ) ) ,
52
+ Edn :: str ( pair. value . to_string ( ) ) ,
59
53
) ;
60
54
}
61
- m. insert ( Edn :: Keyword ( String :: from ( "headers" ) ) , Edn :: Map ( headers) ) ;
55
+ m. insert ( Edn :: kwd ( "headers" ) , Edn :: Map ( headers) ) ;
62
56
63
57
if request. method ( ) != & Method :: Get {
64
58
let mut content = String :: new ( ) ;
65
59
request. as_reader ( ) . read_to_string ( & mut content) . unwrap ( ) ;
66
60
m. insert (
67
- Edn :: Keyword ( String :: from ( "body" ) ) ,
68
- Edn :: Str ( content. to_string ( ) ) ,
61
+ Edn :: kwd ( "body" ) ,
62
+ Edn :: Str ( content. to_string ( ) . into_boxed_str ( ) ) ,
69
63
) ;
70
64
}
71
65
@@ -92,21 +86,21 @@ fn parse_options(d: &Edn) -> Result<HttpServerOptions, String> {
92
86
match d {
93
87
Edn :: Nil => Ok ( HttpServerOptions {
94
88
port : 4000 ,
95
- host : String :: from ( "0.0.0.0" ) ,
89
+ host : String :: from ( "0.0.0.0" ) . into_boxed_str ( ) ,
96
90
} ) ,
97
91
Edn :: Map ( m) => {
98
92
let mut options = HttpServerOptions {
99
93
port : 4000 ,
100
- host : String :: from ( "0.0.0.0" ) ,
94
+ host : String :: from ( "0.0.0.0" ) . into_boxed_str ( ) ,
101
95
} ;
102
- options. port = match m. get ( & Edn :: Keyword ( String :: from ( "port" ) ) ) {
96
+ options. port = match m. get ( & Edn :: kwd ( "port" ) ) {
103
97
Some ( Edn :: Number ( port) ) => * port as u16 ,
104
98
None => 4000 ,
105
99
a => return Err ( format ! ( "invalid config for port: {:?}" , a) ) ,
106
100
} ;
107
- options. host = match m. get ( & Edn :: Keyword ( String :: from ( "host" ) ) ) {
101
+ options. host = match m. get ( & Edn :: kwd ( "host" ) ) {
108
102
Some ( Edn :: Str ( host) ) => host. to_owned ( ) ,
109
- None => String :: from ( "0.0.0.0" ) ,
103
+ None => String :: from ( "0.0.0.0" ) . into_boxed_str ( ) ,
110
104
a => return Err ( format ! ( "invalid config for host: {:?}" , a) ) ,
111
105
} ;
112
106
Ok ( options)
@@ -121,27 +115,27 @@ fn parse_response(info: &Edn) -> Result<ResponseSkeleton, String> {
121
115
let mut res = ResponseSkeleton {
122
116
code : 200 ,
123
117
headers : HashMap :: new ( ) ,
124
- body : String :: from ( "" ) ,
118
+ body : String :: from ( "" ) . into_boxed_str ( ) ,
125
119
} ;
126
- res. code = match m. get ( & Edn :: Keyword ( String :: from ( "code" ) ) ) {
120
+ res. code = match m. get ( & Edn :: kwd ( "code" ) ) {
127
121
Some ( Edn :: Number ( n) ) => * n as u8 ,
128
122
None => 200 ,
129
123
a => return Err ( format ! ( "invalid code: {:?}" , a) ) ,
130
124
} ;
131
- res. body = match m. get ( & Edn :: Keyword ( String :: from ( "body" ) ) ) {
125
+ res. body = match m. get ( & Edn :: kwd ( "body" ) ) {
132
126
Some ( Edn :: Str ( s) ) => s. to_owned ( ) ,
133
- Some ( a) => a. to_string ( ) ,
134
- None => String :: from ( "" ) ,
127
+ Some ( a) => a. to_string ( ) . into_boxed_str ( ) ,
128
+ None => String :: from ( "" ) . into_boxed_str ( ) ,
135
129
} ;
136
- res. headers = match m. get ( & Edn :: Keyword ( String :: from ( "headers" ) ) ) {
130
+ res. headers = match m. get ( & Edn :: kwd ( "headers" ) ) {
137
131
Some ( Edn :: Map ( m) ) => {
138
- let mut hs: HashMap < String , String > = HashMap :: new ( ) ;
132
+ let mut hs: HashMap < Box < str > , Box < str > > = HashMap :: new ( ) ;
139
133
for ( k, v) in m {
140
134
if let Edn :: Keyword ( s) = k {
141
135
if let Edn :: Str ( s2) = v {
142
- hs. insert ( s. to_owned ( ) , s2. to_owned ( ) ) ;
136
+ hs. insert ( s. to_str ( ) , s2. to_owned ( ) ) ;
143
137
} else {
144
- hs. insert ( s. to_owned ( ) , v. to_string ( ) ) ;
138
+ hs. insert ( s. to_str ( ) , v. to_string ( ) . into_boxed_str ( ) ) ;
145
139
}
146
140
} else {
147
141
return Err ( format ! ( "invalid head entry: {}" , k) ) ;
0 commit comments