File Coverage

blib/lib/AnyEvent/HTTPD/SendMultiHeaderPatch.pm
Criterion Covered Total %
statement 32 83 38.5
branch 0 38 0.0
condition 0 12 0.0
subroutine 11 16 68.7
pod 0 3 0.0
total 43 152 28.2


line stmt bran cond sub pod time code
1             package AnyEvent::HTTPD::SendMultiHeaderPatch;
2              
3 1     1   12727 use 5.006;
  1         2  
4 1     1   4 use strict;
  1         0  
  1         18  
5 1     1   3 use warnings FATAL => 'all';
  1         3  
  1         30  
6              
7 1     1   447 use AnyEvent::HTTPD::HTTPConnection;
  1         44357  
  1         30  
8              
9             =head1 NAME
10              
11             AnyEvent::HTTPD::SendMultiHeaderPatch -
12             Patch (hack) of AnyEvent::HTTPD for sending multiple headers with the same field name.
13              
14             =head1 VERSION
15              
16             Version 0.01
17              
18             =cut
19              
20 1     1   429 use version;
  1         1310  
  1         4  
21             our $VERSION = qv 'v0.1.2';
22              
23 1     1   487 use AnyEvent::HTTPD;
  1         7921  
  1         28  
24              
25             package AnyEvent::HTTPD::Util;
26              
27 1     1   5 use strict;
  1         1  
  1         16  
28 1     1   3 use warnings;
  1         1  
  1         140  
29              
30             our @EXPORT;
31             push @EXPORT, qw(header_add header_gets);
32              
33             sub header_add {
34 0     0 0   my ($hdrs, $name, $value) = @_;
35 0           $name = _header_transform_case_insens ($hdrs, $name);
36 0 0         if( exists $hdrs->{$name} ) {
37 0           $hdrs->{$name} .= "\0".$value;
38             }
39             else {
40 0           $hdrs->{$name} = $value;
41             }
42             }
43              
44             sub header_gets {
45 0     0 0   my ($hdrs, $name) = @_;
46 0           $name = _header_transform_case_insens ($hdrs, $name);
47 0 0         exists $hdrs->{$name} ? [split /\0/, $hdrs->{$name}] : []
48             }
49              
50             package AnyEvent::HTTPD::HTTPConnection;
51              
52 1     1   4 use strict;
  1         1  
  1         14  
53 1     1   3 use warnings;
  1         1  
  1         25  
54 1     1   2 no warnings 'redefine';
  1         1  
  1         441  
