version 1.7.3.0
[socat.git] / proxyecho.sh
blob4644b893cae978d05f8cc629b67f04275e9319ee
1 #! /bin/bash
2 # source: proxyecho.sh
3 # Copyright Gerhard Rieger 2003-2009
4 # Published under the GNU General Public License V.2, see file COPYING
6 # perform primitive simulation of a proxy server with echo function via stdio.
7 # accepts and answers correct HTTP CONNECT requests, but then just echoes data.
8 # it is required for test.sh
9 # for TCP, use this script as:
10 # socat tcp-l:8080,reuseaddr,crlf system:"proxyecho.sh"
12 if type socat >/dev/null 2>&1; then
13 SOCAT=socat
14 else
15 SOCAT=./socat
18 case `uname` in
19 HP-UX|OSF1)
20 CAT="$SOCAT -u stdin stdout"
23 CAT=cat
25 esac
27 SPACES=" "
28 while [ -n "$1" ]; do
29 case "$1" in
30 -w) n="$2"; while [ "$n" -gt 0 ]; do SPACES="$SPACES "; n=$((n-1)); done
31 shift ;;
32 #-s) STAT="$2"; shift ;;
33 esac
34 shift
35 done
37 # read and parse HTTP request
38 read l
39 if echo "$l" |egrep '^CONNECT +[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+ +HTTP/1.[01]$' >/dev/null
40 then
41 : go on below
42 else
43 echo "HTTP/1.0${SPACES}500 Bad Request"
44 echo
45 exit
48 # read more headers until empty line
49 while [ -n "$l" ]; do
50 read l
51 done
53 # send status
54 echo "HTTP/1.0${SPACES}200 OK"
55 # send empty line
56 echo
58 # perform echo function
59 exec $CAT