Browse Source

Request checking

master
Noelle 4 years ago
parent
commit
52bfc591e2
1 changed files with 14 additions and 6 deletions
  1. 14
    6
      src/main.rs

+ 14
- 6
src/main.rs View File

use std::net::{TcpStream, TcpListener}; use std::net::{TcpStream, TcpListener};
use std::io::prelude::*; use std::io::prelude::*;
use std::fs;


fn main() { fn main() {
let listener = TcpListener::bind("127.0.0.1:26382").unwrap(); let listener = TcpListener::bind("127.0.0.1:26382").unwrap();


fn handle_connecton(mut stream: TcpStream) { fn handle_connecton(mut stream: TcpStream) {
let mut buffer = [0; 512]; let mut buffer = [0; 512];

stream.read(&mut buffer).unwrap(); stream.read(&mut buffer).unwrap();


let contents = fs::read_to_string("hello.html").unwrap();
let response = format!("HTTP/1.1 200 OK\r\n\r\n{}", contents);
stream.write(response.as_bytes()).unwrap();
let get = "GET / HTTP/1.1\r\n";

if buffer.starts_with(get) {
// It's the request we want
let contents = fs::read_to_string("hello.html").unwrap();
let response = format!("HTTP/1.1 200 OK\r\n\r\n{}", contents);
stream.write(response.as_bytes()).unwrap();
stream.flush().unwrap();
} else {
// a different request so who cares
}



stream.flush().unwrap();


// println!("Request: {}", String::from_utf8_lossy(&buffer[..])); // println!("Request: {}", String::from_utf8_lossy(&buffer[..]));
} }

Loading…
Cancel
Save