| 1 | package Net::Google::Calendar::Server::Handler::Apache; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Apache::Constants qw(:common); | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Net::Google::Calendar::Server::handler::Apache - an Apache handler to pretend to be Google Calendar | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | <Location /calendar> | ||
| 13 | SetHandler perl-script | ||
| 14 | PerlHandler Net::Google::Calendar::Server::Handler::Apache | ||
| 15 | PerlSendHeader On | ||
| 16 | PerlSetVar CalendarBackend ICalendar | ||
| 17 | PerlSetVar CalendarBackendFile /path/to/calendar.ics | ||
| 18 | PerlSetVar CalendarAuth Dummy | ||
| 19 | </Location> | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | Backend and Auth vars | ||
| 24 | |||
| 25 | =cut | ||
| 26 | |||
| 27 | |||
| 28 | sub handler($$) { | ||
| 29 | my ($class, $r) = @_; | ||
| 30 | |||
| 31 | my $self = bless { r => $r }, $class; | ||
| 32 | # Fetch config variables | ||
| 33 | my $vars = $r->per_dir_config; | ||
| 34 | |||
| 35 | |||
| 36 | my $dp_class = $vars->get('CalendarBackend'); | ||
| 37 | return $self->error("You must set a Backend class via PerlSetVar CalendarBackend <backend class>") unless defined $dp_class; | ||
| 38 | my $au_class = $vars->get('CalendarAuth'); | ||
| 39 | return $self->error("You must set a Auth class via PerlSetVar CalendarAuth <auth class>") unless defined $dp_class; | ||
| 40 | |||
| 41 | |||
| 42 | # fetch the variables for the Backend and the Auth classes | ||
| 43 | my %backend_opts; | ||
| 44 | while(my($key,$val) = each %$vars) { | ||
| 45 | next unless $key =~ m!^CalendarBackend(.+)$!; | ||
| 46 | $backend_opts{lc($1)} = $val; | ||
| 47 | } | ||
| 48 | |||
| 49 | my %auth_opts; | ||
| 50 | while(my($key,$val) = each %$vars) { | ||
| 51 | next unless $key =~ m!^CalendarAuth(.+)$!; | ||
| 52 | $auth_opts{lc($1)} = $val; | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | my $cal = eval { Net::Google::Calendar::Server->new( backend_class => $dp_class, backend_opts => \%backend_opts, | ||
| 57 | auth_class => $au_class, auth_opts => \%auth_opts, | ||
| 58 | ) }; | ||
| 59 | |||
| 60 | return $self->error($@) if $@; | ||
| 61 | return $cal->handle_request($self); | ||
| 62 | |||
| 63 | } | ||
| 64 | |||
| 65 | |||
| 66 | sub error { | ||
| 67 | my $self = shift; | ||
| 68 | my $error = shift; | ||
| 69 | my $code = shift; | ||
| 70 | $code = 'SERVER_ERROR' unless defined $code; | ||
| 71 | $code = $self->response_code($code); | ||
| 72 | |||
| 73 | $self->{r}->custom_response($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}->send_http_header( $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( Location => $where ); | ||
| 98 | return $self->response_code(REDIRECT); | ||
| 99 | } | ||
| 100 | |||
| 101 | sub get_args { | ||
| 102 | my $self = shift; | ||
| 103 | return $self->{r}->params(); | ||
| 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 Apache::Constants->$code(); | ||
| 132 | |||
| 133 | } | ||
| 134 | 1; |