Browse Source

Implement plaintext

Steve Klabnik 10 years ago
parent
commit
ec0407e05a

+ 4 - 0
frameworks/Rust/hyper/Cargo.toml

@@ -2,3 +2,7 @@
 name = "hello"
 version = "0.1.0"
 authors = ["Steve Klabnik <[email protected]>"]
+
+[dependencies]
+
+hyper="0.5.2"

+ 1 - 0
frameworks/Rust/hyper/benchmark_config.json

@@ -3,6 +3,7 @@
   "tests": [{
     "default": {
       "setup_file": "setup",
+      "plaintext_url": "/plaintext", 
       "port": 8080,
       "approach": "Realistic",
       "classification": "Micro",

+ 1 - 1
frameworks/Rust/hyper/setup.sh

@@ -2,7 +2,7 @@
 
 sed -i 's|tcp(.*:3306)|tcp('"${DBHOST}"':3306)|g' src/main.rs
 
-set CARGO = $IROOT/rust/bin/cargo
+CARGO = $IROOT/rust/bin/cargo
 
 $CARGO build --release
 

+ 26 - 1
frameworks/Rust/hyper/src/main.rs

@@ -1,3 +1,28 @@
+extern crate hyper;
+
+use hyper::server::{Server, Request, Response};
+use hyper::status::StatusCode;
+use hyper::uri::RequestUri;
+use hyper::header::ContentType;
+use hyper::header::ContentLength;
+use hyper::header;
+
+const HELLO_WORLD: &'static [u8; 14] = b"Hello, World!\n";
+
 fn main() {
-    println!("Hello, world");
+    Server::http(|req: Request, mut res: Response| {
+        match (req.method, req.uri) {
+            (hyper::Get, RequestUri::AbsolutePath(ref path)) if path == "/plaintext" => {
+                res.headers_mut().set(ContentLength(HELLO_WORLD.len() as u64));
+                res.headers_mut().set(ContentType::plaintext());
+                res.headers_mut().set(header::Server("Hyper".to_owned()));
+
+                res.send(HELLO_WORLD).unwrap();
+
+                StatusCode::Ok
+            }
+            (hyper::Get, _) => StatusCode::NotFound,
+            _ => StatusCode::MethodNotAllowed,
+        };
+    }).listen("0.0.0.0:8080").unwrap();
 }