Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Suppress 400 if client disconnected (mk 2) #20

Merged
merged 3 commits into from

2 participants

@athomason

This updates #18, more correctly this time. This is a new pull requests github didn't pull the latest commit into that one.

@miyagawa miyagawa merged commit 505bb86 into miyagawa:master
@miyagawa
Owner

Just a thought - is it possible to write a unit test for this?

@athomason

Sure, I've added one (845ceda) that tests that empty or incomplete-header requests followed by a shutdown(2) call cause Twiggy to not respond with anything. This doesn't cover the case where the client calls close(2), but since both events are seen as PIPE signals on the server side, that should be ok. A test that actually watches the wire with tcpdump or something (which is how I noticed this originally) seems rather more complicated than necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 4, 2011
  1. @athomason
Commits on Sep 2, 2011
  1. @athomason
Commits on Sep 8, 2011
  1. @athomason
This page is out of date. Refresh to see the latest.
Showing with 15 additions and 11 deletions.
  1. +15 −11 lib/Twiggy/Server.pm
View
26 lib/Twiggy/Server.pm
@@ -160,7 +160,8 @@ sub _accept_handler {
1;
}) {
- $self->_bad_request($sock);
+ my $disconnected = ($@ =~ /^client disconnected/);
+ $self->_bad_request($sock, $disconnected);
}
};
}
@@ -217,24 +218,27 @@ sub _create_req_parsing_watcher {
} catch {
undef $headers_io_watcher;
undef $timeout_timer;
- $self->_bad_request($sock);
+ my $disconnected = /^client disconnected/;
+ $self->_bad_request($sock, $disconnected);
}
};
}
sub _bad_request {
- my ( $self, $sock ) = @_;
+ my ( $self, $sock, $disconnected ) = @_;
return unless defined $sock and defined fileno $sock;
- $self->_write_psgi_response(
- $sock,
- [
- 400,
- [ 'Content-Type' => 'text/plain' ],
- [ ],
- ],
- );
+ my $response = [
+ 400,
+ [ 'Content-Type' => 'text/plain' ],
+ [ ],
+ ];
+
+ # if client is already gone, don't try to write to it
+ $response = [] if $disconnected;
+
+ $self->_write_psgi_response($sock, $response);
return;
}
Something went wrong with that request. Please try again.