| 1 | # $Id: /mirror/gungho/lib/Gungho/Request.pm 31624 2007-12-01T04:20:00.298198Z lestrrat $ | ||
| 2 | # | ||
| 3 | # Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp> | ||
| 4 | # All rights reserved. | ||
| 5 | |||
| 6 | package Gungho::Request; | ||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | use base qw(HTTP::Request); | ||
| 10 | use Storable qw(dclone); | ||
| 11 | use UNIVERSAL::require; | ||
| 12 | use Regexp::Common qw(net); | ||
| 13 | |||
| 14 | our $DIGEST; | ||
| 15 | |||
| 16 | sub _find_digest_class | ||
| 17 | { | ||
| 18 | $DIGEST ||= do { | ||
| 19 | my $pkg; | ||
| 20 | foreach my $x qw(SHA1 MD5) { | ||
| 21 | my $candidate = "Digest::$x"; | ||
| 22 | if ($candidate->require()) { | ||
| 23 | $pkg = $candidate; | ||
| 24 | last; | ||
| 25 | } | ||
| 26 | } | ||
| 27 | $pkg; | ||
| 28 | }; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub new | ||
| 32 | { | ||
| 33 | my $class = shift; | ||
| 34 | my $self = $class->SUPER::new(@_); | ||
| 35 | $self->id; # Forcefully make the ID here. | ||
| 36 | $self->{_notes} = {}; | ||
| 37 | return $self; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub id | ||
| 41 | { | ||
| 42 | my $self = shift; | ||
| 43 | |||
| 44 | local $@ = undef; | ||
| 45 | $self->{_id} ||= do { | ||
| 46 | my $pkg = _find_digest_class() || die "Could not find Digest class"; | ||
| 47 | my $digest = $pkg->new; | ||
| 48 | |||
| 49 | $digest->add(map { defined $_ ? $_ : '' } (time(), {}, rand(), $self->method, $self->uri, $self->protocol)); | ||
| 50 | $self->headers->scan(sub { | ||
| 51 | $digest->add(join(':', $_[0], $_[1])); | ||
| 52 | }); | ||
| 53 | $digest->hexdigest; | ||
| 54 | }; | ||
| 55 | die $@ if $@; | ||
| 56 | $self->{_id}; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub clone | ||
| 60 | { | ||
| 61 | my $self = shift; | ||
| 62 | my $clone = $self->SUPER::clone; | ||
| 63 | |||
| 64 | my $cloned_notes = dclone $self->notes; | ||
| 65 | foreach my $note (keys %$cloned_notes) { | ||
| 66 | $clone->notes( $note => $cloned_notes->{$note} ); | ||
| 67 | } | ||
| 68 | return $clone; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub notes | ||
| 72 | { | ||
| 73 | my $self = shift; | ||
| 74 | my $key = shift; | ||
| 75 | |||
| 76 | return $self->{_notes} unless $key; | ||
| 77 | |||
| 78 | my $value = $self->{_notes}{$key}; | ||
| 79 | if (@_) { | ||
| 80 | $self->{_notes}{$key} = $_[0]; | ||
| 81 | } | ||
| 82 | return $value; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub original_uri | ||
| 86 | { | ||
| 87 | my $self = shift; | ||
| 88 | my $uri = $self->uri->clone; | ||
| 89 | if (my $host = $self->notes('original_host')) { | ||
| 90 | $uri->host($host); | ||
| 91 | } | ||
| 92 | return $uri; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub requires_name_lookup | ||
| 96 | { | ||
| 97 | my $self = shift; | ||
| 98 | return ! $self->notes('resolved_ip') && | ||
| 99 | ($self->uri->can('host') && $self->uri->host() !~ /^$RE{net}{IPv4}$/); | ||
| 100 | } | ||
| 101 | |||
| 102 | sub format | ||
| 103 | { | ||
| 104 | my $self = shift; | ||
| 105 | my $scheme = $self->uri->scheme; | ||
| 106 | my $pkg = "Gungho::Request::$scheme"; | ||
| 107 | |||
| 108 | require Class::Inspector; | ||
| 109 | Class::Inspector->loaded($pkg) or $pkg->require or die; | ||
| 110 | |||
| 111 | my $protocol = $pkg->new; | ||
| 112 | $protocol->format($self); | ||
| 113 | } | ||
| 114 | |||
| 115 | 1; | ||
| 116 | |||
| 117 | __END__ |