@@ -545,19 +545,16 @@ sub DESTROY {
545
545
546
546
# Handles timeout processing for a given http reguest
547
547
# Returns 1 if timeout expipres. Calcultes next timeout_at
548
- sub handle_timeout {
548
+ sub _handle_timeout {
549
549
my $self = shift ;
550
550
my $hashref = shift ;
551
551
552
552
my $id = $hashref -> {id };
553
553
my $s = $hashref -> {handle };
554
554
555
-
556
555
# Check that we have not timed-out.
557
556
if ( time > $hashref -> {timeout_at }
558
- || time > $hashref -> {finish_by } )
559
- {
560
-
557
+ || time > $hashref -> {finish_by } ) {
561
558
# warn sprintf "Timeout: %.3f > %.3f", #
562
559
# time, $hashref->{timeout_at};
563
560
@@ -569,32 +566,30 @@ sub handle_timeout {
569
566
content => ' Timed out' ,
570
567
);
571
568
572
- $self -> _io_select -> remove ($s );
569
+ $self -> _remove_io_select ($s );
573
570
delete $$self {fileno_to_id }{ $s -> fileno };
574
571
return 1;
575
572
}
576
573
577
- # Reset the timeout.
578
- $hashref -> {timeout_at } = time + $self -> _get_opt( ' timeout' , $id );
579
- # warn "recieved - timeout set to '$hashref->{timeout_at}'";
580
574
581
575
return 0;
582
-
583
-
584
576
}
585
577
586
578
# Handles async ssl connection. Should be called repeatedly until ssl connection is fully established
587
- sub handle_ssl {
579
+ sub _handle_ssl {
588
580
my $self = shift ;
589
581
my $hashref = shift ;
590
582
591
583
my $id = $hashref -> {id };
592
584
my $s = $hashref -> {handle };
585
+ my $reset_timeout = 0;
593
586
594
587
if (defined ($s -> connect_SSL()))
595
588
{
596
589
$hashref -> {ssl_connected } = 1;
597
- $self -> write_request($hashref );
590
+ $self -> _write_request($hashref );
591
+ $reset_timeout = 1;
592
+ $self -> _io_write_select-> remove($s ); # remove from write only
598
593
}
599
594
elsif ($! == EWOULDBLOCK)
600
595
{
@@ -610,10 +605,16 @@ sub handle_ssl {
610
605
' request' => $hashref -> {request },
611
606
' previous' => $hashref -> {previous }
612
607
);
613
- $self -> _io_select -> remove ($s );
608
+ $self -> _remove_io_select ($s );
614
609
delete $$self {fileno_to_id }{ $s -> fileno };
615
610
}
616
611
612
+ if ( $reset_timeout )
613
+ {
614
+ # Reset the timeout.
615
+ $hashref -> {timeout_at } = time + $self -> _get_opt( ' timeout' , $id );
616
+ # warn "recieved - timeout set to '$hashref->{timeout_at}'";
617
+ }
617
618
}
618
619
619
620
# Go through all the values on the select list and check to see if
@@ -624,7 +625,7 @@ sub _process_in_progress {
624
625
my %seen_ids = ();
625
626
626
627
HANDLE_WRITE:
627
- foreach my $s ($self -> _io_select -> can_write(0))
628
+ foreach my $s ($self -> _io_write_select -> can_write(0))
628
629
{
629
630
my $id = $self -> {fileno_to_id }{ $s -> fileno }
630
631
|| die " INTERNAL ERROR: could not got id for fileno" ;
@@ -633,17 +634,18 @@ sub _process_in_progress {
633
634
my $hashref = $$self {in_progress }{$id };
634
635
635
636
# only pending ssl connection requires writes
636
- if (!defined ($hashref -> {ssl_connected }) || $hashref -> {ssl_connected } )
637
+ if ( !defined ($hashref -> {ssl_connected }) || $hashref -> {ssl_connected } )
637
638
{
639
+ # FIXME: possible error here?
638
640
next HANDLE_WRITE;
639
641
}
640
642
641
- if ($self -> handle_timeout ($hashref ))
643
+ if ($self -> _handle_timeout ($hashref ))
642
644
{
643
645
next HANDLE_WRITE;
644
646
}
645
647
646
- $self -> handle_ssl ($hashref );
648
+ $self -> _handle_ssl ($hashref );
647
649
}
648
650
649
651
HANDLE_READ:
@@ -660,15 +662,15 @@ sub _process_in_progress {
660
662
661
663
# warn Dumper $hashref;
662
664
663
- if ($self -> handle_timeout ($hashref ))
665
+ if ($self -> _handle_timeout ($hashref ))
664
666
{
665
667
next HANDLE_READ;
666
668
}
667
669
668
670
# only pending ssl connection requires writes
669
671
if (defined ($hashref -> {ssl_connected }) && !$hashref -> {ssl_connected } )
670
672
{
671
- $self -> handle_ssl ($hashref );
673
+ $self -> _handle_ssl ($hashref );
672
674
673
675
next HANDLE_READ;
674
676
}
@@ -704,7 +706,7 @@ sub _process_in_progress {
704
706
' request' => $hashref -> {request },
705
707
' previous' => $hashref -> {previous }
706
708
);
707
- $self -> _io_select -> remove ($s );
709
+ $self -> _remove_io_select ($s );
708
710
delete $$self {fileno_to_id }{ $s -> fileno };
709
711
next HANDLE_READ;
710
712
}
@@ -721,11 +723,15 @@ sub _process_in_progress {
721
723
}
722
724
}
723
725
726
+ # Reset the timeout.
727
+ $hashref -> {timeout_at } = time + $self -> _get_opt( ' timeout' , $id );
728
+ # warn "recieved - timeout set to '$hashref->{timeout_at}'";
729
+
724
730
# If the message is complete then create a request and add it
725
731
# to 'to_return';
726
732
if ( $$tmp {is_complete } ) {
727
733
delete $$self {fileno_to_id }{ $s -> fileno };
728
- $self -> _io_select -> remove ($s );
734
+ $self -> _remove_io_select ($s );
729
735
730
736
# warn Dumper $$hashref{content};
731
737
@@ -832,7 +838,7 @@ sub _process_in_progress {
832
838
);
833
839
834
840
my $s = $hashref -> {handle };
835
- $self -> _io_select -> remove ($s );
841
+ $self -> _remove_io_select ($s );
836
842
delete $$self {fileno_to_id }{ $s -> fileno };
837
843
}
838
844
}
@@ -934,10 +940,9 @@ sub _send_request {
934
940
);
935
941
}
936
942
my $s = eval { $net_http_class -> new(%args ) };
937
-
938
943
# We could not create a request - fake up a 503 response with
939
944
# error as content.
940
- if (!$s || defined ($s -> connect_SSL()) || $! != EWOULDBLOCK)
945
+ if (!$s || ( $ssl_en && ( defined ($s -> connect_SSL()) || $! != EWOULDBLOCK) ) )
941
946
{
942
947
$self -> _add_error_response_to_return(
943
948
id => $id ,
@@ -970,7 +975,6 @@ sub _send_request {
970
975
# 0: ssl https, conenction is not fully established yet
971
976
# 1: ssl https, connection is ready, possible to conitnue with send_request
972
977
$entry -> {ssl_connected } = $ssl_en ? 0 : undef ;
973
-
974
978
975
979
$entry -> {timeout_at } = $time + $self -> _get_opt( ' timeout' , $id );
976
980
# warn "sent - timeout set to '$entry->{timeout_at}'";
@@ -981,11 +985,18 @@ sub _send_request {
981
985
$entry -> {redirects_left } = $self -> _get_opt( ' max_redirect' , $id )
982
986
unless exists $entry -> {redirects_left };
983
987
988
+ if ( $ssl_en ) {
989
+ $self -> _io_write_select-> add($s ); # only add SSL requests to the can_write IO select
990
+ }
991
+ else {
992
+ $self -> _write_request($entry );
993
+ }
994
+
984
995
return 1;
985
996
}
986
997
987
998
# write_request is called after ssl_connection is fully established to transmit an http request over the ssl
988
- sub write_request {
999
+ sub _write_request {
989
1000
my $this = shift ;
990
1001
my $entry = shift ;
991
1002
@@ -1002,8 +1013,6 @@ sub write_request {
1002
1013
croak " Could not write request to @{[$request ->uri]} '$! '"
1003
1014
unless $entry -> {handle }-> write_request( $request -> method, $request_uri , %headers ,
1004
1015
$request -> content );
1005
-
1006
-
1007
1016
}
1008
1017
1009
1018
sub _strip_host_from_uri {
@@ -1023,6 +1032,17 @@ sub _io_select {
1023
1032
return $$self {io_select } ||= IO::Select-> new();
1024
1033
}
1025
1034
1035
+ sub _io_write_select {
1036
+ my $self = shift ;
1037
+ return $$self {io_write_select } ||= IO::Select-> new();
1038
+ }
1039
+
1040
+ sub _remove_io_select {
1041
+ my ($self ,$s ) = @_ ;
1042
+ $self -> _io_select-> remove($s );
1043
+ $self -> _io_write_select-> remove($s );
1044
+ }
1045
+
1026
1046
sub _make_url_absolute {
1027
1047
my %args = @_ ;
1028
1048
0 commit comments