From de4031dd70a80e0b645d6e456510e309bacd4c92 Mon Sep 17 00:00:00 2001 From: Roland Hieber Date: Sat, 11 Mar 2017 05:45:44 +0100 Subject: [PATCH] refactor HTTP parsing We don't need the headers at all afterwards, but when parsing XML, they are in the way. The "found no matches" error is no longer needed in the future. --- Org.pm | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/Org.pm b/Org.pm index 8f699a7..f5227b6 100644 --- a/Org.pm +++ b/Org.pm @@ -197,32 +197,36 @@ Accept-Language: en_US, en\r\n); # # parse dict.leo.org output # - my @line = <$conn>; - close $conn or die "Connection failed: $!\n"; - $this->debug( "connection: done"); - - $site = join "", @line; - - if ($site !~ /HTTP\/1\.(0|1) 200 OK/i) { - if ($site =~ /HTTP\/1\.(0|1) (\d+) /i) { - # got HTTP error - my $err = $2; - if ($err == 407) { - croak "proxy auth required or access denied!\n"; - } - else { - if ($site =~ /Leider konnten wir zu Ihrem Suchbegriff/ || - $site =~ /found no matches for your search/ - ) { + $site = ""; + my $got_headers = 0; + while (<$conn>) { + if ($got_headers) { + $site .= $_; + } + elsif (/^\r?$/) { + $got_headers = 1; + } + elsif ($_ !~ /HTTP\/1\.(0|1) 200 OK/i) { + if (/HTTP\/1\.(0|1) (\d+) /i) { + # got HTTP error + my $err = $2; + if ($err == 407) { + croak "proxy auth required or access denied!\n"; + close $conn; return (); } else { croak "got HTTP error $err!\n"; + close $conn; + return (); } } } } + close $conn or die "Connection failed: $!\n"; + $this->debug( "connection: done"); + my @request = ( { id => 2,