http_async/
method.rs

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/// Method of this Hyper Text Transfer Protocol Request.
28#[allow(dead_code)]
29pub enum      Method
30{
31  /// Placeholder.
32  Dummy,
33  /// Establish a Tunnel to the Server identified by the Target Resource.
34  Connect,
35  /// Delete the Specified Resource.
36  Delete,
37  /// Request a Representation of the Specified Resource. Requests using this Method shall only retrieve data.
38  Get,
39  /// Asks for a Response identical to that of a GET Request, but without the Response Content.
40  Head,
41  /// Describe the Communication Options for the Target Resource.
42  Options,
43  /// Apply Partial Modifications to a Resource.
44  Patch,
45  /// Submit an Entity to the Specified Resource, often causing a Change in State or Side Effects on the Server.
46  Post,
47  /// Replace all Current Representations of the Target Resource with the Request Payload.
48  Put,
49  /// Perform a Message Loop-Back Test along the Path to the Target Resource.
50  Trace,
51}
52
53impl          Method
54{
55  /// Try to parse Hyper Text Transfer Protocol Request Method from Transmission Control Protocol Stream.
56  ///
57  /// # Arguments
58  /// * `stream`                        – Transmission Control Protocol Stream.
59  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}