Gungho/Request/http.pm untested

filter: Statementstmt 0.0% 0 / 60 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 130 CC 16 SCAR 52.9
CC
16
Cov
0%
format
272.0
new
2.0
prepare_request
2.0
CRAP 198.4
1 # $Id: /mirror/gungho/lib/Gungho/Request/http.pm 2473 2007-09-04T07:08:58.221716Z lestrrat $
2 #
3 # Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp>
4 # All rightsreserved.
5  
6 package Gungho::Request::http;
7 use strict;
8 use warnings;
9 use base qw(Gungho::Base);
10  
11 __PACKAGE__->mk_accessors($_) for qw(peer_http_version send_te keep_alive);
12  
13 my $CRLF = "\015\012";
14  
15 sub new
16 {
17 my $class = shift;
18 $class->next::method(peer_http_version => "1.0", send_te => 0, @_);
19 }
20  
21 sub prepare_request{}
22  
23 sub format
24 {
25 my $self = shift;
26 my $request = shift;
27  
28 $self->prepare_request($request);
29  
30 my $method = $request->method || 'GET';
31 my $uri = $request->uri->path || '/';
32  
33 my $content = (@_ % 2) ? pop : "";
34  
35 for ($method, $uri) {
36 require Carp;
37 Carp::croak("Bad method or uri") if /\s/ || !length;
38 }
39
40 my $protocol = $request->protocol || 'HTTP/1.1';
41 my ($ver) = ($protocol =~ /(\d+\.\d+)\s*$/);
42 my $peer_ver = $self->peer_http_version || "1.0";
43
44 my @h;
45 my @connection;
46 my %given = (host => $request->header('Host') || 0, "content-length" => 0, "te" => 0);
47 while (@_) {
48 my($k, $v) = splice(@_, 0, 2);
49 my $lc_k = lc($k);
50 if ($lc_k eq "connection") {
51 $v =~ s/^\s+//;
52 $v =~ s/\s+$//;
53 push(@connection, split(/\s*,\s*/, $v));
54 next;
55 }
56  
57 if (exists $given{$lc_k}) {
58 $given{$lc_k}++;
59 }
60 push(@h, "$k: $v");
61 }
62
63 if (length($content) && !$given{'content-length'}) {
64 push(@h, "Content-Length: " . length($content));
65 }
66
67 my @h2;
68 if ($given{te}) {
69 push(@connection, "TE") unless grep lc($_) eq "te", @connection;
70 } elsif ($self->send_te && zlib_ok()) {
71 # gzip is less wanted since the Compress::Zlib interface for
72 # it does not really allow chunked decoding to take place easily.
73 push(@h2, "TE: deflate,gzip;q=0.3");
74 push(@connection, "TE");
75 }
76  
77 unless (grep lc($_) eq "close", @connection) {
78 if ($self->keep_alive) {
79 if ($peer_ver eq "1.0") {
80 # from looking at Netscape's headers
81 push(@h2, "Keep-Alive: 300");
82 unshift(@connection, "Keep-Alive");
83 }
84 } else {
85 push(@connection, "close") if $ver ge "1.1";
86 }
87 }
88 push(@h2, "Connection: " . join(", ", @connection)) if @connection;
89 unless ($given{host}) {
90 my $h = $request->uri->host;
91 push(@h2, "Host: $h") if $h;
92 }
93  
94 return join($CRLF, "$method $uri HTTP/$ver", @h2, @h, "", $content);
95 }
96  
97 1;
98  
99 __END__