-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathscanhandler.go
45 lines (36 loc) · 927 Bytes
/
scanhandler.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
package main
import (
// standard
"encoding/json"
"fmt"
"net/http"
// external
"github.com/gorilla/mux"
)
func ScanHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
filename := vars["filename"]
info.Printf("Scanning %s\n", filename)
// check that file exists with traversal safe function
fileexists, err := fileExists(filename)
if err != nil {
elog.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if fileexists != true {
http.Error(w, fmt.Sprintf("%s does not exist", filename), http.StatusInternalServerError)
return
}
// send request for scanning
newRequest := NewScanRequest(filename)
scanrequests <- newRequest
response := <-newRequest.ResponseChan
output, err := json.Marshal(response)
if err != nil {
elog.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintf(w, string(output))
}