annotate pkg/controllers/proxy.go @ 502:af1a198391f3 metamorph-for-all

Merged default into metamorph-for-all branch.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 24 Aug 2018 15:33:33 +0200
parents 76a76691a298
children 9ccf2c6b7275
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
335
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 package controllers
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 import (
352
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
4 "compress/flate"
344
e98033e3683a Be more precise with HTTP headers in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 335
diff changeset
5 "compress/gzip"
408
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
6 "crypto/hmac"
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
7 "crypto/sha256"
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
8 "encoding/base64"
335
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 "encoding/xml"
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 "io"
352
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
11 "io/ioutil"
335
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 "log"
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13 "net/http"
346
ad0e47c1fedf Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 344
diff changeset
14 "net/url"
408
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
15 "regexp"
335
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 "strings"
369
a43495c91856 Show timing of writing in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 365
diff changeset
17 "time"
335
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 "github.com/gorilla/mux"
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 "golang.org/x/net/html/charset"
414
c1047fd04a3a Moved project specific Go packages to new pkg folder.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 409
diff changeset
21
c1047fd04a3a Moved project specific Go packages to new pkg folder.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 409
diff changeset
22 "gemma.intevation.de/gemma/pkg/config"
335
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 )
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24
408
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
25 // proxyBlackList is a set of URLs that should not be rewritten by the proxy.
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
26 var proxyBlackList = map[string]struct{}{
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
27 "http://www.w3.org/2001/XMLSchema-instance": struct{}{},
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
28 "http://www.w3.org/1999/xlink": struct{}{},
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
29 "http://www.w3.org/2001/XMLSchema": struct{}{},
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
30 "http://www.w3.org/XML/1998/namespace": struct{}{},
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
31 "http://www.opengis.net/wfs/2.0": struct{}{},
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
32 "http://www.opengis.net/ows/1.1": struct{}{},
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
33 "http://www.opengis.net/gml/3.2": struct{}{},
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
34 "http://www.opengis.net/fes/2.0": struct{}{},
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
35 "http://schemas.opengis.net/gml": struct{}{},
344
e98033e3683a Be more precise with HTTP headers in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 335
diff changeset
36 }
e98033e3683a Be more precise with HTTP headers in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 335
diff changeset
37
421
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
38 func proxyDirector(lookup func(string) (string, bool)) func(*http.Request) {
346
ad0e47c1fedf Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 344
diff changeset
39
421
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
40 return func(req *http.Request) {
350
1ea90a22bd15 Better replacements for WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 349
diff changeset
41
421
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
42 log.Printf("proxyDirector: %s\n", req.RequestURI)
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
43
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
44 abort := func(format string, args ...interface{}) {
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
45 log.Printf(format, args...)
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
46 panic(http.ErrAbortHandler)
408
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
47 }
346
ad0e47c1fedf Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 344
diff changeset
48
421
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
49 vars := mux.Vars(req)
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
50
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
51 var s string
408
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
52
421
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
53 if entry, found := vars["entry"]; found {
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
54 if s, found = lookup(entry); !found {
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
55 abort("Cannot find entry '%s'\n", entry)
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
56 }
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
57 } else {
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
58 expectedMAC, err := base64.URLEncoding.DecodeString(vars["hash"])
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
59 if err != nil {
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
60 abort("Cannot base64 decode hash: %v\n", err)
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
61 }
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
62 url, err := base64.URLEncoding.DecodeString(vars["url"])
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
63 if err != nil {
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
64 abort("Cannot base64 decode url: %v\n", err)
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
65 }
346
ad0e47c1fedf Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 344
diff changeset
66
421
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
67 mac := hmac.New(sha256.New, config.ProxyKey())
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
68 mac.Write(url)
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
69 messageMAC := mac.Sum(nil)
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
70
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
71 s = string(url)
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
72
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
73 if !hmac.Equal(messageMAC, expectedMAC) {
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
74 abort("HMAC of URL %s failed.\n", s)
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
75 }
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
76 }
346
ad0e47c1fedf Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 344
diff changeset
77
421
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
78 nURL := s + "?" + req.URL.RawQuery
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
79 //log.Printf("%v\n", nURL)
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
80
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
81 u, err := url.Parse(nURL)
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
82 if err != nil {
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
83 abort("Invalid url: %v\n", err)
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
84 }
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
85 req.URL = u
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
86
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
87 req.Host = u.Host
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
88 //req.Header.Del("If-None-Match")
c37457f12b8e Differ between internal and external proxies.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 419
diff changeset
89 //log.Printf("headers: %v\n", req.Header)
346
ad0e47c1fedf Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 344
diff changeset
90 }
344
e98033e3683a Be more precise with HTTP headers in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 335
diff changeset
91 }
e98033e3683a Be more precise with HTTP headers in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 335
diff changeset
92
352
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
93 type nopCloser struct {
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
94 io.Writer
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
95 }
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
96
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
97 func (nopCloser) Close() error { return nil }
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
98
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
99 func encoding(h http.Header) (
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
100 func(io.Reader) (io.ReadCloser, error),
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
101 func(io.Writer) (io.WriteCloser, error),
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
102 ) {
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
103 switch enc := h.Get("Content-Encoding"); {
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
104 case strings.Contains(enc, "gzip"):
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
105 log.Println("gzip compression")
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
106 return func(r io.Reader) (io.ReadCloser, error) {
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
107 return gzip.NewReader(r)
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
108 },
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
109 func(w io.Writer) (io.WriteCloser, error) {
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
110 return gzip.NewWriter(w), nil
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
111 }
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
112 case strings.Contains(enc, "deflate"):
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
113 log.Println("Deflate compression")
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
114 return func(r io.Reader) (io.ReadCloser, error) {
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
115 return flate.NewReader(r), nil
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
116 },
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
117 func(w io.Writer) (io.WriteCloser, error) {
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
118 return flate.NewWriter(w, flate.DefaultCompression)
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
119 }
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
120 default:
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
121 log.Println("No content compression")
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
122 return func(r io.Reader) (io.ReadCloser, error) {
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
123 if r2, ok := r.(io.ReadCloser); ok {
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
124 return r2, nil
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
125 }
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
126 return ioutil.NopCloser(r), nil
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
127 },
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
128 func(w io.Writer) (io.WriteCloser, error) {
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
129 if w2, ok := w.(io.WriteCloser); ok {
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
130 return w2, nil
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
131 }
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
132 return nopCloser{w}, nil
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
133 }
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
134 }
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
135 }
23d4a9104b0c Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 351
diff changeset
136
419
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
137 func proxyModifyResponse(suffix string) func(*http.Response) error {
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
138
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
139 return func(resp *http.Response) error {
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
140
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
141 if !isXML(resp.Header) {
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
142 return nil
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
143 }
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
144
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
145 pr, pw := io.Pipe()
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
146
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
147 var (
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
148 r io.ReadCloser
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
149 w io.WriteCloser
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
150 err error
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
151 )
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
152
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
153 reader, writer := encoding(resp.Header)
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
154
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
155 if r, err = reader(resp.Body); err != nil {
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
156 return err
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
157 }
346
ad0e47c1fedf Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 344
diff changeset
158
419
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
159 if w, err = writer(pw); err != nil {
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
160 return err
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
161 }
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
162
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
163 go func(force io.ReadCloser) {
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
164 start := time.Now()
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
165 defer func() {
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
166 //r.Close()
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
167 w.Close()
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
168 pw.Close()
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
169 force.Close()
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
170 log.Printf("rewrite took %s\n", time.Since(start))
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
171 }()
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
172 if err := rewrite(suffix, w, r); err != nil {
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
173 log.Printf("rewrite failed: %v\n", err)
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
174 return
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
175 }
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
176 log.Println("rewrite successful")
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
177 }(resp.Body)
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
178
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
179 resp.Body = pr
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
180
355
e170075c22ac Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 354
diff changeset
181 return nil
e170075c22ac Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 354
diff changeset
182 }
344
e98033e3683a Be more precise with HTTP headers in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 335
diff changeset
183 }
e98033e3683a Be more precise with HTTP headers in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 335
diff changeset
184
365
765e056ab4e8 Recognize more XML content types in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 359
diff changeset
185 var xmlContentTypes = []string{
765e056ab4e8 Recognize more XML content types in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 359
diff changeset
186 "application/xml",
765e056ab4e8 Recognize more XML content types in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 359
diff changeset
187 "text/xml",
765e056ab4e8 Recognize more XML content types in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 359
diff changeset
188 "application/gml+xml",
765e056ab4e8 Recognize more XML content types in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 359
diff changeset
189 }
765e056ab4e8 Recognize more XML content types in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 359
diff changeset
190
344
e98033e3683a Be more precise with HTTP headers in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 335
diff changeset
191 func isXML(h http.Header) bool {
e98033e3683a Be more precise with HTTP headers in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 335
diff changeset
192 for _, t := range h["Content-Type"] {
e98033e3683a Be more precise with HTTP headers in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 335
diff changeset
193 t = strings.ToLower(t)
365
765e056ab4e8 Recognize more XML content types in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 359
diff changeset
194 for _, ct := range xmlContentTypes {
765e056ab4e8 Recognize more XML content types in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 359
diff changeset
195 if strings.Contains(t, ct) {
765e056ab4e8 Recognize more XML content types in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 359
diff changeset
196 return true
765e056ab4e8 Recognize more XML content types in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 359
diff changeset
197 }
344
e98033e3683a Be more precise with HTTP headers in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 335
diff changeset
198 }
e98033e3683a Be more precise with HTTP headers in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 335
diff changeset
199 }
e98033e3683a Be more precise with HTTP headers in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 335
diff changeset
200 return false
e98033e3683a Be more precise with HTTP headers in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 335
diff changeset
201 }
e98033e3683a Be more precise with HTTP headers in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 335
diff changeset
202
408
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
203 var replaceRe = regexp.MustCompile(`\b(https?://[^\s\?]*)`)
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
204
419
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
205 func replace(suffix, s string) string {
408
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
206
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
207 proxyKey := config.ProxyKey()
419
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
208 proxyPrefix := config.ProxyPrefix() + suffix
408
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
209
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
210 return replaceRe.ReplaceAllStringFunc(s, func(s string) string {
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
211 if _, found := proxyBlackList[s]; found {
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
212 return s
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
213 }
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
214 mac := hmac.New(sha256.New, proxyKey)
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
215 b := []byte(s)
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
216 mac.Write(b)
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
217 expectedMAC := mac.Sum(nil)
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
218
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
219 hash := base64.URLEncoding.EncodeToString(expectedMAC)
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
220 enc := base64.URLEncoding.EncodeToString(b)
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
221 return proxyPrefix + hash + "/" + enc
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
222 })
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
223 }
ac23905e64b1 Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 372
diff changeset
224
419
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
225 func rewrite(suffix string, w io.Writer, r io.Reader) error {
335
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
226
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
227 decoder := xml.NewDecoder(r)
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
228 decoder.CharsetReader = charset.NewReaderLabel
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
229
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
230 encoder := xml.NewEncoder(w)
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
231
372
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
232 var n nsdef
335
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
233
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
234 tokens:
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
235 for {
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
236 tok, err := decoder.Token()
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
237 switch {
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
238 case tok == nil && err == io.EOF:
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
239 break tokens
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
240 case err != nil:
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
241 return err
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
242 }
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
243
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
244 switch t := tok.(type) {
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
245 case xml.StartElement:
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
246 t = t.Copy()
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
247
372
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
248 isDef := n.isDef(t.Name.Space)
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
249 n = n.push()
335
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
250
372
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
251 for i := range t.Attr {
419
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
252 t.Attr[i].Value = replace(suffix, t.Attr[i].Value)
372
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
253 n.checkDef(&t.Attr[i])
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
254 }
351
b89138a25f9e Fixed namespace compression in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 350
diff changeset
255
372
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
256 for i := range t.Attr {
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
257 n.adjust(&t.Attr[i])
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
258 }
351
b89138a25f9e Fixed namespace compression in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 350
diff changeset
259
372
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
260 switch {
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
261 case isDef:
351
b89138a25f9e Fixed namespace compression in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 350
diff changeset
262 t.Name.Space = ""
372
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
263 default:
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
264 if s := n.lookup(t.Name.Space); s != "" {
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
265 t.Name.Space = ""
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
266 t.Name.Local = s + ":" + t.Name.Local
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
267 }
351
b89138a25f9e Fixed namespace compression in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 350
diff changeset
268 }
335
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
269 tok = t
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
270
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
271 case xml.CharData:
419
6627c48363a0 First attempt for user injection of proxy for using GeoServer with role based security.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
272 tok = xml.CharData(replace(suffix, string(t)))
335
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
273
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
274 case xml.EndElement:
372
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
275 s := n.lookup(t.Name.Space)
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
276
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
277 n = n.pop()
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
278
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
279 if n.isDef(t.Name.Space) {
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
280 t.Name.Space = ""
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
281 } else if s != "" {
351
b89138a25f9e Fixed namespace compression in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 350
diff changeset
282 t.Name.Space = ""
b89138a25f9e Fixed namespace compression in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 350
diff changeset
283 t.Name.Local = s + ":" + t.Name.Local
b89138a25f9e Fixed namespace compression in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 350
diff changeset
284 }
372
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
285 tok = t
335
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
286 }
372
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
287
335
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
288 if err := encoder.EncodeToken(tok); err != nil {
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
289 return err
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
290 }
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
291 }
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
292
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
293 return encoder.Flush()
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
294 }
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
295
372
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
296 type nsframe struct {
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
297 def string
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
298 ns map[string]string
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
299 }
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
300
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
301 type nsdef []nsframe
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
302
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
303 func (n nsdef) setDef(def string) {
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
304 if l := len(n); l > 0 {
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
305 n[l-1].def = def
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
306 }
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
307 }
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
308
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
309 func (n nsdef) isDef(s string) bool {
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
310 for i := len(n) - 1; i >= 0; i-- {
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
311 if x := n[i].def; x != "" {
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
312 return s == x
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
313 }
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
314 }
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
315 return false
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
316 }
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
317
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
318 func (n nsdef) define(ns, s string) {
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
319 if l := len(n); l > 0 {
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
320 n[l-1].ns[ns] = s
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
321 }
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
322 }
335
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
323
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
324 func (n nsdef) lookup(ns string) string {
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
325 for i := len(n) - 1; i >= 0; i-- {
372
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
326 if s := n[i].ns[ns]; s != "" {
335
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
327 return s
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
328 }
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
329 }
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
330 return ""
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
331 }
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
332
372
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
333 func (n nsdef) checkDef(at *xml.Attr) {
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
334 if at.Name.Space == "" && at.Name.Local == "xmlns" {
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
335 n.setDef(at.Value)
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
336 }
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
337 }
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
338
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
339 func (n nsdef) adjust(at *xml.Attr) {
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
340 switch {
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
341 case at.Name.Space == "xmlns":
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
342 n.define(at.Value, at.Name.Local)
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
343 at.Name.Local = "xmlns:" + at.Name.Local
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
344 at.Name.Space = ""
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
345
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
346 case at.Name.Space != "":
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
347 if n.isDef(at.Name.Space) {
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
348 at.Name.Space = ""
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
349 } else if s := n.lookup(at.Name.Space); s != "" {
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
350 at.Name.Local = s + ":" + at.Name.Local
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
351 at.Name.Space = ""
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
352 }
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
353 }
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
354 }
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
355
335
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
356 func (n nsdef) push() nsdef {
372
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
357 return append(n, nsframe{ns: make(map[string]string)})
335
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
358 }
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
359
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
360 func (n nsdef) pop() nsdef {
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
361 if l := len(n); l > 0 {
372
15369b41be74 Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 369
diff changeset
362 n[l-1] = nsframe{}
335
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
363 n = n[:l-1]
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
364 }
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
365 return n
bd292a554b6e Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
366 }