-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpserver.go
More file actions
249 lines (216 loc) · 5.46 KB
/
Copy pathhttpserver.go
File metadata and controls
249 lines (216 loc) · 5.46 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"sync"
"time"
)
var (
logger = log.Default()
uploadMutex sync.Map
)
const (
DataDir = "/tmp"
MaxWriteBufferSizeBytes int64 = 64 * 1024
MaxReadBufferSizeBytes int64 = 1024 * 1024
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
logger.Println(r.Method, r.URL.Path)
setCorsHeaders(w)
switch r.Method {
case http.MethodGet:
getHandler(w, r)
case http.MethodOptions:
optionsHandler(w, r)
case http.MethodDelete:
deleteHandler(w, r)
case http.MethodPost:
postHandler(w, r)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
})
http.HandleFunc("/time", func(w http.ResponseWriter, r *http.Request) {
setCorsHeaders(w)
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text/plain")
now := time.Now().Format(time.RFC3339Nano)
w.Write([]byte(now))
})
logger.Println("Server start")
logger.Fatal(http.ListenAndServe(":8001", nil))
}
func postHandler(w http.ResponseWriter, r *http.Request) {
uri := r.URL.Path
_, uploadingInProgress := uploadMutex.LoadOrStore(uri, true)
if uploadingInProgress {
w.WriteHeader(http.StatusConflict)
return
}
defer func() {
uploadMutex.Delete(uri)
}()
if err := processFileUpload(r); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
func processFileUpload(r *http.Request) error {
filePath := getFullFilePath(r.URL.Path)
file, openFileErr := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
if openFileErr != nil {
logger.Println("POST openfile err", filePath, openFileErr.Error())
return openFileErr
}
defer func(file *os.File) {
if err := file.Close(); err != nil {
logger.Println("POST closefile err", filePath, err.Error())
}
}(file)
defer func(Body io.ReadCloser) {
if err := Body.Close(); err != nil {
logger.Println("POST closebody err", filePath, err.Error())
}
}(r.Body)
var postBytesTotal int
for {
buffer := make([]byte, MaxWriteBufferSizeBytes)
postBytes, readErr := r.Body.Read(buffer)
if postBytes != 0 {
postBytesTotal += postBytes
_, writeErr := file.Write(buffer[:postBytes])
if writeErr != nil {
logger.Println("Write file error", filePath, writeErr)
return writeErr
}
}
switch readErr {
case nil:
continue
case io.EOF:
logger.Println("POST EOF", filePath, postBytesTotal)
return nil
default:
logger.Println("POST read body error", filePath, readErr.Error())
return readErr
}
}
}
func deleteHandler(w http.ResponseWriter, r *http.Request) {
filePath := getFullFilePath(r.URL.Path)
if !fileExists(filePath) {
w.WriteHeader(http.StatusNotFound)
return
}
err := os.Remove(filePath)
if err != nil {
logger.Println("DELETE error", filePath, err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
func optionsHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
}
func getHandler(w http.ResponseWriter, r *http.Request) {
uri := r.URL.Path
filePath := getFullFilePath(r.URL.Path)
if !fileExists(filePath) {
w.WriteHeader(http.StatusNotFound)
return
}
_, uploading := uploadMutex.Load(uri)
if uploading {
if err := serveUploadingFile(w, r); err != nil {
msg := fmt.Sprintf("Serve uploading file error %s:%s", uri, err.Error())
http.Error(w, msg, http.StatusInternalServerError)
return
}
return
}
http.ServeFile(w, r, filePath)
}
func serveUploadingFile(w http.ResponseWriter, r *http.Request) error {
uri := r.URL.Path
filePath := getFullFilePath(uri)
file, err := os.Open(filePath)
if err != nil {
return err
}
defer func(f *os.File) {
err := f.Close()
if err != nil {
logger.Println("Close file error", f.Name(), err.Error())
}
}(file)
flusher, ok := w.(http.Flusher)
if !ok {
return fmt.Errorf("expected http.ResponseWriter to be an http.Flusher")
}
connectionClosed := false
go func() {
<-r.Context().Done()
connectionClosed = true
}()
var readOffset int64
for {
if connectionClosed {
logger.Println(fmt.Sprintf("GET %s conection closed %d", filePath, readOffset))
return nil
}
fi, err := file.Stat()
if err != nil {
return err
}
readBufferSizeBytes := fi.Size() - readOffset + 1
if readBufferSizeBytes > MaxReadBufferSizeBytes {
readBufferSizeBytes = MaxReadBufferSizeBytes
}
buffer := make([]byte, readBufferSizeBytes)
readBytes, readErr := file.ReadAt(buffer, readOffset)
if readBytes != 0 {
readOffset += int64(readBytes)
if _, writeErr := w.Write(buffer[:readBytes]); writeErr != nil {
logger.Println("HTTP write error ", writeErr)
return writeErr
}
flusher.Flush()
}
switch readErr {
case nil:
continue
case io.EOF:
_, uploading := uploadMutex.Load(uri)
if uploading {
continue
}
logger.Println("GET EOF", filePath, readOffset)
return nil
default:
msg := fmt.Sprintf("file.ReadAt error:%s", readErr.Error())
logger.Println(msg)
return readErr
}
}
}
func setCorsHeaders(w http.ResponseWriter) {
w.Header().Set("Access-Control-Allow-Headers", "Origin,Range,Accept-Encoding,Referer")
w.Header().Set("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS")
w.Header().Set("Access-Control-Allow-Origin", "*")
}
func fileExists(filePath string) bool {
info, err := os.Stat(filePath)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func getFullFilePath(f string) string {
return DataDir + f
}