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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
extern crate minreq;
extern crate tiny_http;
use self::tiny_http::{Header, Method, Response, Server, StatusCode};
use std::str::FromStr;
use std::sync::{Arc, Once};
use std::thread;
use std::time::Duration;
static INIT: Once = Once::new();
pub fn setup() {
INIT.call_once(|| {
let server = Arc::new(Server::http("localhost:35562").unwrap());
for _ in 0..4 {
let server = server.clone();
thread::spawn(move || loop {
let mut request = {
if let Ok(request) = server.recv() {
request
} else {
continue; // If .recv() fails, just try again.
}
};
let mut content = String::new();
request.as_reader().read_to_string(&mut content).ok();
let headers = Vec::from(request.headers());
let url = String::from(request.url().split('#').next().unwrap());
match request.method() {
Method::Get if url == "/header_pong" => {
for header in headers {
if header.field.as_str() == "Ping" {
let response = Response::from_string(format!("{}", header.value));
request.respond(response).ok();
return;
}
}
request.respond(Response::from_string("No header!")).ok();
}
Method::Get if url == "/slow_a" => {
thread::sleep(Duration::from_secs(2));
let response = Response::from_string(format!("j: {}", content));
request.respond(response).ok();
}
Method::Get if url == "/a" => {
let response = Response::from_string(format!("j: {}", content));
request.respond(response).ok();
}
Method::Post if url == "/a" => {
let response = Response::from_string("POST to /a is not valid.");
request.respond(response).ok();
}
Method::Get if url == "/long_header" => {
let mut long_header = String::with_capacity(1000);
long_header += "Very-Long-Header: ";
for _ in 0..1000 - long_header.len() {
long_header += ".";
}
let long_header = Header::from_str(&long_header).unwrap();
let response = Response::empty(200).with_header(long_header);
request.respond(response).ok();
}
Method::Get if url == "/massive_content_length" => {
let status = StatusCode(200);
let body = std::io::empty();
let length = 1_000_000_000_000_000;
let response = Response::new(status, vec![], body, Some(length), None)
.with_chunked_threshold(2 * length);
request.respond(response).ok();
}
Method::Get if url == "/long_status_line" => {
request.respond(Response::empty(203)).ok();
}
Method::Get if url == "/redirect-baz" => {
let response = Response::empty(301).with_header(
Header::from_str("Location: http://localhost:35562/a#baz").unwrap(),
);
request.respond(response).ok();
}
Method::Get if url == "/redirect" => {
let response = Response::empty(301).with_header(
Header::from_bytes(&b"Location"[..], &b"http://localhost:35562/a"[..])
.unwrap(),
);
request.respond(response).ok();
}
Method::Post if url == "/redirect" => {
let response = Response::empty(303).with_header(
Header::from_bytes(&b"Location"[..], &b"http://localhost:35562/a"[..])
.unwrap(),
);
request.respond(response).ok();
}
Method::Get if url == "/infiniteredirect" => {
let response = Response::empty(301).with_header(
Header::from_bytes(
&b"Location"[..],
&b"http://localhost:35562/redirectpong"[..],
)
.unwrap(),
);
request.respond(response).ok();
}
Method::Get if url == "/redirectpong" => {
let response = Response::empty(301).with_header(
Header::from_bytes(
&b"Location"[..],
&b"http://localhost:35562/infiniteredirect"[..],
)
.unwrap(),
);
request.respond(response).ok();
}
Method::Get if url == "/relativeredirect" => {
let response = Response::empty(303)
.with_header(Header::from_bytes(&b"Location"[..], &b"/a"[..]).unwrap());
request.respond(response).ok();
}
Method::Post if url == "/echo" => {
request.respond(Response::from_string(content)).ok();
}
Method::Head if url == "/b" => {
request.respond(Response::empty(418)).ok();
}
Method::Post if url == "/c" => {
let response = Response::from_string(format!("l: {}", content));
request.respond(response).ok();
}
Method::Put if url == "/d" => {
let response = Response::from_string(format!("m: {}", content));
request.respond(response).ok();
}
Method::Delete if url == "/e" => {
let response = Response::from_string(format!("n: {}", content));
request.respond(response).ok();
}
Method::Trace if url == "/f" => {
let response = Response::from_string(format!("o: {}", content));
request.respond(response).ok();
}
Method::Options if url == "/g" => {
let response = Response::from_string(format!("p: {}", content));
request.respond(response).ok();
}
Method::Connect if url == "/h" => {
let response = Response::from_string(format!("q: {}", content));
request.respond(response).ok();
}
Method::Patch if url == "/i" => {
let response = Response::from_string(format!("r: {}", content));
request.respond(response).ok();
}
_ => {
request
.respond(Response::from_string("Not Found").with_status_code(404))
.ok();
}
}
});
}
});
}
pub fn url(req: &str) -> String {
format!("http://localhost:35562{}", req)
}
pub fn get_body(request: Result<minreq::Response, minreq::Error>) -> String {
match request {
Ok(response) => match response.as_str() {
Ok(str) => String::from(str),
Err(err) => {
println!("\n[ERROR]: {}\n", err);
String::new()
}
},
Err(err) => {
println!("\n[ERROR]: {}\n", err);
String::new()
}
}
}
pub fn get_status_code(request: Result<minreq::Response, minreq::Error>) -> i32 {
match request {
Ok(response) => response.status_code,
Err(err) => {
println!("\n[ERROR]: {}\n", err);
-1
}
}
}
|