about summary refs log tree commit diff
path: root/src/main.rs
diff options
context:
space:
mode:
authorgennyble <gen@nyble.dev>2025-06-16 22:02:13 -0500
committergennyble <gen@nyble.dev>2025-06-16 22:02:13 -0500
commit208aa2791d45927bcd2cfe3d11111b26d8f5703c (patch)
treefb8b808dc39032343a0951b3b19455bbb9c65f46 /src/main.rs
parentdf11f96162f89c740aba87d62e4512cfc46049bb (diff)
downloadpiper-main.tar.gz
piper-main.zip
support xml response type main
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs7
1 files changed, 7 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs
index 3859754..2d1d20a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,6 +2,7 @@ use core::fmt;
 use std::{
 	borrow::Cow,
 	collections::HashMap,
+	fmt::format,
 	io::{self, BufRead, BufReader, BufWriter, Write},
 	net::{IpAddr, SocketAddr},
 	path::PathBuf,
@@ -183,6 +184,7 @@ async fn form_response<'req>(
 		Err(e) => return e,
 		Ok(None) | Ok(Some("text/plain")) => ContentType::Plain,
 		Ok(Some("application/json")) => ContentType::Json,
+		Ok(Some("application/xml")) | Ok(Some("text/xml")) => ContentType::Xml,
 		Ok(Some(_)) => return error(415, "unsupported content-type"),
 	};
 
@@ -266,6 +268,9 @@ fn make_ip_response(requested_type: ContentType, response: Response) -> Vec<u8>
 	let body = match requested_type {
 		ContentType::Plain => format!("{client_addr}"),
 		ContentType::Json => format!(r#"{{"ip": "{client_addr}"}}"#),
+		ContentType::Xml => {
+			format!("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<ip>{client_addr}</ip>")
+		}
 	};
 	let length = body.len();
 
@@ -457,6 +462,7 @@ impl Database {
 enum ContentType {
 	Plain,
 	Json,
+	Xml,
 }
 
 impl fmt::Display for ContentType {
@@ -464,6 +470,7 @@ impl fmt::Display for ContentType {
 		match self {
 			ContentType::Plain => write!(f, "text/plain"),
 			ContentType::Json => write!(f, "application/json"),
+			ContentType::Xml => write!(f, "application/xml"),
 		}
 	}
 }