xref: /titanic_41/usr/src/lib/libshell/common/scripts/shtinyurl.sh (revision d24234c24aeaca4ca56ee3ac2794507968f274c4)
1#!/usr/bin/ksh93
2
3#
4# CDDL HEADER START
5#
6# The contents of this file are subject to the terms of the
7# Common Development and Distribution License (the "License").
8# You may not use this file except in compliance with the License.
9#
10# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11# or http://www.opensolaris.org/os/licensing.
12# See the License for the specific language governing permissions
13# and limitations under the License.
14#
15# When distributing Covered Code, include this CDDL HEADER in each
16# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17# If applicable, add the following below this CDDL HEADER, with the
18# fields enclosed by brackets "[]" replaced with your own identifying
19# information: Portions Copyright [yyyy] [name of copyright owner]
20#
21# CDDL HEADER END
22#
23
24#
25# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
26# Use is subject to license terms.
27#
28
29# Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant
30export PATH=/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin
31
32# Make sure all math stuff runs in the "C" locale to avoid problems
33# with alternative # radix point representations (e.g. ',' instead of
34# '.' in de_DE.*-locales). This needs to be set _before_ any
35# floating-point constants are defined in this script).
36if [[ "${LC_ALL}" != "" ]] ; then
37    export \
38        LC_MONETARY="${LC_ALL}" \
39        LC_MESSAGES="${LC_ALL}" \
40        LC_COLLATE="${LC_ALL}" \
41        LC_CTYPE="${LC_ALL}"
42        unset LC_ALL
43fi
44export LC_NUMERIC=C
45
46function fatal_error
47{
48	print -u2 "${progname}: $*"
49	exit 1
50}
51
52# parse HTTP return code, cookies etc.
53function parse_http_response
54{
55	nameref response="$1"
56	typeset h statuscode statusmsg i
57
58	# we use '\r' as additional IFS to filter the final '\r'
59	IFS=$' \t\r' read -r h statuscode statusmsg  # read HTTP/1.[01] <code>
60	[[ "$h" != ~(Eil)HTTP/.* ]]         && { print -u2 -f $"%s: HTTP/ header missing\n" "$0" ; return 1 ; }
61	[[ "$statuscode" != ~(Elr)[0-9]* ]] && { print -u2 -f $"%s: invalid status code\n"  "$0" ; return 1 ; }
62	response.statuscode="$statuscode"
63	response.statusmsg="$statusmsg"
64
65	# skip remaining headers
66	while IFS='' read -r i ; do
67		[[ "$i" == $'\r' ]] && break
68
69		# strip '\r' at the end
70		i="${i/~(Er)$'\r'/}"
71
72		case "$i" in
73			~(Eli)Content-Type:.*)
74				response.content_type="${i/~(El).*:[[:blank:]]*/}"
75				;;
76			~(Eli)Content-Length:[[:blank:]]*[0-9]*)
77				integer response.content_length="${i/~(El).*:[[:blank:]]*/}"
78				;;
79			~(Eli)Transfer-Encoding:.*)
80				response.transfer_encoding="${i/~(El).*:[[:blank:]]*/}"
81				;;
82		esac
83	done
84
85	return 0
86}
87
88function cat_http_body
89{
90	typeset emode="$1"
91	typeset hexchunksize="0"
92	integer chunksize=0
93
94	if [[ "${emode}" == "chunked" ]] ; then
95		while IFS=$'\r' read hexchunksize &&
96			[[ "${hexchunksize}" == ~(Elri)[0-9abcdef]* ]] &&
97			(( chunksize=16#${hexchunksize} )) && (( chunksize > 0 )) ; do
98			dd bs=1 count="${chunksize}" 2>/dev/null
99		done
100	else
101		cat
102	fi
103
104	return 0
105}
106
107function request_tinyurl
108{
109	# site setup
110	typeset url_host="tinyurl.com"
111	typeset url_path="/api-create.php"
112	typeset url="http://${url_host}${url_path}"
113	integer netfd # http stream number
114	typeset inputurl="$1"
115	compound httpresponse # http response
116	typeset request=""
117
118	# we assume "inputurl" is a correctly encoded URL which doesn't
119	# require any further mangling
120	url_path+="?url=${inputurl}"
121
122	request="GET ${url_path} HTTP/1.1\r\n"
123	request+="Host: ${url_host}\r\n"
124	request+="User-Agent: ${http_user_agent}\r\n"
125	request+="Connection: close\r\n"
126
127	redirect {netfd}<> "/dev/tcp/${url_host}/80"
128	(( $? != 0 )) && { print -u2 -f $"%s: Could not open connection to %s.\n" "$0" "${url_host}" ;  return 1 ; }
129
130	# send http post
131	{
132		print -n -- "${request}\r\n"
133	}  >&${netfd}
134
135	# process reply
136	parse_http_response httpresponse <&${netfd}
137	response="${ cat_http_body "${httpresponse.transfer_encoding}" <&${netfd} ; }"
138
139	# close connection
140	redirect {netfd}<&-
141
142	if (( httpresponse.statuscode >= 200 && httpresponse.statuscode <= 299 )) ; then
143		print -r -- "${response}"
144		return 0
145	else
146		print -u2 -f $"tinyurl response was (%s,%s):\n%s\n" "${httpresponse.statuscode}" "${httpresponse.statusmsg}" "${response}"
147		return 1
148	fi
149
150	# not reached
151}
152
153function request_trimurl
154{
155	# site setup
156	typeset url_host="api.tr.im"
157	typeset url_path="/api/trim_url.xml"
158	typeset url="http://${url_host}${url_path}"
159	integer netfd # http stream number
160	typeset inputurl="$1"
161	compound httpresponse # http response
162	typeset request=""
163
164	# we assume "inputurl" is a correctly encoded URL which doesn't
165	# require any further mangling
166	url_path+="?url=${inputurl}"
167
168	request="GET ${url_path} HTTP/1.1\r\n"
169	request+="Host: ${url_host}\r\n"
170	request+="User-Agent: ${http_user_agent}\r\n"
171	request+="Connection: close\r\n"
172
173	redirect {netfd}<> "/dev/tcp/${url_host}/80"
174	(( $? != 0 )) && { print -u2 -f $"%s: Could not open connection to %s.\n" "$0" "${url_host}" ;  return 1 ; }
175
176	# send http post
177	{
178		print -n -- "${request}\r\n"
179	}  >&${netfd}
180
181	# process reply
182	parse_http_response httpresponse <&${netfd}
183	response="${ cat_http_body "${httpresponse.transfer_encoding}" <&${netfd} ; }"
184
185	# close connection
186	redirect {netfd}<&-
187
188	if (( httpresponse.statuscode >= 200 && httpresponse.statuscode <= 299 )) ; then
189		# the statement below should really parse the XML...
190		print -r -- "${response/~(Elr).*(\<url\>)(.*)(\<\/url\>).*/\2}"
191		return 0
192	else
193		print -u2 -f $"tr.im response was (%s,%s):\n%s\n" "${httpresponse.statuscode}" "${httpresponse.statusmsg}" "${response}"
194		return 1
195	fi
196
197	# not reached
198}
199
200function usage
201{
202	OPTIND=0
203	getopts -a "${progname}" "${shtinyurl_usage}" OPT '-?'
204	exit 2
205}
206
207# program start
208builtin basename
209builtin cat
210builtin date
211builtin uname
212
213typeset progname="${ basename "${0}" ; }"
214
215# HTTP protocol client identifer
216typeset -r http_user_agent="shtinyurl/ksh93 (2009-08-12; ${ uname -s -r -p ; })"
217
218typeset -r shtinyurl_usage=$'+
219[-?\n@(#)\$Id: shtinyurl (Roland Mainz) 2009-08-12 \$\n]
220[-author?Roland Mainz <roland.mainz@nrubsig.org>]
221[+NAME?shtinyurl - create short alias URL from long URL]
222[+DESCRIPTION?\bshtinyurl\b is a small utility which passes a given URL
223	to internet service which creates short aliases in the
224	form of http://<servicename>/XXXXXXXX to redirect long URLs.]
225[+?The first arg \burl\b describes a long URL which is transformed into
226	a tinyurl.com short alias.]
227[P:provider?Service provider (either \'tinyurl.com\' or \'tr.im\').]:[mode]
228
229url
230
231[+SEE ALSO?\bksh93\b(1), \brssread\b(1), \bshtwitter\b(1), http://www.tinyurl.com, http://tr.im]
232'
233
234typeset service_provider="tr.im"
235
236while getopts -a "${progname}" "${shtinyurl_usage}" OPT ; do
237#	printmsg "## OPT=|${OPT}|, OPTARG=|${OPTARG}|"
238	case ${OPT} in
239		P)	service_provider="${OPTARG}" ;;
240		*)	usage ;;
241	esac
242done
243shift $((OPTIND-1))
244
245# expecting at least one more argument
246(( $# >= 1 )) || usage
247
248typeset url="$1"
249shift
250
251case "${service_provider}" in
252	"tinyurl.com")
253		request_tinyurl "${url}"
254		exit $?
255		;;
256	"tr.im")
257		request_trimurl "${url}"
258		exit $?
259		;;
260	*)
261		fatal_error "Unsupported service provider."
262esac
263
264# not reached
265
266# EOF.
267