-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
205 lines (177 loc) · 3.86 KB
/
main.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"bufio"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"fmt"
"encoding/hex"
"io"
"net/http"
"os"
"strconv"
"strings"
)
type Message struct {
Status string
StatusCode int
Header Header
Exit int
}
type Header map[string][]string
func (h Header) Add(key, value string) {
h[key] = append(h[key], value)
}
func (h Header) Get(key string) string {
if value, ok := h[key]; ok {
if len(value) > 0 {
return value[0]
}
}
return ""
}
func (m *Message) String() string {
s := []string{m.Status}
for k, values := range m.Header {
for _, v := range values {
s = append(s, k+": "+v)
}
}
return strings.Join(s, "\n") + "\n\n"
}
func main() {
c := make(chan *Message)
go output(c)
sendCapabilities(c)
stdin := bufio.NewScanner(os.Stdin)
for stdin.Scan() {
line := stdin.Text()
if line == "" {
continue
}
s := strings.SplitN(line, " ", 2)
code, err := strconv.Atoi(s[0])
if err != nil {
fmt.Println("Malformed message!")
os.Exit(100)
}
request := &Message{
Status: line,
StatusCode: code,
Header: Header{},
}
for stdin.Scan() {
line := stdin.Text()
if line == "" {
process(c, request)
break
}
s := strings.SplitN(line, ": ", 2)
request.Header.Add(s[0], s[1])
}
}
}
func output(c <-chan *Message) {
for {
m := <-c
os.Stdout.Write([]byte(m.String()))
if m.Exit != 0 {
os.Exit(m.Exit)
}
}
}
func sendCapabilities(c chan<- *Message) {
caps := &Message{
Status: "100 Capabilities",
Header: Header{},
}
caps.Header.Add("Version", "1.2")
caps.Header.Add("Pipeline", "true")
caps.Header.Add("Send-Config", "true")
c <- caps
}
func process(c chan<- *Message, m *Message) {
switch m.StatusCode {
case 600:
go fetch(c, m)
case 601:
// TODO: parse config?
default:
fail := &Message{
Status: "401 General Failure",
Header: Header{},
Exit: 100,
}
fail.Header.Add("Message", "Status code not implemented")
c <- fail
}
}
func fetch(c chan<- *Message, m *Message) {
uri := m.Header.Get("URI")
filename := m.Header.Get("Filename")
// TODO: If-Modified-Since
file, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
c <- &Message{
Status: "400 URI Failure",
Header: Header{
"Message": []string{"Could not open file: " + err.Error()},
"URI": []string{uri},
},
}
return
}
defer file.Close()
// TODO: Fix bug with appending to existing files
// TODO: implement range requests if file already exists
realURI := strings.TrimPrefix(uri, "go-")
resp, err := http.Get(realURI)
if err != nil {
c <- &Message{
Status: "400 URI Failure",
Header: Header{
"Message": []string{"Could not fetch URI: " + err.Error()},
"URI": []string{uri},
},
}
return
}
defer resp.Body.Close()
started := &Message{
Status: "200 URI Start",
Header: Header{
"URI": []string{uri},
},
}
// TODO: add Last-Modified header
c <- started
md5Hash := md5.New()
sha1Hash := sha1.New()
sha256Hash := sha256.New()
sha512Hash := sha512.New()
if _, err = io.Copy(io.MultiWriter(file, md5Hash, sha1Hash, sha256Hash, sha512Hash), resp.Body); err != nil {
c <- &Message{
Status: "400 URI Failure",
Header: Header{
"Message": []string{"Could not write file: " + err.Error()},
"URI": []string{uri},
},
}
return
}
success := &Message{
Status: "201 URI Done",
Header: Header{},
}
success.Header.Add("URI", uri)
success.Header.Add("Filename", filename)
// TODO Size, Last-Modified
md5Hex := hex.EncodeToString(md5Hash.Sum(nil)[:])
success.Header.Add("MD5-Hash", md5Hex)
success.Header.Add("MD5Sum-Hash", md5Hex)
success.Header.Add("SHA1-Hash", hex.EncodeToString(sha1Hash.Sum(nil)[:]))
success.Header.Add("SHA256-Hash", hex.EncodeToString(sha256Hash.Sum(nil)[:]))
success.Header.Add("SHA512-Hash", hex.EncodeToString(sha512Hash.Sum(nil)[:]))
c <- success
}