… | |
… | |
733 | |
733 | |
734 | 1 |
734 | 1 |
735 | } |
735 | } |
736 | }; |
736 | }; |
737 | |
737 | |
|
|
738 | =item regex => $accept[, $reject[, $skip], $cb->($data) |
|
|
739 | |
|
|
740 | Makes a regex match against the regex object C<$accept> and returns |
|
|
741 | everything up to and including the match. |
|
|
742 | |
|
|
743 | Example: read a single line terminated by '\n'. |
|
|
744 | |
|
|
745 | $handle->push_read (regex => qr<\n>, sub { ... }); |
|
|
746 | |
|
|
747 | If C<$reject> is given and not undef, then it determines when the data is |
|
|
748 | to be rejected: it is matched against the data when the C<$accept> regex |
|
|
749 | does not match and generates an C<EBADMSG> error when it matches. This is |
|
|
750 | useful to quickly reject wrong data (to avoid waiting for a timeout or a |
|
|
751 | receive buffer overflow). |
|
|
752 | |
|
|
753 | Example: expect a single decimal number followed by whitespace, reject |
|
|
754 | anything else (not the use of an anchor). |
|
|
755 | |
|
|
756 | $handle->push_read (regex => qr<^[0-9]+\s>, qr<[^0-9]>, sub { ... }); |
|
|
757 | |
|
|
758 | If C<$skip> is given and not C<undef>, then it will be matched against |
|
|
759 | the receive buffer when neither C<$accept> nor C<$reject> match, |
|
|
760 | and everything preceding and including the match will be accepted |
|
|
761 | unconditionally. This is useful to skip large amounts of data that you |
|
|
762 | know cannot be matched, so that the C<$accept> or C<$reject> regex do not |
|
|
763 | have to start matching from the beginning. This is purely an optimisation |
|
|
764 | and is usually worth only when you expect more than a few kilobytes. |
|
|
765 | |
|
|
766 | Example: expect a http header, which ends at C<\015\012\015\012>. Since we |
|
|
767 | expect the header to be very large (it isn't in practise, but...), we use |
|
|
768 | a skip regex to skip initial portions. The skip regex is tricky in that |
|
|
769 | it only accepts something not ending in either \015 or \012, as these are |
|
|
770 | required for the accept regex. |
|
|
771 | |
|
|
772 | $handle->push_read (regex => |
|
|
773 | qr<\015\012\015\012>, |
|
|
774 | undef, # no reject |
|
|
775 | qr<^.*[^\015\012]>, |
|
|
776 | sub { ... }); |
|
|
777 | |
|
|
778 | =cut |
|
|
779 | |
|
|
780 | register_read_type regex => sub { |
|
|
781 | my ($self, $cb, $accept, $reject, $skip) = @_; |
|
|
782 | |
|
|
783 | my $data; |
|
|
784 | my $rbuf = \$self->{rbuf}; |
|
|
785 | |
|
|
786 | sub { |
|
|
787 | # accept |
|
|
788 | if ($$rbuf =~ $accept) { |
|
|
789 | $data .= substr $$rbuf, 0, $+[0], ""; |
|
|
790 | $cb->($self, $data); |
|
|
791 | return 1; |
|
|
792 | } |
|
|
793 | |
|
|
794 | # reject |
|
|
795 | if ($reject && $$rbuf =~ $reject) { |
|
|
796 | $! = &Errno::EBADMSG; |
|
|
797 | $self->error; |
|
|
798 | } |
|
|
799 | |
|
|
800 | # skip |
|
|
801 | if ($skip && $$rbuf =~ $skip) { |
|
|
802 | $data .= substr $$rbuf, 0, $+[0], ""; |
|
|
803 | } |
|
|
804 | |
|
|
805 | () |
|
|
806 | } |
|
|
807 | }; |
|
|
808 | |
738 | =back |
809 | =back |
739 | |
810 | |
740 | =item AnyEvent::Handle::register_read_type type => $coderef->($self, $cb, @args) |
811 | =item AnyEvent::Handle::register_read_type type => $coderef->($self, $cb, @args) |
741 | |
812 | |
742 | This function (not method) lets you add your own types to C<push_read>. |
813 | This function (not method) lets you add your own types to C<push_read>. |