Rust reimplementation of Angry.Im
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.rs 1.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. use actix_web::dev::ConnectionInfo;
  2. use rand::{self, Rng};
  3. use std::str;
  4. /*
  5. * Generate a random alphanumeric string of a specified length
  6. */
  7. const DICTIONARY: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  8. pub fn rand_str(len: usize) -> String {
  9. let mut rng = rand::thread_rng();
  10. let mut ret: Vec<u8> = Vec::with_capacity(len);
  11. for _ in 0..len {
  12. ret.push(DICTIONARY[rng.gen_range(0, DICTIONARY.len())]);
  13. }
  14. str::from_utf8(ret.as_slice()).unwrap().to_string()
  15. }
  16. /*
  17. * Construct scheme://host from ConnectionInfo
  18. */
  19. pub fn conn_scheme_host_port(info: &ConnectionInfo) -> String {
  20. format!("{}://{}", info.scheme(), info.host())
  21. }
  22. /*
  23. * Return true if the user agent is considered a browser
  24. */
  25. const BROWSER_AGENTS: &[&str] = &[
  26. "chrome",
  27. "msie",
  28. "trident",
  29. "firefox",
  30. "safari",
  31. ];
  32. pub fn ua_is_browser(ua: &str) -> bool {
  33. let ua = ua.to_lowercase();
  34. for ba in BROWSER_AGENTS {
  35. if ua.contains(ba) {
  36. return true;
  37. }
  38. }
  39. return false;
  40. }