Skip to content

Commit 56fdb2d

Browse files
committed
Use get_subject
1 parent 66e0c8d commit 56fdb2d

File tree

6 files changed

+59
-81
lines changed

6 files changed

+59
-81
lines changed

server/src/handlers/get_resource.rs

Lines changed: 3 additions & 30 deletions
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

Lines changed: 3 additions & 30 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub async fn search_query(
8080
let subjects = docs_to_subjects(top_docs, &fields, &searcher)?;
8181

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

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

server/src/handlers/single_page_app.rs

Lines changed: 6 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 45 additions & 18 deletions
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

@@ -248,7 +248,9 @@ pub fn get_subject(
248248
req: &actix_web::HttpRequest,
249249
conn: &actix_web::dev::ConnectionInfo,
250250
appstate: &AppState,
251-
) -> AtomicServerResult<String> {
251+
) -> AtomicServerResult<(String, ContentType)> {
252+
let content_type = get_accept(req.headers());
253+
252254
let domain = &appstate.config.opts.domain;
253255
let host = conn.host();
254256
let subdomain = if let Some(index) = host.find(domain) {
@@ -267,23 +269,48 @@ pub fn get_subject(
267269
}
268270
let server_without_last_slash = subject_url.to_string().trim_end_matches('/').to_string();
269271
let subject = format!("{}{}", server_without_last_slash, &req.uri().to_string());
270-
Ok(subject)
272+
// if let Some((ct, path)) = try_extension(req.path()) {
273+
// content_type = ct;
274+
// return Ok((path.to_string(), content_type));
275+
// }
276+
Ok((subject, content_type))
271277
}
272278

273-
/// Finds the extension
274-
pub fn try_extension(path: &str) -> Option<(ContentType, &str)> {
275-
let items: Vec<&str> = path.split('.').collect();
276-
if items.len() == 2 {
277-
let path = items[0];
278-
let content_type = match items[1] {
279-
"json" => ContentType::Json,
280-
"jsonld" => ContentType::JsonLd,
281-
"jsonad" => ContentType::JsonAd,
282-
"html" => ContentType::Html,
283-
"ttl" => ContentType::Turtle,
284-
_ => return None,
285-
};
286-
return Some((content_type, path));
279+
/// Finds the extension of a supported serialization format.
280+
/// Not used right now, see: https://github.com/atomicdata-dev/atomic-data-rust/issues/601
281+
#[allow(dead_code)]
282+
fn try_extension(path: &str) -> Option<(ContentType, &str)> {
283+
// Check if path ends with one of the folliwing extensions
284+
let extensions = [
285+
".json",
286+
".jsonld",
287+
".jsonad",
288+
".html",
289+
".ttl",
290+
".nt",
291+
".nq",
292+
".ntriples",
293+
".nt",
294+
];
295+
let mut found = None;
296+
for ext in extensions.iter() {
297+
if path.ends_with(ext) {
298+
println!("Found extension: {}", ext);
299+
let path = &path[0..path.len() - ext.len()];
300+
let content_type = match *ext {
301+
".json" => Some(ContentType::Json),
302+
".jsonld" => Some(ContentType::JsonLd),
303+
".jsonad" => Some(ContentType::JsonAd),
304+
".html" => Some(ContentType::Html),
305+
".ttl" => Some(ContentType::Turtle),
306+
".nt" => Some(ContentType::NTriples),
307+
".ntriples" => Some(ContentType::NTriples),
308+
_ => None,
309+
};
310+
if let Some(ct) = content_type {
311+
found = Some((ct, path));
312+
}
313+
}
287314
}
288-
None
315+
found
289316
}

0 commit comments

Comments
 (0)