Server: avoids big if let blocks

pull/300/head
Tpt 2 years ago committed by Thomas Tanon
parent 74c565a690
commit 38fdffc147
  1. 323
      server/src/main.rs

@ -244,73 +244,69 @@ fn handle_request(request: &mut Request, store: Store) -> Result<Response, HttpE
configure_and_evaluate_sparql_query(store, &[url_query(request)], None, request) configure_and_evaluate_sparql_query(store, &[url_query(request)], None, request)
} }
("/query", "POST") => { ("/query", "POST") => {
if let Some(content_type) = content_type(request) { let content_type =
if content_type == "application/sparql-query" { content_type(request).ok_or_else(|| bad_request("No Content-Type given"))?;
let mut buffer = String::new(); if content_type == "application/sparql-query" {
request let mut buffer = String::new();
.body_mut() request
.take(MAX_SPARQL_BODY_SIZE) .body_mut()
.read_to_string(&mut buffer) .take(MAX_SPARQL_BODY_SIZE)
.map_err(bad_request)?; .read_to_string(&mut buffer)
configure_and_evaluate_sparql_query( .map_err(bad_request)?;
store, configure_and_evaluate_sparql_query(
&[url_query(request)], store,
Some(buffer), &[url_query(request)],
request, Some(buffer),
) request,
} else if content_type == "application/x-www-form-urlencoded" { )
let mut buffer = Vec::new(); } else if content_type == "application/x-www-form-urlencoded" {
request let mut buffer = Vec::new();
.body_mut() request
.take(MAX_SPARQL_BODY_SIZE) .body_mut()
.read_to_end(&mut buffer) .take(MAX_SPARQL_BODY_SIZE)
.map_err(bad_request)?; .read_to_end(&mut buffer)
configure_and_evaluate_sparql_query( .map_err(bad_request)?;
store, configure_and_evaluate_sparql_query(
&[url_query(request), &buffer], store,
None, &[url_query(request), &buffer],
request, None,
) request,
} else { )
Err(unsupported_media_type(&content_type))
}
} else { } else {
Err(bad_request("No Content-Type given")) Err(unsupported_media_type(&content_type))
} }
} }
("/update", "POST") => { ("/update", "POST") => {
if let Some(content_type) = content_type(request) { let content_type =
if content_type == "application/sparql-update" { content_type(request).ok_or_else(|| bad_request("No Content-Type given"))?;
let mut buffer = String::new(); if content_type == "application/sparql-update" {
request let mut buffer = String::new();
.body_mut() request
.take(MAX_SPARQL_BODY_SIZE) .body_mut()
.read_to_string(&mut buffer) .take(MAX_SPARQL_BODY_SIZE)
.map_err(bad_request)?; .read_to_string(&mut buffer)
configure_and_evaluate_sparql_update( .map_err(bad_request)?;
store, configure_and_evaluate_sparql_update(
&[url_query(request)], store,
Some(buffer), &[url_query(request)],
request, Some(buffer),
) request,
} else if content_type == "application/x-www-form-urlencoded" { )
let mut buffer = Vec::new(); } else if content_type == "application/x-www-form-urlencoded" {
request let mut buffer = Vec::new();
.body_mut() request
.take(MAX_SPARQL_BODY_SIZE) .body_mut()
.read_to_end(&mut buffer) .take(MAX_SPARQL_BODY_SIZE)
.map_err(bad_request)?; .read_to_end(&mut buffer)
configure_and_evaluate_sparql_update( .map_err(bad_request)?;
store, configure_and_evaluate_sparql_update(
&[url_query(request), &buffer], store,
None, &[url_query(request), &buffer],
request, None,
) request,
} else { )
return Err(unsupported_media_type(&content_type));
}
} else { } else {
Err(bad_request("No Content-Type given")) return Err(unsupported_media_type(&content_type));
} }
} }
(path, "GET") if path.starts_with("/store") => { (path, "GET") if path.starts_with("/store") => {
@ -364,59 +360,55 @@ fn handle_request(request: &mut Request, store: Store) -> Result<Response, HttpE
} }
} }
(path, "PUT") if path.starts_with("/store") => { (path, "PUT") if path.starts_with("/store") => {
if let Some(content_type) = content_type(request) { let content_type =
if let Some(target) = store_target(request)? { content_type(request).ok_or_else(|| bad_request("No Content-Type given"))?;
if let Some(format) = GraphFormat::from_media_type(&content_type) { if let Some(target) = store_target(request)? {
let new = !match &target { let format = GraphFormat::from_media_type(&content_type)
NamedGraphName::NamedNode(target) => { .ok_or_else(|| unsupported_media_type(&content_type))?;
if store let new = !match &target {
.contains_named_graph(target) NamedGraphName::NamedNode(target) => {
.map_err(internal_server_error)? if store
{ .contains_named_graph(target)
store.clear_graph(target).map_err(internal_server_error)?; .map_err(internal_server_error)?
true {
} else { store.clear_graph(target).map_err(internal_server_error)?;
store true
.insert_named_graph(target)
.map_err(internal_server_error)?;
false
}
}
NamedGraphName::DefaultGraph => {
store
.clear_graph(GraphNameRef::DefaultGraph)
.map_err(internal_server_error)?;
true
}
};
store
.load_graph(
BufReader::new(request.body_mut()),
format,
GraphName::from(target).as_ref(),
None,
)
.map_err(bad_request)?;
Ok(Response::builder(if new {
Status::CREATED
} else { } else {
Status::NO_CONTENT store
}) .insert_named_graph(target)
.build()) .map_err(internal_server_error)?;
} else { false
Err(unsupported_media_type(&content_type)) }
} }
} else if let Some(format) = DatasetFormat::from_media_type(&content_type) { NamedGraphName::DefaultGraph => {
store.clear().map_err(internal_server_error)?; store
store .clear_graph(GraphNameRef::DefaultGraph)
.load_dataset(BufReader::new(request.body_mut()), format, None) .map_err(internal_server_error)?;
.map_err(internal_server_error)?; true
Ok(Response::builder(Status::NO_CONTENT).build()) }
};
store
.load_graph(
BufReader::new(request.body_mut()),
format,
GraphName::from(target).as_ref(),
None,
)
.map_err(bad_request)?;
Ok(Response::builder(if new {
Status::CREATED
} else { } else {
Err(unsupported_media_type(&content_type)) Status::NO_CONTENT
} })
.build())
} else { } else {
Err(bad_request("No Content-Type given")) let format = DatasetFormat::from_media_type(&content_type)
.ok_or_else(|| unsupported_media_type(&content_type))?;
store.clear().map_err(internal_server_error)?;
store
.load_dataset(BufReader::new(request.body_mut()), format, None)
.map_err(internal_server_error)?;
Ok(Response::builder(Status::NO_CONTENT).build())
} }
} }
(path, "DELETE") if path.starts_with("/store") => { (path, "DELETE") if path.starts_with("/store") => {
@ -447,47 +439,44 @@ fn handle_request(request: &mut Request, store: Store) -> Result<Response, HttpE
Ok(Response::builder(Status::NO_CONTENT).build()) Ok(Response::builder(Status::NO_CONTENT).build())
} }
(path, "POST") if path.starts_with("/store") => { (path, "POST") if path.starts_with("/store") => {
if let Some(content_type) = content_type(request) { let content_type =
if let Some(target) = store_target(request)? { content_type(request).ok_or_else(|| bad_request("No Content-Type given"))?;
if let Some(format) = GraphFormat::from_media_type(&content_type) { if let Some(target) = store_target(request)? {
let new = assert_that_graph_exists(&store, &target).is_ok(); if let Some(format) = GraphFormat::from_media_type(&content_type) {
store let new = assert_that_graph_exists(&store, &target).is_ok();
.load_graph(
BufReader::new(request.body_mut()),
format,
GraphName::from(target).as_ref(),
None,
)
.map_err(bad_request)?;
Ok(Response::builder(if new {
Status::CREATED
} else {
Status::NO_CONTENT
})
.build())
} else {
Err(unsupported_media_type(&content_type))
}
} else if let Some(format) = DatasetFormat::from_media_type(&content_type) {
store
.load_dataset(BufReader::new(request.body_mut()), format, None)
.map_err(bad_request)?;
Ok(Response::builder(Status::NO_CONTENT).build())
} else if let Some(format) = GraphFormat::from_media_type(&content_type) {
let graph =
resolve_with_base(request, &format!("/store/{:x}", random::<u128>()))?;
store store
.load_graph(BufReader::new(request.body_mut()), format, &graph, None) .load_graph(
BufReader::new(request.body_mut()),
format,
GraphName::from(target).as_ref(),
None,
)
.map_err(bad_request)?; .map_err(bad_request)?;
Ok(Response::builder(Status::CREATED) Ok(Response::builder(if new {
.with_header(HeaderName::LOCATION, graph.into_string()) Status::CREATED
.unwrap() } else {
.build()) Status::NO_CONTENT
})
.build())
} else { } else {
Err(unsupported_media_type(&content_type)) Err(unsupported_media_type(&content_type))
} }
} else if let Some(format) = DatasetFormat::from_media_type(&content_type) {
store
.load_dataset(BufReader::new(request.body_mut()), format, None)
.map_err(bad_request)?;
Ok(Response::builder(Status::NO_CONTENT).build())
} else if let Some(format) = GraphFormat::from_media_type(&content_type) {
let graph = resolve_with_base(request, &format!("/store/{:x}", random::<u128>()))?;
store
.load_graph(BufReader::new(request.body_mut()), format, &graph, None)
.map_err(bad_request)?;
Ok(Response::builder(Status::CREATED)
.with_header(HeaderName::LOCATION, graph.into_string())
.unwrap()
.build())
} else { } else {
Err(bad_request("No Content-Type given")) Err(unsupported_media_type(&content_type))
} }
} }
(path, "HEAD") if path.starts_with("/store") => { (path, "HEAD") if path.starts_with("/store") => {
@ -553,18 +542,15 @@ fn configure_and_evaluate_sparql_query(
} }
} }
} }
if let Some(query) = query { let query = query.ok_or_else(|| bad_request("You should set the 'query' parameter"))?;
evaluate_sparql_query( evaluate_sparql_query(
store, store,
query, query,
use_default_graph_as_union, use_default_graph_as_union,
default_graph_uris, default_graph_uris,
named_graph_uris, named_graph_uris,
request, request,
) )
} else {
Err(bad_request("You should set the 'query' parameter"))
}
} }
fn evaluate_sparql_query( fn evaluate_sparql_query(
@ -685,18 +671,15 @@ fn configure_and_evaluate_sparql_update(
} }
} }
} }
if let Some(update) = update { let update = update.ok_or_else(|| bad_request("You should set the 'update' parameter"))?;
evaluate_sparql_update( evaluate_sparql_update(
store, store,
update, update,
use_default_graph_as_union, use_default_graph_as_union,
default_graph_uris, default_graph_uris,
named_graph_uris, named_graph_uris,
request, request,
) )
} else {
Err(bad_request("You should set the 'update' parameter"))
}
} }
fn evaluate_sparql_update( fn evaluate_sparql_update(

Loading…
Cancel
Save