Skip to content

Commit 3a7d79b

Browse files
committed
Do not always require an authority
This fixes connections where a local UNIX domain socket path is provided, where the authority contains the full path to the *.sock file. Signed-off-by: Sascha Grunert <[email protected]>
1 parent 3bb01aa commit 3a7d79b

File tree

1 file changed

+90
-33
lines changed

1 file changed

+90
-33
lines changed

src/server.rs

+90-33
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@
116116
//! [`TcpListener`]: https://docs.rs/tokio-core/0.1/tokio_core/net/struct.TcpListener.html
117117
118118
use crate::codec::{Codec, RecvError, UserError};
119-
use crate::frame::{self, Pseudo, PushPromiseHeaderError, Reason, Settings, StreamId};
119+
use crate::frame::{
120+
self, Pseudo, PushPromiseHeaderError, Reason, Settings, StreamId,
121+
};
120122
use crate::proto::{self, Config, Prioritized};
121123
use crate::{FlowControl, PingPong, RecvStream, SendStream};
122124

@@ -396,15 +398,18 @@ where
396398
/// Accept the next incoming request on this connection.
397399
pub async fn accept(
398400
&mut self,
399-
) -> Option<Result<(Request<RecvStream>, SendResponse<B>), crate::Error>> {
401+
) -> Option<Result<(Request<RecvStream>, SendResponse<B>), crate::Error>>
402+
{
400403
futures_util::future::poll_fn(move |cx| self.poll_accept(cx)).await
401404
}
402405

