Handles an HTTP request. After parsing the request, the handler could decide to send an associated API request down to the VMM API server to e.g. create or start a VM. The request will block waiting for an answer from the API server and translate that into an HTTP response.
(
&self,
req: &Request,
api_notifier: EventFd,
api_sender: Sender<ApiRequest>,
)
| 109 | /// or start a VM. The request will block waiting for an answer from the |
| 110 | /// API server and translate that into an HTTP response. |
| 111 | fn handle_request( |
| 112 | &self, |
| 113 | req: &Request, |
| 114 | api_notifier: EventFd, |
| 115 | api_sender: Sender<ApiRequest>, |
| 116 | ) -> Response { |
| 117 | // Cloning the files here is very important as it dup() the file |
| 118 | // descriptors, leaving open the one that was received. This way, |
| 119 | // rebooting the VM will work since the VM will be created from the |
| 120 | // original file descriptors. |
| 121 | let files = req.files.iter().map(|f| f.try_clone().unwrap()).collect(); |
| 122 | let res = match req.method() { |
| 123 | Method::Put => self.put_handler(api_notifier, api_sender, &req.body, files), |
| 124 | Method::Get => self.get_handler(api_notifier, api_sender, &req.body), |
| 125 | _ => return Response::new(Version::Http11, StatusCode::BadRequest), |
| 126 | }; |
| 127 | |
| 128 | match res { |
| 129 | Ok(response_body) => { |
| 130 | if let Some(body) = response_body { |
| 131 | let mut response = Response::new(Version::Http11, StatusCode::OK); |
| 132 | response.set_body(body); |
| 133 | response |
| 134 | } else { |
| 135 | Response::new(Version::Http11, StatusCode::NoContent) |
| 136 | } |
| 137 | } |
| 138 | Err(e @ HttpError::BadRequest) => error_response(e, StatusCode::BadRequest), |
| 139 | Err(e @ HttpError::SerdeJsonDeserialize(_)) => { |
| 140 | error_response(e, StatusCode::BadRequest) |
| 141 | } |
| 142 | Err(e @ HttpError::TooManyRequests) => error_response(e, StatusCode::TooManyRequests), |
| 143 | Err(e) => error_response(e, StatusCode::InternalServerError), |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | fn put_handler( |
| 148 | &self, |
no test coverage detected