Skip to content

Commit cc913df

Browse files
committed
Use get_subject
1 parent 2d0c25c commit cc913df

File tree

7 files changed

+61
-83
lines changed

7 files changed

+61
-83
lines changed

lib/src/parse.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ fn parse_json_ad_map_to_resource(
347347
validate_timestamp: false,
348348
validate_rights: parse_opts.for_agent != ForAgent::Sudo,
349349
validate_previous_commit: false,
350-
validate_for_agent: parse_opts.for_agent.clone(),
350+
validate_for_agent: Some(parse_opts.for_agent.to_string()),
351351
validate_subject_url_parent: true,
352352
update_index: true,
353353
};

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
@@ -35,7 +35,7 @@ pub async fn upload_handler(
3535
) -> AtomicServerResult<HttpResponse> {
3636
let store = &appstate.store;
3737
let parent = store.get_resource(&query.parent)?;
38-
let subject = get_subject(&req, &conn, &appstate)?;
38+
let (subject, _) = get_subject(&req, &conn, &appstate)?;
3939
let for_agent = get_client_agent(req.headers(), &appstate, subject)?;
4040
check_write(store, &parent, &for_agent)?;
4141

server/src/helpers.rs

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use atomic_lib::Storelike;
1010
use percent_encoding::percent_decode_str;
1111
use std::str::FromStr;
1212

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

@@ -160,7 +160,7 @@ pub fn get_client_agent(
160160
&appstate.store,
161161
)
162162
.map_err(|e| format!("Authentication failed: {}", e))?;
163-
Ok(for_agent.into())
163+
Ok(for_agent)
164164
}
165165

166166
fn session_cookies_from_header(header: &HeaderValue) -> AtomicServerResult<Vec<String>> {
@@ -250,7 +250,9 @@ pub fn get_subject(
250250
req: &actix_web::HttpRequest,
251251
conn: &actix_web::dev::ConnectionInfo,
252252
appstate: &AppState,
253-
) -> AtomicServerResult<String> {
253+
) -> AtomicServerResult<(String, ContentType)> {
254+
let content_type = get_accept(req.headers());
255+
254256
let domain = &appstate.config.opts.domain;
255257
let host = conn.host();
256258
let subdomain = if let Some(index) = host.find(domain) {
@@ -269,23 +271,48 @@ pub fn get_subject(
269271
}
270272
let server_without_last_slash = subject_url.to_string().trim_end_matches('/').to_string();
271273
let subject = format!("{}{}", server_without_last_slash, &req.uri().to_string());
272-
Ok(subject)
274+
// if let Some((ct, path)) = try_extension(req.path()) {
275+
// content_type = ct;
276+
// return Ok((path.to_string(), content_type));
277+
// }
278+
Ok((subject, content_type))
273279
}
274280

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

0 commit comments

Comments
 (0)