403406
#[doc(hidden)]
404407
pub fn poll_accept(
405408
&mut self,
406409
cx: &mut Context<'_>,
407-
) -> Poll<Option<Result<(Request<RecvStream>, SendResponse<B>), crate::Error>>> {
410+
) -> Poll<
411+
Option<Result<(Request<RecvStream>, SendResponse<B>), crate::Error>>,
412+
> {
408413
// Always try to advance the internal state. Getting Pending also is
409414
// needed to allow this function to return Pending.
410415
if let Poll::Ready(_) = self.poll_closed(cx)? {
@@ -416,7 +421,8 @@ where
416421
if let Some(inner) = self.connection.next_incoming() {
417422
tracing::trace!("received incoming");
418423
let (head, _) = inner.take_request().into_parts();
419-
let body = RecvStream::new(FlowControl::new(inner.clone_to_opaque()));
424+
let body =
425+
RecvStream::new(FlowControl::new(inner.clone_to_opaque()));
420426

421427
let request = Request::from_parts(head, body);
422428
let respond = SendResponse { inner };
@@ -462,7 +468,10 @@ where
462468
///
463469
/// Returns an error if a previous call is still pending acknowledgement
464470
/// from the remote endpoint.
465-
pub fn set_initial_window_size(&mut self, size: u32) -> Result<(), crate::Error> {
471+
pub fn set_initial_window_size(
472+
&mut self,
473+
size: u32,
474+
) -> Result<(), crate::Error> {
466475
assert!(size <= proto::MAX_WINDOW_SIZE);
467476
self.connection.set_initial_window_size(size)?;
468477
Ok(())
@@ -481,13 +490,19 @@ where
481490
/// [`poll_accept`]: struct.Connection.html#method.poll_accept
482491
/// [`RecvStream`]: ../struct.RecvStream.html
483492
/// [`SendStream`]: ../struct.SendStream.html
484-
pub fn poll_closed(&mut self, cx: &mut Context) -> Poll<Result<(), crate::Error>> {
493+
pub fn poll_closed(
494+
&mut self,
495+
cx: &mut Context,
496+
) -> Poll<Result<(), crate::Error>> {
485497
self.connection.poll(cx).map_err(Into::into)
486498
}
487499

488500
#[doc(hidden)]
489501
#[deprecated(note = "renamed to poll_closed")]
490-
pub fn poll_close(&mut self, cx: &mut Context) -> Poll<Result<(), crate::Error>> {
502+
pub fn poll_close(
503+
&mut self,
504+
cx: &mut Context,
505+
) -> Poll<Result<(), crate::Error>> {
491506
self.poll_closed(cx)
492507
}
493508

@@ -539,7 +554,10 @@ where
539554
{
540555
type Item = Result<(Request<RecvStream>, SendResponse<B>), crate::Error>;
541556

542-
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
557+
fn poll_next(
558+
mut self: Pin<&mut Self>,
559+
cx: &mut Context<'_>,
560+
) -> Poll<Option<Self::Item>> {
543561
self.poll_accept(cx)
544562
}
545563
}
@@ -586,7 +604,9 @@ impl Builder {
586604
/// ```
587605
pub fn new() -> Builder {
588606
Builder {
589-
reset_stream_duration: Duration::from_secs(proto::DEFAULT_RESET_STREAM_SECS),
607+
reset_stream_duration: Duration::from_secs(
608+
proto::DEFAULT_RESET_STREAM_SECS,
609+
),
590610
reset_stream_max: proto::DEFAULT_RESET_STREAM_MAX,
591611
settings: Settings::default(),
592612
initial_target_connection_window_size: None,
@@ -1023,7 +1043,10 @@ impl<B: Buf> SendResponse<B> {
10231043
///
10241044
/// Calling this method after having called `send_response` will return
10251045
/// a user error.
1026-
pub fn poll_reset(&mut self, cx: &mut Context) -> Poll<Result<Reason, crate::Error>> {
1046+
pub fn poll_reset(
1047+
&mut self,
1048+
cx: &mut Context,
1049+
) -> Poll<Result<Reason, crate::Error>> {
10271050
self.inner.poll_reset(cx, proto::PollReset::AwaitingHeaders)
10281051
}
10291052

@@ -1095,7 +1118,10 @@ impl<B: Buf> SendPushedResponse<B> {
10951118
///
10961119
/// Calling this method after having called `send_response` will return
10971120
/// a user error.
1098-
pub fn poll_reset(&mut self, cx: &mut Context) -> Poll<Result<Reason, crate::Error>> {
1121+
pub fn poll_reset(
1122+
&mut self,
1123+
cx: &mut Context,
1124+
) -> Poll<Result<Reason, crate::Error>> {
10991125
self.inner.poll_reset(cx)
11001126
}
11011127

@@ -1124,9 +1150,13 @@ where
11241150
{
11251151
type Output = Result<Codec<T, B>, crate::Error>;
11261152

1127-
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
1153+
fn poll(
1154+
mut self: Pin<&mut Self>,
1155+
cx: &mut Context<'_>,
1156+
) -> Poll<Self::Output> {
11281157
// Flush the codec
1129-
ready!(self.codec.as_mut().unwrap().flush(cx)).map_err(crate::Error::from_io)?;
1158+
ready!(self.codec.as_mut().unwrap().flush(cx))
1159+
.map_err(crate::Error::from_io)?;
11301160

11311161
// Return the codec
11321162
Poll::Ready(Ok(self.codec.take().unwrap()))
@@ -1153,18 +1183,23 @@ where
11531183
{
11541184
type Output = Result<Codec<T, B>, crate::Error>;
11551185

1156-
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
1186+
fn poll(
1187+
mut self: Pin<&mut Self>,
1188+
cx: &mut Context<'_>,
1189+
) -> Poll<Self::Output> {
11571190
let mut buf = [0; 24];
11581191
let mut rem = PREFACE.len() - self.pos;
11591192

11601193
while rem > 0 {
11611194
let n = ready!(Pin::new(self.inner_mut()).poll_read(cx, &mut buf[..rem]))
11621195
.map_err(crate::Error::from_io)?;
11631196
if n == 0 {
1164-
return Poll::Ready(Err(crate::Error::from_io(io::Error::new(
1165-
io::ErrorKind::UnexpectedEof,
1166-
"connection closed before reading preface",
1167-
))));
1197+
return Poll::Ready(Err(crate::Error::from_io(
1198+
io::Error::new(
1199+
io::ErrorKind::UnexpectedEof,
1200+
"connection closed before reading preface",
1201+
),
1202+
)));
11681203
}
11691204

11701205
if PREFACE[self.pos..self.pos + n] != buf[..n] {
@@ -1190,7 +1225,10 @@ where
11901225
{
11911226
type Output = Result<Connection<T, B>, crate::Error>;
11921227

1193-
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
1228+
fn poll(
1229+
mut self: Pin<&mut Self>,
1230+
cx: &mut Context<'_>,
1231+
) -> Poll<Self::Output> {
11941232
let span = self.span.clone(); // XXX(eliza): T_T
11951233
let _e = span.enter();
11961234
tracing::trace!(state = ?self.state);
@@ -1245,7 +1283,8 @@ where
12451283

12461284
tracing::trace!("connection established!");
12471285
let mut c = Connection { connection };
1248-
if let Some(sz) = self.builder.initial_target_connection_window_size {
1286+
if let Some(sz) = self.builder.initial_target_connection_window_size
1287+
{
12491288
c.set_target_window_size(sz);
12501289
}
12511290
Ok(c)
@@ -1383,7 +1422,9 @@ impl proto::Peer for Peer {
13831422

13841423
// Specifying :status for a request is a protocol error
13851424
if pseudo.status.is_some() {
1386-
tracing::trace!("malformed headers: :status field on request; PROTOCOL_ERROR");
1425+
tracing::trace!(
1426+
"malformed headers: :status field on request; PROTOCOL_ERROR"
1427+
);
13871428
return Err(RecvError::Connection(Reason::PROTOCOL_ERROR));
13881429
}
13891430

@@ -1393,14 +1434,21 @@ impl proto::Peer for Peer {
13931434
// A request translated from HTTP/1 must not include the :authority
13941435
// header
13951436
if let Some(authority) = pseudo.authority {
1396-
let maybe_authority = uri::Authority::from_maybe_shared(authority.clone().into_inner());
1397-
parts.authority = Some(maybe_authority.or_else(|why| {
1398-
malformed!(
1399-
"malformed headers: malformed authority ({:?}): {}",
1400-
authority,
1401-
why,
1402-
)
1403-
})?);
1437+
// When connecting to a UNIX Domain Socket (UDS), then we might get a path for the
1438+
// authority field. If it's a local path and exists, then we do not error in that case
1439+
// and assume an UDS.
1440+
if !authority.as_str().ends_with(".sock") {
1441+
let maybe_authority = uri::Authority::from_maybe_shared(
1442+
authority.clone().into_inner(),
1443+
);
1444+
parts.authority = Some(maybe_authority.or_else(|why| {
1445+
malformed!(
1446+
"malformed headers: malformed authority ({:?}): {}",
1447+
authority,
1448+
why,
1449+
)
1450+
})?);
1451+
}
14041452
}
14051453

14061454
// A :scheme is required, except CONNECT.
@@ -1437,9 +1485,14 @@ impl proto::Peer for Peer {
14371485
malformed!("malformed headers: missing path");
14381486
}
14391487

1440-
let maybe_path = uri::PathAndQuery::from_maybe_shared(path.clone().into_inner());
1488+
let maybe_path =
1489+
uri::PathAndQuery::from_maybe_shared(path.clone().into_inner());
14411490
parts.path_and_query = Some(maybe_path.or_else(|why| {
1442-
malformed!("malformed headers: malformed path ({:?}): {}", path, why,)
1491+
malformed!(
1492+
"malformed headers: malformed path ({:?}): {}",
1493+
path,
1494+
why,
1495+
)
14431496
})?);
14441497
}
14451498

@@ -1474,7 +1527,9 @@ where
14741527
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
14751528
match *self {
14761529
Handshaking::Flushing(_) => write!(f, "Handshaking::Flushing(_)"),
1477-
Handshaking::ReadingPreface(_) => write!(f, "Handshaking::ReadingPreface(_)"),
1530+
Handshaking::ReadingPreface(_) => {
1531+
write!(f, "Handshaking::ReadingPreface(_)")
1532+
}
14781533
Handshaking::Empty => write!(f, "Handshaking::Empty"),
14791534
}
14801535
}
@@ -1498,7 +1553,9 @@ where
14981553
{
14991554
#[inline]
15001555
fn from(read: ReadPreface<T, Prioritized<B>>) -> Self {
1501-
Handshaking::ReadingPreface(read.instrument(tracing::trace_span!("read_preface")))
1556+
Handshaking::ReadingPreface(
1557+
read.instrument(tracing::trace_span!("read_preface")),
1558+
)
15021559
}
15031560
}
15041561

0 commit comments

Comments
 (0)