forked from smacfarlane/mapwrap-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
155 lines (134 loc) · 3.63 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"fmt"
"io"
"log"
"net"
"net/http"
"net/http/cgi"
"net/url"
"strings"
"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) {
if r.Method == "OPTIONS" {
// Short-circuit options with the correct CORS headers
addCORSHeaders(w, r)
w.WriteHeader(204)
log.Println(buildCommonLogFormat(r, time.Now(), 204, 0))
return
}
// log.Printf("[DEBUG] Serving %s", r.URL)
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))
io.WriteString(w, "400")
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: "/usr/bin/mapserv",
Dir: config.Directory,
Env: []string{queryString},
}
addCORSHeaders(w, r)
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)
for _, value := range values {
v.Add(normalizedParam, value)
}
}
}
func invalidException(value string) bool {
for _, e := range exceptions {
if strings.ToLower(value) == e {
return false
}
}
return true
}
func addCORSHeaders(w http.ResponseWriter, r *http.Request) {
// Set response headers so we can use it CORS
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,Keep-Alive,X-Requested-With,If-Modified-Since")
// Tell client that this pre-flight info is valid for 20 days
w.Header().Set("Access-Control-Max-Age", "1728000")
}