9front - general discussion about 9front
 help / color / mirror / Atom feed
From: "Ethan Gardener" <eekee57@fastmail.fm>
To: 9front@9front.org
Subject: Re: [9front] Content-range support for rc-httpd(8)
Date: Thu, 16 Jan 2020 17:23:46 +0000	[thread overview]
Message-ID: <ac947e6d-a3c8-43b8-9d85-bba408cb3bb7@www.fastmail.com> (raw)
In-Reply-To: <5F78518AF28D01DD965738D7D8D4CF45@musolino.id.au>

Looks good to me, with the caveat that I haven't looked up how it's supposed to work, nor tested it.

I'm not sure why it uses both hoc and bc, but that's a very minor point.

On Tue, Jan 7, 2020, at 7:15 AM, Alex Musolino wrote:
> Some time ago I had a crack at implementing content-range support in
> rc-httpd(8).  A previous email regarding a dissatisfied (non-)customer
> prompted me to make the following patch public.  It works for my
> purposes and may serve as a starting point for something that could
> eventually be included in 9front.
> 
> No refunds.
> 
> diff -r 5423b63716e7 rc/bin/rc-httpd/handlers/serve-static
> --- a/rc/bin/rc-httpd/handlers/serve-static	Tue Jan 07 17:28:59 2020 +1030
> +++ b/rc/bin/rc-httpd/handlers/serve-static	Tue Jan 07 18:13:26 2020 +1100
> @@ -16,7 +16,6 @@
>  	error 503
>  	exit
>  }
> -do_log 200
>  switch($full_path){
>  case *.html *.htm
>          type=text/html
> @@ -36,8 +35,14 @@
>  if(~ $type text/*)
>  	type=$type^'; charset=utf-8'
>  max_age=3600	# 1 hour
> +if(! ~ $#CONTENT_RANGE 0){
> +	. serve-static-range
> +	exit
> +}
> +do_log 200
>  echo 'HTTP/1.1 200 OK'^$cr
>  emit_extra_headers
> +echo 'Accept-ranges: bytes'^$cr
>  echo 'Content-type: '^$type^$cr
>  echo 'Content-length: '^`{ls -l $full_path | awk '{print $6}'}^$cr
>  echo 'Cache-control: max-age='^$max_age^$cr
> diff -r 5423b63716e7 rc/bin/rc-httpd/handlers/serve-static-range
> --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
> +++ b/rc/bin/rc-httpd/handlers/serve-static-range	Tue Jan 07 18:13:26 2020 +1100
> @@ -0,0 +1,46 @@
> +#!/bin/rc
> +
> +fn range{
> +	syscall seek 0 $1 0
> +	length=$2
> +	while(! ~ $length 0){
> +		switch(`{echo $length^' >= '^$3 | hoc}){
> +		case 1
> +			read -c $3
> +			length=`{echo $length - $3 | bc}
> +		case 0
> +			read -c $length
> +			length=0
> +		}
> +	}
> +}
> +
> +chunk=512000
> +total=`{du -n $full_path}
> +total=$total(1)
> +
> +switch($#CONTENT_RANGE){
> +case 2
> +	start=$CONTENT_RANGE(1)
> +	end=$CONTENT_RANGE(2)
> +case 1
> +	start=$CONTENT_RANGE(1)
> +	end=`{echo $start + $chunk - 1 | bc}
> +case *
> +	error 416
> +	exit
> +}
> +
> +end=`{echo 'if('$end'>='$total'){print '$total'-1}else{print '$end'}' | hoc}
> +length=`{echo $end - $start + 1 | bc}
> +
> +do_log 206
> +echo 'HTTP/1.1 206 Partial Content'^$cr
> +emit_extra_headers
> +echo 'Accept-ranges: bytes'^$cr
> +echo 'Content-type: '^$type^$cr
> +echo 'Cache-control: max-age='^$max_age^$cr
> +echo 'Content-range: bytes '^$start^-^$end^/^$total^$cr
> +echo 'Content-length: '^$length^$cr
> +echo $cr
> +range $start $length $chunk <$full_path
> diff -r 5423b63716e7 rc/bin/rc-httpd/rc-httpd
> --- a/rc/bin/rc-httpd/rc-httpd	Tue Jan 07 17:28:59 2020 +1030
> +++ b/rc/bin/rc-httpd/rc-httpd	Tue Jan 07 18:13:26 2020 +1100
> @@ -51,6 +51,8 @@
>  		HTTP_REFERER=$line(2)
>  	case user-agent:
>  		HTTP_USER_AGENT=`{echo $line | sed 's;[^:]+:[ 	]+;;'}
> +	case range:
> +		CONTENT_RANGE=`{echo $line(2) | sed -e 's/bytes=//' -e 's/-/ /'}
>  	case content-length:
>  		CONTENT_LENGTH=$line(2)
>  	case content-type:
> 
> --
> Cheers,
> Alex Musolino
>


      reply	other threads:[~2020-01-16 17:24 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-07  7:15 Alex Musolino
2020-01-16 17:23 ` Ethan Gardener [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ac947e6d-a3c8-43b8-9d85-bba408cb3bb7@www.fastmail.com \
    --to=eekee57@fastmail.fm \
    --cc=9front@9front.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).