fastcgi_client/
params.rs

1// Copyright 2022 jmjoy
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::{
16    borrow::Cow,
17    collections::HashMap,
18    ops::{Deref, DerefMut},
19};
20
21/// Fastcgi params, please reference to nginx-php-fpm fastcgi_params.
22#[derive(Debug, Clone, PartialEq, Eq)]
23pub struct Params<'a>(HashMap<Cow<'a, str>, Cow<'a, str>>);
24
25impl<'a> Params<'a> {
26    #[inline]
27    pub fn gateway_interface<S: Into<Cow<'a, str>>>(mut self, gateway_interface: S) -> Self {
28        self.insert("GATEWAY_INTERFACE".into(), gateway_interface.into());
29        self
30    }
31
32    #[inline]
33    pub fn server_software<S: Into<Cow<'a, str>>>(mut self, server_software: S) -> Self {
34        self.insert("SERVER_SOFTWARE".into(), server_software.into());
35        self
36    }
37
38    #[inline]
39    pub fn server_protocol<S: Into<Cow<'a, str>>>(mut self, server_protocol: S) -> Self {
40        self.insert("SERVER_PROTOCOL".into(), server_protocol.into());
41        self
42    }
43
44    #[inline]
45    pub fn request_method<S: Into<Cow<'a, str>>>(mut self, request_method: S) -> Self {
46        self.insert("REQUEST_METHOD".into(), request_method.into());
47        self
48    }
49
50    #[inline]
51    pub fn script_filename<S: Into<Cow<'a, str>>>(mut self, script_filename: S) -> Self {
52        self.insert("SCRIPT_FILENAME".into(), script_filename.into());
53        self
54    }
55
56    #[inline]
57    pub fn script_name<S: Into<Cow<'a, str>>>(mut self, script_name: S) -> Self {
58        self.insert("SCRIPT_NAME".into(), script_name.into());
59        self
60    }
61
62    #[inline]
63    pub fn query_string<S: Into<Cow<'a, str>>>(mut self, query_string: S) -> Self {
64        self.insert("QUERY_STRING".into(), query_string.into());
65        self
66    }
67
68    #[inline]
69    pub fn request_uri<S: Into<Cow<'a, str>>>(mut self, request_uri: S) -> Self {
70        self.insert("REQUEST_URI".into(), request_uri.into());
71        self
72    }
73
74    #[inline]
75    pub fn document_root<S: Into<Cow<'a, str>>>(mut self, document_root: S) -> Self {
76        self.insert("DOCUMENT_ROOT".into(), document_root.into());
77        self
78    }
79
80    #[inline]
81    pub fn document_uri<S: Into<Cow<'a, str>>>(mut self, document_uri: S) -> Self {
82        self.insert("DOCUMENT_URI".into(), document_uri.into());
83        self
84    }
85
86    #[inline]
87    pub fn remote_addr<S: Into<Cow<'a, str>>>(mut self, remote_addr: S) -> Self {
88        self.insert("REMOTE_ADDR".into(), remote_addr.into());
89        self
90    }
91
92    #[inline]
93    pub fn remote_port(mut self, remote_port: u16) -> Self {
94        self.insert("REMOTE_PORT".into(), remote_port.to_string().into());
95        self
96    }
97
98    #[inline]
99    pub fn server_addr<S: Into<Cow<'a, str>>>(mut self, server_addr: S) -> Self {
100        self.insert("SERVER_ADDR".into(), server_addr.into());
101        self
102    }
103
104    #[inline]
105    pub fn server_port(mut self, server_port: u16) -> Self {
106        self.insert("SERVER_PORT".into(), server_port.to_string().into());
107        self
108    }
109
110    #[inline]
111    pub fn server_name<S: Into<Cow<'a, str>>>(mut self, server_name: S) -> Self {
112        self.insert("SERVER_NAME".into(), server_name.into());
113        self
114    }
115
116    #[inline]
117    pub fn content_type<S: Into<Cow<'a, str>>>(mut self, content_type: S) -> Self {
118        self.insert("CONTENT_TYPE".into(), content_type.into());
119        self
120    }
121
122    #[inline]
123    pub fn content_length(mut self, content_length: usize) -> Self {
124        self.insert("CONTENT_LENGTH".into(), content_length.to_string().into());
125        self
126    }
127}
128
129impl<'a> Default for Params<'a> {
130    fn default() -> Self {
131        Params(HashMap::new())
132            .gateway_interface("FastCGI/1.0")
133            .server_software("fastcgi-client-rs")
134            .server_protocol("HTTP/1.1")
135    }
136}
137
138impl<'a> Deref for Params<'a> {
139    type Target = HashMap<Cow<'a, str>, Cow<'a, str>>;
140
141    fn deref(&self) -> &Self::Target {
142        &self.0
143    }
144}
145
146impl<'a> DerefMut for Params<'a> {
147    fn deref_mut(&mut self) -> &mut Self::Target {
148        &mut self.0
149    }
150}
151
152impl<'a> From<Params<'a>> for HashMap<Cow<'a, str>, Cow<'a, str>> {
153    fn from(params: Params<'a>) -> Self {
154        params.0
155    }
156}