request.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. use std::fmt;
  5. use super::{
  6. header::{HeaderMap, HeaderValue},
  7. method::Method,
  8. };
  9. /// Represents an HTTP request from the WebView.
  10. ///
  11. /// An HTTP request consists of a head and a potentially optional body.
  12. ///
  13. /// ## Platform-specific
  14. ///
  15. /// - **Linux:** Headers are not exposed.
  16. pub struct Request {
  17. head: RequestParts,
  18. body: Vec<u8>,
  19. }
  20. /// Component parts of an HTTP `Request`
  21. ///
  22. /// The HTTP request head consists of a method, uri, and a set of
  23. /// header fields.
  24. #[derive(Clone)]
  25. pub struct RequestParts {
  26. /// The request's method
  27. pub method: Method,
  28. /// The request's URI
  29. pub uri: String,
  30. /// The request's headers
  31. pub headers: HeaderMap<HeaderValue>,
  32. }
  33. impl Request {
  34. /// Creates a new blank `Request` with the body
  35. #[inline]
  36. pub fn new(body: Vec<u8>) -> Request {
  37. Request {
  38. head: RequestParts::new(),
  39. body,
  40. }
  41. }
  42. /// Creates a new `Request` with the given head and body.
  43. ///
  44. /// # Stability
  45. ///
  46. /// This API is used internally. It may have breaking changes in the future.
  47. #[inline]
  48. #[doc(hidden)]
  49. pub fn new_internal(head: RequestParts, body: Vec<u8>) -> Request {
  50. Request { head, body }
  51. }
  52. /// Returns a reference to the associated HTTP method.
  53. #[inline]
  54. pub fn method(&self) -> &Method {
  55. &self.head.method
  56. }
  57. /// Returns a reference to the associated URI.
  58. #[inline]
  59. pub fn uri(&self) -> &str {
  60. &self.head.uri
  61. }
  62. /// Returns a reference to the associated header field map.
  63. #[inline]
  64. pub fn headers(&self) -> &HeaderMap<HeaderValue> {
  65. &self.head.headers
  66. }
  67. /// Returns a reference to the associated HTTP body.
  68. #[inline]
  69. pub fn body(&self) -> &Vec<u8> {
  70. &self.body
  71. }
  72. /// Consumes the request returning the head and body RequestParts.
  73. ///
  74. /// # Stability
  75. ///
  76. /// This API is used internally. It may have breaking changes in the future.
  77. #[inline]
  78. pub fn into_parts(self) -> (RequestParts, Vec<u8>) {
  79. (self.head, self.body)
  80. }
  81. }
  82. impl Default for Request {
  83. fn default() -> Request {
  84. Request::new(Vec::new())
  85. }
  86. }
  87. impl fmt::Debug for Request {
  88. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  89. f.debug_struct("Request")
  90. .field("method", self.method())
  91. .field("uri", &self.uri())
  92. .field("headers", self.headers())
  93. .field("body", self.body())
  94. .finish()
  95. }
  96. }
  97. impl RequestParts {
  98. /// Creates a new default instance of `RequestParts`
  99. fn new() -> RequestParts {
  100. RequestParts {
  101. method: Method::default(),
  102. uri: "".into(),
  103. headers: HeaderMap::default(),
  104. }
  105. }
  106. }
  107. impl fmt::Debug for RequestParts {
  108. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  109. f.debug_struct("Parts")
  110. .field("method", &self.method)
  111. .field("uri", &self.uri)
  112. .field("headers", &self.headers)
  113. .finish()
  114. }
  115. }