#!/usr/bin/w3tclsh
#
# This script demonstrates the use of the w3-before-reply-hook interface.
#
# The w3-before-reply-hook is a TCL proc that runs just before the reply
# to an HTTP request is generated.  It has the opportunity to review the
# HTTP reply to ensure that no sensitive information is present in the
# reply, due to accidents or bugs in the code.  It can modify the reply
# or generate an error.
#
# Most applications omit the w3-before-reply-hook in which case it is
# a no-op.
#
# This demo is the "self.tcl" demo, with a w3-before-reply-hook added
# that changes all instances of the string "before-reply" into "XXXXXXXXXXXX".
#
package require w3
proc w3-before-reply-hook {} {
  global w3
  dict set w3 .reply \
    [string map {before-reply XXXXXXXXXXXX} [dict get $w3 .reply]]
}
proc common-header {} {
  w3-trim {
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <link href="%url([w3-param SCRIPT_NAME]/style.css)" rel="stylesheet">
    <title>W3 Self-Display Demo</title>
    </head>
    <body>
  }
}
proc common-footer {} {
  w3-trim {
    </body>
    </html>
  }
}
proc w3-default {} {
  w3-cache-control max-age=3600
  common-header
  w3-trim {
    <h1>W3 Self-Display Demo</h1>
    <p>(Strings "&#98;efore-reply" changed into "XXXXXXXXXXXX".)</p>
    <ul>
    <li> <a href='%url([w3-param SCRIPT_NAME])/self'>Show the script
    that generates this page</a>
    <li> <a href='%url([w3-param SCRIPT_NAME])/env'>W3 Environment</a>
    </ul>
  }
  common-footer
}
proc w3-page-env {} {
  w3-allow-xorigin-params
  common-header
  w3-trim {
    <h1>W3 Environment</h1>
    <pre>%html([w3-debug-env])</pre>
  }
  common-footer
}
proc w3-page-self {} {
  w3-cache-control max-age=3600
  common-header
  set fd [open [w3-param SCRIPT_FILENAME] rb]
  set script [read $fd]
  close $fd
  w3-trim {
    <h1>W3 Script That Shows A Copy Of Itself</h1>
    <pre>%html($script)</pre>
  }
  common-footer
}
proc w3-page-style.css {} {
  w3-mimetype text/css
  w3-cache-control max-age=3600
  w3-trim {
    pre {
       border: 1px solid black;
       padding: 1ex;
    }
  }
}
w3-start $argv
