[wip] Not sure exactly what's happening, but the POST requests are returning 200

This commit is contained in:
Adam Cooper 2022-03-06 18:45:16 -05:00
parent 7966f61894
commit d9e0a55a84
3 changed files with 30 additions and 19 deletions

1
Cargo.lock generated
View File

@ -586,6 +586,7 @@ dependencies = [
"log", "log",
"reqwest", "reqwest",
"tokio", "tokio",
"url",
"yaserde", "yaserde",
"yaserde_derive", "yaserde_derive",
] ]

View File

@ -12,5 +12,6 @@ env_logger = "0.8.4"
log = "0.4.0" log = "0.4.0"
reqwest = { version = "0.11", features = ["json"] } reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["full"] }
url = "2.2.2"
yaserde = "0.7.1" yaserde = "0.7.1"
yaserde_derive = "0.7.1" yaserde_derive = "0.7.1"

View File

@ -6,13 +6,12 @@ use reqwest;
use std::collections::HashMap; use std::collections::HashMap;
#[allow(unused)] #[allow(unused)]
use std::io::{self, BufReader, Write}; use std::io::{self, BufReader, Write};
use std::path::PathBuf;
use url::{Url, ParseError};
#[allow(unused)] #[allow(unused)]
use yaserde_derive::{YaDeserialize, YaSerialize}; use yaserde_derive::{YaDeserialize, YaSerialize};
use yaserde::de::from_str; use yaserde::de::from_str;
#[allow(unused)]
fn publicise() {}
#[derive(Default, PartialEq, Debug, YaDeserialize)] #[derive(Default, PartialEq, Debug, YaDeserialize)]
#[yaserde( #[yaserde(
rename = "multistatus", rename = "multistatus",
@ -90,13 +89,13 @@ pub struct Collection {
collection: String, collection: String,
} }
async fn get_folder_contents(url_tail: &String, password: &String) -> Result<String, Box<dyn std::error::Error>> { async fn get_folder_contents(url_tail: &str, password: &str) -> Result<String, Box<dyn std::error::Error>> {
debug!("Entering: get_folder_contents()"); debug!("Entering: get_folder_contents()");
let root_url = "https://theadamcooper.com"; let root_url = Url::parse("https://theadamcooper.com")?;
let method = reqwest::Method::from_bytes(b"PROPFIND").unwrap(); let method = reqwest::Method::from_bytes(b"PROPFIND").unwrap();
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let url = format!("{}{}", root_url, url_tail); let url = root_url.join(url_tail)?;
debug!("url: {}", url); debug!("get_folder_contents: url: {}", url);
let body = String::from( let body = String::from(
r#"<?xml version="1.0" encoding="UTF-8"?> r#"<?xml version="1.0" encoding="UTF-8"?>
<d:propfind xmlns:d="DAV:"> <d:propfind xmlns:d="DAV:">
@ -119,17 +118,18 @@ async fn get_folder_contents(url_tail: &String, password: &String) -> Result<Str
.await? .await?
.text() .text()
.await?; .await?;
debug!("{:?}", response_text); debug!("get_folder_contents: response_text: {:?}", response_text);
Ok(response_text) Ok(response_text)
} }
async fn publicise_it(path: &String, password: &String) -> Result<bool, Box<dyn std::error::Error>> { async fn publicise_it(path: &str, password: &str) -> Result<bool, Box<dyn std::error::Error>> {
debug!("Entering: publicise()"); debug!("Entering: publicise()");
let url = "https://theadamcooper.com/nextcloud/ocs/v2.php/apps/files_sharing/api/v1/shares"; let url = "https://theadamcooper.com/nextcloud/ocs/v2.php/apps/files_sharing/api/v1/shares";
let method = reqwest::Method::POST; let method = reqwest::Method::POST;
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let params = [("path", path.as_str()), ("shareType", "3")]; let params = [("path", path), ("shareType", "3")];
debug!("url: {}", url); debug!("publicise_it: url: {}", url);
debug!("params: {:?}", params);
let response_text = client let response_text = client
.request(method, url) .request(method, url)
.basic_auth("adam", Some(password)) .basic_auth("adam", Some(password))
@ -139,11 +139,11 @@ async fn publicise_it(path: &String, password: &String) -> Result<bool, Box<dyn
.await? .await?
.text() .text()
.await?; .await?;
debug!("{:?}", response_text); debug!("publicise_it: response_text: {:?}", response_text);
Ok(true) Ok(true)
} }
async fn traverse(mut result: Multistatus, password: &String) -> Result<bool, Box<dyn std::error::Error>> { async fn traverse(mut result: Multistatus, password: &str) -> Result<bool, Box<dyn std::error::Error>> {
debug!("Entering: traverse()"); debug!("Entering: traverse()");
// Initialize the indexed "pointer" // Initialize the indexed "pointer"
let mut current_index: usize = 0; let mut current_index: usize = 0;
@ -170,7 +170,7 @@ async fn traverse(mut result: Multistatus, password: &String) -> Result<bool, Bo
) )
.await .await
.unwrap(); .unwrap();
debug!("{:?}", folder_contents); // debug!("{:?}", folder_contents);
// Parse the contents XML into Multistatus // Parse the contents XML into Multistatus
let mut new_result: Multistatus = from_str(&String::from(folder_contents)).unwrap(); let mut new_result: Multistatus = from_str(&String::from(folder_contents)).unwrap();
debug!("\nParsed:\n{:?}", new_result); debug!("\nParsed:\n{:?}", new_result);
@ -181,7 +181,16 @@ async fn traverse(mut result: Multistatus, password: &String) -> Result<bool, Bo
// else it's a node. if it's not public, publicise it. // else it's a node. if it's not public, publicise it.
if !(&mut result.response[current_index].propstat[0].prop.share_types).contains(&ShareType{ share_type: 3 }) { if !(&mut result.response[current_index].propstat[0].prop.share_types).contains(&ShareType{ share_type: 3 }) {
println!("it's not public"); println!("it's not public");
publicise_it(&result.response[current_index].href, password).await.unwrap(); // Search for username and lop.
let username = "adam";
let username_seg_string = format!("/{}/", username);
let username_seg = username_seg_string.as_str();
let index = &result.response[current_index].href.find(username_seg).unwrap_or(0);
let new_index = index + username.len() + 2;
let new_href = &result.response[current_index].href[new_index..];
publicise_it(new_href, password).await.unwrap();
} else {
println!("it's already public");
} }
} }
visited_items.insert(etag, true); visited_items.insert(etag, true);
@ -217,19 +226,19 @@ async fn main() -> std::io::Result<()> {
env_logger::Builder::from_env(Env::default().default_filter_or("trace")) env_logger::Builder::from_env(Env::default().default_filter_or("trace"))
.target(Target::Stdout) .target(Target::Stdout)
.init(); .init();
println!("Publicise it!"); println!("Publicise it!\n\n");
let password: String = get_password().unwrap().trim().to_string(); let password: String = get_password().unwrap().trim().to_string();
let folder_contents: String = let folder_contents: String =
// get_folder_contents("/nextcloud/remote.php/dav/files/adam/test_public/2019_test_public") // get_folder_contents("/nextcloud/remote.php/dav/files/adam/test_public/2019_test_public")
get_folder_contents(&String::from("/nextcloud/remote.php/dav/files/adam/test_public"), &password) get_folder_contents("/nextcloud/remote.php/dav/files/adam/test_public", &password)
.await .await
.unwrap(); .unwrap();
debug!("{:?}", folder_contents); // debug!("{:?}", folder_contents);
let mut result: Multistatus = from_str(&folder_contents).unwrap(); let mut result: Multistatus = from_str(&folder_contents).unwrap();
println!("{:?}", result); println!("{:?}", result);
let result: bool = traverse(result, &password).await.unwrap(); let _ = traverse(result, &password).await.unwrap();
Ok(()) Ok(())
} }