Skip to content

Commit 1fff9e9

Browse files
committed
Auto merge of #2389 - jtgeibel:update/conduit/http-types, r=JohnTitor
Use http types from `http` crate In the upstream conduit crates, I've dropped the custom HTTP related types in favor of types from the `http` crate. This reduces the amount of code to maintain upstream and generally provides a cleaner API, such as named `StatusCode::*` constants instead of naked integers. This should also make it easier and more efficient to support both `civet` and `hyper` as web servers, as they (and our middleware stack) now use the same types and avoid unnecessary conversions. r? @JohnTitor
2 parents c8c8424 + 6985639 commit 1fff9e9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+492
-442
lines changed

Cargo.lock

Lines changed: 23 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,17 @@ lettre = "0.9"
6666
lettre_email = "0.9"
6767
failure = "0.1.1"
6868

69-
conduit = "0.9.0-alpha.0"
70-
conduit-conditional-get = "0.9.0-alpha.0"
71-
conduit-cookie = "0.9.0-alpha.0"
69+
conduit = "0.9.0-alpha.2"
70+
conduit-conditional-get = "0.9.0-alpha.2"
71+
conduit-cookie = "0.9.0-alpha.2"
7272
cookie = { version = "0.12", features = ["secure"] }
73-
conduit-middleware = "0.9.0-alpha.0"
74-
conduit-router = "0.9.0-alpha.0"
75-
conduit-static = "0.9.0-alpha.0"
76-
conduit-git-http-backend = "0.9.0-alpha.0"
77-
civet = "0.12.0-alpha.1"
78-
conduit-hyper = "0.3.0-alpha.0"
73+
conduit-middleware = "0.9.0-alpha.2"
74+
conduit-router = "0.9.0-alpha.2"
75+
conduit-static = "0.9.0-alpha.2"
76+
conduit-git-http-backend = "0.9.0-alpha.2"
77+
civet = "0.12.0-alpha.3"
78+
conduit-hyper = "0.3.0-alpha.2"
79+
http = "0.2"
7980

8081
futures-util = "0.3"
8182
futures-channel = { version = "0.3.1", default-features = false }
@@ -86,7 +87,7 @@ indexmap = "1.0.2"
8687
handlebars = "3.0.1"
8788

8889
[dev-dependencies]
89-
conduit-test = "0.9.0-alpha.0"
90+
conduit-test = "0.9.0-alpha.2"
9091
hyper-tls = "0.4"
9192
lazy_static = "1.0"
9293
diesel_migrations = { version = "1.3.0", features = ["postgres"] }

