Handling "not a known media type" #2503
-
What is the right way of handling mandatory media types that are unknown to Rocket? I'm building an ActivityPub service and it uses a lot of media types that aren't defined as "known" by default: #[get(
"/.well-known/webfinger?<resource>",
format = "application/jrd+json",
rank = 1
)] #[post(
"/api/user/<username>/events/authorize",
format = "application/activity+json",
data = "<authorization>"
)] #[get("/.well-known/host-meta", format = "application/xrd+xml")] And so forth. These are all well-known types (except for maybe Also, is there a way to use multiple media-types in a single definition? I've tried using a vec in the definition, but that doesn't work. I've considered just implementing duplicate endpoints with different media-types, but the ranking system complicates matters. Am I correct in my observation that the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Unfortunately, there's no way to dynamically add a media type to the list of known media types. The challenge is that warnings are emitted at compile-time whereas configuration can be completely interpreted at runtime. That is, using One idea I've had is to add a #[rocket::allow(unknown_media_type)]
#[get("/.well-known/webfinger?<resource>", format = "application/jrd+json", rank = 1)] Since the Alternatively, we could add some special syntax that says "hey, I know this isn't recognized, but I'm okay with it." Maybe even abuse raw strings to do so: #[get("/.well-known/webfinger?<resource>", format = r"application/jrd+json", rank = 1)] That would work, but might semantically be too weird. In any case, I'm open to a solution/implementation of either. The former I would accept directly, the latter would require a bit more thought.
No, but I don't see why we couldn't allow multiple
That's right. It's not clear why Rocket should prefer a route that matches For example, we can have: #[get("/", format = "msgpack")]
fn msgpack() {}
#[get("/", format = "json", rank = 2)]
fn json() {} A request with |
Beta Was this translation helpful? Give feedback.
Unfortunately, there's no way to dynamically add a media type to the list of known media types. The challenge is that warnings are emitted at compile-time whereas configuration can be completely interpreted at runtime. That is, using
Rocket.toml
is the default configuration source, but it can be completely replaced by anything at all, including over the network, a different file, etc. So reading something like Rocket.toml at compile-time will work but changes the meaning of configuration.One idea I've had is to add a
rocket::allow
codegen attribute that can be used on routes to silence warnings: