9front - general discussion about 9front
 help / color / mirror / Atom feed
From: Alex Musolino <alex@musolino.id.au>
To: 9front@9front.org
Subject: Content-range support for rc-httpd(8)
Date: Tue, 7 Jan 2020 17:45:54 +1030	[thread overview]
Message-ID: <5F78518AF28D01DD965738D7D8D4CF45@musolino.id.au> (raw)

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-07  7:15 UTC|newest]

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

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=5F78518AF28D01DD965738D7D8D4CF45@musolino.id.au \
    --to=alex@musolino.id.au \
    --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).