Browse Source

initial http server

ref-scope
AJ ONeal 6 years ago
commit
f6817bcd33
  1. 2
      .gitignore
  2. 4
      Cargo.lock
  3. 6
      Cargo.toml
  4. 11
      public/index.html
  5. 27
      src/main.rs

2
.gitignore

@ -0,0 +1,2 @@
/target
**/*.rs.bk

4
Cargo.lock

@ -0,0 +1,4 @@
[[package]]
name = "httpserve"
version = "0.1.0"

6
Cargo.toml

@ -0,0 +1,6 @@
[package]
name = "httpserve"
version = "0.1.0"
authors = ["AJ ONeal <coolaj86@gmail.com>"]
[dependencies]

11
public/index.html

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello!</title>
</head>
<body>
<h1>Hello!</h1>
<p>Hi from Rust</p>
</body>
</html>

27
src/main.rs

@ -0,0 +1,27 @@
use std::io::prelude::*;
use std::net::TcpStream;
use std::net::TcpListener;
use std::fs;
fn main() {
let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
for stream in listener.incoming() {
let stream = stream.unwrap();
handle_connection(stream);
}
}
fn handle_connection(mut stream: TcpStream) {
let mut buffer = [0; 512];
stream.read(&mut buffer).unwrap();
println!("Request: {}", String::from_utf8_lossy(&buffer[..]));
let response = "HTTP/1.1 200 OK\r\n\r\n";
stream.write(response.as_bytes()).unwrap();
stream.flush().unwrap();
}
Loading…
Cancel
Save