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*12068SRoger.Faulkner@Oracle.COM# Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. 268462SApril.Chin@Sun.COM# 278462SApril.Chin@Sun.COM 288462SApril.Chin@Sun.COM# Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant 298462SApril.Chin@Sun.COMexport PATH=/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin 308462SApril.Chin@Sun.COM 318462SApril.Chin@Sun.COM# Make sure all math stuff runs in the "C" locale to avoid problems 328462SApril.Chin@Sun.COM# with alternative # radix point representations (e.g. ',' instead of 338462SApril.Chin@Sun.COM# '.' in de_DE.*-locales). This needs to be set _before_ any 348462SApril.Chin@Sun.COM# floating-point constants are defined in this script). 358462SApril.Chin@Sun.COMif [[ "${LC_ALL}" != "" ]] ; then 368462SApril.Chin@Sun.COM export \ 378462SApril.Chin@Sun.COM LC_MONETARY="${LC_ALL}" \ 388462SApril.Chin@Sun.COM LC_MESSAGES="${LC_ALL}" \ 398462SApril.Chin@Sun.COM LC_COLLATE="${LC_ALL}" \ 408462SApril.Chin@Sun.COM LC_CTYPE="${LC_ALL}" 418462SApril.Chin@Sun.COM unset LC_ALL 428462SApril.Chin@Sun.COMfi 438462SApril.Chin@Sun.COMexport LC_NUMERIC=C 448462SApril.Chin@Sun.COM 458462SApril.Chin@Sun.COMfunction fatal_error 468462SApril.Chin@Sun.COM{ 478462SApril.Chin@Sun.COM print -u2 "${progname}: $*" 488462SApril.Chin@Sun.COM exit 1 498462SApril.Chin@Sun.COM} 508462SApril.Chin@Sun.COM 518462SApril.Chin@Sun.COM# parse HTTP return code, cookies etc. 528462SApril.Chin@Sun.COMfunction parse_http_response 538462SApril.Chin@Sun.COM{ 548462SApril.Chin@Sun.COM nameref response="$1" 558462SApril.Chin@Sun.COM typeset h statuscode statusmsg i 568462SApril.Chin@Sun.COM 578462SApril.Chin@Sun.COM # we use '\r' as additional IFS to filter the final '\r' 588462SApril.Chin@Sun.COM IFS=$' \t\r' read -r h statuscode statusmsg # read HTTP/1.[01] <code> 598462SApril.Chin@Sun.COM [[ "$h" != ~(Eil)HTTP/.* ]] && { print -u2 -f $"%s: HTTP/ header missing\n" "$0" ; return 1 ; } 608462SApril.Chin@Sun.COM [[ "$statuscode" != ~(Elr)[0-9]* ]] && { print -u2 -f $"%s: invalid status code\n" "$0" ; return 1 ; } 618462SApril.Chin@Sun.COM response.statuscode="$statuscode" 628462SApril.Chin@Sun.COM response.statusmsg="$statusmsg" 638462SApril.Chin@Sun.COM 648462SApril.Chin@Sun.COM # skip remaining headers 658462SApril.Chin@Sun.COM while IFS='' read -r i ; do 668462SApril.Chin@Sun.COM [[ "$i" == $'\r' ]] && break 678462SApril.Chin@Sun.COM 688462SApril.Chin@Sun.COM # strip '\r' at the end 698462SApril.Chin@Sun.COM i="${i/~(Er)$'\r'/}" 708462SApril.Chin@Sun.COM 718462SApril.Chin@Sun.COM case "$i" in 728462SApril.Chin@Sun.COM ~(Eli)Content-Type:.*) 738462SApril.Chin@Sun.COM response.content_type="${i/~(El).*:[[:blank:]]*/}" 748462SApril.Chin@Sun.COM ;; 758462SApril.Chin@Sun.COM ~(Eli)Content-Length:[[:blank:]]*[0-9]*) 768462SApril.Chin@Sun.COM integer response.content_length="${i/~(El).*:[[:blank:]]*/}" 778462SApril.Chin@Sun.COM ;; 788462SApril.Chin@Sun.COM ~(Eli)Transfer-Encoding:.*) 798462SApril.Chin@Sun.COM response.transfer_encoding="${i/~(El).*:[[:blank:]]*/}" 808462SApril.Chin@Sun.COM ;; 818462SApril.Chin@Sun.COM esac 828462SApril.Chin@Sun.COM done 838462SApril.Chin@Sun.COM 848462SApril.Chin@Sun.COM return 0 858462SApril.Chin@Sun.COM} 868462SApril.Chin@Sun.COM 878462SApril.Chin@Sun.COMfunction cat_http_body 888462SApril.Chin@Sun.COM{ 898462SApril.Chin@Sun.COM typeset emode="$1" 908462SApril.Chin@Sun.COM typeset hexchunksize="0" 918462SApril.Chin@Sun.COM integer chunksize=0 928462SApril.Chin@Sun.COM 938462SApril.Chin@Sun.COM if [[ "${emode}" == "chunked" ]] ; then 948462SApril.Chin@Sun.COM while IFS=$'\r' read hexchunksize && 95*12068SRoger.Faulkner@Oracle.COM [[ "${hexchunksize}" == ~(Elri)[0-9abcdef]+ ]] && 96*12068SRoger.Faulkner@Oracle.COM (( chunksize=$( printf "16#%s\n" "${hexchunksize}" ) )) && (( chunksize > 0 )) ; do 978462SApril.Chin@Sun.COM dd bs=1 count="${chunksize}" 2>/dev/null 988462SApril.Chin@Sun.COM done 998462SApril.Chin@Sun.COM else 1008462SApril.Chin@Sun.COM cat 1018462SApril.Chin@Sun.COM fi 1028462SApril.Chin@Sun.COM 1038462SApril.Chin@Sun.COM return 0 1048462SApril.Chin@Sun.COM} 1058462SApril.Chin@Sun.COM 1068462SApril.Chin@Sun.COMfunction request_tinyurl 1078462SApril.Chin@Sun.COM{ 1088462SApril.Chin@Sun.COM # site setup 1098462SApril.Chin@Sun.COM typeset url_host="tinyurl.com" 1108462SApril.Chin@Sun.COM typeset url_path="/api-create.php" 1118462SApril.Chin@Sun.COM typeset url="http://${url_host}${url_path}" 1128462SApril.Chin@Sun.COM integer netfd # http stream number 1138462SApril.Chin@Sun.COM typeset inputurl="$1" 11410898Sroland.mainz@nrubsig.org compound httpresponse # http response 1158462SApril.Chin@Sun.COM typeset request="" 1168462SApril.Chin@Sun.COM 1178462SApril.Chin@Sun.COM # we assume "inputurl" is a correctly encoded URL which doesn't 1188462SApril.Chin@Sun.COM # require any further mangling 1198462SApril.Chin@Sun.COM url_path+="?url=${inputurl}" 1208462SApril.Chin@Sun.COM 1218462SApril.Chin@Sun.COM request="GET ${url_path} HTTP/1.1\r\n" 1228462SApril.Chin@Sun.COM request+="Host: ${url_host}\r\n" 1238462SApril.Chin@Sun.COM request+="User-Agent: ${http_user_agent}\r\n" 1248462SApril.Chin@Sun.COM request+="Connection: close\r\n" 1258462SApril.Chin@Sun.COM 12610898Sroland.mainz@nrubsig.org redirect {netfd}<> "/dev/tcp/${url_host}/80" 12710898Sroland.mainz@nrubsig.org (( $? != 0 )) && { print -u2 -f $"%s: Could not open connection to %s.\n" "$0" "${url_host}" ; return 1 ; } 1288462SApril.Chin@Sun.COM 1298462SApril.Chin@Sun.COM # send http post 1308462SApril.Chin@Sun.COM { 1318462SApril.Chin@Sun.COM print -n -- "${request}\r\n" 1328462SApril.Chin@Sun.COM } >&${netfd} 1338462SApril.Chin@Sun.COM 1348462SApril.Chin@Sun.COM # process reply 1358462SApril.Chin@Sun.COM parse_http_response httpresponse <&${netfd} 1368462SApril.Chin@Sun.COM response="${ cat_http_body "${httpresponse.transfer_encoding}" <&${netfd} ; }" 1378462SApril.Chin@Sun.COM 1388462SApril.Chin@Sun.COM # close connection 1398462SApril.Chin@Sun.COM redirect {netfd}<&- 1408462SApril.Chin@Sun.COM 1418462SApril.Chin@Sun.COM if (( httpresponse.statuscode >= 200 && httpresponse.statuscode <= 299 )) ; then 1428462SApril.Chin@Sun.COM print -r -- "${response}" 1438462SApril.Chin@Sun.COM return 0 1448462SApril.Chin@Sun.COM else 1458462SApril.Chin@Sun.COM print -u2 -f $"tinyurl response was (%s,%s):\n%s\n" "${httpresponse.statuscode}" "${httpresponse.statusmsg}" "${response}" 1468462SApril.Chin@Sun.COM return 1 1478462SApril.Chin@Sun.COM fi 1488462SApril.Chin@Sun.COM 1498462SApril.Chin@Sun.COM # not reached 1508462SApril.Chin@Sun.COM} 1518462SApril.Chin@Sun.COM 15210898Sroland.mainz@nrubsig.orgfunction request_trimurl 15310898Sroland.mainz@nrubsig.org{ 15410898Sroland.mainz@nrubsig.org # site setup 15510898Sroland.mainz@nrubsig.org typeset url_host="api.tr.im" 15610898Sroland.mainz@nrubsig.org typeset url_path="/api/trim_url.xml" 15710898Sroland.mainz@nrubsig.org typeset url="http://${url_host}${url_path}" 15810898Sroland.mainz@nrubsig.org integer netfd # http stream number 15910898Sroland.mainz@nrubsig.org typeset inputurl="$1" 16010898Sroland.mainz@nrubsig.org compound httpresponse # http response 16110898Sroland.mainz@nrubsig.org typeset request="" 16210898Sroland.mainz@nrubsig.org 16310898Sroland.mainz@nrubsig.org # we assume "inputurl" is a correctly encoded URL which doesn't 16410898Sroland.mainz@nrubsig.org # require any further mangling 16510898Sroland.mainz@nrubsig.org url_path+="?url=${inputurl}" 16610898Sroland.mainz@nrubsig.org 16710898Sroland.mainz@nrubsig.org request="GET ${url_path} HTTP/1.1\r\n" 16810898Sroland.mainz@nrubsig.org request+="Host: ${url_host}\r\n" 16910898Sroland.mainz@nrubsig.org request+="User-Agent: ${http_user_agent}\r\n" 17010898Sroland.mainz@nrubsig.org request+="Connection: close\r\n" 17110898Sroland.mainz@nrubsig.org 17210898Sroland.mainz@nrubsig.org redirect {netfd}<> "/dev/tcp/${url_host}/80" 17310898Sroland.mainz@nrubsig.org (( $? != 0 )) && { print -u2 -f $"%s: Could not open connection to %s.\n" "$0" "${url_host}" ; return 1 ; } 17410898Sroland.mainz@nrubsig.org 17510898Sroland.mainz@nrubsig.org # send http post 17610898Sroland.mainz@nrubsig.org { 17710898Sroland.mainz@nrubsig.org print -n -- "${request}\r\n" 17810898Sroland.mainz@nrubsig.org } >&${netfd} 17910898Sroland.mainz@nrubsig.org 18010898Sroland.mainz@nrubsig.org # process reply 18110898Sroland.mainz@nrubsig.org parse_http_response httpresponse <&${netfd} 18210898Sroland.mainz@nrubsig.org response="${ cat_http_body "${httpresponse.transfer_encoding}" <&${netfd} ; }" 18310898Sroland.mainz@nrubsig.org 18410898Sroland.mainz@nrubsig.org # close connection 18510898Sroland.mainz@nrubsig.org redirect {netfd}<&- 18610898Sroland.mainz@nrubsig.org 18710898Sroland.mainz@nrubsig.org if (( httpresponse.statuscode >= 200 && httpresponse.statuscode <= 299 )) ; then 18810898Sroland.mainz@nrubsig.org # the statement below should really parse the XML... 18910898Sroland.mainz@nrubsig.org print -r -- "${response/~(Elr).*(\<url\>)(.*)(\<\/url\>).*/\2}" 19010898Sroland.mainz@nrubsig.org return 0 19110898Sroland.mainz@nrubsig.org else 19210898Sroland.mainz@nrubsig.org print -u2 -f $"tr.im response was (%s,%s):\n%s\n" "${httpresponse.statuscode}" "${httpresponse.statusmsg}" "${response}" 19310898Sroland.mainz@nrubsig.org return 1 19410898Sroland.mainz@nrubsig.org fi 19510898Sroland.mainz@nrubsig.org 19610898Sroland.mainz@nrubsig.org # not reached 19710898Sroland.mainz@nrubsig.org} 19810898Sroland.mainz@nrubsig.org 1998462SApril.Chin@Sun.COMfunction usage 2008462SApril.Chin@Sun.COM{ 2018462SApril.Chin@Sun.COM OPTIND=0 2028462SApril.Chin@Sun.COM getopts -a "${progname}" "${shtinyurl_usage}" OPT '-?' 2038462SApril.Chin@Sun.COM exit 2 2048462SApril.Chin@Sun.COM} 2058462SApril.Chin@Sun.COM 2068462SApril.Chin@Sun.COM# program start 2078462SApril.Chin@Sun.COMbuiltin basename 2088462SApril.Chin@Sun.COMbuiltin cat 2098462SApril.Chin@Sun.COMbuiltin date 2108462SApril.Chin@Sun.COMbuiltin uname 2118462SApril.Chin@Sun.COM 2128462SApril.Chin@Sun.COMtypeset progname="${ basename "${0}" ; }" 2138462SApril.Chin@Sun.COM 2148462SApril.Chin@Sun.COM# HTTP protocol client identifer 215*12068SRoger.Faulkner@Oracle.COMtypeset -r http_user_agent="shtinyurl/ksh93 (2010-03-27; ${ uname -s -r -p ; })" 2168462SApril.Chin@Sun.COM 2178462SApril.Chin@Sun.COMtypeset -r shtinyurl_usage=$'+ 218*12068SRoger.Faulkner@Oracle.COM[-?\n@(#)\$Id: shtinyurl (Roland Mainz) 2010-03-27 \$\n] 2198462SApril.Chin@Sun.COM[-author?Roland Mainz <roland.mainz@nrubsig.org>] 22010898Sroland.mainz@nrubsig.org[+NAME?shtinyurl - create short alias URL from long URL] 2218462SApril.Chin@Sun.COM[+DESCRIPTION?\bshtinyurl\b is a small utility which passes a given URL 22210898Sroland.mainz@nrubsig.org to internet service which creates short aliases in the 22310898Sroland.mainz@nrubsig.org form of http://<servicename>/XXXXXXXX to redirect long URLs.] 2248462SApril.Chin@Sun.COM[+?The first arg \burl\b describes a long URL which is transformed into 2258462SApril.Chin@Sun.COM a tinyurl.com short alias.] 22610898Sroland.mainz@nrubsig.org[P:provider?Service provider (either \'tinyurl.com\' or \'tr.im\').]:[mode] 2278462SApril.Chin@Sun.COM 2288462SApril.Chin@Sun.COMurl 2298462SApril.Chin@Sun.COM 23010898Sroland.mainz@nrubsig.org[+SEE ALSO?\bksh93\b(1), \brssread\b(1), \bshtwitter\b(1), http://www.tinyurl.com, http://tr.im] 2318462SApril.Chin@Sun.COM' 2328462SApril.Chin@Sun.COM 23310898Sroland.mainz@nrubsig.orgtypeset service_provider="tr.im" 23410898Sroland.mainz@nrubsig.org 2358462SApril.Chin@Sun.COMwhile getopts -a "${progname}" "${shtinyurl_usage}" OPT ; do 2368462SApril.Chin@Sun.COM# printmsg "## OPT=|${OPT}|, OPTARG=|${OPTARG}|" 2378462SApril.Chin@Sun.COM case ${OPT} in 23810898Sroland.mainz@nrubsig.org P) service_provider="${OPTARG}" ;; 2398462SApril.Chin@Sun.COM *) usage ;; 2408462SApril.Chin@Sun.COM esac 2418462SApril.Chin@Sun.COMdone 2428462SApril.Chin@Sun.COMshift $((OPTIND-1)) 2438462SApril.Chin@Sun.COM 2448462SApril.Chin@Sun.COM# expecting at least one more argument 24510898Sroland.mainz@nrubsig.org(( $# >= 1 )) || usage 2468462SApril.Chin@Sun.COM 2478462SApril.Chin@Sun.COMtypeset url="$1" 2488462SApril.Chin@Sun.COMshift 2498462SApril.Chin@Sun.COM 25010898Sroland.mainz@nrubsig.orgcase "${service_provider}" in 25110898Sroland.mainz@nrubsig.org "tinyurl.com") 25210898Sroland.mainz@nrubsig.org request_tinyurl "${url}" 25310898Sroland.mainz@nrubsig.org exit $? 25410898Sroland.mainz@nrubsig.org ;; 25510898Sroland.mainz@nrubsig.org "tr.im") 25610898Sroland.mainz@nrubsig.org request_trimurl "${url}" 25710898Sroland.mainz@nrubsig.org exit $? 25810898Sroland.mainz@nrubsig.org ;; 25910898Sroland.mainz@nrubsig.org *) 26010898Sroland.mainz@nrubsig.org fatal_error "Unsupported service provider." 26110898Sroland.mainz@nrubsig.orgesac 26210898Sroland.mainz@nrubsig.org 26310898Sroland.mainz@nrubsig.org# not reached 26410898Sroland.mainz@nrubsig.org 2658462SApril.Chin@Sun.COM# EOF. 266