Skip to content

Commit

Permalink
HTTP improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Furrior authored Jan 27, 2025
1 parent a0a4025 commit c504fe2
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 5 deletions.
2 changes: 2 additions & 0 deletions dmsrc/file.dm
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
// FILE Operations //

#define rustutils_file_write_b64decode(text, fname) CALL_LIB(RUST_UTILS, "file_write")(text, fname, "true")
5 changes: 5 additions & 0 deletions dmsrc/jobs.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// JOBS //

#define RUSTUTILS_JOB_NO_RESULTS_YET "NO RESULTS YET"
#define RUSTUTILS_JOB_NO_SUCH_JOB "NO SUCH JOB"
#define RUSTUTILS_JOB_ERROR "JOB PANICKED"
2 changes: 2 additions & 0 deletions dmsrc/regexp.dm
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
// Regex //

#define rustutils_regex_replace(text, re, re_params, replacement) CALL_LIB(RUST_UTILS, "regex_replace")(text, re, re_params, replacement)
2 changes: 2 additions & 0 deletions dmsrc/transliteration.dm
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
// Transliteration

#define rustutils_cyrillic_to_latin(text) CALL_LIB(RUST_UTILS, "cyrillic_to_latin")("[text]")
#define rustutils_latin_to_cyrillic(text) CALL_LIB(RUST_UTILS, "latin_to_cyrillic")("[text]")
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub enum Error {
JsonSerialization(#[from] serde_json::Error),
#[cfg(feature = "http")]
#[error(transparent)]
Request(#[from] Box<ureq::Error>),
Response(#[from] ureq::Transport),
}

impl From<Utf8Error> for Error {
Expand Down
7 changes: 5 additions & 2 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use serde::{Deserialize, Serialize};
use std::cell::RefCell;
use std::collections::{BTreeMap, HashMap};
use std::io::Write;
use std::time::Duration;
use ureq::OrAnyStatus;

// ----------------------------------------------------------------------------
// Interface
Expand Down Expand Up @@ -95,7 +97,8 @@ fn construct_request(
"head" => client.head(url),
_ => client.get(url),
}
.set("User-Agent", &format!("{PKG_NAME}/{VERSION}"));
.set("User-Agent", &format!("{PKG_NAME}/{VERSION}"))
.timeout(Duration::from_secs(30));

let mut final_body = body.as_bytes().to_vec();

Expand Down Expand Up @@ -138,7 +141,7 @@ fn construct_request(
}

fn submit_request(prep: RequestPrep) -> Result<String> {
let response = prep.req.send_bytes(&prep.body).map_err(Box::new)?;
let response = prep.req.send_bytes(&prep.body).or_any_status()?;

let body;
let mut resp = Response {
Expand Down
6 changes: 6 additions & 0 deletions tests/dm-tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ fn regexp() {
run_dm_tests("regexp");
}

#[cfg(feature = "http")]
#[test]
fn http() {
run_dm_tests("http");
}

fn run_dm_tests(name: &str) {
std::env::remove_var("RUST_BACKTRACE");

Expand Down
2 changes: 1 addition & 1 deletion tests/dm/common.dm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "../../target/rust_utils.dm"

/world/New()
world.log << "Rust is at [RUST_UTILS]"
log << "Rust is at [RUST_UTILS]"
for(var/func in typesof(/test/proc))
log << "[func] [copytext("------------------------------------------------------------------------", length("[func]"))]"
call(new /test, func)()
Expand Down
35 changes: 35 additions & 0 deletions tests/dm/http.dme
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "common.dm"

#define STATUS200URL "https://httpstat.us/200"
#define STATUS400URL "https://httpstat.us/400"

/proc/execute_request(url)
rustutils_create_async_http_client()
var/id = rustutils_http_request_async(RUSTUTILS_HTTP_METHOD_GET, url, "", list(), null)

var/r
do
r = rustutils_http_check_request(id)
// sleep(1)
while(r == RUSTUTILS_JOB_NO_RESULTS_YET)

ASSERT(r != RUSTUTILS_JOB_ERROR)
rustutils_close_async_http_client()

var/list/L = json_decode(r)
return L

/test/proc/test_200()
var/list/L = execute_request(STATUS200URL)
ASSERT(L["status_code"] == 200)

/test/proc/test_400()
var/list/L = execute_request(STATUS400URL)
ASSERT(L["status_code"] == 400)

/test/proc/nonexistent()
try
var/list/L = execute_request("http://nonexistent.nonexistent")
CRASH("Should have failed")
catch
return
1 change: 0 additions & 1 deletion tests/dm/regexp.dme
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ var/expected_error = "Words replace."

/test/proc/crash()
var/output = rustutils_regex_replace(input, pattern, incorrect_pattern_flags, replacement)
world.log << output
ASSERT(output == expected_error)

0 comments on commit c504fe2

Please sign in to comment.