Skip to content

Commit 43d5a1d

Browse files
roypatalxiord
authored andcommitted
Clear all clippy warnings
Additionally, HttpServer::new_from_fd has been marked as unsafe, since the correctness of the unsafe call within the function relies on the caller upholding an invariant Signed-off-by: Patrick Roy <[email protected]>
1 parent d2caafa commit 43d5a1d

File tree

8 files changed

+47
-30
lines changed

8 files changed

+47
-30
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Unreleased
2+
3+
## Added
4+
5+
- Implemented `Eq` for `common::headers::Encoding`, `common::headers::MediaType`,
6+
`common::headers::Headers`, `common::HttpHeaderError`, `common::Body`, `common::Version`,
7+
`common::RequestError`, `request::Uri`, `request::RequestLine`, `response::StatusCode`,
8+
`response::ResponseHeaders`
9+
10+
## Changed
11+
12+
- Mark `HttpServer::new_from_fd` as `unsafe` as the correctness of the unsafe code
13+
in this method relies on an invariant the caller has to uphold
14+
115
# v0.1.0
216

317
- micro-http v0.1.0 first release.

src/common/headers.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl Header {
7878
/// invalidate our request as we don't support the full set of HTTP/1.1 specification.
7979
/// Such header entries are "Transfer-Encoding: identity; q=0", which means a compression
8080
/// algorithm is applied to the body of the request, or "Expect: 103-checkpoint".
81-
#[derive(Debug, PartialEq)]
81+
#[derive(Debug, PartialEq, Eq)]
8282
pub struct Headers {
8383
/// The `Content-Length` header field tells us how many bytes we need to receive
8484
/// from the source after the headers.
@@ -310,7 +310,7 @@ impl Headers {
310310
}
311311

312312
/// Wrapper over supported AcceptEncoding.
313-
#[derive(Clone, Copy, Debug, PartialEq)]
313+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
314314
pub struct Encoding {}
315315

316316
impl Encoding {
@@ -367,7 +367,7 @@ impl Encoding {
367367
}
368368

369369
/// Wrapper over supported Media Types.
370-
#[derive(Clone, Copy, Debug, PartialEq)]
370+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
371371
pub enum MediaType {
372372
/// Media Type: "text/plain".
373373
PlainText,
@@ -450,8 +450,8 @@ mod tests {
450450
fn test_default() {
451451
let headers = Headers::default();
452452
assert_eq!(headers.content_length(), 0);
453-
assert_eq!(headers.chunked(), false);
454-
assert_eq!(headers.expect(), false);
453+
assert!(!headers.chunked());
454+
assert!(!headers.expect());
455455
assert_eq!(headers.accept(), MediaType::PlainText);
456456
assert_eq!(headers.custom_entries(), &HashMap::default());
457457
}

src/common/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub mod ascii {
1515
}
1616

1717
///Errors associated with a header that is invalid.
18-
#[derive(Debug, PartialEq)]
18+
#[derive(Debug, PartialEq, Eq)]
1919
pub enum HttpHeaderError {
2020
/// The header is misformatted.
2121
InvalidFormat(String),
@@ -64,7 +64,7 @@ impl Display for HttpHeaderError {
6464
}
6565

6666
/// Errors associated with parsing the HTTP Request from a u8 slice.
67-
#[derive(Debug, PartialEq)]
67+
#[derive(Debug, PartialEq, Eq)]
6868
pub enum RequestError {
6969
/// No request was pending while the request body was being parsed.
7070
BodyWithoutPendingRequest,
@@ -195,7 +195,7 @@ impl Display for ServerError {
195195
/// assert_eq!(body.raw(), b"This is a test body.");
196196
/// assert_eq!(body.len(), 20);
197197
/// ```
198-
#[derive(Clone, Debug, PartialEq)]
198+
#[derive(Clone, Debug, PartialEq, Eq)]
199199
pub struct Body {
200200
/// Body of the HTTP message as bytes.
201201
pub body: Vec<u8>,
@@ -281,7 +281,7 @@ impl Method {
281281
/// let version = Version::try_from(b"http/1.1");
282282
/// assert!(version.is_err());
283283
/// ```
284-
#[derive(Clone, Copy, Debug, PartialEq)]
284+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
285285
pub enum Version {
286286
/// HTTP/1.0
287287
Http10,

src/connection.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl<T: Read + Write + ScmSocket> HttpConnection<T> {
176176
iov_len: buf.len(),
177177
}];
178178

179-
// Safe because we have mutably borrowed buf and it's safe to write
179+
// SAFETY: Safe because we have mutably borrowed buf and it's safe to write
180180
// arbitrary data to a slice.
181181
let (read_count, fd_count) = unsafe {
182182
self.stream
@@ -189,7 +189,7 @@ impl<T: Read + Write + ScmSocket> HttpConnection<T> {
189189
fds.iter()
190190
.take(fd_count)
191191
.map(|fd| {
192-
// Safe because all fds are owned by us after they have been
192+
// SAFETY: Safe because all fds are owned by us after they have been
193193
// received through the socket.
194194
unsafe { File::from_raw_fd(*fd) }
195195
})
@@ -1048,11 +1048,11 @@ mod tests {
10481048
let mut file1 = TempFile::new().unwrap().into_file();
10491049
let mut file2 = TempFile::new().unwrap().into_file();
10501050
let mut file3 = TempFile::new().unwrap().into_file();
1051-
file1.write(b"foo").unwrap();
1051+
file1.write_all(b"foo").unwrap();
10521052
file1.seek(SeekFrom::Start(0)).unwrap();
1053-
file2.write(b"bar").unwrap();
1053+
file2.write_all(b"bar").unwrap();
10541054
file2.seek(SeekFrom::Start(0)).unwrap();
1055-
file3.write(b"foobar").unwrap();
1055+
file3.write_all(b"foobar").unwrap();
10561056
file3.seek(SeekFrom::Start(0)).unwrap();
10571057

10581058
// Send 2 file descriptors along with 3 bytes of data.

src/request.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub(crate) fn find(bytes: &[u8], sequence: &[u8]) -> Option<usize> {
2626
/// Wrapper over HTTP URIs.
2727
///
2828
/// The `Uri` can not be used directly and it is only accessible from an HTTP Request.
29-
#[derive(Clone, Debug, PartialEq)]
29+
#[derive(Clone, Debug, PartialEq, Eq)]
3030
pub struct Uri {
3131
string: String,
3232
}
@@ -85,7 +85,7 @@ impl Uri {
8585
}
8686

8787
/// Wrapper over an HTTP Request Line.
88-
#[derive(Debug, PartialEq)]
88+
#[derive(Debug, PartialEq, Eq)]
8989
pub struct RequestLine {
9090
method: Method,
9191
uri: Uri,
@@ -505,8 +505,8 @@ mod tests {
505505
assert_eq!(request.uri(), &Uri::new("http://localhost/home"));
506506
assert_eq!(request.http_version(), Version::Http11);
507507
assert_eq!(request.method(), Method::Patch);
508-
assert_eq!(request.headers.chunked(), true);
509-
assert_eq!(request.headers.expect(), true);
508+
assert!(request.headers.chunked());
509+
assert!(request.headers.expect());
510510
assert_eq!(request.headers.content_length(), 26);
511511
assert_eq!(
512512
request.body.unwrap().body,
@@ -539,8 +539,8 @@ mod tests {
539539
assert_eq!(request.uri(), &Uri::new("http://localhost/"));
540540
assert_eq!(request.http_version(), Version::Http10);
541541
assert_eq!(request.method(), Method::Get);
542-
assert_eq!(request.headers.chunked(), false);
543-
assert_eq!(request.headers.expect(), false);
542+
assert!(!request.headers.chunked());
543+
assert!(!request.headers.expect());
544544
assert_eq!(request.headers.content_length(), 0);
545545
assert!(request.body.is_none());
546546

src/response.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::Method;
1212
///
1313
/// The status code is defined as specified in the
1414
/// [RFC](https://tools.ietf.org/html/rfc7231#section-6).
15-
#[derive(Clone, Copy, Debug, PartialEq)]
15+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1616
pub enum StatusCode {
1717
/// 100, Continue
1818
Continue,
@@ -84,7 +84,7 @@ impl StatusLine {
8484
/// Wrapper over the list of headers associated with a HTTP Response.
8585
/// When creating a ResponseHeaders object, the content type is initialized to `text/plain`.
8686
/// The content type can be updated with a call to `set_content_type`.
87-
#[derive(Debug, PartialEq)]
87+
#[derive(Debug, PartialEq, Eq)]
8888
pub struct ResponseHeaders {
8989
content_length: i32,
9090
content_type: MediaType,

src/router.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl<T: Send> HttpRoutes<T> {
9292
request.uri().get_abs_path()
9393
);
9494
let mut response = match self.routes.get(&path) {
95-
Some(route) => route.handle_request(&request, &argument),
95+
Some(route) => route.handle_request(request, argument),
9696
None => Response::new(Version::Http11, StatusCode::NotFound),
9797
};
9898

src/server.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl<T: Read + Write + ScmSocket> ClientConnection<T> {
133133
let mut error_response = Response::new(Version::Http11, StatusCode::BadRequest);
134134
error_response.set_body(Body::new(format!(
135135
"{{ \"error\": \"{}\nAll previous unanswered requests will be dropped.\" }}",
136-
inner.to_string()
136+
inner
137137
)));
138138
self.connection.enqueue_response(error_response);
139139
}
@@ -285,16 +285,17 @@ impl HttpServer {
285285

286286
/// Constructor for `HttpServer`.
287287
///
288-
/// Note that this function requires the socket_fd to be solely owned
288+
/// Returns the newly formed `HttpServer`.
289+
///
290+
/// # Safety
291+
/// This function requires the socket_fd to be solely owned
289292
/// and not be associated with another File in the caller as it uses
290293
/// the unsafe `UnixListener::from_raw_fd method`.
291294
///
292-
/// Returns the newly formed `HttpServer`.
293-
///
294295
/// # Errors
295296
/// Returns an `IOError` when `epoll::create` fails.
296-
pub fn new_from_fd(socket_fd: RawFd) -> Result<Self> {
297-
let socket = unsafe { UnixListener::from_raw_fd(socket_fd) };
297+
pub unsafe fn new_from_fd(socket_fd: RawFd) -> Result<Self> {
298+
let socket = UnixListener::from_raw_fd(socket_fd);
298299
let epoll = epoll::Epoll::new().map_err(ServerError::IOError)?;
299300
Ok(HttpServer {
300301
socket,
@@ -639,6 +640,8 @@ impl HttpServer {
639640

640641
#[cfg(test)]
641642
mod tests {
643+
#![allow(clippy::undocumented_unsafe_blocks)]
644+
642645
use super::*;
643646
use std::io::{Read, Write};
644647
use std::net::Shutdown;
@@ -764,7 +767,7 @@ mod tests {
764767
let socket_listener = UnixListener::bind(path_to_socket.as_path()).unwrap();
765768
let socket_fd = socket_listener.into_raw_fd();
766769

767-
let mut server = HttpServer::new_from_fd(socket_fd).unwrap();
770+
let mut server = unsafe { HttpServer::new_from_fd(socket_fd).unwrap() };
768771
server.start_server().unwrap();
769772

770773
// Test one incoming connection.

0 commit comments

Comments
 (0)