More mod_perl support for MKWS web-server into tools area
[mkws-moved-to-github.git] / tools / mod_perl / MyApache2 / SetACAO.pm
diff --git a/tools/mod_perl/MyApache2/SetACAO.pm b/tools/mod_perl/MyApache2/SetACAO.pm
new file mode 100644 (file)
index 0000000..66334fc
--- /dev/null
@@ -0,0 +1,30 @@
+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 $origin = $f->r->headers_in->get('Origin');
+    if (defined $origin && $origin ne "") {
+       $f->r->headers_out->set('Access-Control-Allow-Origin', $origin);
+       warn "MyApache2::SetACAO copied Origin '$origin' to ACAO";
+    }
+
+    while ($f->read(my $buffer, BUFF_LEN)) {
+       $f->print($buffer);
+    }
+
+    return Apache2::Const::OK;
+}
+
+1;