Correctly parse HTTP Accept headers

This commit is contained in:
Bat 2018-07-18 16:58:28 +02:00
parent 3d436c10b1
commit 2b04b39f5d
4 changed files with 16 additions and 8 deletions

View File

@ -17,7 +17,15 @@ pub mod sign;
pub const CONTEXT_URL: &'static str = "https://www.w3.org/ns/activitystreams"; pub const CONTEXT_URL: &'static str = "https://www.w3.org/ns/activitystreams";
pub const PUBLIC_VISIBILTY: &'static str = "https://www.w3.org/ns/activitystreams#Public"; pub const PUBLIC_VISIBILTY: &'static str = "https://www.w3.org/ns/activitystreams#Public";
pub const AP_ACCEPT_HEADER: &'static str = "application/ld+json; profile=\"https://w3.org/ns/activitystreams\", application/ld+json;profile=\"https://w3.org/ns/activitystreams\", application/activity+json, application/ld+json";
pub fn ap_accept_header() -> Vec<&'static str> {
vec![
"application/ld+json; profile=\"https://w3.org/ns/activitystreams\"",
"application/ld+json;profile=\"https://w3.org/ns/activitystreams\"",
"application/activity+json",
"application/ld+json"
]
}
pub fn context() -> serde_json::Value { pub fn context() -> serde_json::Value {
json!([ json!([

View File

@ -2,14 +2,14 @@ use base64;
use openssl::hash::{Hasher, MessageDigest}; use openssl::hash::{Hasher, MessageDigest};
use reqwest::{ use reqwest::{
mime::Mime, mime::Mime,
header::{ContentType, Date, Headers, UserAgent} header::{Accept, Date, Headers, UserAgent, qitem}
}; };
use std::{ use std::{
str::FromStr, str::FromStr,
time::SystemTime time::SystemTime
}; };
use activity_pub::AP_ACCEPT_HEADER; use activity_pub::ap_accept_header;
use activity_pub::sign::Signer; use activity_pub::sign::Signer;
const USER_AGENT: &'static str = "Plume/0.1.0"; const USER_AGENT: &'static str = "Plume/0.1.0";
@ -26,7 +26,7 @@ pub fn headers() -> Headers {
let mut headers = Headers::new(); let mut headers = Headers::new();
headers.set(UserAgent::new(USER_AGENT)); headers.set(UserAgent::new(USER_AGENT));
headers.set(Date(SystemTime::now().into())); headers.set(Date(SystemTime::now().into()));
headers.set(ContentType(Mime::from_str(AP_ACCEPT_HEADER).unwrap())); headers.set(Accept(ap_accept_header().into_iter().map(|h| qitem(h.parse::<Mime>().expect("Invalid Content-Type"))).collect()));
headers headers
} }

View File

@ -18,7 +18,7 @@ use webfinger::*;
use {BASE_URL, USE_HTTPS}; use {BASE_URL, USE_HTTPS};
use plume_common::activity_pub::{ use plume_common::activity_pub::{
AP_ACCEPT_HEADER, ApSignature, ActivityStream, Id, IntoId, PublicKey, ap_accept_header, ApSignature, ActivityStream, Id, IntoId, PublicKey,
inbox::WithInbox, inbox::WithInbox,
sign sign
}; };
@ -109,7 +109,7 @@ impl Blog {
fn fetch_from_url(conn: &PgConnection, url: String) -> Option<Blog> { fn fetch_from_url(conn: &PgConnection, url: String) -> Option<Blog> {
let req = Client::new() let req = Client::new()
.get(&url[..]) .get(&url[..])
.header(Accept(vec![qitem(AP_ACCEPT_HEADER.parse::<Mime>().unwrap())])) .header(Accept(ap_accept_header().into_iter().map(|h| qitem(h.parse::<Mime>().expect("Invalid Content-Type"))).collect()))
.send(); .send();
match req { match req {
Ok(mut res) => { Ok(mut res) => {

View File

@ -13,7 +13,7 @@ use openssl::{
sign sign
}; };
use plume_common::activity_pub::{ use plume_common::activity_pub::{
AP_ACCEPT_HEADER, ActivityStream, Id, IntoId, ApSignature, PublicKey, ap_accept_header, ActivityStream, Id, IntoId, ApSignature, PublicKey,
inbox::WithInbox, inbox::WithInbox,
sign::{Signer, gen_keypair} sign::{Signer, gen_keypair}
}; };
@ -155,7 +155,7 @@ impl User {
fn fetch_from_url(conn: &PgConnection, url: String) -> Option<User> { fn fetch_from_url(conn: &PgConnection, url: String) -> Option<User> {
let req = Client::new() let req = Client::new()
.get(&url[..]) .get(&url[..])
.header(Accept(vec![qitem(AP_ACCEPT_HEADER.parse::<Mime>().unwrap())])) .header(Accept(ap_accept_header().into_iter().map(|h| qitem(h.parse::<Mime>().expect("Invalid Content-Type"))).collect()))
.send(); .send();
match req { match req {
Ok(mut res) => { Ok(mut res) => {