Skip to content

Commit 93ab91e

Browse files
committed
Use get_subject
1 parent 4ab2077 commit 93ab91e

File tree

6 files changed

+59
-81
lines changed

6 files changed

+59
-81
lines changed

server/src/handlers/get_resource.rs

+3-30
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use crate::{
22
appstate::AppState,
3-
content_types::get_accept,
43
content_types::ContentType,
54
errors::AtomicServerResult,
6-
helpers::{get_client_agent, try_extension},
5+
helpers::{get_client_agent, get_subject},
76
};
87
use actix_web::{web, HttpResponse};
98
use atomic_lib::Storelike;
@@ -16,39 +15,13 @@ pub async fn handle_get_resource(
1615
path: Option<web::Path<String>>,
1716
appstate: web::Data<AppState>,
1817
req: actix_web::HttpRequest,
18+
conn: actix_web::dev::ConnectionInfo,
1919
) -> AtomicServerResult<HttpResponse> {
2020
let mut timer = Timer::new();
2121

2222
let headers = req.headers();
23-
let mut content_type = get_accept(headers);
24-
let server_url = &appstate.config.server_url;
2523
// Get the subject from the path, or return the home URL
26-
let subject = if let Some(subj_end) = path {
27-
let mut subj_end_string = subj_end.as_str();
28-
// If the request is for the root, return the home URL
29-
if subj_end_string.is_empty() {
30-
server_url.to_string()
31-
} else {
32-
if content_type == ContentType::Html {
33-
if let Some((ext, path)) = try_extension(subj_end_string) {
34-
content_type = ext;
35-
subj_end_string = path;
36-
}
37-
}
38-
// Check extensions and set datatype. Harder than it looks to get right...
39-
// This might not be the best way of creating the subject. But I can't access the full URL from any actix stuff!
40-
let querystring = if req.query_string().is_empty() {
41-
"".to_string()
42-
} else {
43-
format!("?{}", req.query_string())
44-
};
45-
let subject = format!("{}/{}{}", server_url, subj_end_string, querystring);
46-
subject
47-
}
48-
} else {
49-
// There is no end string, so It's the root of the URL, the base URL!
50-
String::from(server_url)
51-
};
24+
let (subject, content_type) = get_subject(&req, &conn, &appstate)?;
5225

5326
let store = &appstate.store;
5427
timer.add("parse_headers");

server/src/handlers/post_resource.rs

+3-30
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use crate::{
22
appstate::AppState,
3-
content_types::get_accept,
43
content_types::ContentType,
54
errors::AtomicServerResult,
6-
helpers::{get_client_agent, try_extension},
5+
helpers::{get_client_agent, get_subject},
76
};
87
use actix_web::{web, HttpResponse};
98
use atomic_lib::Storelike;
@@ -16,39 +15,13 @@ pub async fn handle_post_resource(
1615
appstate: web::Data<AppState>,
1716
req: actix_web::HttpRequest,
1817
body: web::Bytes,
18+
conn: actix_web::dev::ConnectionInfo,
1919
) -> AtomicServerResult<HttpResponse> {
2020
let mut timer = Timer::new();
2121

2222
let headers = req.headers();
23-
let mut content_type = get_accept(headers);
24-
let server_url = &appstate.config.server_url;
2523
// Get the subject from the path, or return the home URL
26-
let subject = if let Some(subj_end) = path {
27-
let mut subj_end_string = subj_end.as_str();
28-
// If the request is for the root, return the home URL
29-
if subj_end_string.is_empty() {
30-
server_url.to_string()
31-
} else {
32-
if content_type == ContentType::Html {
33-
if let Some((ext, path)) = try_extension(subj_end_string) {
34-
content_type = ext;
35-
subj_end_string = path;
36-
}
37-
}
38-
// Check extensions and set datatype. Harder than it looks to get right...
39-
// This might not be the best way of creating the subject. But I can't access the full URL from any actix stuff!
40-
let querystring = if req.query_string().is_empty() {
41-
"".to_string()
42-
} else {
43-
format!("?{}", req.query_string())
44-
};
45-
let subject = format!("{}/{}{}", server_url, subj_end_string, querystring);
46-
subject
47-
}
48-
} else {
49-
// There is no end string, so It's the root of the URL, the base URL!
50-
String::from(server_url)
51-
};
24+
let (subject, content_type) = get_subject(&req, &conn, &appstate)?;
5225

5326
let store = &appstate.store;
5427
timer.add("parse_headers");

server/src/handlers/search.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub async fn search_query(
7878
let subjects = docs_to_subjects(top_docs, &fields, &searcher)?;
7979

8080
// Create a valid atomic data resource.
81-
let subject: String = get_subject(&req, &conn, &appstate)?;
81+
let (subject, _) = get_subject(&req, &conn, &appstate)?;
8282

8383
let mut results_resource = atomic_lib::plugins::search::search_endpoint().to_resource(store)?;
8484
results_resource.set_subject(subject.clone());

server/src/handlers/single_page_app.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::fmt::Display;
22
use std::fmt::Formatter;
33

4+
use crate::helpers::get_subject;
45
use crate::{appstate::AppState, errors::AtomicServerResult};
56
use actix_web::HttpResponse;
67

@@ -9,9 +10,13 @@ use actix_web::HttpResponse;
910
pub async fn single_page(
1011
appstate: actix_web::web::Data<AppState>,
1112
path: actix_web::web::Path<String>,
13+
req: actix_web::HttpRequest,
14+
conn: actix_web::dev::ConnectionInfo,
1215
) -> AtomicServerResult<HttpResponse> {
1316
let template = include_str!("../../app_assets/index.html");
14-
let subject = format!("{}/{}", appstate.store.get_server_url(), path);
17+
// We could check the extension and serialize the Resource in the response. However, I'm not sure this is a great idea.
18+
let (subject, _content_type) = get_subject(&req, &conn, &appstate)?;
19+
1520
let meta_tags: MetaTags = if let Ok(resource) =
1621
appstate
1722
.store

server/src/handlers/upload.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub async fn upload_handler(
3636
) -> AtomicServerResult<HttpResponse> {
3737
let store = &appstate.store;
3838
let parent = store.get_resource(&query.parent)?;
39-
let subject = get_subject(&req, &conn, &appstate)?;
39+
let (subject, _) = get_subject(&req, &conn, &appstate)?;
4040
if let Some(agent) = get_client_agent(req.headers(), &appstate, subject)? {
4141
check_write(store, &parent, &agent)?;
4242
} else {

server/src/helpers.rs

+45-18
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use atomic_lib::Storelike;
99
use percent_encoding::percent_decode_str;
1010
use std::str::FromStr;
1111

12-
use crate::content_types::ContentType;
12+
use crate::content_types::{get_accept, ContentType};
1313
use crate::errors::{AppErrorType, AtomicServerError};
1414
use crate::{appstate::AppState, errors::AtomicServerResult};
1515

@@ -224,7 +224,9 @@ pub fn get_subject(
224224
req: &actix_web::HttpRequest,
225225
conn: &actix_web::dev::ConnectionInfo,
226226
appstate: &AppState,
227-
) -> AtomicServerResult<String> {
227+
) -> AtomicServerResult<(String, ContentType)> {
228+
let content_type = get_accept(req.headers());
229+
228230
let domain = &appstate.config.opts.domain;
229231
let host = conn.host();
230232
let subdomain = if let Some(index) = host.find(domain) {
@@ -243,23 +245,48 @@ pub fn get_subject(
243245
}
244246
let server_without_last_slash = subject_url.to_string().trim_end_matches('/').to_string();
245247
let subject = format!("{}{}", server_without_last_slash, &req.uri().to_string());
246-
Ok(subject)
248+
// if let Some((ct, path)) = try_extension(req.path()) {
249+
// content_type = ct;
250+
// return Ok((path.to_string(), content_type));
251+
// }
252+
Ok((subject, content_type))
247253
}
248254

249-
/// Finds the extension
250-
pub fn try_extension(path: &str) -> Option<(ContentType, &str)> {
251-
let items: Vec<&str> = path.split('.').collect();
252-
if items.len() == 2 {
253-
let path = items[0];
254-
let content_type = match items[1] {
255-
"json" => ContentType::Json,
256-
"jsonld" => ContentType::JsonLd,
257-
"jsonad" => ContentType::JsonAd,
258-
"html" => ContentType::Html,
259-
"ttl" => ContentType::Turtle,
260-
_ => return None,
261-
};
262-
return Some((content_type, path));
255+
/// Finds the extension of a supported serialization format.
256+
/// Not used right now, see: https://github.com/atomicdata-dev/atomic-data-rust/issues/601
257+
#[allow(dead_code)]
258+
fn try_extension(path: &str) -> Option<(ContentType, &str)> {
259+
// Check if path ends with one of the folliwing extensions
260+
let extensions = [
261+
".json",
262+
".jsonld",
263+
".jsonad",
264+
".html",
265+
".ttl",
266+
".nt",
267+
".nq",
268+
".ntriples",
269+
".nt",
270+
];
271+
let mut found = None;
272+
for ext in extensions.iter() {
273+
if path.ends_with(ext) {
274+
println!("Found extension: {}", ext);
275+
let path = &path[0..path.len() - ext.len()];
276+
let content_type = match *ext {
277+
".json" => Some(ContentType::Json),
278+
".jsonld" => Some(ContentType::JsonLd),
279+
".jsonad" => Some(ContentType::JsonAd),
280+
".html" => Some(ContentType::Html),
281+
".ttl" => Some(ContentType::Turtle),
282+
".nt" => Some(ContentType::NTriples),
283+
".ntriples" => Some(ContentType::NTriples),
284+
_ => None,
285+
};
286+
if let Some(ct) = content_type {
287+
found = Some((ct, path));
288+
}
289+
}
263290
}
264-
None
291+
found
265292
}

0 commit comments

Comments
 (0)