forked from jpfielding/gorets
/
metadata.go
63 lines (58 loc) · 1.64 KB
/
metadata.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
package proxy
import (
"context"
"fmt"
"io"
"net/http"
"strings"
"github.com/jpfielding/gorets/rets"
)
// Metadata ...
func Metadata(ops map[string]string, srcs Sources) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
sub := strings.TrimPrefix(req.URL.Path, ops["GetMetadata"])
parts := strings.Split(sub, "/")
src := parts[0]
usr := parts[1]
if _, ok := srcs[src]; !ok {
res.WriteHeader(http.StatusNotFound)
fmt.Fprintf(res, "source %s not found", src)
return
}
if _, ok := srcs[src][usr]; !ok {
res.WriteHeader(http.StatusNotFound)
fmt.Fprintf(res, "user %s not found", usr)
return
}
session := srcs[src][usr]
r, urls, err := session.Get()
if err != nil {
res.WriteHeader(http.StatusServiceUnavailable)
fmt.Fprintf(res, "source %s, user %s login failed", src, usr)
return
}
// TOOD also check body in case of POST params in body
values := req.URL.Query()
ctx := context.Background()
response, err := rets.MetadataResponse(ctx, r, rets.MetadataRequest{
URL: urls.GetMetadata,
HTTPMethod: req.Method,
HTTPFormEncodedValues: (req.Method == "POST"),
MetadataParams: rets.MetadataParams{
Format: values.Get("Format"),
MType: values.Get("Type"),
ID: values.Get("ID"),
},
})
defer response.Body.Close()
if err != nil {
res.WriteHeader(http.StatusBadGateway)
fmt.Fprintf(res, "metadata err %s", err)
return
}
// success, send the urls (modified to point to this server)
res.Header()["Content-Type"] = response.Header["Content-Type"]
res.WriteHeader(http.StatusOK)
io.Copy(res, response.Body)
}
}