#!/usr/bin/env jimsh
### arserv-5e72.tcl  -*- Tcl -*-
## A simplistic server implementing two kinds (A, R) of requests.

### Ivan Shmakov, 2020

## To the extent possible under law, the author(s) have dedicated
## all copyright and related and neighboring rights to this software
## to the public domain worldwide.  This software is distributed
## without any warranty.

## You should have received a copy of the CC0 Public Domain Dedication
## along with this software.  If not, see
## <http://creativecommons.org/publicdomain/zero/1.0/>.

### History:

## 0.1  2020-03-18
##      Initial revision.

### Code:

proc process { rq } {
    switch -glob -- $rq {
        "[aA][ \t]*" { return "200 OK [ string range $rq 2 end ]" }
        "[rR][ \t]*" { return "200 OK [ string reverse \
                                            [ string range $rq 2 end ] ]" }
        "*" { return "500 No such request" }
    }
}

while { [ gets stdin rq ] > 0 } {
    switch -glob -- $rq {
        "*\r" { set rq [ string range $rq 0 end-1 ] }
    }
    set r [ process $rq ]
    puts -nonewline stdout "$r\r\n"
    flush stdout
    switch -regexp -- $r {
        "^[^2]" { break }
    }
}

### arserv-5e72.tcl ends here
