9front - general discussion about 9front
 help / color / mirror / Atom feed
* Content-range support for rc-httpd(8)
@ 2020-01-07  7:15 Alex Musolino
  2020-01-16 17:23 ` [9front] " Ethan Gardener
  0 siblings, 1 reply; 2+ messages in thread
From: Alex Musolino @ 2020-01-07  7:15 UTC (permalink / raw)
  To: 9front

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [9front] Content-range support for rc-httpd(8)
  2020-01-07  7:15 Content-range support for rc-httpd(8) Alex Musolino
@ 2020-01-16 17:23 ` Ethan Gardener
  0 siblings, 0 replies; 2+ messages in thread
From: Ethan Gardener @ 2020-01-16 17:23 UTC (permalink / raw)
  To: 9front

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
>


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-01-16 17:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-07  7:15 Content-range support for rc-httpd(8) Alex Musolino
2020-01-16 17:23 ` [9front] " Ethan Gardener

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).