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
15//! FastCGI parameters builder and container.
16//!
17//! This module provides the `Params` struct which acts as a builder
18//! for FastCGI parameters that are sent to the FastCGI server.
19//! It includes convenient methods for setting common CGI parameters.
20
21use std::{
22 borrow::Cow,
23 collections::HashMap,
24 ops::{Deref, DerefMut},
25};
26
27/// Fastcgi params, please reference to nginx-php-fpm fastcgi_params.
28#[derive(Debug, Clone, PartialEq, Eq)]
29pub struct Params<'a>(HashMap<Cow<'a, str>, Cow<'a, str>>);
30
31impl<'a> Params<'a> {
32 /// Sets a custom parameter with the given key and value.
33 ///
34 /// # Arguments
35 ///
36 /// * `key` - The parameter name
37 /// * `value` - The parameter value
38 #[inline]
39 pub fn custom<K: Into<Cow<'a, str>>, S: Into<Cow<'a, str>>>(
40 mut self, key: K, value: S,
41 ) -> Self {
42 self.insert(key.into(), value.into());
43 self
44 }
45
46 /// Sets the GATEWAY_INTERFACE parameter.
47 ///
48 /// # Arguments
49 ///
50 /// * `gateway_interface` - The gateway interface version (e.g., "CGI/1.1")
51 #[inline]
52 pub fn gateway_interface<S: Into<Cow<'a, str>>>(mut self, gateway_interface: S) -> Self {
53 self.insert("GATEWAY_INTERFACE".into(), gateway_interface.into());
54 self
55 }
56
57 /// Sets the SERVER_SOFTWARE parameter.
58 ///
59 /// # Arguments
60 ///
61 /// * `server_software` - The server software name and version
62 #[inline]
63 pub fn server_software<S: Into<Cow<'a, str>>>(mut self, server_software: S) -> Self {
64 self.insert("SERVER_SOFTWARE".into(), server_software.into());
65 self
66 }
67
68 /// Sets the SERVER_PROTOCOL parameter.
69 ///
70 /// # Arguments
71 ///
72 /// * `server_protocol` - The server protocol version (e.g., "HTTP/1.1")
73 #[inline]
74 pub fn server_protocol<S: Into<Cow<'a, str>>>(mut self, server_protocol: S) -> Self {
75 self.insert("SERVER_PROTOCOL".into(), server_protocol.into());
76 self
77 }
78
79 /// Sets the REQUEST_METHOD parameter.
80 ///
81 /// # Arguments
82 ///
83 /// * `request_method` - The HTTP request method (e.g., "GET", "POST")
84 #[inline]
85 pub fn request_method<S: Into<Cow<'a, str>>>(mut self, request_method: S) -> Self {
86 self.insert("REQUEST_METHOD".into(), request_method.into());
87 self
88 }
89
90 /// Sets the SCRIPT_FILENAME parameter.
91 ///
92 /// # Arguments
93 ///
94 /// * `script_filename` - The full path to the script file
95 #[inline]
96 pub fn script_filename<S: Into<Cow<'a, str>>>(mut self, script_filename: S) -> Self {
97 self.insert("SCRIPT_FILENAME".into(), script_filename.into());
98 self
99 }
100
101 /// Sets the SCRIPT_NAME parameter.
102 ///
103 /// # Arguments
104 ///
105 /// * `script_name` - The URI part that identifies the script
106 #[inline]
107 pub fn script_name<S: Into<Cow<'a, str>>>(mut self, script_name: S) -> Self {
108 self.insert("SCRIPT_NAME".into(), script_name.into());
109 self
110 }
111
112 /// Sets the QUERY_STRING parameter.
113 ///
114 /// # Arguments
115 ///
116 /// * `query_string` - The query string part of the URL
117 #[inline]
118 pub fn query_string<S: Into<Cow<'a, str>>>(mut self, query_string: S) -> Self {
119 self.insert("QUERY_STRING".into(), query_string.into());
120 self
121 }
122
123 /// Sets the REQUEST_URI parameter.
124 ///
125 /// # Arguments
126 ///
127 /// * `request_uri` - The full request URI
128 #[inline]
129 pub fn request_uri<S: Into<Cow<'a, str>>>(mut self, request_uri: S) -> Self {
130 self.insert("REQUEST_URI".into(), request_uri.into());
131 self
132 }
133
134 /// Sets the DOCUMENT_ROOT parameter.
135 ///
136 /// # Arguments
137 ///
138 /// * `document_root` - The document root directory path
139 #[inline]
140 pub fn document_root<S: Into<Cow<'a, str>>>(mut self, document_root: S) -> Self {
141 self.insert("DOCUMENT_ROOT".into(), document_root.into());
142 self
143 }
144
145 /// Sets the DOCUMENT_URI parameter.
146 ///
147 /// # Arguments
148 ///
149 /// * `document_uri` - The document URI
150 #[inline]
151 pub fn document_uri<S: Into<Cow<'a, str>>>(mut self, document_uri: S) -> Self {
152 self.insert("DOCUMENT_URI".into(), document_uri.into());
153 self
154 }
155
156 /// Sets the REMOTE_ADDR parameter.
157 ///
158 /// # Arguments
159 ///
160 /// * `remote_addr` - The remote client IP address
161 #[inline]
162 pub fn remote_addr<S: Into<Cow<'a, str>>>(mut self, remote_addr: S) -> Self {
163 self.insert("REMOTE_ADDR".into(), remote_addr.into());
164 self
165 }
166
167 /// Sets the REMOTE_PORT parameter.
168 ///
169 /// # Arguments
170 ///
171 /// * `remote_port` - The remote client port number
172 #[inline]
173 pub fn remote_port(mut self, remote_port: u16) -> Self {
174 self.insert("REMOTE_PORT".into(), remote_port.to_string().into());
175 self
176 }
177
178 /// Sets the SERVER_ADDR parameter.
179 ///
180 /// # Arguments
181 ///
182 /// * `server_addr` - The server IP address
183 #[inline]
184 pub fn server_addr<S: Into<Cow<'a, str>>>(mut self, server_addr: S) -> Self {
185 self.insert("SERVER_ADDR".into(), server_addr.into());
186 self
187 }
188
189 /// Sets the SERVER_PORT parameter.
190 ///
191 /// # Arguments
192 ///
193 /// * `server_port` - The server port number
194 #[inline]
195 pub fn server_port(mut self, server_port: u16) -> Self {
196 self.insert("SERVER_PORT".into(), server_port.to_string().into());
197 self
198 }
199
200 /// Sets the SERVER_NAME parameter.
201 ///
202 /// # Arguments
203 ///
204 /// * `server_name` - The server name or hostname
205 #[inline]
206 pub fn server_name<S: Into<Cow<'a, str>>>(mut self, server_name: S) -> Self {
207 self.insert("SERVER_NAME".into(), server_name.into());
208 self
209 }
210
211 /// Sets the CONTENT_TYPE parameter.
212 ///
213 /// # Arguments
214 ///
215 /// * `content_type` - The content type of the request body
216 #[inline]
217 pub fn content_type<S: Into<Cow<'a, str>>>(mut self, content_type: S) -> Self {
218 self.insert("CONTENT_TYPE".into(), content_type.into());
219 self
220 }
221
222 /// Sets the CONTENT_LENGTH parameter.
223 ///
224 /// # Arguments
225 ///
226 /// * `content_length` - The length of the request body in bytes
227 #[inline]
228 pub fn content_length(mut self, content_length: usize) -> Self {
229 self.insert("CONTENT_LENGTH".into(), content_length.to_string().into());
230 self
231 }
232}
233
234impl<'a> Default for Params<'a> {
235 fn default() -> Self {
236 Params(HashMap::new())
237 .gateway_interface("FastCGI/1.0")
238 .server_software("fastcgi-client-rs")
239 .server_protocol("HTTP/1.1")
240 }
241}
242
243impl<'a> Deref for Params<'a> {
244 type Target = HashMap<Cow<'a, str>, Cow<'a, str>>;
245
246 fn deref(&self) -> &Self::Target {
247 &self.0
248 }
249}
250
251impl<'a> DerefMut for Params<'a> {
252 fn deref_mut(&mut self) -> &mut Self::Target {
253 &mut self.0
254 }
255}
256
257impl<'a> From<Params<'a>> for HashMap<Cow<'a, str>, Cow<'a, str>> {
258 fn from(params: Params<'a>) -> Self {
259 params.0
260 }
261}