src/controllers.rs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,13 @@ mod prelude {
1414
pub use super::helpers::ok_true;
1515
pub use diesel::prelude::*;
1616

17-
pub use conduit::{Request, Response};
17+
pub use conduit::{header, RequestExt, StatusCode};
1818
pub use conduit_router::RequestParams;
1919

2020
pub use crate::db::RequestTransaction;
21-
pub use crate::util::errors::{cargo_err, AppError, AppResult, ChainError}; // TODO: Remove cargo_err from here
22-
2321
pub use crate::middleware::app::RequestApp;
24-
25-
use std::collections::HashMap;
26-
use std::io;
22+
pub use crate::util::errors::{cargo_err, AppError, AppResult, ChainError}; // TODO: Remove cargo_err from here
23+
pub use crate::util::{AppResponse, EndpointResult};
2724

2825
use indexmap::IndexMap;
2926
use serde::Serialize;
@@ -33,18 +30,18 @@ mod prelude {
3330
}
3431

3532
pub trait RequestUtils {
36-
fn redirect(&self, url: String) -> Response;
33+
fn redirect(&self, url: String) -> AppResponse;
3734

38-
fn json<T: Serialize>(&self, t: &T) -> Response;
35+
fn json<T: Serialize>(&self, t: &T) -> AppResponse;
3936
fn query(&self) -> IndexMap<String, String>;
4037
fn wants_json(&self) -> bool;
4138
fn query_with_params(&self, params: IndexMap<String, String>) -> String;
4239

4340
fn log_metadata<V: std::fmt::Display>(&mut self, key: &'static str, value: V);
4441
}
4542

46-
impl<'a> RequestUtils for dyn Request + 'a {
47-
fn json<T: Serialize>(&self, t: &T) -> Response {
43+
impl<'a> RequestUtils for dyn RequestExt + 'a {
44+
fn json<T: Serialize>(&self, t: &T) -> AppResponse {
4845
crate::util::json_response(t)
4946
}
5047

@@ -54,21 +51,19 @@ mod prelude {
5451
.collect()
5552
}
5653

57-
fn redirect(&self, url: String) -> Response {
58-
let mut headers = HashMap::new();
59-
headers.insert("Location".to_string(), vec![url]);
60-
Response {
61-
status: (302, "Found"),
62-
headers,
63-
body: Box::new(io::empty()),
64-
}
54+
fn redirect(&self, url: String) -> AppResponse {
55+
conduit::Response::builder()
56+
.status(StatusCode::FOUND)
57+
.header(header::LOCATION, url)
58+
.body(conduit::Body::empty())
59+
.unwrap() // Should not panic unless url contains "\r\n"
6560
}
6661

6762
fn wants_json(&self) -> bool {
6863
self.headers()
69-
.find("Accept")
70-
.map(|accept| accept.iter().any(|s| s.contains("json")))
71-
.unwrap_or(false)
64+
.get_all(header::ACCEPT)
65+
.iter()
66+
.any(|val| val.to_str().unwrap_or_default().contains("json"))
7267
}
7368

7469
fn query_with_params(&self, new_params: IndexMap<String, String>) -> String {

src/controllers/category.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::schema::categories;
66
use crate::views::{EncodableCategory, EncodableCategoryWithSubcategories};
77

88
/// Handles the `GET /categories` route.
9-
pub fn index(req: &mut dyn Request) -> AppResult<Response> {
9+
pub fn index(req: &mut dyn RequestExt) -> EndpointResult {
1010
let conn = req.db_conn()?;
1111
let query = req.query();
1212
// FIXME: There are 69 categories, 47 top level. This isn't going to
@@ -40,7 +40,7 @@ pub fn index(req: &mut dyn Request) -> AppResult<Response> {
4040
}
4141

4242
/// Handles the `GET /categories/:category_id` route.
43-
pub fn show(req: &mut dyn Request) -> AppResult<Response> {
43+
pub fn show(req: &mut dyn RequestExt) -> EndpointResult {
4444
let slug = &req.params()["category_id"];
4545
let conn = req.db_conn()?;
4646
let cat = Category::by_slug(slug).first::<Category>(&*conn)?;
@@ -77,7 +77,7 @@ pub fn show(req: &mut dyn Request) -> AppResult<Response> {
7777
}
7878

7979
/// Handles the `GET /category_slugs` route.
80-
pub fn slugs(req: &mut dyn Request) -> AppResult<Response> {
80+
pub fn slugs(req: &mut dyn RequestExt) -> EndpointResult {
8181
let conn = req.db_conn()?;
8282
let slugs = categories::table
8383
.select((categories::slug, categories::slug, categories::description))

src/controllers/crate_owner_invitation.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::schema::{crate_owner_invitations, crate_owners};
55
use crate::views::{EncodableCrateOwnerInvitation, InvitationResponse};
66

77
/// Handles the `GET /me/crate_owner_invitations` route.
8-
pub fn list(req: &mut dyn Request) -> AppResult<Response> {
8+
pub fn list(req: &mut dyn RequestExt) -> EndpointResult {
99
let conn = &*req.db_conn()?;
1010
let user_id = req.authenticate(conn)?.user_id();
1111

@@ -31,7 +31,7 @@ struct OwnerInvitation {
3131
}
3232

3333
/// Handles the `PUT /me/crate_owner_invitations/:crate_id` route.
34-
pub fn handle_invite(req: &mut dyn Request) -> AppResult<Response> {
34+
pub fn handle_invite(req: &mut dyn RequestExt) -> EndpointResult {
3535
let mut body = String::new();
3636
req.body().read_to_string(&mut body)?;
3737

@@ -51,7 +51,7 @@ pub fn handle_invite(req: &mut dyn Request) -> AppResult<Response> {
5151
}
5252

5353
/// Handles the `PUT /me/crate_owner_invitations/accept/:token` route.
54-
pub fn handle_invite_with_token(req: &mut dyn Request) -> AppResult<Response> {
54+
pub fn handle_invite_with_token(req: &mut dyn RequestExt) -> EndpointResult {
5555
let conn = req.db_conn()?;
5656
let req_token = &req.params()["token"];
5757

@@ -72,11 +72,11 @@ pub fn handle_invite_with_token(req: &mut dyn Request) -> AppResult<Response> {
7272
}
7373

7474
fn accept_invite(
75-
req: &dyn Request,
75+
req: &dyn RequestExt,
7676
conn: &PgConnection,
7777
crate_invite: InvitationResponse,
7878
user_id: i32,
79-
) -> AppResult<Response> {
79+
) -> EndpointResult {
8080
use diesel::{delete, insert_into};
8181

8282
conn.transaction(|| {
@@ -110,10 +110,10 @@ fn accept_invite(
110110
}
111111

112112
fn decline_invite(
113-
req: &dyn Request,
113+
req: &dyn RequestExt,
114114
conn: &PgConnection,
115115
crate_invite: InvitationResponse,
116-
) -> AppResult<Response> {
116+
) -> EndpointResult {
117117
use diesel::delete;
118118

119119
let user_id = req.authenticate(conn)?.user_id();

src/controllers/helpers.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use crate::util::{errors::AppResult, json_response};
2-
use conduit::Response;
1+
use crate::util::{json_response, EndpointResult};
32

43
pub(crate) mod pagination;
54

65
pub(crate) use self::pagination::Paginate;
76

8-
pub fn ok_true() -> AppResult<Response> {
7+
pub fn ok_true() -> EndpointResult {
98
#[derive(Serialize)]
109
struct R {
1110
ok: bool,

0 commit comments

Comments
 (0)