From d9f1a4414f057b78f30835a0f330d938138e28ca Mon Sep 17 00:00:00 2001 From: Colin Atkinson Date: Mon, 3 Jul 2023 09:05:18 -0400 Subject: [PATCH] Fix regex error when actix-web feature not enabled (#56) * Fix formatting for nightly rustfmt https://github.com/LemmyNet/lemmy/issues/3467 * Fix regex error when actix-web feature not enabled If the crate is built with only the axum feature, compiling the webfinger account regex will fail with an error "Unicode-aware case insensitivity matching is not available..." because of the missing unicode-case feature. This doesn't happen if actix is installed because it pulls in the regex crate with all features (via [actix-router][0]). The failure can be demonstrated by reverting this commit's change to Cargo.toml and running: cargo test --no-default-features --features=axum --doc extract_webfinger_name Resolve this by adding the unicode-case feature to the regex dependency. [0]: https://github.com/actix/actix-web/blob/0e8ed50e3a7ac93d94e6c1c6603d0048e1f33fb8/actix-router/Cargo.toml#L25 --- Cargo.toml | 2 +- examples/live_federation/objects/post.rs | 7 +++++-- .../local_federation/activities/accept.rs | 5 ++++- .../local_federation/activities/follow.rs | 4 +++- examples/local_federation/axum/http.rs | 3 ++- src/actix_web/middleware.rs | 5 ++++- src/fetch/mod.rs | 5 ++++- src/fetch/webfinger.rs | 19 +++++++++++++++++++ 8 files changed, 42 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 90ef2d8..d99e646 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,7 +38,7 @@ bytes = "1.4.0" futures-core = { version = "0.3.28", default-features = false } pin-project-lite = "0.2.9" activitystreams-kinds = "0.3.0" -regex = { version = "1.8.4", default-features = false, features = ["std"] } +regex = { version = "1.8.4", default-features = false, features = ["std", "unicode-case"] } tokio = { version = "1.21.2", features = [ "sync", "rt", diff --git a/examples/live_federation/objects/post.rs b/examples/live_federation/objects/post.rs index 44012b1..9a08b9d 100644 --- a/examples/live_federation/objects/post.rs +++ b/examples/live_federation/objects/post.rs @@ -1,6 +1,9 @@ use crate::{ - activities::create_post::CreatePost, database::DatabaseHandle, error::Error, - generate_object_id, objects::person::DbUser, + activities::create_post::CreatePost, + database::DatabaseHandle, + error::Error, + generate_object_id, + objects::person::DbUser, }; use activitypub_federation::{ config::Data, diff --git a/examples/local_federation/activities/accept.rs b/examples/local_federation/activities/accept.rs index 9305213..c18945f 100644 --- a/examples/local_federation/activities/accept.rs +++ b/examples/local_federation/activities/accept.rs @@ -1,6 +1,9 @@ use crate::{activities::follow::Follow, instance::DatabaseHandle, objects::person::DbUser}; use activitypub_federation::{ - config::Data, fetch::object_id::ObjectId, kinds::activity::AcceptType, traits::ActivityHandler, + config::Data, + fetch::object_id::ObjectId, + kinds::activity::AcceptType, + traits::ActivityHandler, }; use serde::{Deserialize, Serialize}; use url::Url; diff --git a/examples/local_federation/activities/follow.rs b/examples/local_federation/activities/follow.rs index 51e8ee1..865a618 100644 --- a/examples/local_federation/activities/follow.rs +++ b/examples/local_federation/activities/follow.rs @@ -1,5 +1,7 @@ use crate::{ - activities::accept::Accept, generate_object_id, instance::DatabaseHandle, + activities::accept::Accept, + generate_object_id, + instance::DatabaseHandle, objects::person::DbUser, }; use activitypub_federation::{ diff --git a/examples/local_federation/axum/http.rs b/examples/local_federation/axum/http.rs index fd3fba9..3202117 100644 --- a/examples/local_federation/axum/http.rs +++ b/examples/local_federation/axum/http.rs @@ -17,7 +17,8 @@ use axum::{ extract::{Path, Query}, response::IntoResponse, routing::{get, post}, - Json, Router, + Json, + Router, }; use axum_macros::debug_handler; use serde::Deserialize; diff --git a/src/actix_web/middleware.rs b/src/actix_web/middleware.rs index 2bb562b..afa0117 100644 --- a/src/actix_web/middleware.rs +++ b/src/actix_web/middleware.rs @@ -1,7 +1,10 @@ use crate::config::{Data, FederationConfig, FederationMiddleware}; use actix_web::{ dev::{forward_ready, Payload, Service, ServiceRequest, ServiceResponse, Transform}, - Error, FromRequest, HttpMessage, HttpRequest, + Error, + FromRequest, + HttpMessage, + HttpRequest, }; use std::future::{ready, Ready}; diff --git a/src/fetch/mod.rs b/src/fetch/mod.rs index 5faadcd..7a9734c 100644 --- a/src/fetch/mod.rs +++ b/src/fetch/mod.rs @@ -3,7 +3,10 @@ #![doc = include_str!("../../docs/07_fetching_data.md")] use crate::{ - config::Data, error::Error, http_signatures::sign_request, reqwest_shim::ResponseExt, + config::Data, + error::Error, + http_signatures::sign_request, + reqwest_shim::ResponseExt, FEDERATION_CONTENT_TYPE, }; use bytes::Bytes; diff --git a/src/fetch/webfinger.rs b/src/fetch/webfinger.rs index 183bd46..1ccf274 100644 --- a/src/fetch/webfinger.rs +++ b/src/fetch/webfinger.rs @@ -66,6 +66,25 @@ where /// request. For a parameter of the form `acct:gargron@mastodon.social` it returns `gargron`. /// /// Returns an error if query doesn't match local domain. +/// +///``` +/// # use activitypub_federation::config::FederationConfig; +/// # use activitypub_federation::traits::tests::DbConnection; +/// # use activitypub_federation::fetch::webfinger::extract_webfinger_name; +/// # tokio::runtime::Runtime::new().unwrap().block_on(async { +/// # let db_connection = DbConnection; +/// let config = FederationConfig::builder() +/// .domain("example.com") +/// .app_data(db_connection) +/// .build() +/// .await +/// .unwrap(); +/// let data = config.to_request_data(); +/// let res = extract_webfinger_name("acct:test_user@example.com", &data).unwrap(); +/// assert_eq!(res, "test_user"); +/// # Ok::<(), anyhow::Error>(()) +/// }).unwrap(); +///``` pub fn extract_webfinger_name(query: &str, data: &Data) -> Result where T: Clone,