New file, mod_perl output filter SetACOA.pm
authorMike Taylor <mike@indexdata.com>
Thu, 27 Jun 2013 11:00:56 +0000 (13:00 +0200)
committerMike Taylor <mike@indexdata.com>
Thu, 27 Jun 2013 11:00:56 +0000 (13:00 +0200)
etc/mod_perl/MyApache2/SetACAO.pm [new file with mode: 0644]

diff --git a/etc/mod_perl/MyApache2/SetACAO.pm b/etc/mod_perl/MyApache2/SetACAO.pm
new file mode 100644 (file)
index 0000000..594a5eb
--- /dev/null
@@ -0,0 +1,31 @@
+package MyApache2::SetACAO;
+
+use Apache2::Filter ();
+use Apache2::RequestRec ();
+use APR::Table ();
+
+use Apache2::Const -compile => qw(OK);
+
+use constant BUFF_LEN => 1024;
+
+sub handler {
+    my $f = shift;
+
+    # If the client generated an Origin header, echo its content back
+    # in an ACAO header. This is better than just using *, since it
+    # doesnt prevent credentials from being accepted.
+    my $hi = $f->r->headers_in;
+    my $ho = $f->r->headers_out;
+    my $origin = $ho->get('Origin');
+    if (defined $origin && $origin ne "") {
+       $ho->set('Access-Control-Allow-Origin', $origin);
+    }
+
+    while ($f->read(my $buffer, BUFF_LEN)) {
+       $f->print($buffer);
+    }
+
+    return Apache2::Const::OK;
+}
+
+1;