Skip to content
This repository has been archived by the owner on Oct 17, 2021. It is now read-only.

Commit

Permalink
Merge pull request #4 from AffectedArc07/http-sanity
Browse files Browse the repository at this point in the history
Updates + HTTP Client Sanity
  • Loading branch information
variableundefined authored Oct 31, 2020
2 parents 10c7643 + cddb459 commit b786233
Show file tree
Hide file tree
Showing 8 changed files with 794 additions and 854 deletions.
1,357 changes: 622 additions & 735 deletions Cargo.lock

Large diffs are not rendered by default.

36 changes: 17 additions & 19 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,47 +1,45 @@
[package]
name = "rust-g"
edition = "2018"
version = "0.4.5"
version = "0.4.5-P"
authors = ["Bjorn Neergaard <[email protected]>"]
repository = "https://github.com/tgstation/rust-g"
repository = "https://github.com/ParadiseSS13/rust-g"
license-file = "LICENSE"
description = "Offloaded task library for the /tg/ Space Station 13 codebase"
description = "Offloaded task library for the Paradise SS13 codebase. Adapted from /tg/station13"

[lib]
crate-type = ["cdylib"]

[profile.release]
opt-level = 3
codegen-units = 1
lto = true

[dependencies]
failure = "0.1"
chrono = { version = "0.4", optional = true }
crypto-hash = { version = "0.3", optional = true }
hex = { version = "0.3", optional = true }
percent-encoding = { version = "1.0", optional = true }
png = { version = "0.11.0", optional = true }
git2 = { version = "0.7.1", optional = true, default-features = false }
noise = { version = "0.6.0", optional = true}
reqwest = { version = "0.9", optional = true, default-features = false, features = ["rustls-tls"] }
hex = { version = "0.4", optional = true }
percent-encoding = { version = "2.1", optional = true }
url-dep = { version = "2.1", package = "url", optional = true }
png = { version = "0.16", optional = true }
git2 = { version = "0.13", optional = true, default-features = false }
noise = { version = "0.6", optional = true}
reqwest = { version = "0.10.8", optional = true, default-features = false, features = ["blocking", "rustls-tls"] }
serde = { version = "1.0", optional = true }
serde_json = { version = "1.0", optional = true }
serde_derive = { version = "1.0", optional = true }
lazy_static = { version = "1.3", optional = true }

[dependencies.mysql]
version = "18.2"
optional = true
git = "https://github.com/blackbeam/rust-mysql-simple"
rev = "b039fe7a11609aeab9071289bcff496b9366389a"
once_cell = { version = "1.4", optional = true }
mysql = { version = "19.0", optional = true }
dashmap = { version = "3.11", optional = true }

[features]
default = ["dmi", "log", "git", "http", "sql", "noise"]
dmi = ["png"]
file = []
hash = ["crypto-hash", "hex"]
log = ["chrono"]
url = ["percent-encoding"]
url = ["url-dep", "percent-encoding"]
git = ["git2", "chrono"]
http = ["reqwest", "serde", "serde_json", "serde_derive", "lazy_static"]
sql = ["mysql", "serde", "serde_derive", "serde_json", "lazy_static"]
http = ["reqwest", "serde", "serde_json", "serde_derive", "once_cell"]
sql = ["mysql", "serde", "serde_derive", "serde_json", "once_cell", "dashmap"]
2 changes: 2 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ fn main() {
#define rustg_http_request_blocking(method, url, body, headers) call(RUST_G, "http_request_blocking")(method, url, body, headers)
#define rustg_http_request_async(method, url, body, headers) call(RUST_G, "http_request_async")(method, url, body, headers)
#define rustg_http_check_request(req_id) call(RUST_G, "http_check_request")(req_id)
/proc/rustg_create_async_http_client() return call(RUST_G, "start_http_client")()
/proc/rustg_close_async_http_client() return call(RUST_G, "shutdown_http_client")()
"#).unwrap();
}

