AnyEvent-HTTPD-SendMultiHeaderPatch

 view release on metacpan or  search on metacpan

lib/AnyEvent/HTTPD/SendMultiHeaderPatch.pm  view on Meta::CPAN

      $self->response_done;
      return;
   }

   if (ref ($content) eq 'CODE') {
      weaken $self;

      my $chunk_cb = sub {
         my ($chunk) = @_;

         return 0 unless defined ($self) && defined ($self->{hdl}) && !$self->{disconnected};

         delete $self->{transport_polled};

         if (defined ($chunk) && length ($chunk) > 0) {
            $self->{hdl}->push_write ($chunk);

         } else {
            $self->response_done;
         }

         return 1;
      };

      $self->{transfer_cb} = $content;

      $self->{hdl}->on_drain (sub {
         return unless $self;

         if (length $res) {
            my $r = $res;
            undef $res;
            $chunk_cb->($r);

         } elsif (not $self->{transport_polled}) {
            $self->{transport_polled} = 1;
            $self->{transfer_cb}->($chunk_cb) if $self;
         }
      });

   } else {
      $res .= $content;
      $self->{hdl}->push_write ($res);
      $self->response_done;
   }
};

=head1 SYNOPSIS

    use AnyEvent::HTTPD; # Optional,
                         # because the patch module will use it first.
    use AnyEvent::HTTPD::SendMultiHeaderPatch;

    # In the http request handler,
    # separate the multiple values of the same field with \0 character.
    sub {
        my($httpd, $req) = @_;
        # ...
        $req->respond(
            200, 'OK', {
                'Set-Cookie' => "a=123; path=/; domain=.example.com\0b=456; path=/; domain=.example.com"
            }, "Set the cookies"
        );
    }

    # Or use the added util function header_add in AnyEvent::HTTPD::Util.
    use AnyEvent::HTTPD::Util;

    sub {
        my($httpd, $req) = @_;
        # ...
        my %header;
        header_add(\%header, 'Set-Cookie', 'a=123; path=/; domain=.example.com');
        header_add(\%header, 'Set-Cookie', 'b=456; path=/; domain=.example.com');
        $req->respond(200, 'OK', \%header, "Set the cookies");
    }

    # There also introduced another util function header_gets in AnyEvent::HTTPD::Util,
    # to extract multiple values in the header
    sub {
        my($httpd, $req) = @_;
        # ...
        my %header;
        header_add(\%header, 'Example', 'a');
        header_add(\%header, 'Example', 'b');

        my $example_values = header_gets(\%header, 'Example');
        # get ['a', 'b']
        my $no_values = header_gets(\%header, 'None');
        # get []
    }


=head1 CAVEATS

=over 4

=item This is a hack (should be stable)

This module is a hack patch that replace the method 'response' in package AnyEvent::HTTPD::HTTPConnection.
I think that it's still stable since the module AnyEvent::HTTPD has been frozen since 2011.3 (Today is 2013.4)

=item No \0 in your header values

Don't use \0 in your header values since it's used as the separater.

=back

=head1 AUTHOR

Cindy Wang (CindyLinz)

=head1 LICENSE AND COPYRIGHT

Copyright 2013 Cindy Wang (CindyLinz).

This program is free software; you can redistribute it and/or modify it
under the terms of the the Artistic License (2.0). You may obtain a
copy of the full license at:

L<http://www.perlfoundation.org/artistic_license_2_0>

Any use, modification, and distribution of the Standard or Modified
Versions is governed by this Artistic License. By using, modifying or
distributing the Package, you accept this license. Do not use, modify,
or distribute the Package, if you do not accept this license.

If your Modified Version has been derived from a Modified Version made
by someone other than you, you are nevertheless required to ensure that
your Modified Version complies with the requirements of this license.

This license does not grant you the right to use any trademark, service
mark, tradename, or logo of the Copyright Holder.



( run in 1.646 second using v1.01-cache-2.11-cpan-d8267643d1d )