This repository has been archived by the owner on Jul 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpfs.go
138 lines (119 loc) · 3.03 KB
/
httpfs.go
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
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"os"
"strconv"
"strings"
)
var (
verbose = flag.Bool("v", false, "Prints debugging messages.")
port = flag.Int("p", 8080, "Specifies the port number that the server will listen and serve at.")
directory = flag.String("d", ".", "Specifies the directory that the server will use to read/write requested files.")
)
func main() {
flag.Parse()
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
if err != nil {
log.Fatalf("Error listening on port %d: %v", *port, err)
}
defer listener.Close()
if *verbose {
fmt.Println("httpfs is running in verbose mode")
}
// connection-loop:
// handle incomming requests
for {
conn, err := listener.Accept()
if err != nil {
fmt.Printf("Error accepting connection: %v", err)
continue
}
if *verbose {
fmt.Print("\n")
fmt.Println("Connected to ", conn.RemoteAddr())
fmt.Print("\n")
}
go handleConnection(conn)
}
}
func handleConnection(conn net.Conn) {
defer conn.Close()
req, err := NewRequest(conn)
if err != nil {
log.Printf("Error reading the request: %v", err)
return
}
res := NewResponse(conn)
filepath := *directory + req.Path
if req.Method == "GET" {
if req.Path == "/" {
files, err := readDirectory(*directory)
if err != nil {
log.Printf("Error could not read directory: %v", err)
return
}
if err = res.Send(200, strings.Join(files, "\r\n")+"\r\n", ""); err != nil {
log.Printf("Error could not send response: %v", err)
}
} else {
if err = res.Send(200, "", filepath); err != nil {
log.Printf("Error could not send response: %v", err)
}
}
} else if req.Method == "POST" {
if req.Path == "/" {
if err = res.Send(400, "BAD REQUEST: need to pick filename\r\n", ""); err != nil {
log.Printf("Error could not send response: %v", err)
}
return
}
if _, val := req.Headers["Content-Length"]; !val {
if err = res.Send(400, "Content-Length header is required", ""); err != nil {
log.Printf("Error could not send response: %v", err)
}
return
}
l, err := strconv.Atoi(req.Headers["Content-Length"])
if err != nil {
log.Printf("Error could not read content-length: %v. value: %v", err, req.Headers["Content-Length"])
return
}
f, err := os.Create(filepath)
if err != nil {
log.Printf("Error could not open file %s for writing: %v", req.Path[1:], err)
return
}
defer f.Close()
var r io.Reader
if *verbose {
r = io.TeeReader(req, os.Stdout)
} else {
r = req
}
if _, err = io.CopyN(f, r, int64(l)); err != nil {
log.Printf("Error writing to file: %v", err)
return
}
if err = res.SendStatus(200); err != nil {
log.Printf("could not send response: %v", err)
}
}
}
func readDirectory(d string) ([]string, error) {
files, err := ioutil.ReadDir(d)
if err != nil {
return nil, fmt.Errorf("Error reading the directory %v", err)
}
fileList := []string{}
for _, file := range files {
if !file.IsDir() {
fileList = append(fileList, file.Name())
}
}
return fileList, nil
}