Expand Down
9 changes: 5 additions & 4 deletions src/dmi.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::error::{Error, Result};
use png::{Decoder, Encoder, HasParameters, OutputInfo};
use png::{Decoder, Encoder, OutputInfo};
use std::{
fs::{create_dir_all, File},
path::Path,
Expand Down Expand Up @@ -28,7 +28,8 @@ fn read_png(path: &str) -> Result<(OutputInfo, Vec<u8>)> {

fn write_png(path: &str, info: OutputInfo, image: Vec<u8>) -> Result<()> {
let mut encoder = Encoder::new(File::create(path)?, info.width, info.height);
encoder.set(info.color_type).set(info.bit_depth);
encoder.set_color(info.color_type);
encoder.set_depth(info.bit_depth);

let mut writer = encoder.write_header()?;
Ok(writer.write_image_data(&image)?)
Expand Down Expand Up @@ -57,8 +58,8 @@ fn create_png(path: &str, width: &str, height: &str, data: &str) -> Result<()> {
}

let mut encoder = Encoder::new(File::create(path)?, width, height);
encoder.set(png::ColorType::RGB);
encoder.set(png::BitDepth::Eight);
encoder.set_color(png::ColorType::RGB);
encoder.set_depth(png::BitDepth::Eight);
let mut writer = encoder.write_header()?;
Ok(writer.write_image_data(&result)?)
}
122 changes: 80 additions & 42 deletions src/http.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::{error::Result, jobs};
use std::collections::{BTreeMap, HashMap};
use std::io::Write;
use std::cell::RefCell;

// ----------------------------------------------------------------------------
// Interface
Expand Down Expand Up @@ -58,10 +60,10 @@ byond_fn! { http_check_request(id) {
const VERSION: &str = env!("CARGO_PKG_VERSION");
const PKG_NAME: &str = env!("CARGO_PKG_NAME");

fn setup_http_client() -> reqwest::Client {
fn setup_http_client() -> reqwest::blocking::Client {
use reqwest::{
blocking::Client,
header::{HeaderMap, USER_AGENT},
Client,
};

let mut headers = HeaderMap::new();
Expand All @@ -73,15 +75,15 @@ fn setup_http_client() -> reqwest::Client {
Client::builder().default_headers(headers).build().unwrap()
}

lazy_static! {
static ref HTTP_CLIENT: reqwest::Client = setup_http_client();
thread_local! {
static HTTP_CLIENT: RefCell<Option<reqwest::blocking::Client>> = RefCell::new(Some(setup_http_client()));
}

// ----------------------------------------------------------------------------
// Request construction and execution

struct RequestPrep {
req: reqwest::RequestBuilder,
req: reqwest::blocking::RequestBuilder,
output_filename: Option<String>,
}

Expand All @@ -92,38 +94,57 @@ fn construct_request(
headers: &str,
options: Option<&str>,
) -> Result<RequestPrep> {
let mut req = match method {
"post" => HTTP_CLIENT.post(url),
"put" => HTTP_CLIENT.put(url),
"patch" => HTTP_CLIENT.patch(url),
"delete" => HTTP_CLIENT.delete(url),
"head" => HTTP_CLIENT.head(url),
_ => HTTP_CLIENT.get(url),
};

if !body.is_empty() {
req = req.body(body.to_owned());
}

if !headers.is_empty() {
let headers: BTreeMap<&str, &str> = serde_json::from_str(headers)?;
for (key, value) in headers {
req = req.header(key, value);
}
}
HTTP_CLIENT.with(|cell| {
let borrow = cell.borrow_mut();
match &*borrow {
Some(client) => {
let mut req = match method {
"post" => client.post(url),
"put" => client.put(url),
"patch" => client.patch(url),
"delete" => client.delete(url),
"head" => client.head(url),
_ => client.get(url),
};

if !body.is_empty() {
req = req.body(body.to_owned());
}

if !headers.is_empty() {
let headers: BTreeMap<&str, &str> = serde_json::from_str(headers)?;
for (key, value) in headers {
req = req.header(key, value);
}
}

let mut output_filename = None;
if let Some(options) = options {
let options: RequestOptions = serde_json::from_str(options)?;
output_filename = options.output_filename;
if let Some(fname) = options.body_filename {
req = req.body(std::fs::File::open(fname)?);
}
}

Ok(RequestPrep {
req,
output_filename,
})
}

// If we got here we royally fucked up
None => {
let client = setup_http_client();
let req = client.get("");
let output_filename = None;
Ok(RequestPrep {
req,
output_filename,
})
}

let mut output_filename = None;
if let Some(options) = options {
let options: RequestOptions = serde_json::from_str(options)?;
output_filename = options.output_filename;
if let Some(fname) = options.body_filename {
req = req.body(std::fs::File::open(fname)?);
}
}

Ok(RequestPrep {
req,
output_filename,
})
}

Expand All @@ -137,18 +158,35 @@ fn submit_request(prep: RequestPrep) -> Result<String> {
body: None,
};

let headers = response.headers().clone();
for (key, value) in headers.iter() {
if let Ok(value) = value.to_str() {
resp.headers.insert(key.as_str(), value);
}
}

if let Some(output_filename) = prep.output_filename {
std::io::copy(&mut response, &mut std::fs::File::create(&output_filename)?)?;
let mut writer = std::io::BufWriter::new(std::fs::File::create(&output_filename)?);
std::io::copy(&mut response, &mut writer)?;
writer.flush()?;
} else {
body = response.text()?;
resp.body = Some(&body);
}

for (key, value) in response.headers().iter() {
if let Ok(value) = value.to_str() {
resp.headers.insert(key.as_str(), value);
}
}

Ok(serde_json::to_string(&resp)?)
}

byond_fn! { start_http_client() {
HTTP_CLIENT.with(|cell| {
cell.replace(Some(setup_http_client()))
});
Some("")
} }

byond_fn! { shutdown_http_client() {
HTTP_CLIENT.with(|cell| {
cell.replace(None)
});
Some("")
} }
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ extern crate reqwest;
extern crate serde_derive;
#[cfg(any(feature = "http", feature = "sql"))]
extern crate serde_json;
#[cfg(any(feature = "http", feature = "sql"))]
#[macro_use]
extern crate lazy_static;
#[cfg(feature = "sql")]
extern crate mysql;

Expand Down
Loading

0 comments on commit b786233

Please sign in to comment.