1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
use std::fmt;
use std::collections::HashMap;
use std::collections::hash_map::RandomState;
use std::io::{Error, ErrorKind};
use std::str::FromStr;
use url::{Url, Position};
use async_std::io::{Read, Write};
use async_uninet::{SocketAddr, Stream};
use async_httplib::{read_first_line, parse_version, parse_status, read_header_line,
    write_slice, write_all, write_exact, write_chunks, flush_write};
use crate::{Method, Version, Response, read_content_length};

#[derive(Debug)]
pub struct Request {
    url: Url,
    method: Method,
    version: Version,
    headers: HashMap<String, String>,
    relay: Option<String>,
    body_limit: Option<usize>,
}

impl Request {

    pub fn default() -> Self {
        Self {
            url: Url::parse("http://localhost").unwrap(),
            method: Method::Get,
            version: Version::Http1_1,
            headers: HashMap::with_hasher(RandomState::new()),
            relay: None,
            body_limit: None,
        }
    }

    pub fn parse_url<U>(url: U) -> Result<Self, Error>
        where
        U: Into<String>,
    {
        let mut req = Request::default();
        req.set_url_str(url.into())?;
        Ok(req)
    }

    pub fn url(&self) -> &Url {
        &self.url
    }

    fn scheme(&self) -> &str {
        self.url.scheme()
    }

    fn host(&self) -> &str {
        match self.url.host_str() {
            Some(host) => host,
            None => "localhost",
        }
    }

    fn port(&self) -> u16 {
        match self.url.port_or_known_default() {
            Some(port) => port,
            None => 80,
        }
    }

    fn host_with_port(&self) -> String {
        format!("{}:{}", self.host(), self.port())
    }

    fn socket_address(&self) -> String {
        match &self.relay {
            Some(relay) => relay.to_string(),
            None => self.host_with_port(),
        }
    }

    fn uri(&self) -> &str {
        &self.url[Position::BeforePath..]
    }

    pub fn method(&self) -> &Method {
        &self.method
    }

    pub fn version(&self) -> &Version {
        &self.version
    }

    pub fn headers(&self) -> &HashMap<String, String> {
        &self.headers
    }

    pub fn header<N: Into<String>>(&self, name: N) -> Option<&String> {
        self.headers.get(&name.into())
    }

    pub fn relay(&self) -> &Option<String> {
        &self.relay
    }

    pub fn body_limit(&self) -> &Option<usize> {
        &self.body_limit
    }

    pub fn headers_mut(&mut self) -> &mut HashMap<String, String> {
        &mut self.headers
    }

    pub fn has_method(&self, value: Method) -> bool {
        self.method == value
    }

    pub fn has_version(&self, value: Version) -> bool {
        self.version == value
    }

    pub fn has_header<N: Into<String>>(&self, name: N) -> bool {
        self.headers.contains_key(&name.into())
    }

    pub fn has_body_limit(&self) -> bool {
        self.body_limit.is_some()
    }

    pub fn set_url(&mut self, value: Url) {
        self.url = value;
    }

    pub fn set_url_str<V: Into<String>>(&mut self, value: V) -> Result<(), Error> {
        self.url = match Url::parse(&value.into()) {
            Ok(url) => url,
            Err(e) => return Err(Error::new(ErrorKind::InvalidInput, e.to_string())),
        };
        Ok(())
    }

    pub fn set_method(&mut self, value: Method) {
        self.method = value;
    }

    pub fn set_method_str(&mut self, value: &str) -> Result<(), Error> {
        self.method = Method::from_str(value)?;
        Ok(())
    }

    pub fn set_version(&mut self, value: Version) {
        self.version = value;
    }

    pub fn set_version_str(&mut self, value: &str) -> Result<(), Error> {
        self.version = Version::from_str(value)?;
        Ok(())
    }

    pub fn set_header<N: Into<String>, V: Into<String>>(&mut self, name: N, value: V) {
        self.headers.insert(name.into(), value.into());
    }

    pub fn set_relay<V: Into<String>>(&mut self, value: V) {
        self.relay = Some(value.into());
    }

    pub fn set_body_limit(&mut self, length: usize) {
        self.body_limit = Some(length);
    }

    pub fn remove_header<N: Into<String>>(&mut self, name: N) {
        self.headers.remove(&name.into());
    }

    pub fn remove_relay(&mut self) {
        self.relay = None;
    }

    pub fn clear_headers(&mut self) {
        self.headers.clear();
    }

    pub fn to_proto_string(&self) -> String {
        let mut output = String::new();

        match self.version {
            Version::Http0_9 => {
                output.push_str(&format!("GET {}\r\n", self.uri()));
            },
            _ => {
                output.push_str(&format!("{} {} {}\r\n", self.method(), self.uri(), self.version()));
                for (name, value) in self.headers.iter() {
                    output.push_str(&format!("{}: {}\r\n", name, value));
                }
                output.push_str("\r\n");
            },
        };

        output
    }

    pub async fn send<'a>(&mut self) -> Result<Response<'a>, Error> {
        self.update_host_header();

        match self.scheme() {
            "http" => self.send_http(&mut "".as_bytes()).await,
            "https" => self.send_https(&mut "".as_bytes()).await,
            s => Err(Error::new(ErrorKind::InvalidInput, format!("The URL scheme `{}` is invalid.", s))),
        }
    }

