Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 153 additions & 28 deletions crates/engine/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ use chrono::Utc;
use crate::Engine;
use crate::db::{self, lock};
use crate::error::{AppError, FileContext};
use crate::logged::written;
use crate::sensitive::Sensitive;
use crate::tools::off_thread;
use collection_run::{RunStep, run_step, say_run, wait};
use flight::Flight;
Expand Down Expand Up @@ -61,7 +63,9 @@ pub fn create_http_collection(engine: &Engine, name: &str) -> Result<HttpCollect

let mut connection = lock(db)?;

Ok(store::create_collection(&mut connection, &name, Utc::now())?)
let collection = store::create_collection(&mut connection, &name, Utc::now())?;
log::info!("created collection {} {}", collection.id, Sensitive(&collection.name));
Ok(collection)
}

/// `parent_id` `None` puts it at the collection's root.
Expand All @@ -76,13 +80,15 @@ pub fn create_http_folder(

let mut connection = lock(db)?;

Ok(store::create_folder(
let folder = store::create_folder(
&mut connection,
collection_id,
parent_id.map(std::string::String::as_str),
&name,
Utc::now(),
)?)
)?;
log::info!("created folder {} {} in collection {collection_id}", folder.id, Sensitive(&folder.name));
Ok(folder)
}

pub fn create_http_request(engine: &Engine, draft: &HttpRequestDraft) -> Result<HttpRequest, AppError> {
Expand All @@ -91,7 +97,15 @@ pub fn create_http_request(engine: &Engine, draft: &HttpRequestDraft) -> Result<

let mut connection = lock(db)?;

Ok(store::create_request(&mut connection, draft, &name, Utc::now())?)
let request = store::create_request(&mut connection, draft, &name, Utc::now())?;
log_created(&request);
Ok(request)
}

fn log_created(request: &HttpRequest) {
let (id, method, name) = (&request.id, request.method, Sensitive(&request.name));
let url = Sensitive(&request.document.url);
log::info!("created request {id} {method} {name} in collection {}: url {url}", request.collection_id);
}

/// The first save of a request where nothing holds it yet: what `create` names is created
Expand All @@ -108,14 +122,16 @@ pub fn create_http_request_in(

let mut connection = lock(db)?;

Ok(store::create_request_in(
let request = store::create_request_in(
&mut connection,
draft,
&name,
collection.as_deref(),
folder.as_deref(),
Utc::now(),
)?)
)?;
log_created(&request);
Ok(request)
}

pub fn get_http_request(engine: &Engine, id: &str) -> Result<HttpRequest, AppError> {
Expand All @@ -135,7 +151,38 @@ pub fn save_http_request(

let mut connection = lock(db)?;

Ok(store::save_request(&mut connection, id, patch, name.as_deref(), Utc::now())?)
let request = store::save_request(&mut connection, id, patch, name.as_deref(), Utc::now())?;
log::info!("saved request {id}: {}", saved(patch));
Ok(request)
}

/// The parts a patch sets: the name, the method, the URL; the rest of the document by its counts.
fn saved(patch: &HttpRequestPatch) -> String {
let mut parts = Vec::new();
if let Some(name) = &patch.name {
parts.push(format!("name {}", Sensitive(name)));
}
if let Some(method) = patch.method {
parts.push(format!("method {method}"));
}
if let Some(document) = &patch.document {
let (params, headers) = (document.params.len(), document.headers.len());
let url = Sensitive(&document.url);
parts.push(format!("url {url}, {params} params, {headers} headers, body and scripts"));
}
if parts.is_empty() {
return "nothing".to_owned();
}
parts.join(", ")
}

/// How an item is named in a line.
fn kind_of(item: &HttpItem) -> &'static str {
match item.kind {
model::HttpItemKind::Collection => "collection",
model::HttpItemKind::Folder => "folder",
model::HttpItemKind::Request => "request",
}
}

pub fn rename_http_item(engine: &Engine, item: &HttpItem, name: &str) -> Result<(), AppError> {
Expand All @@ -144,7 +191,9 @@ pub fn rename_http_item(engine: &Engine, item: &HttpItem, name: &str) -> Result<

let mut connection = lock(db)?;

Ok(store::rename(&mut connection, item, &name)?)
store::rename(&mut connection, item, &name)?;
log::info!("{} {} renamed {}", kind_of(item), item.id, Sensitive(&name));
Ok(())
}

/// What a deletion would take with it, for the confirmation that comes first.
Expand All @@ -159,7 +208,9 @@ pub fn delete_http_item(engine: &Engine, item: &HttpItem) -> Result<(), AppError
let db = &engine.db;
let mut connection = lock(db)?;

Ok(store::delete(&mut connection, item)?)
store::delete(&mut connection, item)?;
log::info!("{} {} deleted", kind_of(item), item.id);
Ok(())
}

