From ee01d128388943abf31fb9c9ac4c9bd3eae9e91f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Mon, 27 Dec 2021 18:19:27 +0100 Subject: [PATCH] parsers: http_upload: Make Header an enum as required per the XEP MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- parsers/src/http_upload.rs | 77 ++++++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 19 deletions(-) diff --git a/parsers/src/http_upload.rs b/parsers/src/http_upload.rs index 740904bf..beec863f 100644 --- a/parsers/src/http_upload.rs +++ b/parsers/src/http_upload.rs @@ -4,8 +4,12 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +use std::convert::TryFrom; + use crate::iq::{IqGetPayload, IqResultPayload}; -use crate::util::helpers::Text; +use crate::ns; +use crate::util::error::Error; +use crate::Element; generate_element!( /// Requesting a slot @@ -24,18 +28,55 @@ generate_element!( impl IqGetPayload for SlotRequest {} -generate_element!( - /// Slot header - Header, "header", HTTP_UPLOAD, - attributes: [ - /// Name of the header - name: Required = "name" - ], - text: ( - /// Content of the header - data: Text - ) -); +/// Slot header +#[derive(Debug, Clone, PartialEq)] +pub enum Header { + /// Authorization header + Authorization(String), + + /// Cookie header + Cookie(String), + + /// Expires header + Expires(String), +} + +impl TryFrom for Header { + type Error = Error; + fn try_from(elem: Element) -> Result { + check_self!(elem, "header", HTTP_UPLOAD); + check_no_children!(elem, "header"); + check_no_unknown_attributes!(elem, "header", ["name"]); + let name: String = get_attr!(elem, "name", Required); + let text = String::from(elem.text()); + + Ok(match name.as_str() { + "Authorization" => Header::Authorization(text), + "Cookie" => Header::Cookie(text), + "Expires" => Header::Expires(text), + _ => { + return Err(Error::ParseError( + "Header name must be either 'Authorization', 'Cookie', or 'Expires'.", + )) + } + }) + } +} + +impl From
for Element { + fn from(elem: Header) -> Element { + let (attr, val) = match elem { + Header::Authorization(val) => ("Authorization", val), + Header::Cookie(val) => ("Cookie", val), + Header::Expires(val) => ("Expires", val), + }; + + Element::builder("header", ns::HTTP_UPLOAD) + .attr("name", attr) + .append(val) + .build() + } +} generate_element!( /// Put URL @@ -105,15 +146,13 @@ mod tests { .unwrap(); let slot = SlotResult::try_from(elem).unwrap(); assert_eq!(slot.put.url, String::from("https://upload.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/tr%C3%A8s%20cool.jpg")); - assert_eq!(slot.put.headers[0].name, String::from("Authorization")); assert_eq!( - slot.put.headers[0].data, - String::from("Basic Base64String==") + slot.put.headers[0], + Header::Authorization(String::from("Basic Base64String==")) ); - assert_eq!(slot.put.headers[1].name, String::from("Cookie")); assert_eq!( - slot.put.headers[1].data, - String::from("foo=bar; user=romeo") + slot.put.headers[1], + Header::Cookie(String::from("foo=bar; user=romeo")) ); assert_eq!(slot.get.url, String::from("https://download.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/tr%C3%A8s%20cool.jpg")); }