#!/bin/sh
# outputs a file over HTTP 1.1 and pushes updates when new lines are appended
# © 2014 Nils Dagsson Moskopp (erlehmann) – license: GPLv3+
# httptaild is intended for usage with tcpserver(1) from ucspi-tcp:
# "tcpserver 0 8008 httptaild log" serves log on http://localhost:8008

status () { printf '%s\r\n' "HTTP/1.1 200 Ok"; }
header () { printf '%s\r\n' "$1"; }
divide () { printf '%s\r\n' "$1"; }
length () { printf '%x\r\n' "$1"; }
body_n () { printf '%s\n\r\n' "$1"; }

status
header "Content-type: $(file --brief --mime "$1")"
header "Transfer-Encoding: chunked"
divide
length "$(wc -c <"$1")"
body_n "$(cat "$1")"

tail -n0 -f "$1" | while IFS=$(printf '\n') read -r line; do
  length "$(printf '%s\n' "$line" | wc -c)"
  body_n "$line"
done
