#!/bin/sh # # gnu awk port of : # # * http://www.vidarholen.net/contents/blog/?p=17 # # "Just run wwwshare filename, and it'll print an URL and start a # wannabe http server on a random port (8000-9000) for a single # session. When the file is downloaded, it exits. No setup or cleanup # required." # # Author: # CVS: $Id: wwwshare,v 1.2 2010/02/04 08:07:21 pgas Exp $ # exec gawk -v exe="${0##*/}" ' function die (message) { print message > "/dev/stderr" exit 1 } BEGIN { # ip = "localhost" # set ip/host to bypass automatic detection # port = 8080 # set port to bypass random assignment if (ARGC != 2) die("Usage: " exe " filename") if ((getline < ARGV[1])<0) die("No such file: " ARGV[1]) FS=": |" if (!ip) { checkip = "/inet/tcp/0/checkip.dyndns.com/80" printf "GET /\r\n\r\n" |& checkip while ((checkip |& getline) > 0); ip = $2 close(checkip) } if (!port) { srand() port = int(8000 + rand()*1000) } ORS = "" filename = basename = ARGV[1] gsub(/\047/, "\047\\\047\047", filename) ( "wc -c < \047" filename "\047") | getline len printf "%d bytes to transfer\n", len sub(/.*\//,"",basename) printf "http://%s:%s/%s\n", ip, port, basename www = "/inet/tcp/" port "/0/0" www |& getline var printf "%s\r\n", "HTTP/1.0 200 Ok" |& www printf "%s\r\n", "Content-Type: application/octet-stream" |& www printf "Content-Length: %d\r\n\r\n", len |& www } { print $0 RT |& www }' ${1+"$1"} # Notes: # # I use a shell wrapper because gawk is not installed in the same dir # across my different systems # # The below trick allows to calculate the length of the file using # only gawk BUT it doesn't work with a multibyte locale. Changing this # would mean using a shell wrapper which would defeat the purpose of # a gawk only solution. # # BEGIN { # #causes the file to be parsed twice # filename = ARGV[1] # ARGV[ARGC++] = filename # } # NR==FNR { # # first pass # len+=length(RT)+length($0) # next # } # FNR==1 { # #beginning of the second pass # printf "%d bytes to transfer\n",len # } # { # print $0 RT |& www # } # emacs stuff overrides shell mode ### Local Variables: ### mode: awk ### End: