-
Notifications
You must be signed in to change notification settings - Fork 3
/
map.go
135 lines (115 loc) · 2.96 KB
/
map.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
package main
import(
"fmt"
"strings"
"net"
"net/http"
"net/url"
"net/http/cgi"
"log"
"time"
)
var exceptions = []string{"blank", "image", "xml"}
type Map struct {
Name string
Projections []string
Aliases map[string][]string
Path string
}
func (m Map) Mapfile(proj string) string {
srs := strings.Split(proj, ":")
srid := srs[len(srs)-1:len(srs)][0]
for projection, aliases := range m.Aliases {
for _, alias := range aliases {
if alias == srid {
srid = projection
}
}
}
for _, p := range m.Projections {
if p == srid {
return fmt.Sprintf("%s_%s.map", m.Name, srid)
}
}
return fmt.Sprintf("%s.map", m.Name)
}
func (m Map) UrlPath() string {
p := m.Path
if p == "" {
p = m.Name
}
if !strings.HasPrefix(p, "/") {
p = "/" + p
}
if !strings.HasSuffix(p, "/") {
p = p + "/"
}
return p
}
func (m Map) serveMap(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
//TODO: Check that this is the correct status code to useg
log.Printf(buildCommonLogFormat(r, time.Now(), 404, 0))
return
}
normalizeKeys(r.Form, strings.ToUpper)
if r.Form.Get("REQUEST") == "" {
r.Form.Set("REQUEST", "GetCapabilities")
}
if r.Form.Get("SERVICE") == "" {
r.Form.Set("SERVICE", "WMS")
}
//Don't let the user set the mapfile.
r.Form.Del("MAP")
r.Form.Set("MAP", m.Mapfile(r.Form.Get("SRS")))
//ESRI software sends an invalid value?
//Force it to xml unless it's a valid value
if invalidException(r.Form.Get("EXCEPTIONS")) {
r.Form.Set("EXCEPTIONS", "xml")
}
queryString := "QUERY_STRING=" + r.Form.Encode()
// env := append(config.Environment, queryString)
handler := cgi.Handler{
Path: config.Mapserv,
Dir: config.Directory,
Env: []string{queryString},
}
handler.ServeHTTP(w, r)
//Should be able to say w.Header().Get("Status"), w.Header().Get("Length(?)")
log.Println(buildCommonLogFormat(r, time.Now(), 200, 0))
}
func buildCommonLogFormat(r *http.Request, ts time.Time, status, size int) string {
username := "-"
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
host = r.RemoteAddr
}
return fmt.Sprintf("%s - %s [%s] \"%s %s %s\" %d %d",
host,
username,
ts.Format("02/Jan/2006:15:04:05 -0700"),
r.Method,
r.URL.RequestURI(),
r.Proto,
status,
size,
)
}
func normalizeKeys(v url.Values, normalFunc func(string) string) {
for param, values := range v {
normalizedParam := normalFunc(param)
v.Del(param)
// Mapserv doesn't take multiple values per param
// Save a little time and only set the first one
v.Set(normalizedParam, values[0])
}
}
func invalidException(value string) bool {
for _, e := range exceptions {
if strings.ToLower(value) == e {
return false
}
}
return true
}