[lastbash-cvs] lastbash lastbash,1.71,1.72
Status: Beta
Brought to you by:
cstroie
|
From: Costin S. <cs...@us...> - 2006-12-04 16:28:46
|
Update of /cvsroot/lastbash/lastbash In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv8089 Modified Files: lastbash Log Message: Added support for external http clients (see the ubuntu bug). Index: lastbash =================================================================== RCS file: /cvsroot/lastbash/lastbash/lastbash,v retrieving revision 1.71 retrieving revision 1.72 diff -u -d -r1.71 -r1.72 --- lastbash 30 Nov 2006 21:49:05 -0000 1.71 +++ lastbash 4 Dec 2006 16:28:39 -0000 1.72 @@ -24,12 +24,13 @@ # User settings DEBUG="n" -USE_PLAYER="y" HISTORY_SCROLL_DOWN="y" HISTORY_FIELD_UNITS=(2 2 3) REFRESH_INTERVAL="30" PLAYER="auto" # $PLAYERS, auto +USE_PLAYER="y" AUTO_PLAY="y" +HTTP_CLIENT="auto" # $HTTP_CLIENTS, builtin, auto # Program identification PROG_NAME="lastbash" @@ -61,6 +62,7 @@ # Internal stuff PLAYERS="mplayer" +HTTP_CLIENTS="wget" REQ_PROGRAMS="rm date sleep tput md5sum dd chmod" declare -a HTTP_HEADERS HTTP_RESPONSE CR=$'\x0d' @@ -183,7 +185,7 @@ break fi done - # No player is not present + # No player is present [ ! "${PLAYER}" ] && debug "No supported player found." else if type -t "${PLAYER}" >/dev/null 2>&1 @@ -195,6 +197,42 @@ PLAYER="" fi fi + + # Check for http client + if [ "${HTTP_CLIENT}" == "builtin" ] + then + debug "Using the builtin HTTP client." + elif [ "${HTTP_CLIENT}" == "auto" ] + then + HTTP_CLIENT="" + debug "Searching for available and supported HTTP clients..." + # Search for the first supported http client + for P in ${HTTP_CLIENTS} + do + debug "Trying ${P}..." + if type -t "${P}" >/dev/null 2>&1 + then + debug "HTTP client found: ${P}" + HTTP_CLIENT="${P}" + break + fi + done + # No HTTP client is present + if [ ! "${HTTP_CLIENT}" ] + then + debug "No supported HTTP client found, using the builtin one." + HTTP_CLIENT="builtin" + fi + else + if type -t "${HTTP_CLIENT}" >/dev/null 2>&1 + then + debug "HTTP client found: ${HTTP_CLIENT}" + else + # The HTTP client is not present, switching to builtin + debug "HTTP client ${HTTP_CLIENT} not found." + HTTP_CLIENT="builtin" + fi + fi } # Function: Save the settings {{{1 @@ -233,7 +271,7 @@ done } -# Function: HTTP GET method implementation {{{1 +# Function: HTTP GET method {{{1 #----------------------------------------------------------------------------- function http_get() { @@ -241,58 +279,104 @@ # $2 - port # $3 - path - local REQUEST LINE RSP="n" N=0 RET + local HTTP_CLIENT_FUNCTION RET + local GET_PATH + local LINE RSP="n" N=0 local TMP_FILE="${HOME_DIR}/http.tmp" - REQUEST="GET $3 HTTP/1.0\r\nHost: $1\r\n\r\n" - # Map the FD no.5 to tcp connection - exec 5<>/dev/tcp/$1/$2 + # Select the HTTP client function + case "${HTTP_CLIENT}" in + "wget") HTTP_CLIENT_FUNCTION="http_get_wget" ;; + *) HTTP_CLIENT_FUNCTION="http_get_builtin" ;; + esac - # Send the request - debug "${FUNCNAME}: Q> ${REQUEST}" - # FIXME add error processing - echo -ne "${REQUEST}" >&5 2>/dev/null + # Make sure the GET path starts with "/" + GET_PATH="$3" + [ "${GET_PATH:0:1}" != "/" ] && GET_PATH="/${GET_PATH}" - # Save the response to a temporary file - dd <&5 >"${TMP_FILE}" 2>/dev/null - # Append one (missing) EOL - echo >> "${TMP_FILE}" + # Log the request + debug "${FUNCNAME}: Q> http://${1}:${2}${GET_PATH}" - # Close the connection - exec 5>&- + # Do the HTTP request + if "${HTTP_CLIENT_FUNCTION}" "${TMP_FILE}" "$1" "$2" "${GET_PATH}" + then + # Append one (missing) EOL (and make sure the file exits) + echo >> "${TMP_FILE}" - # Clear the HTTP_* arrays - HTTP_HEADERS=() - HTTP_RESPONSE=() + # Clear the HTTP_* arrays + HTTP_HEADERS=() + HTTP_RESPONSE=() - # Parse the result - while read LINE - do - LINE="${LINE//$CR/}" - if [ "${LINE}" == "" ] - then - N=0 - RSP="y" - else - if [ "${RSP}" == "n" ] + # Parse the result + while read LINE + do + LINE="${LINE//$CR/}" + if [ "${LINE}" == "" ] then - HTTP_HEADERS[$N]="${LINE}" - debug "${FUNCNAME}: H> ${LINE}" + N=0 + RSP="y" else - HTTP_RESPONSE[$N]="${LINE}" - debug "${FUNCNAME}: R> ${LINE}" + if [ "${RSP}" == "n" ] + then + HTTP_HEADERS[$N]="${LINE}" + debug "${FUNCNAME}: H> ${LINE}" + else + HTTP_RESPONSE[$N]="${LINE}" + debug "${FUNCNAME}: R> ${LINE}" + fi fi - fi - # Increment the index - (( N++ )) - done < "${TMP_FILE}" + # Increment the index + (( N++ )) + done < "${TMP_FILE}" + + # Check the status + if [[ "${HTTP_HEADERS[0]}" =~ "200" ]] + then + RET="0" + else + debug "${FUNCNAME}: HTTP GET status is not OK" + RET="1" + fi + else + debug "${FUNCNAME}: HTTP GET error" + RET="2" + fi # Remove the temporary file rm -f "${TMP_FILE}" - # Check the status - if [[ "${HTTP_HEADERS[0]}" =~ "200" ]] + # Return the status code + return "${RET}" +} + +# Function: HTTP GET method: wget implementation {{{1 +#----------------------------------------------------------------------------- +function http_get_wget() +{ + # $1 - output file + # $2 - host + # $3 - port + # $4 - path + + local RET ERRCODE + + # Send the request + wget --quiet \ + --tries="3" \ + --timeout="5" \ + --save-headers \ + --header="Connection: Close" \ + --user-agent="${PROG_TITLE}/${PROG_VER}" \ + --output-file="/dev/null" \ + --output-document="${1}" \ + "http://${2}:${3}${4}" + + # Keep the error code + ERRCODE="$?" + + # Check error code + if [ "${ERRCODE}" == "0" ] then RET="0" else @@ -303,6 +387,40 @@ return "${RET}" } +# Function: HTTP GET method: builtin implementation {{{1 +#----------------------------------------------------------------------------- +function http_get_builtin() +{ + # $1 - output file + # $2 - host + # $3 - port + # $4 - path + + local REQUEST RET="0" + + # Construct the request + REQUEST="GET $4 HTTP/1.0\r\nHost: $2:$3\r\nUser-Agent: ${PROG_TITLE}/${PROG_VER}\r\nConnection: Close\r\n\r\n" + + # Map the FD no.5 to tcp connection + exec 5<>/dev/tcp/$2/$3 + [ "$?" != "0" ] && RET="1" + + # Send the request + echo -ne "${REQUEST}" >&5 2>/dev/null + [ "$?" != "0" ] && RET="2" + + # Save the response to the temporary file + dd <&5 >"${1}" 2>/dev/null + [ "$?" != "0" ] && RET="3" + + # Close the connection + exec 5>&- + [ "$?" != "0" ] && RET="4" + + # Return the status code + return "${RET}" +} + # Function: Connect to Last.fm and save a playlist {{{1 #----------------------------------------------------------------------------- function lastfm_connect() |