summaryrefslogtreecommitdiff
path: root/lib/YAWF/Response.pm
blob: 5bcc126705b0aaa30f2ef5e312cc0ced03178bcb (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

package YAWF::Response;


use strict;
use warnings;
use Exporter 'import';


our @EXPORT = qw|
  resInit resHeader resBuffer resFd resStatus resRedirect resFinish
|;


# Initialises response data and resets all headers and content to their
# defaults. This method can be called mutliple times per request to clear
# any previous changes and to create a new response.
sub resInit {
  my $self = shift;

  $self->{_YAWF}{Res} = {
    status => 200,
    headers => [
      'Content-Type' => 'text/html; charset=UTF-8',
      'X-Powered-By' => 'Perl-YAWF',
    ],
    content => '',
  };

  open $self->{_YAWF}{Res}{fd}, '>', \$self->{_YAWF}{Res}{content};

  # enable output compression by default if the PerlIO::gzip module is available
  # (we don't check for browser support or even content, but it's possible to
  #  disable the gzip compression layer later on via resBuffer)
  eval { require PerlIO::gzip; };
  if(!$@) {
    binmode $self->{_YAWF}{Res}{fd}, ':gzip';
    $self->resHeader('Content-Encoding' => 'gzip');
  }
  binmode $self->{_YAWF}{Res}{fd}, ':utf8';
}


# Arguments       Action
#  name            returns list of values of the header, first found in scalar context, undef if not exists
#  name, value     sets header, overwrites existing header with same name, removes duplicates
#  name, undef     removes header
#  name, value, 1  adds header, not checking whether it already exists
# Header names are case-insensitive
sub resHeader {
  my($self, $name, $value, $add) = @_;
  my $h = $self->{_YAWF}{Res}{headers};

  if($add) {
    push @$h, $name, $value;
    return $value;
  }

  my @h;
  my @r;
  for (@$h ? 0..$#$h/2 : ()) {
    if(lc($h->[$_*2]) eq lc($name)) {
      push @h, $h->[$_*2+1];
      if(@_ == 3 && defined $value) {
        $h->[$_*2+1] = $value;
        push @r, $_ if @h > 1
      } elsif(@_ == 3) {
        push @r, $_;
      }
    }
  }

  push @$h, $name, $value if @_ == 3 && defined $value && !@h;

  splice @$h, $r[$_]*2-$_*2, 2
    for (@r ? 0..$#r : ());

  return @_ == 3 || @_ == 2 && !wantarray ? $h[0] : @h;
}


# Argument    Action
#  none        Returns whether gzip compression is enabled or not
#  undef       Clears the internal buffer
#  0           Clears buffer and disables gzip
#  1           Clears and enables gzip
# Enabling compression if PerlIO::gzip isn't installed will result in an error
sub resBuffer {
  my $self = shift;
  my $i = $self->{_YAWF}{Res};
  my $h = $self->resHeader('Content-Encoding');
  $h = $h && $h eq 'gzip';

  if(@_) {
    # clear buffer
    close $i->{fd};
    $i->{content} = '';
    open $i->{fd}, '>', \$i->{content};

    if(!defined $_[0] && $h || $_[0]) {
      binmode $i->{fd}, ':gzip';
      $self->resHeader('Content-Encoding', 'gzip');
    } else {
      $self->resHeader('Content-Encoding', undef);
    }
    binmode $i->{fd}, ':utf8';
  }

  $h = $self->resHeader('Content-Encoding');
  return $h && $h eq 'gzip';
}


# Returns the file descriptor where output functions can 'print' to
sub resFd {
  return shift->{_YAWF}{Res}{fd};
}


# Returns or sets the HTTP status
sub resStatus {
  my($self, $new) = @_;
  $self->{_YAWF}{Res}{status} = $new if $new;
  return $self->{_YAWF}{Res}{status};
}


# Redirect to an other page, accepts an URL (relative to current hostname) and
# an optional type consisting of 'temp' (temporary) or 'post' (after posting a form).
# No type argument means a permanent redirect.
sub resRedirect {
  my($self, $url, $type) = @_;

  $self->resInit;
  my $fd = $self->resFd();
  print $fd 'Redirecting...';
  $self->resHeader('Location' => $self->reqBaseURI().$url);
  $self->resStatus(!$type ? 301 : $type eq 'temp' ? 307 : 303);
}


# Send everything we have buffered to the client
sub resFinish {
  my $self = shift;
  my $i = $self->{_YAWF}{Res};

  close $i->{fd};
  $self->resHeader('Content-Length' => length($i->{content}));

  printf "Status: %d\r\n", $i->{status};
  printf "%s: %s\r\n", $i->{headers}[$_*2], $i->{headers}[$_*2+1]
    for (0..$#{$i->{headers}}/2);
  print  "\r\n";
  print  $i->{content};

  # free the memory used for the reponse data
  $self->resInit;
}




1;