/// The copy's name comes from the front: no user-visible word is Rust's.
Expand All @@ -169,14 +220,18 @@ pub fn duplicate_http_item(engine: &Engine, item: &HttpItem, name: &str) -> Resu

let mut connection = lock(db)?;

Ok(store::duplicate(&mut connection, item, &name, Utc::now())?)
let copy = store::duplicate(&mut connection, item, &name, Utc::now())?;
log::info!("{} {} duplicated into {} {}", kind_of(item), item.id, copy.id, Sensitive(&name));
Ok(copy)
}

pub fn move_http_item(engine: &Engine, item: &HttpItem, place: &HttpPlace) -> Result<(), AppError> {
let db = &engine.db;
let mut connection = lock(db)?;

Ok(store::move_item(&mut connection, item, place)?)
store::move_item(&mut connection, item, place)?;
log::info!("{} {} moved", kind_of(item), item.id);
Ok(())
}

pub fn http_settings(engine: &Engine, item: &HttpItem) -> Result<model::ContainerSettings, AppError> {
Expand Down Expand Up @@ -212,7 +267,10 @@ pub fn save_http_settings(
let settings =
model::ContainerSettings { run_order: store::settings(&mut connection, item)?.run_order, ..settings };

Ok(store::save_settings(&mut connection, item, &settings)?)
store::save_settings(&mut connection, item, &settings)?;
let (headers, variables) = (settings.headers.len(), settings.variables.len());
log::info!("settings of {} {} saved: {headers} headers, {variables} variables", kind_of(item), item.id);
Ok(())
}

/// What a request placed there inherits, and from where: « héritée de la collection … ».
Expand Down Expand Up @@ -278,7 +336,9 @@ pub fn reorder_http_collection(engine: &Engine, id: &str, index: u32) -> Result<
let db = &engine.db;
let mut connection = lock(db)?;

Ok(store::reorder_collection(&mut connection, id, index)?)
store::reorder_collection(&mut connection, id, index)?;
log::info!("collection {id} moved to {index}");
Ok(())
}

/// What it inherits is read under the lock, its files outside it, and the send waits on nothing
Expand All @@ -287,9 +347,21 @@ pub async fn send_http_request(
engine: Arc<Engine>,
request: send::SendRequest,
) -> Result<send::SentResponse, AppError> {
let sent = format!(
"{} {} {}",
request.request_id.as_deref().unwrap_or("a draft"),
request.method,
Sensitive(&request.document.url)
);
let mut flight = Flight::new(engine, &request.id);
let answer = flight.send(request).await;
flight.keep().await;
match &answer {
Ok(response) => {
log::info!("sent {sent}: {} in {} ms, {} bytes", response.status, response.millis, response.size);
}
Err(error) => log::info!("sent {sent}: {:?}", error.code),
}
answer
}

Expand Down Expand Up @@ -418,7 +490,9 @@ pub fn save_http_run_order(
let mut connection = lock(db)?;
let settings = model::ContainerSettings { run_order: order, ..store::settings(&mut connection, item)? };

Ok(store::save_settings(&mut connection, item, &settings)?)
store::save_settings(&mut connection, item, &settings)?;
log::info!("run order of {} {} saved: {} requests", kind_of(item), item.id, settings.run_order.len());
Ok(())
}

