args.rs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // -*- coding: utf-8 -*-
  2. //
  3. // Simple CMS
  4. //
  5. // Copyright (C) 2011-2024 Michael Büsch <m@bues.ch>
  6. //
  7. // Licensed under the Apache License version 2.0
  8. // or the MIT license, at your option.
  9. // SPDX-License-Identifier: Apache-2.0 OR MIT
  10. use crate::{cookie::Cookie, query::Query};
  11. use cms_ident::CheckedIdent;
  12. pub fn html_safe_escape(text: &str) -> String {
  13. html_escape::encode_safe(text).to_string()
  14. }
  15. pub struct CmsGetArgs {
  16. pub _host: String,
  17. pub path: CheckedIdent,
  18. pub _cookie: Cookie,
  19. pub query: Query,
  20. pub https: bool,
  21. }
  22. impl CmsGetArgs {
  23. pub fn protocol_str(&self) -> &str {
  24. if self.https {
  25. "https"
  26. } else {
  27. "http"
  28. }
  29. }
  30. }
  31. pub struct CmsPostArgs {
  32. pub body: Vec<u8>,
  33. pub body_mime: String,
  34. }
  35. pub fn get_query_var(get: &CmsGetArgs, variable_name: &str, escape: bool) -> String {
  36. if let Some(index) = variable_name.find('_') {
  37. let qname = &variable_name[index + 1..];
  38. if !qname.is_empty() {
  39. let qvalue = get.query.get_str(qname).unwrap_or_default();
  40. if escape {
  41. return html_safe_escape(&qvalue);
  42. } else {
  43. return qvalue;
  44. }
  45. }
  46. }
  47. Default::default()
  48. }
  49. // vim: ts=4 sw=4 expandtab