Hello,

As I recently played werc on Plan 9, I would like to share a couple of things I found.

First, in the Docs page (http://werc.cat-v.org/docs/web-server-setup/plan-9-httpd), it says

> you will need to replace the ‘%($“extraHeaders%)’ in lib/headers.tpl with ‘% echo $"extraHeaders’ no clue why.

The reason is %(...%) invokes "echo -n", and when the variable is empty (in many cases $extraHeaders is), it calls write() with zero length, which, if writes to a pipe, causes the other end of the pipe read()'s zero byte (as pipe(3) says, write boundary is preserved), and it may think it's got to the end. For example, cat, /sys/src/cmd/cat.c:10

    while((n=read(f, buf, (long)sizeof buf))>0)

$“extraHeaders is used in bin/werc.rc:140

    template $headers $master_template | awk_buffer

which also suffers the same problem. I have played many different ways to work around it, and my current solution (I believe covering all cases) is to wrap echo as

fn echo { builtin echo $* | cat }

so when it is echoing nothing, "cat" will consume the zero-length write(). Any comment or idea? I don't have p9p so I don't know why there is no such problem. Probably unix's pipe implemented differently.


Second, in the same Docs page, the werc-wrapper's arguments seem not matched with httpd's magic. Is it outdated? Or I missed anything? The following works for me:

SERVER_NAME=$*(2)
REQUEST_URI=`{echo $*(22)}
if(~ $#REQUEST_URI 0)
    REQUEST_URI=/
REQUEST_METHOD=$*(20)
PLAN9=/


Another thing is about remapping the root directory. Httpd does not handle it, so we put all stuff into a sub directory, but many werc generated links are still pointing to something under the root, e.g. the side-bar, and even css. Certainly a way is to hack werc. My alternative approach is generating rewriting rules mapping every first-level directories, putting it into a script starting httpd, like,

    echo /w @/magic/werc-wrap > /tmp/rewrite
    echo / @/w >> /tmp/rewrite
    for(i in `{ls -p $sitedir})
        echo /$i @/w/$i >> /tmp/rewrite

    bind /tmp/rewrite /sys/lib/httpd.rewrite

    Kill httpd | rc
    ip/httpd/httpd

A problem is adding new stuff to the root requires a restart of httpd, but this happens probably not very often.

Hope this is useful, and sorry for a long email.

Thanks,
- cherry