1use
2{
3 super::
4 {
5 request::
6 {
7 Request,
8 },
9 },
10 async_std::
11 {
12 net::
13 {
14 TcpStream,
15 },
16 },
17 std::
18 {
19 fmt::
20 {
21 self,
22 Display,
23 },
24 },
25};
26
27#[allow(dead_code)]
29pub enum Method
30{
31 Dummy,
33 Connect,
35 Delete,
37 Get,
39 Head,
41 Options,
43 Patch,
45 Post,
47 Put,
49 Trace,
51}
52
53impl Method
54{
55 pub async fn parse
60 (
61 mut stream: &mut TcpStream,
62 )
63 -> Option < Method >
64 {
65 match Request::readChar ( &mut stream )
66 {
67 Some ( 'C' )
68 => Some ( Method::Connect )
69 .and_then ( | method | Request::ifChar ( 'O', &mut stream, method ) )
70 .and_then ( | method | Request::ifChar ( 'N', &mut stream, method ) )
71 .and_then ( | method | Request::ifChar ( 'N', &mut stream, method ) )
72 .and_then ( | method | Request::ifChar ( 'E', &mut stream, method ) )
73 .and_then ( | method | Request::ifChar ( 'C', &mut stream, method ) )
74 .and_then ( | method | Request::ifChar ( 'T', &mut stream, method ) ),
75 Some ( 'D' )
76 => Some ( Method::Delete )
77 .and_then ( | method | Request::ifChar ( 'E', &mut stream, method ) )
78 .and_then ( | method | Request::ifChar ( 'L', &mut stream, method ) )
79 .and_then ( | method | Request::ifChar ( 'E', &mut stream, method ) )
80 .and_then ( | method | Request::ifChar ( 'T', &mut stream, method ) )
81 .and_then ( | method | Request::ifChar ( 'E', &mut stream, method ) ),
82 Some ( 'G' )
83 => Some ( Method::Get )
84 .and_then ( | method | Request::ifChar ( 'E', &mut stream, method ) )
85 .and_then ( | method | Request::ifChar ( 'T', &mut stream, method ) ),
86 Some ( 'H' )
87 => Some ( Method::Post )
88 .and_then ( | method | Request::ifChar ( 'E', &mut stream, method ) )
89 .and_then ( | method | Request::ifChar ( 'A', &mut stream, method ) )
90 .and_then ( | method | Request::ifChar ( 'D', &mut stream, method ) ),
91 Some ( 'O' )
92 => Some ( Method::Options )
93 .and_then ( | method | Request::ifChar ( 'P', &mut stream, method ) )
94 .and_then ( | method | Request::ifChar ( 'T', &mut stream, method ) )
95 .and_then ( | method | Request::ifChar ( 'I', &mut stream, method ) )
96 .and_then ( | method | Request::ifChar ( 'O', &mut stream, method ) )
97 .and_then ( | method | Request::ifChar ( 'N', &mut stream, method ) )
98 .and_then ( | method | Request::ifChar ( 'S', &mut stream, method ) ),
99 Some ( 'P' )
100 => match Request::readChar ( &mut stream )
101 {
102 Some ( 'A' )
103 => Some ( Method::Patch )
104 .and_then ( | method | Request::ifChar ( 'T', &mut stream, method ) )
105 .and_then ( | method | Request::ifChar ( 'C', &mut stream, method ) )
106 .and_then ( | method | Request::ifChar ( 'H', &mut stream, method ) ),
107 Some ( 'O' )
108 => Some ( Method::Post )
109 .and_then ( | method | Request::ifChar ( 'S', &mut stream, method ) )
110 .and_then ( | method | Request::ifChar ( 'T', &mut stream, method ) ),
111 Some ( 'U' )
112 => Some ( Method::Put )
113 .and_then ( | method | Request::ifChar ( 'T', &mut stream, method ) ),
114 _
115 => None,
116 },
117 Some ( 'T' )
118 => Some ( Method::Trace )
119 .and_then ( | method | Request::ifChar ( 'R', &mut stream, method ) )
120 .and_then ( | method | Request::ifChar ( 'A', &mut stream, method ) )
121 .and_then ( | method | Request::ifChar ( 'C', &mut stream, method ) )
122 .and_then ( | method | Request::ifChar ( 'E', &mut stream, method ) ),
123 Some ( char )
124 => {
125 println!("Char: {}", char);
126 None
127 },
128 _
129 => None,
130 }
131 .and_then ( | method | Request::ifChar ( ' ', &mut stream, method ) )
132 }
133}
134
135impl Display for Method
136{
137 fn fmt
138 (
139 &self,
140 formatter: &mut fmt::Formatter<'_>
141 )
142 -> fmt::Result
143 {
144 formatter
145 .write_str
146 (
147 match self
148 {
149 Method::Connect => "CONNECT",
150 Method::Delete => "DELETE",
151 Method::Dummy => "????",
152 Method::Get => "GET",
153 Method::Head => "HEAD",
154 Method::Options => "OPTIONS",
155 Method::Patch => "PATCH",
156 Method::Post => "POST",
157 Method::Put => "PUT",
158 Method::Trace => "TRACE",
159 }
160 )
161 }
162}