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
use regex::Regex;
use std::{
    io::{Result, Write},
    net::{TcpListener, TcpStream},
    str,
};

use crate::{
    http::{request::Request, Http, CRLF},
    log::{Log, LogLevel},
};
pub mod constants;

/// Set spaces before capitalize letters. For change [`Http::Status`] enum items.
pub fn space_bef_cap(src: String) -> String {
    let chars = src.chars().into_iter();
    let mut res = "".to_string();
    for v in chars {
        let mut buf = [0; 2];
        v.encode_utf8(&mut buf);
        let ch = Regex::new(r"[A-Z]{1}")
            .unwrap()
            .captures(&str::from_utf8(&buf).unwrap());
        if let Some(_) = ch {
            if src != "OK" {
                res.push(' ');
            } else if v == 'O' {
                res.push(' ')
            }
        }
        res.push(v);
    }
    res
}

pub fn target(addr: &str) -> Result<()> {
    let listener = TcpListener::bind(addr)?;
    println!("listening target on {}", addr);
    for stream in listener.incoming() {
        handle_target(stream?)?;
    }
    Ok(())
}

pub fn handle_target(client: TcpStream) -> Result<()> {
    const TAG: &str = "Handle target";
    let _log = Log::new(&super::LOG_LEVEL);
    let mut client = Http::from(client);
    _log.println(LogLevel::Info, TAG, "start client", &client);

    let req_heads = client.read_headers()?;
    let req = Request::new(req_heads);
    _log.println(LogLevel::Info, TAG, "headers", &req.headers);

    let res_heads = format!(
        "Content-Type: plain/text{CRLF}Transfer-Encoding: chunked{CRLF}Server: echo-rs{CRLF}"
    );

    client.set_status(200)?;
    client.write(res_heads.as_bytes())?;
    client.set_end_line()?;

    if req.content_length != 0 {
        let body = client.read_body(&req)?;
        _log.println(LogLevel::Info, TAG, "body", str::from_utf8(&body).unwrap());
        for i in body {
            let chunk = format!("1{CRLF}{}{CRLF}", str::from_utf8(&[i]).unwrap());
            client.write(chunk.as_bytes())?;
        }
    }

    client.set_zero_byte()?;
    client.flush()?;
    _log.println(LogLevel::Info, TAG, "end client", client);
    Ok(())
}