Plan 9 from Bell Labs’s /usr/web/sources/contrib/maht/rc/httplib.rc

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


# usage . /bin/httplib.rc
# GET|POST|HEAD [-r offset count] [-t content_type (for POST)] [-e referer] [-c cookie'='value] http[s]://uri
# for https you'll also need /n/sources/contrib/maht/tlspipeclient.c


fn length {
	echo -n $1 | wc -c | tr -d \x0a\x20
}

fn hdr {
	echo $"* ^
}

fn crack_uri {
	ofs = $ifs
	ifs = /
	bits = `{echo -n $1}
	if(~ $bits(1) https:) {  
		transport = ssl
		port = 443
	}
	if not {
		transport = plain
		port = 80
	}

	ifs = ' 	
'
	host = $bits(2)
	uri = `{echo $1 | sed 's/^'^$bits(1) ^'\/\/' ^$host ^'//'}

	if(echo $host | grep -s '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+')
		ip = $host
	if not {
		bits = `{echo $host | ndb/dnsquery}
		ip = $bits(4)
	}
	
	ifs = $ofs
}

fn plain {
	ofs = $ifs
	ifs ='
'
	clonefile = /net/tcp/clone
	<[4] $clonefile {
		netdir=`{basename -d $clonefile} ^ / ^  `{cat /fd/4}
		if(echo connect $ip!$port > $netdir/ctl) {
			cat > $netdir/data
			chunked = 0
			header = `{read $netdir/data}
			while(! ~ $header  
) {
				if(~ $header *chunked*)
					 chunked = 1
				echo $header | tr -d 
 >[1=2] 
				header = `{read $netdir/data}
			}
			if(~ $chunked 1) {
				chunksize = `{ read $netdir/data | tr -d 
 | awk ' { printf "ibase=16; 0%s\n", toupper($0) ; } '   | bc} 
				while(test $chunksize -gt 0) {
					dd -if $netdir/data -bs $chunksize -count 1 -quiet 1
					dd -if $netdir/data -bs 2 -count 1 -quiet 1 > /dev/null
					chunksize = `{ read $netdir/data | tr -d 
 | awk ' { printf "ibase=16; 0%s\n", toupper($0) ; } '   | bc} 
				}
			}
			if not
				cat $netdir/data
		}
		if not {
			echo Connection to $ip failed >[1=2] 
		}
	}
	ifs = $ofs
}

fn ssl { 
	tlspipeclient tcp!$ip!$port
}

fn HEAD {
	method = HEAD
	perform_method $*
}

fn GET {
	method = GET
	perform_method $*
}

fn POST {
	method = POST
	perform_method $*
}

fn perform_method {
	if(~ $method POST)
		content_type = application/x-www-form-urlencoded
	cookie = (' ')
	count = None
	referer = ''
	while(~ $1 -*) {
		switch($1) {
		case -t
			content_type = $2
			shift 2
		case -c
			cookie = ($cookie $2';')
			shift 2
		case -r
			offset = $2
			count = $3
			shift 3	
		case -e
			referer = $2
			shift 2
		}	
	}
	crack_uri $1
	if(~ $ip [0-9]*) {
		if(~ $method POST)
			body = `{cat}
		{
			hdr $method $uri HTTP/1.1
			hdr host: $host
			if(~ $method POST) {
				hdr 'Content-type: '$content_type
				hdr 'Content-length: ' `{wc -c /env/body | sed -e 's/^ +//' -e 's/ .*//'}
			}
			hdr 'Cookie: '$"cookie
			if(! ~ $count None)
				hdr Range: bytes'='$offset-$count
			if(test -n $referer)
				hdr Referer: $referer
			hdr Connection: Close
			hdr ''
			if(~ $method POST)
				cat /env/body
		} | tee /fd/2 | $transport 
	}
	if not {
		echo DNS failed for $host >[1=2]
	}
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.