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
pub mod headers;
pub mod request;
pub mod status;
use self::request::Request;
use status::Status;

use super::log::{Log, LogLevel};
use super::prelude::constants::*;
///! Module [`Http`].
///! The minimum set of methods to work through [`TcpStream`].
use regex::Regex;
use std::{
    io::{Error, ErrorKind, Read, Result, Write},
    net::TcpStream,
    str,
};

/// End of line constant ([`\r\n`])
pub const CRLF: &str = "\r\n";
/// Version of HTTP protocol ([`HTTP/1.1`])
pub const VERSION: &str = "HTTP/1.1";

const TAG: &str = "Http";

#[derive(Debug)]
pub struct Http {
    pub socket: TcpStream,
}

impl Http {
    /// Create [`Http`] with new TCP connection
    pub fn connect(address: &str) -> Result<Http> {
        let socket = TcpStream::connect(address)?;
        Ok(Http::from(socket))
    }

    /// Create [`Http`] from exists socket
    pub fn from(socket: TcpStream) -> Http {
        Http { socket }
    }

    #[deprecated]
    /// Write HTTP status to response
    pub fn set_status(&mut self, code: u16) -> Result<usize> {
        let status = Status::new(code);
        println!("{} {} {}{}", VERSION, status.code, status.name, CRLF);
        self.write(format!("{} {} {}{}", VERSION, status.code, status.name, CRLF).as_bytes())
    }

    #[deprecated]
    /// Write content length header
    pub fn set_content_length<T>(&mut self, len: T) -> Result<usize>
    where
        T: Sized + std::fmt::Debug,
    {
        self.write(format!("Content-Length: {:?}{CRLF}", len).as_bytes())
    }

    #[deprecated]
    /// Write end line to socket
    pub fn set_end_line(&mut self) -> Result<usize> {
        self.write(format!("{CRLF}").as_bytes())
    }

    /// Write end of request
    pub fn set_zero_byte(&mut self) -> Result<usize> {
        self.write(format!("0{CRLF}{CRLF}").as_bytes())
    }

    /// Read request body
    pub fn read_body(&mut self, req: &Request) -> Result<Vec<u8>> {
        let mut buf: Vec<u8> = vec![];
        if req.method.to_uppercase() == "GET" {
            return Ok(vec![]);
        }
        loop {
            let mut chunk = [0; super::CHUNK_SIZE];
            self.read(&mut chunk)?;
            let mut exit = false;
            'b: for ch in chunk {
                if ch == 0 {
                    exit = true;
                    break 'b;
                }
                buf.push(ch);
            }
            if exit == true {
                break;
            }
        }
        Ok(buf)
    }

    /// Body to string
    pub fn body_to_string(&mut self, body: Vec<u8>) -> Result<String> {
        let res = str::from_utf8(&body);
        if let Err(err) = res {
            println!("{}", err);
            return Err(Error::new(
                ErrorKind::InvalidData,
                "Failed to parse body to string",
            ));
        }
        let result = res.unwrap();
        let rec = Regex::new(r"\d*\r\n(0$)?").unwrap().replace_all(result, "");
        Ok(rec.to_string())
    }

    /// Client - Target tunnel core
    pub fn tunnel(&mut self, http: &mut Self, _log: &Log) -> Result<usize> {
        let mut size: usize = 0;
        loop {
            let mut b = [0; CHUNK_SIZE];
            let r_res = http.read(&mut b);
            if let Err(e) = r_res {
                let log_l = match e.kind() {
                    ErrorKind::ConnectionReset => LogLevel::Info,
                    _ => LogLevel::Error,
                };
                _log.println(log_l, TAG, "Failed read chunk", e);
            }
            let mut buf = vec![];
            b.map(|_b| {
                if _b != 0 {
                    buf.push(_b);
                    return true;
                }
                false
            });

            size += buf.len();

            if buf.len() == 0 {
                break;
            }

            self.write(&buf)?;
        }

        Ok(size)
    }

    /// Read request headers by one byte for fist empty line
    pub fn read_headers(&mut self) -> Result<Vec<u8>> {
        let mut buf: Vec<u8> = vec![];
        loop {
            let mut b = [0; 1];
            let len = self.read(&mut b)?;
            if len == 0 {
                break;
            }
            let b = b[0];
            let len = buf.len();
            if len > 2
                && b == 10
                && (buf[len - 1] == 10 || (buf[len - 1] == 13 && buf[len - 2] == 10))
            {
                buf.push(b);
                break;
            }
            buf.push(b);
        }
        Ok(buf)
    }
}

impl Read for Http {
    /// Read chunk bytes from request
    fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
        self.socket.read(buf)
    }
}

impl Write for Http {
    fn write(&mut self, data: &[u8]) -> Result<usize> {
        self.socket.write(data)
    }

    fn flush(&mut self) -> Result<()> {
        self.socket.flush()
    }
}