File Coverage

blib/lib/Gungho/Request/http.pm
Criterion Covered Total %
statement 9 54 16.6
branch 0 30 0.0
condition 0 19 0.0
subroutine 3 6 50.0
pod 3 3 100.0
total 15 112 13.3


line stmt bran cond sub pod time code
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 1     1   1990 use strict;
  1         1  
  1         21  
8 1     1   3 use warnings;
  1         1  
  1         20  
9 1     1   3 use base qw(Gungho::Base);
  1         1  
  1         11  
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 0     0 1   my $class = shift;
18 0           $class->next::method(peer_http_version => "1.0", send_te => 0, @_);
19             }
20              
21       0 1   sub prepare_request{}
22              
23             sub format
24             {
25 0     0 1   my $self = shift;
26 0           my $request = shift;
27              
28 0           $self->prepare_request($request);
29              
30 0   0       my $method = $request->method || 'GET';
31 0   0       my $uri = $request->uri->path || '/';
32              
33 0 0         my $content = (@_ % 2) ? pop : "";
34              
35 0           for ($method, $uri) {
36 0           require Carp;
37 0 0 0       Carp::croak("Bad method or uri") if /\s/ || !length;
38             }
39            
40 0   0       my $protocol = $request->protocol || 'HTTP/1.1';
41 0           my ($ver) = ($protocol =~ /(\d+\.\d+)\s*$/);
42 0   0       my $peer_ver = $self->peer_http_version || "1.0";
43            
44 0           my @h;
45             my @connection;
46 0   0       my %given = (host => $request->header('Host') || 0, "content-length" => 0, "te" => 0);
47 0           while (@_) {
48 0           my($k, $v) = splice(@_, 0, 2);
49 0           my $lc_k = lc($k);
50 0 0         if ($lc_k eq "connection") {
51 0           $v =~ s/^\s+//;
52 0           $v =~ s/\s+$//;
53 0           push(@connection, split(/\s*,\s*/, $v));
54 0           next;
55             }
56              
57 0 0         if (exists $given{$lc_k}) {
58 0           $given{$lc_k}++;
59             }
60 0           push(@h, "$k: $v");
61             }
62            
63 0 0 0       if (length($content) && !$given{'content-length'}) {
64 0           push(@h, "Content-Length: " . length($content));
65             }
66            
67 0           my @h2;
68 0 0 0       if ($given{te}) {
    0          
69 0 0         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 0           push(@h2, "TE: deflate,gzip;q=0.3");
74 0           push(@connection, "TE");
75             }
76              
77 0 0         unless (grep lc($_) eq "close", @connection) {
78 0 0         if ($self->keep_alive) {
79 0 0         if ($peer_ver eq "1.0") {
80             # from looking at Netscape's headers
81 0           push(@h2, "Keep-Alive: 300");
82 0           unshift(@connection, "Keep-Alive");
83             }
84             } else {
85 0 0         push(@connection, "close") if $ver ge "1.1";
86             }
87             }
88 0 0         push(@h2, "Connection: " . join(", ", @connection)) if @connection;
89 0 0         unless ($given{host}) {
90 0           my $h = $request->uri->host;
91 0 0         push(@h2, "Host: $h") if $h;
92             }
93              
94 0           return join($CRLF, "$method $uri HTTP/$ver", @h2, @h, "", $content);
95             }
96              
97             1;
98              
99             __END__
100              
101             =head1 NAME
102              
103             Gungho::Request::http - HTTP specific utilities
104              
105             =head1 METHODS
106              
107             =head2 new
108              
109             =head2 prepare_request
110              
111             =head2 format
112              
113             =cut