18462SApril.Chin@Sun.COM#!/usr/bin/ksh93
28462SApril.Chin@Sun.COM
38462SApril.Chin@Sun.COM#
48462SApril.Chin@Sun.COM# CDDL HEADER START
58462SApril.Chin@Sun.COM#
68462SApril.Chin@Sun.COM# The contents of this file are subject to the terms of the
78462SApril.Chin@Sun.COM# Common Development and Distribution License (the "License").
88462SApril.Chin@Sun.COM# You may not use this file except in compliance with the License.
98462SApril.Chin@Sun.COM#
108462SApril.Chin@Sun.COM# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
118462SApril.Chin@Sun.COM# or http://www.opensolaris.org/os/licensing.
128462SApril.Chin@Sun.COM# See the License for the specific language governing permissions
138462SApril.Chin@Sun.COM# and limitations under the License.
148462SApril.Chin@Sun.COM#
158462SApril.Chin@Sun.COM# When distributing Covered Code, include this CDDL HEADER in each
168462SApril.Chin@Sun.COM# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
178462SApril.Chin@Sun.COM# If applicable, add the following below this CDDL HEADER, with the
188462SApril.Chin@Sun.COM# fields enclosed by brackets "[]" replaced with your own identifying
198462SApril.Chin@Sun.COM# information: Portions Copyright [yyyy] [name of copyright owner]
208462SApril.Chin@Sun.COM#
218462SApril.Chin@Sun.COM# CDDL HEADER END
228462SApril.Chin@Sun.COM#
238462SApril.Chin@Sun.COM
248462SApril.Chin@Sun.COM#
25*10898Sroland.mainz@nrubsig.org# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
268462SApril.Chin@Sun.COM# Use is subject to license terms.
278462SApril.Chin@Sun.COM#
288462SApril.Chin@Sun.COM
298462SApril.Chin@Sun.COM# Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant
308462SApril.Chin@Sun.COMexport PATH=/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin
318462SApril.Chin@Sun.COM
328462SApril.Chin@Sun.COM# Make sure all math stuff runs in the "C" locale to avoid problems
338462SApril.Chin@Sun.COM# with alternative # radix point representations (e.g. ',' instead of
348462SApril.Chin@Sun.COM# '.' in de_DE.*-locales). This needs to be set _before_ any
358462SApril.Chin@Sun.COM# floating-point constants are defined in this script).
368462SApril.Chin@Sun.COMif [[ "${LC_ALL}" != "" ]] ; then
378462SApril.Chin@Sun.COM    export \
388462SApril.Chin@Sun.COM        LC_MONETARY="${LC_ALL}" \
398462SApril.Chin@Sun.COM        LC_MESSAGES="${LC_ALL}" \
408462SApril.Chin@Sun.COM        LC_COLLATE="${LC_ALL}" \
418462SApril.Chin@Sun.COM        LC_CTYPE="${LC_ALL}"
428462SApril.Chin@Sun.COM        unset LC_ALL
438462SApril.Chin@Sun.COMfi
448462SApril.Chin@Sun.COMexport LC_NUMERIC=C
458462SApril.Chin@Sun.COM
468462SApril.Chin@Sun.COMfunction fatal_error
478462SApril.Chin@Sun.COM{
488462SApril.Chin@Sun.COM	print -u2 "${progname}: $*"
498462SApril.Chin@Sun.COM	exit 1
508462SApril.Chin@Sun.COM}
518462SApril.Chin@Sun.COM
528462SApril.Chin@Sun.COM# parse HTTP return code, cookies etc.
538462SApril.Chin@Sun.COMfunction parse_http_response
548462SApril.Chin@Sun.COM{
558462SApril.Chin@Sun.COM	nameref response="$1"
568462SApril.Chin@Sun.COM	typeset h statuscode statusmsg i
578462SApril.Chin@Sun.COM
588462SApril.Chin@Sun.COM	# we use '\r' as additional IFS to filter the final '\r'
598462SApril.Chin@Sun.COM	IFS=$' \t\r' read -r h statuscode statusmsg  # read HTTP/1.[01] <code>
608462SApril.Chin@Sun.COM	[[ "$h" != ~(Eil)HTTP/.* ]]         && { print -u2 -f $"%s: HTTP/ header missing\n" "$0" ; return 1 ; }
618462SApril.Chin@Sun.COM	[[ "$statuscode" != ~(Elr)[0-9]* ]] && { print -u2 -f $"%s: invalid status code\n"  "$0" ; return 1 ; }
628462SApril.Chin@Sun.COM	response.statuscode="$statuscode"
638462SApril.Chin@Sun.COM	response.statusmsg="$statusmsg"
648462SApril.Chin@Sun.COM
658462SApril.Chin@Sun.COM	# skip remaining headers
668462SApril.Chin@Sun.COM	while IFS='' read -r i ; do
678462SApril.Chin@Sun.COM		[[ "$i" == $'\r' ]] && break
688462SApril.Chin@Sun.COM
698462SApril.Chin@Sun.COM		# strip '\r' at the end
708462SApril.Chin@Sun.COM		i="${i/~(Er)$'\r'/}"
718462SApril.Chin@Sun.COM
728462SApril.Chin@Sun.COM		case "$i" in
738462SApril.Chin@Sun.COM			~(Eli)Content-Type:.*)
748462SApril.Chin@Sun.COM				response.content_type="${i/~(El).*:[[:blank:]]*/}"
758462SApril.Chin@Sun.COM				;;
768462SApril.Chin@Sun.COM			~(Eli)Content-Length:[[:blank:]]*[0-9]*)
778462SApril.Chin@Sun.COM				integer response.content_length="${i/~(El).*:[[:blank:]]*/}"
788462SApril.Chin@Sun.COM				;;
798462SApril.Chin@Sun.COM			~(Eli)Transfer-Encoding:.*)
808462SApril.Chin@Sun.COM				response.transfer_encoding="${i/~(El).*:[[:blank:]]*/}"
818462SApril.Chin@Sun.COM				;;
828462SApril.Chin@Sun.COM		esac
838462SApril.Chin@Sun.COM	done
848462SApril.Chin@Sun.COM
858462SApril.Chin@Sun.COM	return 0
868462SApril.Chin@Sun.COM}
878462SApril.Chin@Sun.COM
888462SApril.Chin@Sun.COMfunction cat_http_body
898462SApril.Chin@Sun.COM{
908462SApril.Chin@Sun.COM	typeset emode="$1"
918462SApril.Chin@Sun.COM	typeset hexchunksize="0"
928462SApril.Chin@Sun.COM	integer chunksize=0
938462SApril.Chin@Sun.COM
948462SApril.Chin@Sun.COM	if [[ "${emode}" == "chunked" ]] ; then
958462SApril.Chin@Sun.COM		while IFS=$'\r' read hexchunksize &&
968462SApril.Chin@Sun.COM			[[ "${hexchunksize}" == ~(Elri)[0-9abcdef]* ]] &&
978462SApril.Chin@Sun.COM			(( chunksize=16#${hexchunksize} )) && (( chunksize > 0 )) ; do
988462SApril.Chin@Sun.COM			dd bs=1 count="${chunksize}" 2>/dev/null
998462SApril.Chin@Sun.COM		done
1008462SApril.Chin@Sun.COM	else
1018462SApril.Chin@Sun.COM		cat
1028462SApril.Chin@Sun.COM	fi
1038462SApril.Chin@Sun.COM
1048462SApril.Chin@Sun.COM	return 0
1058462SApril.Chin@Sun.COM}
1068462SApril.Chin@Sun.COM
1078462SApril.Chin@Sun.COMfunction request_tinyurl
1088462SApril.Chin@Sun.COM{
1098462SApril.Chin@Sun.COM	# site setup
1108462SApril.Chin@Sun.COM	typeset url_host="tinyurl.com"
1118462SApril.Chin@Sun.COM	typeset url_path="/api-create.php"
1128462SApril.Chin@Sun.COM	typeset url="http://${url_host}${url_path}"
1138462SApril.Chin@Sun.COM	integer netfd # http stream number
1148462SApril.Chin@Sun.COM	typeset inputurl="$1"
115*10898Sroland.mainz@nrubsig.org	compound httpresponse # http response
1168462SApril.Chin@Sun.COM	typeset request=""
1178462SApril.Chin@Sun.COM
1188462SApril.Chin@Sun.COM	# we assume "inputurl" is a correctly encoded URL which doesn't
1198462SApril.Chin@Sun.COM	# require any further mangling
1208462SApril.Chin@Sun.COM	url_path+="?url=${inputurl}"
1218462SApril.Chin@Sun.COM
1228462SApril.Chin@Sun.COM	request="GET ${url_path} HTTP/1.1\r\n"
1238462SApril.Chin@Sun.COM	request+="Host: ${url_host}\r\n"
1248462SApril.Chin@Sun.COM	request+="User-Agent: ${http_user_agent}\r\n"
1258462SApril.Chin@Sun.COM	request+="Connection: close\r\n"
1268462SApril.Chin@Sun.COM
127*10898Sroland.mainz@nrubsig.org	redirect {netfd}<> "/dev/tcp/${url_host}/80"
128*10898Sroland.mainz@nrubsig.org	(( $? != 0 )) && { print -u2 -f $"%s: Could not open connection to %s.\n" "$0" "${url_host}" ;  return 1 ; }
1298462SApril.Chin@Sun.COM
1308462SApril.Chin@Sun.COM	# send http post
1318462SApril.Chin@Sun.COM	{
1328462SApril.Chin@Sun.COM		print -n -- "${request}\r\n"
1338462SApril.Chin@Sun.COM	}  >&${netfd}
1348462SApril.Chin@Sun.COM
1358462SApril.Chin@Sun.COM	# process reply
1368462SApril.Chin@Sun.COM	parse_http_response httpresponse <&${netfd}
1378462SApril.Chin@Sun.COM	response="${ cat_http_body "${httpresponse.transfer_encoding}" <&${netfd} ; }"
1388462SApril.Chin@Sun.COM
1398462SApril.Chin@Sun.COM	# close connection
1408462SApril.Chin@Sun.COM	redirect {netfd}<&-
1418462SApril.Chin@Sun.COM
1428462SApril.Chin@Sun.COM	if (( httpresponse.statuscode >= 200 && httpresponse.statuscode <= 299 )) ; then
1438462SApril.Chin@Sun.COM		print -r -- "${response}"
1448462SApril.Chin@Sun.COM		return 0
1458462SApril.Chin@Sun.COM	else
1468462SApril.Chin@Sun.COM		print -u2 -f $"tinyurl response was (%s,%s):\n%s\n" "${httpresponse.statuscode}" "${httpresponse.statusmsg}" "${response}"
1478462SApril.Chin@Sun.COM		return 1
1488462SApril.Chin@Sun.COM	fi
1498462SApril.Chin@Sun.COM
1508462SApril.Chin@Sun.COM	# not reached
1518462SApril.Chin@Sun.COM}
1528462SApril.Chin@Sun.COM
153*10898Sroland.mainz@nrubsig.orgfunction request_trimurl
154*10898Sroland.mainz@nrubsig.org{
155*10898Sroland.mainz@nrubsig.org	# site setup
156*10898Sroland.mainz@nrubsig.org	typeset url_host="api.tr.im"
157*10898Sroland.mainz@nrubsig.org	typeset url_path="/api/trim_url.xml"
158*10898Sroland.mainz@nrubsig.org	typeset url="http://${url_host}${url_path}"
159*10898Sroland.mainz@nrubsig.org	integer netfd # http stream number
160*10898Sroland.mainz@nrubsig.org	typeset inputurl="$1"
161*10898Sroland.mainz@nrubsig.org	compound httpresponse # http response
162*10898Sroland.mainz@nrubsig.org	typeset request=""
163*10898Sroland.mainz@nrubsig.org
164*10898Sroland.mainz@nrubsig.org	# we assume "inputurl" is a correctly encoded URL which doesn't
165*10898Sroland.mainz@nrubsig.org	# require any further mangling
166*10898Sroland.mainz@nrubsig.org	url_path+="?url=${inputurl}"
167*10898Sroland.mainz@nrubsig.org
168*10898Sroland.mainz@nrubsig.org	request="GET ${url_path} HTTP/1.1\r\n"
169*10898Sroland.mainz@nrubsig.org	request+="Host: ${url_host}\r\n"
170*10898Sroland.mainz@nrubsig.org	request+="User-Agent: ${http_user_agent}\r\n"
171*10898Sroland.mainz@nrubsig.org	request+="Connection: close\r\n"
172*10898Sroland.mainz@nrubsig.org
173*10898Sroland.mainz@nrubsig.org	redirect {netfd}<> "/dev/tcp/${url_host}/80"
174*10898Sroland.mainz@nrubsig.org	(( $? != 0 )) && { print -u2 -f $"%s: Could not open connection to %s.\n" "$0" "${url_host}" ;  return 1 ; }
175*10898Sroland.mainz@nrubsig.org
176*10898Sroland.mainz@nrubsig.org	# send http post
177*10898Sroland.mainz@nrubsig.org	{
178*10898Sroland.mainz@nrubsig.org		print -n -- "${request}\r\n"
179*10898Sroland.mainz@nrubsig.org	}  >&${netfd}
180*10898Sroland.mainz@nrubsig.org
181*10898Sroland.mainz@nrubsig.org	# process reply
182*10898Sroland.mainz@nrubsig.org	parse_http_response httpresponse <&${netfd}
183*10898Sroland.mainz@nrubsig.org	response="${ cat_http_body "${httpresponse.transfer_encoding}" <&${netfd} ; }"
184*10898Sroland.mainz@nrubsig.org
185*10898Sroland.mainz@nrubsig.org	# close connection
186*10898Sroland.mainz@nrubsig.org	redirect {netfd}<&-
187*10898Sroland.mainz@nrubsig.org
188*10898Sroland.mainz@nrubsig.org	if (( httpresponse.statuscode >= 200 && httpresponse.statuscode <= 299 )) ; then
189*10898Sroland.mainz@nrubsig.org		# the statement below should really parse the XML...
190*10898Sroland.mainz@nrubsig.org		print -r -- "${response/~(Elr).*(\<url\>)(.*)(\<\/url\>).*/\2}"
191*10898Sroland.mainz@nrubsig.org		return 0
192*10898Sroland.mainz@nrubsig.org	else
193*10898Sroland.mainz@nrubsig.org		print -u2 -f $"tr.im response was (%s,%s):\n%s\n" "${httpresponse.statuscode}" "${httpresponse.statusmsg}" "${response}"
194*10898Sroland.mainz@nrubsig.org		return 1
195*10898Sroland.mainz@nrubsig.org	fi
196*10898Sroland.mainz@nrubsig.org
197*10898Sroland.mainz@nrubsig.org	# not reached
198*10898Sroland.mainz@nrubsig.org}
199*10898Sroland.mainz@nrubsig.org
2008462SApril.Chin@Sun.COMfunction usage
2018462SApril.Chin@Sun.COM{
2028462SApril.Chin@Sun.COM	OPTIND=0
2038462SApril.Chin@Sun.COM	getopts -a "${progname}" "${shtinyurl_usage}" OPT '-?'
2048462SApril.Chin@Sun.COM	exit 2
2058462SApril.Chin@Sun.COM}
2068462SApril.Chin@Sun.COM
2078462SApril.Chin@Sun.COM# program start
2088462SApril.Chin@Sun.COMbuiltin basename
2098462SApril.Chin@Sun.COMbuiltin cat
2108462SApril.Chin@Sun.COMbuiltin date
2118462SApril.Chin@Sun.COMbuiltin uname
2128462SApril.Chin@Sun.COM
2138462SApril.Chin@Sun.COMtypeset progname="${ basename "${0}" ; }"
2148462SApril.Chin@Sun.COM
2158462SApril.Chin@Sun.COM# HTTP protocol client identifer
216*10898Sroland.mainz@nrubsig.orgtypeset -r http_user_agent="shtinyurl/ksh93 (2009-08-12; ${ uname -s -r -p ; })"
2178462SApril.Chin@Sun.COM
2188462SApril.Chin@Sun.COMtypeset -r shtinyurl_usage=$'+
219*10898Sroland.mainz@nrubsig.org[-?\n@(#)\$Id: shtinyurl (Roland Mainz) 2009-08-12 \$\n]
2208462SApril.Chin@Sun.COM[-author?Roland Mainz <roland.mainz@nrubsig.org>]
221*10898Sroland.mainz@nrubsig.org[+NAME?shtinyurl - create short alias URL from long URL]
2228462SApril.Chin@Sun.COM[+DESCRIPTION?\bshtinyurl\b is a small utility which passes a given URL
223*10898Sroland.mainz@nrubsig.org	to internet service which creates short aliases in the
224*10898Sroland.mainz@nrubsig.org	form of http://<servicename>/XXXXXXXX to redirect long URLs.]
2258462SApril.Chin@Sun.COM[+?The first arg \burl\b describes a long URL which is transformed into
2268462SApril.Chin@Sun.COM	a tinyurl.com short alias.]
227*10898Sroland.mainz@nrubsig.org[P:provider?Service provider (either \'tinyurl.com\' or \'tr.im\').]:[mode]
2288462SApril.Chin@Sun.COM
2298462SApril.Chin@Sun.COMurl
2308462SApril.Chin@Sun.COM
231*10898Sroland.mainz@nrubsig.org[+SEE ALSO?\bksh93\b(1), \brssread\b(1), \bshtwitter\b(1), http://www.tinyurl.com, http://tr.im]
2328462SApril.Chin@Sun.COM'
2338462SApril.Chin@Sun.COM
234*10898Sroland.mainz@nrubsig.orgtypeset service_provider="tr.im"
235*10898Sroland.mainz@nrubsig.org
2368462SApril.Chin@Sun.COMwhile getopts -a "${progname}" "${shtinyurl_usage}" OPT ; do
2378462SApril.Chin@Sun.COM#	printmsg "## OPT=|${OPT}|, OPTARG=|${OPTARG}|"
2388462SApril.Chin@Sun.COM	case ${OPT} in
239*10898Sroland.mainz@nrubsig.org		P)	service_provider="${OPTARG}" ;;
2408462SApril.Chin@Sun.COM		*)	usage ;;
2418462SApril.Chin@Sun.COM	esac
2428462SApril.Chin@Sun.COMdone
2438462SApril.Chin@Sun.COMshift $((OPTIND-1))
2448462SApril.Chin@Sun.COM
2458462SApril.Chin@Sun.COM# expecting at least one more argument
246*10898Sroland.mainz@nrubsig.org(( $# >= 1 )) || usage
2478462SApril.Chin@Sun.COM
2488462SApril.Chin@Sun.COMtypeset url="$1"
2498462SApril.Chin@Sun.COMshift
2508462SApril.Chin@Sun.COM
251*10898Sroland.mainz@nrubsig.orgcase "${service_provider}" in
252*10898Sroland.mainz@nrubsig.org	"tinyurl.com")
253*10898Sroland.mainz@nrubsig.org		request_tinyurl "${url}"
254*10898Sroland.mainz@nrubsig.org		exit $?
255*10898Sroland.mainz@nrubsig.org		;;
256*10898Sroland.mainz@nrubsig.org	"tr.im")
257*10898Sroland.mainz@nrubsig.org		request_trimurl "${url}"
258*10898Sroland.mainz@nrubsig.org		exit $?
259*10898Sroland.mainz@nrubsig.org		;;
260*10898Sroland.mainz@nrubsig.org	*)
261*10898Sroland.mainz@nrubsig.org		fatal_error "Unsupported service provider."
262*10898Sroland.mainz@nrubsig.orgesac
263*10898Sroland.mainz@nrubsig.org
264*10898Sroland.mainz@nrubsig.org# not reached
265*10898Sroland.mainz@nrubsig.org
2668462SApril.Chin@Sun.COM# EOF.
267