Skip to content

Commit bdc1214

Browse files
committed
Use get_subject
1 parent 50aba70 commit bdc1214

File tree

7 files changed

+58
-82
lines changed

7 files changed

+58
-82
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: 4 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,11 @@ 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!("../../../browser/data-browser/dist/index.html");
14-
let subject = format!("{}/{}", appstate.store.get_server_url(), path);
17+
let (subject, _content_type) = get_subject(&req, &conn, &appstate)?;
1518
let meta_tags: MetaTags = if let Ok(resource) =
1619
appstate
1720
.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: 45 additions & 18 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

@@ -286,7 +286,9 @@ pub fn get_subject(
286286
req: &actix_web::HttpRequest,
287287
conn: &actix_web::dev::ConnectionInfo,
288288
appstate: &AppState,
289-
) -> AtomicServerResult<String> {
289+
) -> AtomicServerResult<(String, ContentType)> {
290+
let content_type = get_accept(req.headers());
291+
290292
let domain = &appstate.config.opts.domain;
291293
let host = conn.host();
292294
let subdomain = if let Some(index) = host.find(domain) {
@@ -305,23 +307,48 @@ pub fn get_subject(
305307
}
306308
let server_without_last_slash = subject_url.to_string().trim_end_matches('/').to_string();
307309
let subject = format!("{}{}", server_without_last_slash, &req.uri().to_string());
308-
Ok(subject)
310+
// if let Some((ct, path)) = try_extension(req.path()) {
311+
// content_type = ct;
312+
// return Ok((path.to_string(), content_type));
313+
// }
314+
Ok((subject, content_type))
309315
}
310316

311-
/// Finds the extension
312-
pub fn try_extension(path: &str) -> Option<(ContentType, &str)> {
313-
let items: Vec<&str> = path.split('.').collect();
314-
if items.len() == 2 {
315-
let path = items[0];
316-
let content_type = match items[1] {
317-
"json" => ContentType::Json,
318-
"jsonld" => ContentType::JsonLd,
319-
"jsonad" => ContentType::JsonAd,
320-
"html" => ContentType::Html,
321-
"ttl" => ContentType::Turtle,
322-
_ => return None,
323-
};
324-
return Some((content_type, path));
317+
/// Finds the extension of a supported serialization format.
318+
/// Not used right now, see: https://github.com/atomicdata-dev/atomic-data-rust/issues/601
319+
#[allow(dead_code)]
320+
fn try_extension(path: &str) -> Option<(ContentType, &str)> {
321+
// Check if path ends with one of the folliwing extensions
322+
let extensions = [
323+
".json",
324+
".jsonld",
325+
".jsonad",
326+
".html",
327+
".ttl",
328+
".nt",
329+
".nq",
330+
".ntriples",
331+
".nt",
332+
];
333+
let mut found = None;
334+
for ext in extensions.iter() {
335+
if path.ends_with(ext) {
336+
println!("Found extension: {}", ext);
337+
let path = &path[0..path.len() - ext.len()];
338+
let content_type = match *ext {
339+
".json" => Some(ContentType::Json),
340+
".jsonld" => Some(ContentType::JsonLd),
341+
".jsonad" => Some(ContentType::JsonAd),
342+
".html" => Some(ContentType::Html),
343+
".ttl" => Some(ContentType::Turtle),
344+
".nt" => Some(ContentType::NTriples),
345+
".ntriples" => Some(ContentType::NTriples),
346+
_ => None,
347+
};
348+
if let Some(ct) = content_type {
349+
found = Some((ct, path));
350+
}
351+
}
325352
}
326-
None
353+
found
327354
}

0 commit comments

Comments
 (0)