Handle server-wide OPTIONS requests

This commit is contained in:
Mike Dilger 2024-11-17 09:14:02 +13:00
parent aa9063e60e
commit 48b08e93f5
No known key found for this signature in database
GPG Key ID: 47581A78D4329BA4

View File

@ -3,8 +3,9 @@ mod nip11;
use crate::error::Error;
use crate::ip::HashedPeer;
use http::Method;
use http_body_util::combinators::BoxBody;
use http_body_util::{BodyExt, Full};
use http_body_util::{BodyExt, Empty, Full};
use hyper::body::{Bytes, Incoming};
use hyper::{Request, Response, StatusCode};
@ -12,6 +13,22 @@ pub async fn serve_http(
peer: HashedPeer,
request: Request<Incoming>,
) -> Result<Response<BoxBody<Bytes, Error>>, Error> {
// Handle server-wide OPTIONS requests
let p = request.uri().path();
if p == "*" && request.method() == Method::OPTIONS {
let response = Response::builder()
.header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Headers", "Authorization, *")
.header(
"Access-Control-Allow-Methods",
"OPTIONS, GET, HEAD, PUT, DELETE",
)
.header("Allow", "OPTIONS, GET, HEAD, PUT, DELETE")
.status(StatusCode::OK)
.body(Empty::new().map_err(|e| e.into()).boxed())?;
return Ok(response);
}
// check for Accept header of application/nostr+json
if let Some(accept) = request.headers().get("Accept") {
if let Ok(s) = accept.to_str() {