summaryrefslogtreecommitdiff
path: root/httpshare.sh
blob: 474b6364e132dbb3a16fe80d0fbb59a25aa80ce7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161

#!/bin/sh -eu
#
#  httpshare -- A simple shell script that shares a file over HTTP
#
#  Copyright (c) 2019 Samuel Lidén Borell <samuel@kodafritt.se>
#
#  Permission is hereby granted, free of charge, to any person obtaining a copy
#  of this software and associated documentation files (the "Software"), to deal
#  in the Software without restriction, including without limitation the rights
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#  copies of the Software, and to permit persons to whom the Software is
#  furnished to do so, subject to the following conditions:
#
#  The above copyright notice and this permission notice shall be included in
#  all copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#  THE SOFTWARE.
#

export LC_ALL=C.UTF-8

log() {
    level="$1"
    message="$2"
    date="$(date --rfc-3339=seconds)"
    printf '%s\n' "$date [$level] $message" >&2
}

if [ $# = 0 ] || [ "$1" = --help ]; then
    cat <<EOF
usage: $0 FILE

Listens on port 1111 and sends the given file to each HTTP client
that connects. The HTTP client can be, for example, a browser, or
a download utility such as curl or wget.

Note: This script is intended for transferring files over a firewalled
network (or a secure point-to-point network link). It does not offer any
security at all, and it does not handle concurrent users.
EOF
    [ $# = 1 ]
    exit
fi

if [ "$1" != --internal-send ]; then
    if [ "$1" = -- ]; then
        shift
    fi
    port=1111
    log WARNING "This script does NOT offer any security at all!"
    # Determine IP address, so we can show a IP
    # FIXME this is IPv4 only for now, but this script is intended for LAN use only
    # example.com = 93.184.216.34
    ip=$(ip route get 93.184.216.34 | grep -oE 'src [^ \t]+' | cut -c '5-' || true)
    urltext="${ip:+, URL is http://$ip:$port/}"
    log INFO "Listening at port $port$urltext"
    log INFO "Sharing file \"$1\""
    while nc.traditional -c "'$0' --internal-send '$1'" -n -l -p "$port"; do
        true
    done
    exit
fi

log INFO "Client connected"
cr=$(printf '\r')

resp_header() {
    respcode="$1"
    statusline="$2"
    cat <<EOF
HTTP/1.0 $respcode $statusline$cr
Content-Type: text/html; charset=UTF-8$cr
X-Frame-Options: DENY$cr
X-Content-Type-Options: nosniff$cr
Content-Security-Policy: default-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'$cr
$cr
EOF
}

resp_message() {
    respcode="$1"
    statusline="$2"
    resp_header "$respcode" "$statusline"
    cat <<EOF
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type; charset=UTF-8">
<title>$statusline</title>
</head>
<body>
<h2>$statusline</h2>
</body>
</html>
EOF
}

skip_headers() {
    while read -r line; do
        line=${line%$cr}
        [ -n "$line" ] || break
    done
}

read -r reqline
without_get=${reqline#GET }
without_get=${without_get#get }
without_head=${reqline#HEAD }
without_head=${without_head#head }
if [ "${without_get}" != "$reqline" ]; then
    # GET request
    method=GET
    url_ver=$without_get
elif [ "${without_head}" != "$reqline" ]; then
    # HEAD request
    method=HEAD
    url_ver=$without_head
else
    skip_headers
    resp_message 405 'Method not allowed'
    exit
fi

if [ "${url_ver#/ }" = "$url_ver" ]; then
    skip_headers
    resp_message 404 'Incorrect URL'
    exit
fi

# TODO parse range requests?
skip_headers

filename=$2
filesize=$(stat -Lc %s -- "$filename")
basename=$(basename -- "$filename")

# FIXME long filenames are not sent in accordance to RFC-2183
# FIXME escape the filename
cat <<EOF
HTTP/1.0 200 Ok$cr
Content-Type: application/octet-stream; charset=UTF-8$cr
Content-Length: $filesize$cr
Content-Disposition: attachment; filename="$basename"$cr
X-Frame-Options: DENY$cr
X-Content-Type-Options: nosniff$cr
$cr
EOF

if [ "$method" = HEAD ]; then
    exit
fi

log INFO "Sending file \"$filename\""
cat -- "$filename"
log INFO "Successfully sent file."