55              
56             sub response {
57 0     0 0   my ($self, $code, $msg, $hdr, $content, $no_body) = @_;
58 0 0         return if $self->{disconnected};
59 0 0         return unless $self->{hdl};
60              
61 0           my $res = "HTTP/1.0 $code $msg\015\012";
62 0 0         header_set ($hdr, 'Date' => _time_to_http_date time)
63             unless header_exists ($hdr, 'Date');
64 0 0         header_set ($hdr, 'Expires' => header_get ($hdr, 'Date'))
65             unless header_exists ($hdr, 'Expires');
66 0 0         header_set ($hdr, 'Cache-Control' => "max-age=0")
67             unless header_exists ($hdr, 'Cache-Control');
68             header_set ($hdr, 'Connection' =>
69 0 0         ($self->{keep_alive} ? 'Keep-Alive' : 'close'));
70              
71 0 0 0       header_set ($hdr, 'Content-Length' => length "$content")
72             unless header_exists ($hdr, 'Content-Length')
73             || ref $content;
74              
75 0 0         unless (defined header_get ($hdr, 'Content-Length')) {
76             # keep alive with no content length will NOT work.
77 0           delete $self->{keep_alive};
78 0           header_set ($hdr, 'Connection' => 'close');
79             }
80              
81 0           while (my ($h, $v) = each %$hdr) {
82 0 0         next unless defined $v;
83 0           for my $vv ( split /\0/, $v ) {
84 0           $res .= "$h: $vv\015\012";
85             }
86             }
87              
88 0           $res .= "\015\012";
89              
90 0 0         if ($no_body) { # for HEAD requests!
91 0           $self->{hdl}->push_write ($res);
92 0           $self->response_done;
93 0           return;
94             }
95              
96 0 0         if (ref ($content) eq 'CODE') {
97 0           weaken $self;
98              
99             my $chunk_cb = sub {
100 0     0     my ($chunk) = @_;
101              
102 0 0 0       return 0 unless defined ($self) && defined ($self->{hdl}) && !$self->{disconnected};
      0        
103              
104 0           delete $self->{transport_polled};
105              
106 0 0 0       if (defined ($chunk) && length ($chunk) > 0) {
107 0           $self->{hdl}->push_write ($chunk);
108              
109             } else {
110 0           $self->response_done;
111             }
112              
113 0           return 1;
114 0           };
115              
116 0           $self->{transfer_cb} = $content;
117              
118             $self->{hdl}->on_drain (sub {
119 0 0   0     return unless $self;
120              
121 0 0         if (length $res) {
    0          
122 0           my $r = $res;
123 0           undef $res;
124 0           $chunk_cb->($r);
125              
126             } elsif (not $self->{transport_polled}) {
127 0           $self->{transport_polled} = 1;
128 0 0         $self->{transfer_cb}->($chunk_cb) if $self;
129             }
130 0           });
131              
132             } else {
133 0           $res .= $content;
134 0           $self->{hdl}->push_write ($res);
135 0           $self->response_done;
136             }
137             }
138              
139             =head1 SYNOPSIS
140              
141             use AnyEvent::HTTPD; # Optional,
142             # because the patch module will use it first.
143             use AnyEvent::HTTPD::SendMultiHeaderPatch;
144              
145             # In the http request handler,
146             # separate the multiple values of the same field with \0 character.
147             sub {
148             my($httpd, $req) = @_;
149             # ...
150             $req->respond(
151             200, 'OK', {
152             'Set-Cookie' => "a=123; path=/; domain=.example.com\0b=456; path=/; domain=.example.com"
153             }, "Set the cookies"
154             );
155             }
156              
157             # Or use the added util function header_add in AnyEvent::HTTPD::Util.
158             use AnyEvent::HTTPD::Util;
159              
160             sub {
161             my($httpd, $req) = @_;
162             # ...
163             my %header;
164             header_add(\%header, 'Set-Cookie', 'a=123; path=/; domain=.example.com');
165             header_add(\%header, 'Set-Cookie', 'b=456; path=/; domain=.example.com');
166             $req->respond(200, 'OK', \%header, "Set the cookies");
167             }
168              
169             # There also introduced another util function header_gets in AnyEvent::HTTPD::Util,
170             # to extract multiple values in the header
171             sub {
172             my($httpd, $req) = @_;
173             # ...
174             my %header;
175             header_add(\%header, 'Example', 'a');
176             header_add(\%header, 'Example', 'b');
177              
178             my $example_values = header_gets(\%header, 'Example');
179             # get ['a', 'b']
180             my $no_values = header_gets(\%header, 'None');
181             # get []
182             }
183              
184              
185             =head1 CAVEATS
186              
187             =over 4
188              
189             =item This is a hack (should be stable)
190              
191             This module is a hack patch that replace the method 'response' in package AnyEvent::HTTPD::HTTPConnection.
192             I think that it's still stable since the module AnyEvent::HTTPD has been frozen since 2011.3 (Today is 2013.4)
193              
194             =item No \0 in your header values
195              
196             Don't use \0 in your header values since it's used as the separater.
197              
198             =back
199              
200             =head1 AUTHOR
201              
202             Cindy Wang (CindyLinz)
203              
204             =head1 LICENSE AND COPYRIGHT
205              
206             Copyright 2013 Cindy Wang (CindyLinz).
207              
208             This program is free software; you can redistribute it and/or modify it
209             under the terms of the the Artistic License (2.0). You may obtain a
210             copy of the full license at:
211              
212             L
213              
214             Any use, modification, and distribution of the Standard or Modified
215             Versions is governed by this Artistic License. By using, modifying or
216             distributing the Package, you accept this license. Do not use, modify,
217             or distribute the Package, if you do not accept this license.
218              
219             If your Modified Version has been derived from a Modified Version made
220             by someone other than you, you are nevertheless required to ensure that
221             your Modified Version complies with the requirements of this license.
222              
223             This license does not grant you the right to use any trademark, service
224             mark, tradename, or logo of the Copyright Holder.
225              
226             This license includes the non-exclusive, worldwide, free-of-charge
227             patent license to make, have made, use, offer to sell, sell, import and
228             otherwise transfer the Package with respect to any patent claims
229             licensable by the Copyright Holder that are necessarily infringed by the
230             Package. If you institute patent litigation (including a cross-claim or
231             counterclaim) against any party alleging that the Package constitutes
232             direct or contributory patent infringement, then this Artistic License
233             to you shall terminate on the date that such litigation is filed.
234              
235             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
236             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
237             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
238             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
239             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
240             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
241             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
242             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
243              
244              
245             =cut
246              
247             1; # End of AnyEvent::HTTPD::SendMultiHeaderPatch