xref: /titanic_50/usr/src/lib/libshell/common/scripts/shtinyurl.sh (revision b5a2d8455dfa3190fc977c4bec53e91c99012767)
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 2008 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	typeset -C 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: Couldn't 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 usage
154{
155	OPTIND=0
156	getopts -a "${progname}" "${shtinyurl_usage}" OPT '-?'
157	exit 2
158}
159
160# program start
161builtin basename
162builtin cat
163builtin date
164builtin uname
165
166typeset progname="${ basename "${0}" ; }"
167
168# HTTP protocol client identifer
169typeset -r http_user_agent="shtinyurl/ksh93 (2008-10-14; ${ uname -s -r -p ; })"
170
171typeset -r shtinyurl_usage=$'+
172[-?\n@(#)\$Id: shtinyurl (Roland Mainz) 2008-10-14 \$\n]
173[-author?Roland Mainz <roland.mainz@nrubsig.org>]
174[+NAME?shtinyurl - create short tinyurl.com alias URL from long URL]
175[+DESCRIPTION?\bshtinyurl\b is a small utility which passes a given URL
176	to the tinyurl.com service which creates short aliases in the
177	form of http://tinyurl.com/XXXXXXXX to redirect long URLs.]
178[+?The first arg \burl\b describes a long URL which is transformed into
179	a tinyurl.com short alias.]
180
181url
182
183[+SEE ALSO?\bksh93\b(1), \brssread\b(1), \bshtwitter\b(1), http://www.tinyurl.com]
184'
185
186while getopts -a "${progname}" "${shtinyurl_usage}" OPT ; do
187#	printmsg "## OPT=|${OPT}|, OPTARG=|${OPTARG}|"
188	case ${OPT} in
189		*)	usage ;;
190	esac
191done
192shift $((OPTIND-1))
193
194# expecting at least one more argument
195(($# >= 1)) || usage
196
197typeset url="$1"
198shift
199
200request_tinyurl "${url}"
201exit $?
202# EOF.
203