/// A collection or a folder run, one request after the other, each sent as a send is — its
Expand Down Expand Up @@ -512,9 +586,13 @@ pub fn cancel_http_send(engine: &Engine, id: &str) -> Result<bool, AppError> {
/// The whole body of the last answer to `id`, not the text shown: `false` when there is none.
pub async fn save_http_response(engine: Arc<Engine>, id: String, path: String) -> Result<bool, AppError> {
crate::transfer::model::validate_path(&path)?;
off_thread(move || engine.sending.save(&id, Path::new(&path)).context(&path))
.await?
.map_err(Into::into)
let saved = off_thread(move || {
let saved = engine.sending.save(&id, Path::new(&path)).context(&path)?;
log::info!("answer of {id} saved to {}", Sensitive(&path));
Ok::<_, crate::error::StorageError>(saved)
})
.await??;
Ok(saved)
}

/// A closed tab lets its last body go.
Expand Down Expand Up @@ -565,22 +643,28 @@ pub fn clear_http_history(engine: &Engine) -> Result<u32, AppError> {
let db = &engine.db;
let mut connection = lock(db)?;

Ok(history::store::clear(&mut connection)?)
let cleared = history::store::clear(&mut connection)?;
log::info!("history cleared: {cleared} sends");
Ok(cleared)
}

/// One entry gone, answered whole: its undo hands it back to `restore_http_history_entry`.
pub fn delete_http_history_entry(engine: &Engine, id: &str) -> Result<history::HistoryEntry, AppError> {
let db = &engine.db;
let mut connection = lock(db)?;

Ok(history::store::delete(&mut connection, id)?)
let entry = history::store::delete(&mut connection, id)?;
log::info!("history entry {id} deleted");
Ok(entry)
}

pub fn restore_http_history_entry(engine: &Engine, entry: &history::HistoryEntry) -> Result<(), AppError> {
let db = &engine.db;
let mut connection = lock(db)?;

Ok(history::store::restore(&mut connection, entry)?)
history::store::restore(&mut connection, entry)?;
log::info!("history entry {} put back", entry.item.id);
Ok(())
}

/// A send put back onto the request it came from, as changes to save or not, the request's own
Expand Down Expand Up @@ -679,14 +763,18 @@ pub fn delete_http_cookie(engine: &Engine, id: &str) -> Result<u32, AppError> {
let db = &engine.db;
let mut connection = lock(db)?;

Ok(cookies::store::delete(&mut connection, id)?)
let deleted = cookies::store::delete(&mut connection, id)?;
log::info!("cookie {id} deleted");
Ok(deleted)
}

pub fn delete_http_cookie_domain(engine: &Engine, domain: &str) -> Result<u32, AppError> {
let db = &engine.db;
let mut connection = lock(db)?;

Ok(cookies::store::delete_domain(&mut connection, domain)?)
let deleted = cookies::store::delete_domain(&mut connection, domain)?;
log::info!("cookies of {} deleted: {deleted}", Sensitive(domain));
Ok(deleted)
}

/// What « Vider le bocal » says it removes.
Expand All @@ -701,7 +789,9 @@ pub fn clear_http_cookies(engine: &Engine) -> Result<u32, AppError> {
let db = &engine.db;
let mut connection = lock(db)?;

Ok(cookies::store::clear(&mut connection)?)
let cleared = cookies::store::clear(&mut connection)?;
log::info!("cookie jar emptied: {cleared} cookies");
Ok(cleared)
}

/// The globals and every environment, in their order.
Expand All @@ -718,7 +808,9 @@ pub fn create_http_environment(engine: &Engine, name: &str) -> Result<environmen

let mut connection = lock(db)?;

Ok(environments::store::create(&mut connection, &name, Utc::now())?)
let environment = environments::store::create(&mut connection, &name, Utc::now())?;
log::info!("created environment {} {}", environment.id, Sensitive(&environment.name));
Ok(environment)
}

pub fn save_http_environment(
Expand All @@ -731,7 +823,34 @@ pub fn save_http_environment(

let mut connection = lock(db)?;

Ok(environments::store::save(&mut connection, id, patch, name.as_deref())?)
let environment = environments::store::save(&mut connection, id, patch, name.as_deref())?;
log::info!("saved environment {id}: {}", environment_saved(patch));
Ok(environment)
}

/// The parts an environment's patch sets, its variables by name and value through `Sensitive`.
fn environment_saved(patch: &environments::EnvironmentPatch) -> String {
let mut parts = Vec::new();
if let Some(name) = &patch.name {
parts.push(format!("name {}", Sensitive(name)));
}
if let Some(colour) = patch.colour {
parts.push(format!("colour {colour}"));
}
if let Some(protected) = patch.protected {
parts.push(format!("protected {protected}"));
}
if let Some(variables) = &patch.variables {
parts.push(format!("{} variables {}", variables.len(), variables_written(variables)));
}
if parts.is_empty() {
return "nothing".to_owned();
}
parts.join(", ")
}

fn variables_written(variables: &[environments::Variable]) -> String {
written(variables.iter().map(|variable| format!("{}={}", variable.name, variable.value)))
}

pub fn save_http_globals(
Expand All @@ -741,7 +860,9 @@ pub fn save_http_globals(
let db = &engine.db;
let mut connection = lock(db)?;

Ok(environments::store::save_globals(&mut connection, variables, Utc::now())?)
let saved = environments::store::save_globals(&mut connection, variables, Utc::now())?;
log::info!("globals saved: {} variables {}", saved.len(), variables_written(&saved));
Ok(saved)
}

/// The copy's name comes from the front: no user-visible word is Rust's.
Expand All @@ -755,12 +876,16 @@ pub fn duplicate_http_environment(

let mut connection = lock(db)?;

Ok(environments::store::duplicate(&mut connection, id, &name, Utc::now())?)
let copy = environments::store::duplicate(&mut connection, id, &name, Utc::now())?;
log::info!("environment {id} duplicated into {} {}", copy.id, Sensitive(&copy.name));
Ok(copy)
}

pub fn delete_http_environment(engine: &Engine, id: &str) -> Result<(), AppError> {
let db = &engine.db;
let mut connection = lock(db)?;

Ok(environments::store::delete(&mut connection, id)?)
environments::store::delete(&mut connection, id)?;
log::info!("environment {id} deleted");
Ok(())
}
Loading
Loading