    pub async fn send_stream<'a, R>(&mut self, body: &mut R) -> Result<Response<'a>, Error>
        where
        R: Read + Unpin,
    {
        self.update_host_header();
        self.update_body_headers();
        
        match self.scheme() {
            "http" => self.send_http(body).await,
            "https" => self.send_https(body).await,
            s => Err(Error::new(ErrorKind::InvalidInput, format!("The URL scheme `{}` is invalid.", s))),
        }
    }

    pub async fn send_slice<'a>(&mut self, body: &[u8]) -> Result<Response<'a>, Error> {
        self.set_header("Content-Length", body.len().to_string());
        self.send_stream(&mut body.clone()).await
    }

    pub async fn send_str<'a>(&mut self, body: &str) -> Result<Response<'a>, Error> {
        self.set_header("Content-Length", body.len().to_string());
        self.send_stream(&mut body.as_bytes()).await
    }

    #[cfg(feature = "json")]
    pub async fn send_json<'a>(&mut self, body: &serde_json::Value) -> Result<Response<'a>, Error> {
        let body = body.to_string();
        self.set_header("Content-Length", body.len().to_string());
        self.send_stream(&mut body.as_bytes()).await
    }

    pub async fn send_http<'a, R>(&mut self, body: &mut R) -> Result<Response<'a>, Error>
        where
        R: Read + Unpin
    {
        let mut stream = self.build_conn().await?;
        self.write_request(&mut stream, body).await?;
        self.build_response(stream).await
    }

    pub async fn send_https<'a, R>(&mut self, body: &mut R) -> Result<Response<'a>, Error>
        where
        R: Read + Unpin
    {
        let stream = self.build_conn().await?;

        let mut stream = match async_native_tls::connect(self.host(), stream).await {
            Ok(stream) => stream,
            Err(e) => return Err(Error::new(ErrorKind::Interrupted, e.to_string())),
        };

        self.write_request(&mut stream, body).await?;
        self.build_response(stream).await
    }

    fn update_host_header(&mut self) {
        if self.version >= Version::Http1_1 && !self.has_header("Host") {
            self.set_header("Host", self.host_with_port());
        }
    }

    fn update_body_headers(&mut self) {
        if self.version >= Version::Http0_9 && self.method.has_body() && !self.has_header("Content-Length") {
            self.set_header("Transfer-Encoding", "chunked");
        }
    }

    async fn write_request<S, R>(&self, stream: &mut S, body: &mut R) -> Result<(), Error>
        where
        S: Write + Unpin,
        R: Read + Unpin,
    {
        self.write_proto(stream).await?;
        self.write_body(stream, body).await
    }

    async fn write_proto<S>(&self, stream: &mut S) -> Result<(), Error>
        where
        S: Write + Unpin,
    {
        write_slice(stream, self.to_string().as_bytes()).await?;
        flush_write(stream).await
    }

    async fn write_body<S, R>(&self, stream: &mut S, body: &mut R) -> Result<(), Error>
        where
        S: Write + Unpin,
        R: Read + Unpin,
    {
        if self.has_version(Version::Http0_9) {
            write_all(stream, body, self.body_limit).await?;
        } else if self.has_header("Content-Length") { // exact
            write_exact(stream, body, read_content_length(&self.headers, self.body_limit)?).await?;
        } else { // chunked
            write_chunks(stream, body, (Some(1024), self.body_limit)).await?;
        }
        flush_write(stream).await
    }

    async fn build_conn(&mut self) -> Result<Stream, Error> {
        let addr = self.socket_address();

        match SocketAddr::from_str(&addr).await {
            Ok(addr) => Stream::connect(&addr).await,
            Err(_) => Err(Error::new(ErrorKind::AddrNotAvailable, format!("The address `{}` is invalid.", addr))),
        }
    }

    async fn build_response<'a, S>(&mut self, mut stream: S) -> Result<Response<'a>, Error>
        where
        S: Read + Unpin + 'a,
    {
        let mut res: Response<'a> = Response::default();

        let (mut version, mut status, mut message) = (vec![], vec![], vec![]);
        read_first_line(&mut stream, (&mut version, &mut status, &mut message), None).await?;
        res.set_version(parse_version(version)?);
        res.set_status(parse_status(status)?);
    
        loop {
            let (mut name, mut value) = (vec![], vec![]);
            read_header_line(&mut stream, (&mut name, &mut value), None).await?;
            
            if name.is_empty() {
                break;
            }

            res.set_header(
                match String::from_utf8(name) {
                    Ok(name) => name,
                    Err(_) => return Err(Error::new(ErrorKind::InvalidData, format!("The response header `#{}` is invalid.", res.headers().len()))),
                },
                match String::from_utf8(value) {
                    Ok(value) => value,
                    Err(_) => return Err(Error::new(ErrorKind::InvalidData, format!("The response header `#{}` is invalid.", res.headers().len()))),
                },
            );
        }

        res.set_reader(stream);
        Ok(res)
    }
}

impl fmt::Display for Request {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        write!(fmt, "{}", self.to_proto_string())
    }
}

impl From<Request> for String {
    fn from(item: Request) -> String {
        item.to_string()
    }
}