-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserve
More file actions
40 lines (32 loc) · 811 Bytes
/
serve
File metadata and controls
40 lines (32 loc) · 811 Bytes
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
#!/bin/bash
#BASH script to serve files
#Usage: serve <filename>
#if root then port 80 is used, else port 660
FILE=$1;
#TODO: split FILENAME from FILE
PORT=6600;
#TODO: Add dynamic ports
[[ $UID = 0 ]] && PORT=80 #I know it's a bad idea
if [[ ! -e $FILE ]]; then
echo "File does not exist";
exit 1;
fi
date=$(date +"%a, %d %b %Y %H:%M:%S %Z")
size=`du -b $FILE | awk '{print $1}'`
echo $size
header="HTTP/1.1 200 OK\r\n\
Server: Netcat File Server\r\n\
Date: $date\r\n\
Content-type: application/octet-stream\r\n\
Content-Length: $size\r\n\
Connection: close\r\n\
Content-Disposition: attachment; filename=\"$FILE\"
Last Modified:$date\r\n\r\n";
shout(){
printf "%s$header";
cat $FILE
printf "%s\c"
}
#TODO: Include Error if port is in use
shout | nc -l -p $PORT
#TODO: Read about file descriptors