HTTP POST using sed/wc/nc

On limited systems where there is no memory for cURL or a full wget, like DD-WRT routers, you can easily do some HTTP-POSTs using the mostly available sed, wc and nc tools.

First you'll need this sed-file:

urlencode.sed
s/%/%25/g
s/ /%20/g
s/ /%09/g
s/!/%21/g
s/"/%22/g
s/#/%23/g
s/\$/%24/g
s/\&/%26/g
s/'\''/%27/g
s/(/%28/g
s/)/%29/g
s/\*/%2a/g
s/+/%2b/g
s/,/%2c/g
s/-/%2d/g
s/\./%2e/g
s/\//%2f/g
s/:/%3a/g
s/;/%3b/g
s//%3e/g
s/?/%3f/g
s/@/%40/g
s/\[/%5b/g
s/\\/%5c/g
s/\]/%5d/g
s/\^/%5e/g
s/_/%5f/g
s/`/%60/g
s/{/%7b/g
s/|/%7c/g
s/}/%7d/g
s/~/%7e/g
s/	/%09/g

Now use this script:

prowl.sh
#!/bin/sh
 
if [ -z $1 ] || [ -z "$2" ]; then
    echo "Use: $0 \"event\" prio \"message\""
    exit 1
fi
 
SEDSCRIPT=/tmp/root/urlencode.sed
 
EVENT=`echo $1 | sed -f $SEDSCRIPT`
MESSAGE=`echo $3 | sed -f $SEDSCRIPT`
APP=`echo "DD-WRT" | sed -f $SEDSCRIPT`
 
RECIPIENT=0123456789abcdef0123456789abcdef01234567
 
# Priority can be -2 .. 2 (-2 = lowest, 2 = highest)
PRIORITY=$2
 
HOST="my.server.com"
PORT="80"
URI="/path/to/prowlProxy.php?add"
 
POST_DATA="apikey=$RECIPIENT&priority=$PRIORITY&application=$APP&event=$EVENT&description=$MESSAGE"
 
POST_LEN=`echo "$POST_DATA" | wc -c`
 
HTTP_QUERY="POST $URI HTTP/1.1
Host: $HOST
Content-Length: $POST_LEN
Content-Type: application/x-www-form-urlencoded
 
$POST_DATA"
 
# echo "$HTTP_QUERY"
 
echo "$HTTP_QUERY" | nc -w 10 $HOST $PORT > /dev/null
 
if [ $? -eq "0" ]; then
    exit 0
else
    exit 2
fi

Prowl without HTTPS

If you don't have cURL/wget with HTTPS available on the box, you might need to use this little proxy script I wrote for sending Prowl messages. Put it on a HTTP server:

prowlProxy.php
<?php
 
$URL = 'https://prowl.weks.net/publicapi/';
 
$API = $_SERVER["QUERY_STRING"];
 
if ( empty($API) ) die('No command given.');
 
$URL .= $API;
 
$post_data = file_get_contents( 'php://input' );
 
#echo '>>>' . $post_data . '<<<'; die(); 

$context = stream_context_create( array(
    'http' => array(
        'method' => 'POST',
        'header' => "Content-Type: application/x-www-form-urlencoded\r\n" .
                    "Content-Length: " . strlen($post_data) . "\r\n",
        'content' => $post_data,
    ),
) );
 
$result = file_get_contents( $URL, false, $context );
if ( $result === false ) die( 'Proxying failed.' );
 
echo $result;
 
?>

Then change the HOST and URI to:

HOST="yourserver.com"
URI="/path/to/prowlProxy.php?add"

(The ?add is important as you set the Prowl function with it.)


 
software/sh/http-post.txt · Last modified: 2010-01-15 13:40.34 by mbirth
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki Contents powered by Club-Mate Contents powered by BassDrive.com Labelled with ICRA