Skip to content

Commit 7910959

Browse files
committed
Splitted some functions
1 parent 34b84cd commit 7910959

File tree

4 files changed

+49
-46
lines changed

4 files changed

+49
-46
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target/

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ name = "tcp_responder"
33
version = "0.1.0"
44
edition = "2024"
55

6-
[dependencies]
6+
[dependencies]

src/main.rs

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,7 @@ use std::error::Error;
22
use std::io::{self, BufRead, Read, Write};
33
use std::net::{TcpListener, TcpStream};
44

5-
#[inline]
6-
fn print_char(c: &u8) {
7-
print!(
8-
"{}",
9-
if c.is_ascii_graphic() && *c != 0x7f {
10-
*c as char
11-
} else {
12-
'.'
13-
}
14-
);
15-
}
16-
17-
fn hex_print(hex: &[u8], validlen: usize) {
18-
if hex.len() > 65535 {
19-
eprintln!("Array with more than 65535 elements isn't supported");
20-
std::process::exit(1);
21-
}
22-
let mut bslice = [0_u8; 8];
23-
for (index, cc) in hex[..validlen].iter().enumerate() {
24-
bslice[index % 8] = *cc;
25-
print!("{:02x} ", cc);
26-
if index % 16 == 0 || index + 1 == validlen {
27-
print!(" | ");
28-
for pc in bslice.iter() {
29-
print_char(pc);
30-
}
31-
println!();
32-
} else if index % 8 == 0 {
33-
print!("- ");
34-
}
35-
}
36-
}
5+
mod tools;
376

387
fn respond(stream: &mut TcpStream, print_tcp: bool) -> Result<(), Box<dyn Error>> {
398
let mut buf = [0_u8; 65535];
@@ -45,7 +14,7 @@ fn respond(stream: &mut TcpStream, print_tcp: bool) -> Result<(), Box<dyn Error>
4514
println!("\n=====\n\nSocket Input:\n");
4615
// Print hex if requested
4716
if print_tcp {
48-
hex_print(&buf, n);
17+
tools::hex_print(&buf, n, true);
4918
println!("\n=====\n");
5019
}
5120

@@ -54,14 +23,14 @@ fn respond(stream: &mut TcpStream, print_tcp: bool) -> Result<(), Box<dyn Error>
5423
if *cc == 0x20_u8 {
5524
print!(" ");
5625
} else {
57-
print_char(cc);
26+
tools::print_char(cc);
5827
}
5928
}
6029

6130
println!("\n\n=====\n");
62-
println!(
63-
"Please send the HTTP response back:\nType '?REVERT?' at new line to undo the last line.\nType '?END?' at new line to send.\n"
64-
);
31+
println!("Please send the data back:");
32+
println!("Type '?UNDO?' at a new line to undo the last line,");
33+
println!("Type '?SEND?' at a new line to send.\n");
6534

6635
// Response writer
6736
let stdin = io::stdin();
@@ -74,13 +43,17 @@ fn respond(stream: &mut TcpStream, print_tcp: bool) -> Result<(), Box<dyn Error>
7443
}
7544

7645
let trimmed = line.trim().to_string();
77-
if trimmed == "?END?" {
46+
if trimmed == "?SEND?" {
7847
break;
79-
} else if trimmed == "?REVERT?" {
80-
lines.pop();
81-
println!("\nLine reverted. Now:");
82-
for line in &lines {
83-
println!("{}", line);
48+
} else if trimmed == "?UNDO?" {
49+
if !lines.is_empty() {
50+
lines.pop().unwrap();
51+
println!("\nLine reverted. Now:");
52+
} else {
53+
println!("\nNothing to revert! Now:");
54+
}
55+
for pline in &lines {
56+
println!("{}", pline);
8457
}
8558
} else {
8659
lines.push(trimmed);
@@ -164,8 +137,7 @@ fn main() {
164137
// Ready
165138
println!("Listening at {}", addr);
166139
loop {
167-
let (mut stream, sockaddr) = listener.accept().unwrap();
168-
println!("[DEBUG] Socket address: {:?}", sockaddr);
140+
let (mut stream, _) = listener.accept().unwrap();
169141
std::thread::spawn(move || {
170142
respond(&mut stream, param.1).unwrap();
171143
});

src/tools.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#[inline]
2+
pub fn print_char(c: &u8) {
3+
print!(
4+
"{}",
5+
if c.is_ascii_graphic() && *c != 0x7f {
6+
*c as char
7+
} else {
8+
'.'
9+
}
10+
);
11+
}
12+
13+
pub fn hex_print(hex: &[u8], validlen: usize, printchar: bool) {
14+
let mut bslice = [0_u8; 8];
15+
for (index, cc) in hex[..validlen].iter().enumerate() {
16+
bslice[index % 8] = *cc;
17+
print!("{:02x} ", cc);
18+
if index % 16 == 0 || index + 1 == validlen {
19+
if printchar {
20+
print!(" | ");
21+
for pc in bslice.iter() {
22+
print_char(pc);
23+
}
24+
}
25+
println!();
26+
} else if index % 8 == 0 {
27+
print!("- ");
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)