| 1 | package Net::Google::Calendar::Server::Handler::Apache2; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Apache2::RequestRec (); | ||
| 5 | use Apache2::Const -compile => qw(':common'); | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Net::Google::Calendar::Server::handler::Apache2 - an Apache2 handler to pretend to be Google Calendar | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | <Location /calendar> | ||
| 14 | SetHandler perl-script | ||
| 15 | PerlHandler Net::Google::Calendar::Server::Handler::Apache2 | ||
| 16 | PerlSendHeader On | ||
| 17 | PerlSetVar CalendarBackend ICalendar | ||
| 18 | PerlSetVar CalendarBackendFile /path/to/calendar.ics | ||
| 19 | PerlSetVar CalendarAuth Dummy | ||
| 20 | </Location> | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | Backend and Auth vars | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | |||
| 29 | sub handler($$) : method { | ||
| 30 | my ($class, $r) = @_; | ||
| 31 | |||
| 32 | my $self = bless { r => $r }, $class; | ||
| 33 | # Fetch config variables | ||
| 34 | my $vars = $r->per_dir_config; | ||
| 35 | |||
| 36 | |||
| 37 | my $dp_class = $vars->get('CalendarBackend'); | ||
| 38 | return $self->error("You must set a Backend class via PerlSetVar CalendarBackend <backend class>") unless defined $dp_class; | ||
| 39 | my $au_class = $vars->get('CalendarAuth'); | ||
| 40 | return $self->error("You must set a Auth class via PerlSetVar CalendarAuth <auth class>") unless defined $dp_class; | ||
| 41 | |||
| 42 | |||
| 43 | # fetch the variables for the Backend and the Auth classes | ||
| 44 | my %backend_opts; | ||
| 45 | while(my($key,$val) = each %$vars) { | ||
| 46 | next unless $key =~ m!^CalendarBackend(.+)$!; | ||
| 47 | $backend_opts{lc($1)} = $val; | ||
| 48 | } | ||
| 49 | |||
| 50 | my %auth_opts; | ||
| 51 | while(my($key,$val) = each %$vars) { | ||
| 52 | next unless $key =~ m!^CalendarAuth(.+)$!; | ||
| 53 | $auth_opts{lc($1)} = $val; | ||
| 54 | } | ||
| 55 | |||
| 56 | |||
| 57 | my $cal = eval { Net::Google::Calendar::Server->new( backend_class => $dp_class, backend_opts => \%backend_opts, | ||
| 58 | auth_class => $au_class, auth_opts => \%auth_opts, | ||
| 59 | ) }; | ||
| 60 | |||
| 61 | return $self->error($@) if $@; | ||
| 62 | return $cal->handle_request($self); | ||
| 63 | |||
| 64 | } | ||
| 65 | |||
| 66 | |||
| 67 | sub error { | ||
| 68 | my $self = shift; | ||
| 69 | my $error = shift; | ||
| 70 | my $code = shift; | ||
| 71 | $code = 'SERVER_ERROR' unless defined $code; | ||
| 72 | $code = $self->response_code($code); | ||
| 73 | $self->{r}->status_line("$code $error"); | ||
| 74 | return $code; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub get_request_method { | ||
| 78 | my $self = shift; | ||
| 79 | return $self->{r}->method; | ||
| 80 | |||
| 81 | } | ||
| 82 | |||
| 83 | sub send_response { | ||
| 84 | my $self = shift; | ||
| 85 | my %what = @_; | ||
| 86 | |||
| 87 | $self->{r}->content_type( $what{type} ); | ||
| 88 | # TODO - other headers | ||
| 89 | print $what{body}."\n"; | ||
| 90 | return $self->response_code($what{code}); | ||
| 91 | } | ||
| 92 | |||
| 93 | sub redirect { | ||
| 94 | my $self = shift; | ||
| 95 | my $where = shift; | ||
| 96 | |||
| 97 | $self->{r}->header_out->set( Location => $where ); | ||
| 98 | return $self->response_code(REDIRECT); | ||
| 99 | } | ||
| 100 | |||
| 101 | sub get_args { | ||
| 102 | my $self = shift; | ||
| 103 | return $self->{r}->args(); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub header_in { | ||
| 107 | my $self = shift; | ||
| 108 | my $what = shift || return; | ||
| 109 | return $self->{r}->header_in($what); | ||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | sub path { | ||
| 114 | my $self = shift; | ||
| 115 | my $r = $self->{r}; | ||
| 116 | |||
| 117 | # get all the path info | ||
| 118 | my $loc = $r->location; $loc =~ s!/$!!; | ||
| 119 | my $rest = $r->filename; $rest .= "/" unless $rest =~ m!/$!; | ||
| 120 | $rest =~ s!^$loc!!; | ||
| 121 | $rest =~ s!/$!!; | ||
| 122 | $rest .= $r->path_info; | ||
| 123 | $rest =~ s!(^/|/$)!!g; | ||
| 124 | |||
| 125 | return $rest; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub response_code { | ||
| 129 | my $self = shift; | ||
| 130 | my $code = shift; | ||
| 131 | return Apache2::Const->$code; | ||
| 132 | |||
| 133 | } | ||
| 134 | 1; |