Skip to content

Commit d1788d6

Browse files
committed
feat(server): Make the server code an optional feature
BREAKING CHANGE: The HTTP server code is now an optional feature. To enable the server, add `features = ["server"]` to the dependency in your `Cargo.toml`.
1 parent 4e55583 commit d1788d6

File tree

18 files changed

+182
-99
lines changed

18 files changed

+182
-99
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,15 @@ default = [
7575
"runtime",
7676
"stream",
7777
"client",
78+
"server",
7879
"http1",
7980
"http2",
8081
]
8182
full = [
8283
"client",
8384
"http1",
8485
"http2",
86+
"server",
8587
"stream",
8688
"runtime",
8789
]
@@ -100,7 +102,9 @@ tcp = [
100102
http1 = []
101103
http2 = ["h2"]
102104

105+
# Client/Server
103106
client = []
107+
server = []
104108

105109
# `impl Stream` for things
106110
stream = []

src/body/body.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::common::{task, watch, Pin, Poll};
2121
#[cfg(any(feature = "http1", feature = "http2"))]
2222
#[cfg(feature = "client")]
2323
use crate::common::{Future, Never};
24-
#[cfg(feature = "http2")]
24+
#[cfg(all(feature = "http2", any(feature = "client", feature = "server")))]
2525
use crate::proto::h2::ping;
2626
use crate::upgrade::OnUpgrade;
2727

@@ -46,7 +46,7 @@ enum Kind {
4646
want_tx: watch::Sender,
4747
rx: mpsc::Receiver<Result<Bytes, crate::Error>>,
4848
},
49-
#[cfg(feature = "http2")]
49+
#[cfg(all(feature = "http2", any(feature = "client", feature = "server")))]
5050
H2 {
5151
ping: ping::Recorder,
5252
content_length: DecodedLength,
@@ -200,7 +200,7 @@ impl Body {
200200
Body { kind, extra: None }
201201
}
202202

203-
#[cfg(feature = "http2")]
203+
#[cfg(all(feature = "http2", any(feature = "client", feature = "server")))]
204204
pub(crate) fn h2(
205205
recv: h2::RecvStream,
206206
content_length: DecodedLength,
@@ -301,7 +301,7 @@ impl Body {
301301
None => Poll::Ready(None),
302302
}
303303
}
304-
#[cfg(feature = "http2")]
304+
#[cfg(all(feature = "http2", any(feature = "client", feature = "server")))]
305305
Kind::H2 {
306306
ref ping,
307307
recv: ref mut h2,
@@ -359,7 +359,7 @@ impl HttpBody for Body {
359359
#[cfg_attr(not(feature = "http2"), allow(unused))] cx: &mut task::Context<'_>,
360360
) -> Poll<Result<Option<HeaderMap>, Self::Error>> {
361361
match self.kind {
362-
#[cfg(feature = "http2")]
362+
#[cfg(all(feature = "http2", any(feature = "client", feature = "server")))]
363363
Kind::H2 {
364364
recv: ref mut h2,
365365
ref ping,
@@ -379,7 +379,7 @@ impl HttpBody for Body {
379379
match self.kind {
380380
Kind::Once(ref val) => val.is_none(),
381381
Kind::Chan { content_length, .. } => content_length == DecodedLength::ZERO,
382-
#[cfg(feature = "http2")]
382+
#[cfg(all(feature = "http2", any(feature = "client", feature = "server")))]
383383
Kind::H2 { recv: ref h2, .. } => h2.is_end_stream(),
384384
#[cfg(feature = "stream")]
385385
Kind::Wrapped(..) => false,
@@ -405,7 +405,7 @@ impl HttpBody for Body {
405405
#[cfg(feature = "stream")]
406406
Kind::Wrapped(..) => SizeHint::default(),
407407
Kind::Chan { content_length, .. } => opt_len!(content_length),
408-
#[cfg(feature = "http2")]
408+
#[cfg(all(feature = "http2", any(feature = "client", feature = "server")))]
409409
Kind::H2 { content_length, .. } => opt_len!(content_length),
410410
}
411411
}

src/cfg.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@ macro_rules! cfg_feature {
1111
}
1212
}
1313

14-
macro_rules! cfg_any_http {
14+
macro_rules! cfg_proto {
1515
($($item:item)*) => {
1616
cfg_feature! {
17-
#![any(feature = "http1", feature = "http2")]
17+
#![all(
18+
any(feature = "http1", feature = "http2"),
19+
any(feature = "client", feature = "server"),
20+
)]
1821
$($item)*
1922
}
2023
}
2124
}
2225

23-
cfg_any_http! {
26+
cfg_proto! {
2427
macro_rules! cfg_http1 {
2528
($($item:item)*) => {
2629
cfg_feature! {
@@ -47,4 +50,13 @@ cfg_any_http! {
4750
}
4851
}
4952
}
53+
54+
macro_rules! cfg_server {
55+
($($item:item)*) => {
56+
cfg_feature! {
57+
#![feature = "server"]
58+
$($item)*
59+
}
60+
}
61+
}
5062
}

src/common/exec.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,23 @@ use std::future::Future;
33
use std::pin::Pin;
44
use std::sync::Arc;
55

6+
#[cfg(feature = "server")]
67
use crate::body::{Body, HttpBody};
78
#[cfg(feature = "http2")]
9+
#[cfg(feature = "server")]
810
use crate::proto::h2::server::H2Stream;
911
use crate::rt::Executor;
12+
#[cfg(feature = "server")]
1013
use crate::server::conn::spawn_all::{NewSvcTask, Watcher};
14+
#[cfg(feature = "server")]
1115
use crate::service::HttpService;
1216

17+
#[cfg(feature = "server")]
1318
pub trait ConnStreamExec<F, B: HttpBody>: Clone {
1419
fn execute_h2stream(&mut self, fut: H2Stream<F, B>);
1520
}
1621

22+
#[cfg(feature = "server")]
1723
pub trait NewSvcExec<I, N, S: HttpService<Body>, E, W: Watcher<I, S, E>>: Clone {
1824
fn execute_new_svc(&mut self, fut: NewSvcTask<I, N, S, E, W>);
1925
}
@@ -60,6 +66,7 @@ impl fmt::Debug for Exec {
6066
}
6167
}
6268

69+
#[cfg(feature = "server")]
6370
impl<F, B> ConnStreamExec<F, B> for Exec
6471
where
6572
H2Stream<F, B>: Future<Output = ()> + Send + 'static,
@@ -70,6 +77,7 @@ where
7077
}
7178
}
7279

80+
#[cfg(feature = "server")]
7381
impl<I, N, S, E, W> NewSvcExec<I, N, S, E, W> for Exec
7482
where
7583
NewSvcTask<I, N, S, E, W>: Future<Output = ()> + Send + 'static,
@@ -83,6 +91,7 @@ where
8391

8492
// ==== impl Executor =====
8593

94+
#[cfg(feature = "server")]
8695
impl<E, F, B> ConnStreamExec<F, B> for E
8796
where
8897
E: Executor<H2Stream<F, B>> + Clone,
@@ -94,6 +103,7 @@ where
94103
}
95104
}
96105

106+
#[cfg(feature = "server")]
97107
impl<I, N, S, E, W> NewSvcExec<I, N, S, E, W> for E
98108
where
99109
E: Executor<NewSvcTask<I, N, S, E, W>> + Clone,
@@ -119,7 +129,7 @@ pub struct H2Stream<F, B>(std::marker::PhantomData<(F, B)>);
119129
impl<F, B, E> Future for H2Stream<F, B>
120130
where
121131
F: Future<Output = Result<http::Response<B>, E>>,
122-
B: HttpBody,
132+
B: crate::body::HttpBody,
123133
B::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
124134
E: Into<Box<dyn std::error::Error + Send + Sync>>,
125135
{

src/common/io/rewind.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub(crate) struct Rewind<T> {
1515

1616
impl<T> Rewind<T> {
1717
#[cfg(any(feature = "http2", test))]
18+
#[cfg(feature = "server")]
1819
pub(crate) fn new(io: T) -> Self {
1920
Rewind {
2021
pre: None,
@@ -29,7 +30,7 @@ impl<T> Rewind<T> {
2930
}
3031
}
3132

32-
#[cfg(any(all(feature = "http1", feature = "http2"), test))]
33+
#[cfg(any(all(feature = "http1", feature = "http2", feature = "server"), test))]
3334
pub(crate) fn rewind(&mut self, bs: Bytes) {
3435
debug_assert!(self.pre.is_none());
3536
self.pre = Some(bs);

src/common/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ macro_rules! ready {
99

1010
pub(crate) mod buf;
1111
#[cfg(any(feature = "http1", feature = "http2"))]
12+
#[cfg(feature = "server")]
1213
pub(crate) mod date;
1314
#[cfg(any(feature = "http1", feature = "http2"))]
15+
#[cfg(feature = "server")]
1416
pub(crate) mod drain;
1517
#[cfg(any(feature = "http1", feature = "http2"))]
1618
pub(crate) mod exec;
@@ -24,15 +26,14 @@ pub(crate) mod sync_wrapper;
2426
pub(crate) mod task;
2527
pub(crate) mod watch;
2628

27-
//#[cfg(any(feature = "http1", feature = "http2"))]
28-
//pub(crate) use self::exec::{BoxSendFuture, Exec};
2929
#[cfg(any(feature = "http1", feature = "http2"))]
3030
#[cfg(feature = "client")]
3131
pub(crate) use self::lazy::{lazy, Started as Lazy};
3232
pub use self::never::Never;
3333
pub(crate) use self::task::Poll;
3434

3535
// group up types normally needed for `Future`
36-
#[cfg(any(feature = "http1", feature = "http2"))]
37-
pub(crate) use std::marker::Unpin;
36+
cfg_proto! {
37+
pub(crate) use std::marker::Unpin;
38+
}
3839
pub(crate) use std::{future::Future, pin::Pin};

src/error.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,15 @@ pub(crate) enum Kind {
3636
/// Error occurred while connecting.
3737
Connect,
3838
/// Error creating a TcpListener.
39-
#[cfg(all(any(feature = "http1", feature = "http2"), feature = "tcp"))]
39+
#[cfg(all(
40+
any(feature = "http1", feature = "http2"),
41+
feature = "tcp",
42+
feature = "server"
43+
))]
4044
Listen,
4145
/// Error accepting on an Incoming stream.
4246
#[cfg(any(feature = "http1", feature = "http2"))]
47+
#[cfg(feature = "server")]
4348
Accept,
4449
/// Error while reading a body from connection.
4550
#[cfg(any(feature = "http1", feature = "http2", feature = "stream"))]
@@ -77,6 +82,7 @@ pub(crate) enum User {
7782
Body,
7883
/// Error calling user's MakeService.
7984
#[cfg(any(feature = "http1", feature = "http2"))]
85+
#[cfg(feature = "server")]
8086
MakeService,
8187
/// Error from future of user's Service.
8288
#[cfg(any(feature = "http1", feature = "http2"))]
@@ -85,6 +91,7 @@ pub(crate) enum User {
8591
///
8692
/// For example, sending both `content-length` and `transfer-encoding`.
8793
#[cfg(feature = "http1")]
94+
#[cfg(feature = "server")]
8895
UnexpectedHeader,
8996
/// User tried to create a Request with bad version.
9097
#[cfg(any(feature = "http1", feature = "http2"))]
@@ -96,6 +103,7 @@ pub(crate) enum User {
96103
UnsupportedRequestMethod,
97104
/// User tried to respond with a 1xx (not 101) response code.
98105
#[cfg(feature = "http1")]
106+
#[cfg(feature = "server")]
99107
UnsupportedStatusCode,
100108
/// User tried to send a Request with Client with non-absolute URI.
101109
#[cfg(any(feature = "http1", feature = "http2"))]
@@ -178,6 +186,7 @@ impl Error {
178186
}
179187

180188
#[cfg(feature = "http1")]
189+
#[cfg(feature = "server")]
181190
pub(crate) fn kind(&self) -> &Kind {
182191
&self.inner.kind
183192
}
@@ -234,11 +243,13 @@ impl Error {
234243
}
235244

236245
#[cfg(all(any(feature = "http1", feature = "http2"), feature = "tcp"))]
246+
#[cfg(feature = "server")]
237247
pub(crate) fn new_listen<E: Into<Cause>>(cause: E) -> Error {
238248
Error::new(Kind::Listen).with(cause)
239249
}
240250

241251
#[cfg(any(feature = "http1", feature = "http2"))]
252+
#[cfg(feature = "server")]
242253
pub(crate) fn new_accept<E: Into<Cause>>(cause: E) -> Error {
243254
Error::new(Kind::Accept).with(cause)
244255
}
@@ -272,6 +283,7 @@ impl Error {
272283
}
273284

274285
#[cfg(feature = "http1")]
286+
#[cfg(feature = "server")]
275287
pub(crate) fn new_user_header() -> Error {
276288
Error::new_user(User::UnexpectedHeader)
277289
}
@@ -289,6 +301,7 @@ impl Error {
289301
}
290302

291303
#[cfg(feature = "http1")]
304+
#[cfg(feature = "server")]
292305
pub(crate) fn new_user_unsupported_status_code() -> Error {
293306
Error::new_user(User::UnsupportedStatusCode)
294307
}
@@ -309,6 +322,7 @@ impl Error {
309322
}
310323

311324
#[cfg(any(feature = "http1", feature = "http2"))]
325+
#[cfg(feature = "server")]
312326
pub(crate) fn new_user_make_service<E: Into<Cause>>(cause: E) -> Error {
313327
Error::new_user(User::MakeService).with(cause)
314328
}
@@ -354,8 +368,10 @@ impl Error {
354368
Kind::Connect => "error trying to connect",
355369
Kind::Canceled => "operation was canceled",
356370
#[cfg(all(any(feature = "http1", feature = "http2"), feature = "tcp"))]
371+
#[cfg(feature = "server")]
357372
Kind::Listen => "error creating server listener",
358373
#[cfg(any(feature = "http1", feature = "http2"))]
374+
#[cfg(feature = "server")]
359375
Kind::Accept => "error accepting connection",
360376
#[cfg(any(feature = "http1", feature = "http2", feature = "stream"))]
361377
Kind::Body => "error reading a body from connection",
@@ -372,10 +388,12 @@ impl Error {
372388
#[cfg(any(feature = "http1", feature = "http2"))]
373389
Kind::User(User::Body) => "error from user's HttpBody stream",
374390
#[cfg(any(feature = "http1", feature = "http2"))]
391+
#[cfg(feature = "server")]
375392
Kind::User(User::MakeService) => "error from user's MakeService",
376393
#[cfg(any(feature = "http1", feature = "http2"))]
377394
Kind::User(User::Service) => "error from user's Service",
378395
#[cfg(feature = "http1")]
396+
#[cfg(feature = "server")]
379397
Kind::User(User::UnexpectedHeader) => "user sent unexpected header",
380398
#[cfg(any(feature = "http1", feature = "http2"))]
381399
#[cfg(feature = "client")]
@@ -384,6 +402,7 @@ impl Error {
384402
#[cfg(feature = "client")]
385403
Kind::User(User::UnsupportedRequestMethod) => "request has unsupported HTTP method",
386404
#[cfg(feature = "http1")]
405+
#[cfg(feature = "server")]
387406
Kind::User(User::UnsupportedStatusCode) => {
388407
"response has 1xx status code, not supported by server"
389408
}

src/headers.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ fn connection_has(value: &HeaderValue, needle: &str) -> bool {
3030
}
3131

3232
#[cfg(feature = "http1")]
33+
#[cfg(feature = "server")]
3334
pub fn content_length_parse(value: &HeaderValue) -> Option<u64> {
3435
value.to_str().ok().and_then(|s| s.parse().ok())
3536
}

src/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,9 @@ pub mod rt;
6868
pub mod service;
6969
pub mod upgrade;
7070

71-
cfg_any_http! {
71+
cfg_proto! {
7272
mod headers;
7373
mod proto;
74-
pub mod server;
75-
76-
pub use crate::server::Server;
7774
}
7875

7976
cfg_feature! {
@@ -82,3 +79,10 @@ cfg_feature! {
8279
pub mod client;
8380
pub use crate::client::Client;
8481
}
82+
83+
cfg_feature! {
84+
#![all(feature = "server", any(feature = "http1", feature = "http2"))]
85+
86+
pub mod server;
87+
pub use crate::server::Server;
88+
}

0 commit comments

Comments
 (0)