From 208aa2791d45927bcd2cfe3d11111b26d8f5703c Mon Sep 17 00:00:00 2001 From: gennyble Date: Mon, 16 Jun 2025 22:02:13 -0500 Subject: support xml response type --- README.md | 18 ++++++++++++++---- src/main.rs | 7 +++++++ 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 + +1.1.1.1 +``` \ 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 let body = match requested_type { ContentType::Plain => format!("{client_addr}"), ContentType::Json => format!(r#"{{"ip": "{client_addr}"}}"#), + ContentType::Xml => { + format!("\n{client_addr}") + } }; 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"), } } } -- cgit 1.4.1-3-g733a5