Incorrect example of Net::HTTP::NB usage [rt.cpan.org #107770]
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 35/100
- Loại issue
- Tài liệu
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- perl
- Lĩnh vực
- documentation
Hướng nghiên cứu
Bắt đầu với ví dụ SYNOPSIS của Net::HTTP::NB được mô tả trong issue và tái hiện ví dụ đó với máy chủ kiểm thử keep-alive được cung cấp. Xem xét cách read_response_headers và read_entity_body tương tác với bộ đệm nội bộ, sau đó cập nhật ví dụ trong tài liệu để dữ liệu phản hồi đã được đệm được xử lý đúng cách. Xác nhận rằng ví dụ đã sửa nhận được phần thân mà không bị hết thời gian chờ.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Migrated from rt.cpan.org#107770 (status was 'new')
Requestors:
From [email protected] on 2015-10-14 10:15:16:
In SYNOPSIS section we can see an example of how you can send request and read response with Net::HTTP::NB. But this example has potential problems. Let's see this test server:
use strict;
use IO::Socket;
my $serv = IO::Socket::INET->new(Listen => 10, LocalPort => 8080)
or die $@;
my $body = 'This is the body';
my $header = join(
"\r\n",
"HTTP/1.1 200 OK",
"Server: nginx/1.0.4",
"Date: Thu, 06 Oct 2011 16:14:01 GMT",
"Content-Type: text/html",
"Content-Length: ".length($body),
"Connection: keep-alive",
"Vary: Accept-Encoding",
"X-Powered-By: PHP/5.3.6",
"\r\n"
);
while (warn("waiting for next request...\n") and my $client = $serv->accept()) {
my $req;
while ($req !~ /\r\n\r\n$/) {
$client->sysread($req, 1024, length $req) or die $!;
}
$client->syswrite($header.$body);
<$client>; # keep-alive ;)
}
__END__
And this client (a little reworked example from SYNOPSIS):
use strict;
use Net::HTTP::NB;
my $s = Net::HTTP::NB->new(Host => "localhost:8080") || die $@;
$s->write_request(GET => "/", 'User-Agent' => "Mozilla/5.0");
use IO::Select;
my $sel = IO::Select->new($s);
READ_HEADER: {
die "Header timeout" unless $sel->can_read(10);
my($code, $mess, %h) = $s->read_response_headers;
redo READ_HEADER unless $code;
}
while (1) {
die "Body timeout" unless $sel->can_read(10);
my $buf;
my $n = $s->read_entity_body($buf, 1024);
last unless $n;
print $buf;
}
__END__
And output of this client will be "Body timeout" error, instead of expected "This is the body" body content.
The problem is that Net::HTTP::Methods internally uses a buffer when reading data from the server. Here read_response_headers() call readed both headers and data, returned headers for us and stored body in the buffer. So, body now in the buffer instead of a socket and socket will not be available for read anymore (until server will close connection, but our uses keep-alive, so will not do it). This is why our can_read(10) call timed out after 10 seconds.
And this is how this example may looks like to work properly:
use strict;
use Net::HTTP::NB;
use Errno qw/EAGAIN EWOULDBLOCK/;
my $s = Net::HTTP::NB->new(Host => "localhost:8080", KeepAlive => 1) || die $@;
$s->write_request(GET => "/", 'User-Agent' => "Mozilla/5.0");
use IO::Select;
my $sel = IO::Select->new($s);
READ_HEADER: {
die "Header timeout" unless $sel->can_read(10);
my($code, $mess, %h) = $s->read_response_headers;
redo READ_HEADER unless $code;
}
# Net::HTTP::NB uses internal buffer, so we should check it before
# socket check by calling read_entity_body()
# make socket non-blocking, so read_entity_body() will not block
$s->blocking(0);
while (1) {
my $buf;
my $n;
# try to read until error or all data received
while (1) {
my $tmp_buf;
$n = $s->read_entity_body($tmp_buf, 1024);
if ($n == -1 || (!defined($n) && ($! == EWOULDBLOCK || $! == EAGAIN))) {
last; # no data available this time
}
elsif ($n) {
$buf .= $tmp_buf; # data received
}
elsif (defined $n) {
last; # $n == 0, all readed
}
else {
die "Read error occured: ", $!; # $n == undef
}
}
print $buf if length $buf;
last if defined $n && $n == 0; # all readed
die "Body timeout" unless $sel->can_read(10); # wait for new data
}
__END__
The bad news is that most modules which uses Net::HTTP::NB doing it wrong, as showed in the example from the documentation.
- Ngôn ngữ chính
- Perl
- Star
- 20
- Fork
- 27
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của libwww-perl/Net-HTTP
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 50/100
libwww-perl/Net-HTTP#99 ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 45/100
libwww-perl/Net-HTTP#95 ·
-
t/live.t may failĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 45/100
libwww-perl/Net-HTTP#87 · 2 bình luận ·
-
Improve error messageĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 45/100
libwww-perl/Net-HTTP#79 · 6 bình luận ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 48/100
libwww-perl/Net-HTTP#74 ·
Tất cả issue của libwww-perl/Net-HTTP
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
libwww-perl/HTTP-Message#236 ·
-
1.severity: security
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
-
RTCP SR/RR length check ignores the fixed header, report blocks are read past the packet endĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 2 ngày
-
wave-followup
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
-
benchmarks/README: 'perl BNG2.pl model.bngl' runs the whole protocol, not just generate_networkĐang mởdocumentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày