about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--README.md18
-rw-r--r--src/main.rs7
2 files changed, 21 insertions, 4 deletions
diff --git a/README.md b/README.md
index d925d8d..f49d182 100644
--- a/README.md
+++ b/README.md
@@ -23,11 +23,21 @@ A `200` status code will be returned, with an IP in the body, if:
 A `302` status code will be returned, with no body, if:  
 - The last request to use the auth value comes from the same IP address
 
-You may specify a `Content-Type` of `application/json` or `text/plain`. Example
-responses are below:
+There are multiple `Content-Type`s supported. Below is a list of types and
+example responses. If you do not provide a `Content-Type`, plaintext is assumed.
 
 **`application/json`**  
-`{"ip": "127.0.0.1"}`
+```json
+{"ip": "127.0.0.1"}
+```
 
 **`text/plain`**  
-`127.0.0.1`
\ No newline at end of file
+```
+127.0.0.1
+```
+
+**`application/xml`**  
+```xml
+<?xml version="1.0" encoding="utf-8"?>
+<ip>1.1.1.1</ip>
+```
\ No newline at end of file
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"),
 		}
 	}
 }