summaryrefslogtreecommitdiff
path: root/lib/VNDB/Util/Response.pm
blob: 619429aed0a42975bd92c20e8ec6b9213b1354ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238

package VNDB::Util::Response;


use strict;
use warnings;
use POSIX ();
use Encode;
use XML::Writer;
use Compress::Zlib;
use Exporter 'import';
require bytes;

use vars ('$VERSION', '@EXPORT');
$VERSION = $NTL::VERSION;
@EXPORT = qw| ResRedirect ResNotFound ResDown ResDenied ResFile
  ResForceBody ResSetContentType ResAddHeader ResAddTpl ResAddDefaultStuff
  ResStartXML ResGetXML ResGetBody ResGet ResGetCGI ResSetModPerl |;

sub new {
  my $self = shift;
  my $tplo = shift;
  my $type = ref($self) || $self;
  my $me = bless {
    headers => [ ],
    contenttype => 'text/html; charset=UTF-8',
    code => 200,
    tplo => $tplo,
    tpl => { },
    body => undef,
    xmlobj => undef,
    xmldata => undef,
    whattouse => 1,
    rc => 0,
  }, $type;
 
  return $me;
}


## Some ready-to-use methods
sub ResRedirect {
  my $self = shift;
  my $url = shift; # should start with '/', if no URL specified, use referer or '/'
  my $type = shift;
  my $info = $self->{_Res} || $self;
  
  if(!$url) {
    $url = "/";
    my $ref = $self->ReqHeader('Referer');
    ($url = $ref) =~ s/^$self->{root_url}// if $ref;
  }
  
  my $code = !$type ? 301 :
    $type eq 'temp' ? 307 :
    $type eq 'post' ? 303 : 301;
  $info->{code} = $code;
  $info->{tpl} = { 
    error => {
      url => $url,
      code => $code,
    }
  };
  $info->{headers} = [ 'Location', "$self->{root_url}$url" ];
  $info->{contenttype} = 'text/html; charset=UTF-8';
  $info->{whattouse} = 2;
}

sub ResNotFound {
  my $s = shift;
  my $i = $s->{_Res};
  $i->{code} = 404;
  $i->{whattouse} = 4;
  push @{$i->{headers}}, 'X-Sendfile' => '/www/vndb/www/files/notfound.html';
}

sub ResDown {
  my $self = shift;
  my $msg = shift || '';
  my $info = $self->{_Res} || $self;
  
  $info->{code} = 200;
  $info->{tpl} = {
    error => {
      code => 1,
      msg => $msg, # specifies which message should be displayed
    }
  };
  $info->{contenttype} = 'text/html; charset=UTF-8';
  $info->{whattouse} = 2;
}

sub ResDenied {
  my $self = shift;
  $self->ResRedirect('/u/register?n=1', 'temp');
}

sub ResFile {
  my($s,$f,@h) = @_;
  my $i = $s->{_Res};
  $i->{whattouse} = 4;
  $i->{code} = 200;
  $i->{contenttype} = '';
  push @{$i->{headers}},
    'X-Sendfile' => $f,
    'Cache-Control' => sprintf('max-age=%d, public', 7*24*3600),
    @h;
}

## And some often-used methods
sub ResForceBody {
  my $self = shift;
  my $body = shift; 
  my $info = $self->{_Res} || $self;
  $info->{whattouse} = 1;
  $info->{body} = $body;
}

sub ResSetContentType {
  my $self = shift;
  my $ctype = shift;
  my $info = $self->{_Res} || $self;
  $info->{contenttype} = $ctype;
  return 1;
}

sub ResAddHeader {
  my $self = shift;
  die("Odd number in parameters, must be in key => value format!") unless ((@_ % 2) == 0);
  my $info = $self->{_Res} || $self;
  $info->{headers} = [ @{$info->{headers}}, @_ ];
  return 1;
}

