Skip to main content

rustyphoenixrequest/
phoenix_request.rs

1/***************************************
2	Auteur : Pierre Aubert
3	Mail : pierre.aubert@lapp.in2p3.fr
4	Licence : CeCILL-C
5****************************************/
6
7use crate::pmimetype::PMimeType;
8
9#[derive(Debug, Default)]
10pub struct PRequestParam {
11	///Command of the request (i.e. GET)
12	command: String,
13	///Url of the request (i.e. /)
14	url: String,
15	///Protocol of the request (i.e. HTTP/1.1)
16	protocol: String,
17}
18
19impl PRequestParam {
20	///Set the command of the PRequestParam
21	/// # Parameters
22	/// - `command` : command of the request (i.e. GET)
23	pub fn set_command(&mut self, command: &String) {
24		self.command = command.to_string();
25	}
26	///Set the url of the PRequestParam
27	/// # Parameters
28	/// - `url` : url of the request (i.e. /)
29	pub fn set_url(&mut self, url: &String) {
30		self.url = url.to_string();
31	}
32	///Set the protocol of the PRequestParam
33	/// # Parameters
34	/// - `protocol` : protocol of the request (i.e. HTTP/1.1)
35	pub fn set_protocol(&mut self, protocol: &String) {
36		self.protocol = protocol.to_string();
37	}
38	///Set the url of the PRequestParam
39	/// # Returns
40	/// url of the request (i.e. /)
41	pub fn get_url(&self) -> &String {
42		&self.url
43	}
44}
45
46
47#[derive(Debug, Default)]
48pub struct PResponseParam {
49	///Status of the response (i.e. HTTP/1.1 200 OK)
50	pub status: String,
51	///Type of the content string
52	pub mimetype: PMimeType,
53	///Content of the response (i.e. some html page)
54	pub content: String,
55}
56
57impl PResponseParam {
58	///Constructor of the PResponseParam
59	/// # Parameters
60	/// - `status` : status of the response (i.e. HTTP/1.1 200 OK)
61	/// - `mimetype` : type of the content string
62	/// - `content` : content of the response (i.e. some html page)
63	pub fn new(status: String, mimetype: PMimeType, content: &String) -> Self{
64		PResponseParam {
65			status: status,
66			mimetype: mimetype,
67			content: content.to_owned()
68		}
69	}
70	///Set the status of the PResponseParam
71	/// # Parameters
72	/// - `status` : status of the request (i.e. GET)
73	pub fn set_status(&mut self, status: &String) {
74		self.status = status.to_string();
75	}
76	///Set the content of the PResponseParam
77	/// # Parameters
78	/// - `content` : content of the response (i.e. GET)
79	pub fn set_content(&mut self, content: &String) {
80		self.content = content.to_string();
81	}
82	///Set the mime type of the response
83	/// # Parameters
84	/// - `mimetype` : type of the content of the response
85	pub fn set_mime_type(&mut self, mimetype: &PMimeType){
86		self.mimetype = *mimetype;
87	}
88}
89
90pub trait PhoenixRequestStream {
91	///Recieved a request from a client
92	/// # Parameters
93	/// - `request` : PRequestParam recievred from the client
94	/// # Returns
95	/// corresponding request
96	fn recv_request(&mut self, request: &mut PRequestParam) -> bool;
97
98	///Send response to the client
99	/// # Returns
100	/// corresponding request
101	fn send_response(&mut self, response: &PResponseParam);
102}
103
104pub trait PhoenixRequestMockStream {
105	///Set the record mode in the mock
106	/// # Parameters
107	/// - `is_mock_record` - true if the mock is in record mode
108	fn set_is_record(&mut self, is_mock_record: bool);
109
110	///Set the prefix of the current mock
111	/// # Parameters
112	/// - `prefix` - prefix of the current mock
113	fn set_prefix(&mut self, prefix: &String);
114}
115
116pub trait PhoenixRequestServer {
117	type Stream;
118	///Initialise the Request server
119	/// # Parameters
120	/// - `hostname` : address of the server
121	/// - `port` : port of the server
122	/// # Returns
123	/// initialised Request server
124	fn new(hostname: &String, port: u16) -> Self;
125	///Accept the connection and get a corresponding stream
126	/// # Returns
127	/// corresponding stream
128	fn accept(&self) -> Self::Stream;
129}
130
131pub trait PhoenixRequestMockServer {
132	///Set the record mode in the mock
133	/// # Parameters
134	/// - `is_mock_record` - true if the mock is in record mode
135	fn set_is_record(&mut self, is_mock_record: bool);
136
137	///Set the prefix of the current mock
138	/// # Parameters
139	/// - `prefix` - prefix of the current mock
140	fn set_prefix(&mut self, prefix: &String);
141}