Giter Site home page Giter Site logo

gahr / tcl-scgi Goto Github PK

View Code? Open in Web Editor NEW
8.0 4.0 1.0 96 KB

SCGI handler for Tcl scripts - Mirror of https://code.ptrcrt.ch/tcl-scgi

Home Page: https://code.ptrcrt.ch/tcl-scgi

License: BSD 2-Clause "Simplified" License

Tcl 88.55% Shell 4.40% Makefile 3.24% Awk 0.77% Vim Script 3.04%
www tcl scgi

tcl-scgi's Introduction

tcl-scgi

This is a Simple Common Gateway Interface (SCGI) handler implemented as a multi-threaded server using the Tcl programming language. Each request is first parsed by the main thread then dispatched to be served by a dedicated thread. The scgi.tcl software requires Tcl 8.6 and the Thread extension.

The result of the following example can be seen here.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>tcl-scgi example</title>
    </head>
    <body>
        <p><label style="font-weight: bold">gray-scale table</label></p>
        <table><tr>
        <?
            set lvl 0
            while {$lvl <= 255} {
                set color "#[string range [format %02X $lvl] 3]"
                @ [::scgi::html::td \
                    [list style "background-color: $color; width: 20px; height: 20px" \
                          title $color] \
                    {}]
                @ "\n"
                if {[incr lvl] % 16 == 0 && $lvl != 256} {
                    @ "</tr>\n<tr>\n"
                }
            }
        ?>
        </tr></table>
    </body>
</html>

Server

The handler can be invoked with the following arguments:

 -addr value              Listen on the specified address <127.0.0.1>
 -port value              Listen on the specified port <4000>
 -path value              Script path <DOCUMENT_ROOT>
 -fork                    Fork and return the pid of the child process
 -max_threads value       Maximum number of threads to spawn <50>
 -min_threads value       Minimum number of threads to spawn <1>
 -thread_keepalive value  Number of seconds an idle thread is kept alive <60>
 -conn_keepalive value    Number of seconds an idle connection is kept alive <-1>
 -verbose                 Dump verbose information to stdout
 --                       Forcibly stop option processing
 -help                    Print this message
 -?                       Print this message

Scripts

User scripts consist of pure HTML code with interleaved Tcl scripts enclosed in <? and ?> tags.

The following special commands are available:

@ arg ... Synonym to [puts], except it takes multiple arguments. The arguments are evaluated by Tcl if not enclosed in braces. Example:

@ [info hostname]
@ "Using Tcl version " [info patchlevel]
@ {Here [square brakets] are dumped literally}

::scgi::header key value ?replace? Append the HTTP header "Key: value" to the output buffer. If replace is true (the default), a previous header with the same key is replaced by the one specified.

::scgi::flush Send the output buffered (including headers and body data) to the client and close the connection. Once called, no further output is possible, but the script stays alive and can continue processing data.

::scgi::die msg Output the message msg and quit. The Status header is set to 500 Internal Server Error.

::scgi::exit Send the output buffered (including headers and body data) to the client and terminate the execution of the current script. [exit] is aliased to ::scgi::exit and can be used too.

xml arg arg arg Produce an xml tag. This command is transparent to the user. The line <?xml version="1.0" encoding="utf-8"?> is a starting <? tag, an xml command, two arguments, and a closing ?> tag.

::scgi::html::* attrs children The html namespace exposes commands named after the HTML tags. Each takes an optional (even) list of attributes as key-value pairs and an optional list of children. Example:

namespace path scgi
@ [html::a {title "Take me home" href https://example.com} \
    [list [html::pre {} [list "Go to home"]]] ]

Additionally, the following variables are available to client scripts:

::scgi::params A dictionary with the request parameters.

::scgi::headers A dictionary with the request headers.

::scgi::body The raw request body.

Short tags are also available by using a combination of an opening tag <? and @ command. Example

Running Tcl version <?@ [info patchlevel] ?>

tcl-scgi's People

Contributors

web-flow avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

h0nza

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.