sub ResAddTpl {
  my $self = shift;
  die("Odd number in parameters, must be in key=>value format") unless ((@_ % 2) == 0);
  my $info = $self->{_Res} || $self;
  $info->{tpl} = { page => { } } if !$info->{tpl}->{page};
  $info->{tpl}->{page} = { %{$info->{tpl}->{page}}, @_ };
  $info->{whattouse} = 2;
  return 1;  
}

sub ResStartXML {
  my $self = shift;
  my $info = $self->{_Res} || $self;
  $info->{xmldata} = undef;
  $info->{xmlobj} = XML::Writer->new(
    OUTPUT => \$info->{xmldata}, 
    NEWLINES => 0, 
    ENCODING => 'UTF-8', 
    DATA_MODE => 1, 
    DATA_INDENT => 2,
  );
  $info->{xmlobj}->xmlDecl();
  $info->{contenttype} = "text/xml; charset=UTF-8";
 # disable caching on XML content, IE < 7 has "some" bugs...
  $self->ResAddHeader('Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
                      'Pragma' => 'public');
  $info->{whattouse} = 3;
  return $info->{xmlobj};
}

## And of course some methods to get the information
sub ResGetXML {
  my $self = shift;
  my $info = $self->{_Res} || $self;
  return undef if !$info->{xmlobj} || !$info->{xmldata};
  $info->{xmlobj}->end();
  my $tmpvar = $info->{xmldata};
  undef $info->{xmldata};
  return $tmpvar;
}

sub ResGetBody {
  my $self = shift;
  my $info = $self->{_Res} || $self;
  my $whattouse = shift || $info->{whattouse};
  if($whattouse == 1)  { return $info->{body};  }
  if($whattouse == 2)  {
    $self->AddDefaultStuff() if exists $info->{tpl}->{page};
    my $start = [Time::HiRes::gettimeofday()] if $self->{debug} && $Time::HiRes::VERSION;
    my $output = $info->{tplo}->compile($info->{tpl});
    $info->{_tpltime} = Time::HiRes::tv_interval($start) if $self->{debug} && $Time::HiRes::VERSION;
    return $output;
  }
  if($whattouse == 3)  { return $self->ResGetXML; }
}

sub ResGet {
  my $self = shift;
  my $info = $self->{_Res} || $self;
  my $whattouse = shift || $info->{whattouse};

  return ($info->{code}, $info->{headers}, $info->{contenttype}, $self->ResGetBody($whattouse));
}


my %scodes = (
 # just a few useful codes
  200 => 'OK',
  301 => 'Moved Permanently',
  302 => 'Found',
  303 => 'See Other',
  304 => 'Not Modified',
  307 => 'Temporary Redirect',
  403 => 'Forbidden',
  404 => 'Not Found',
  500 => 'Internal Server Error'
);

# don't rename!
sub ResSetModPerl {
  my $s = shift;
  my $i = $s->{_Res};
  printf "Status: %d %s\r\n", $i->{code}, $scodes{$i->{code}};
  print  "X-Powered-By: Perl\r\n";
  printf "Content-Type: %s\r\n", $i->{contenttype} if $i->{contenttype};
  my $c=0;
  printf "%s: %s\r\n", $i->{headers}[$c++], $i->{headers}[$c++]
    while ($c<$#{$i->{headers}});

  my $b = $s->ResGetBody||'';
  if($b && $s->ReqHeader('Accept-Encoding') =~ /gzip/ && $i->{contenttype} =~ /^text/) {
    my $ol = bytes::length($b) if $s->{debug};
    $b = Compress::Zlib::memGzip(Encode::encode_utf8($b));
    $i->{_gzip} = [ $ol, bytes::length($b) ];
    print "Content-Encoding: gzip\n";
  }
  my $l = bytes::length($b);
  printf "Content-Length: %d\r\n", $l if $l;
  print  "\r\n";
  print  $b;
  $FCGI::Handler::outputted = 1;
}

1;