Mercurial > gemma
annotate controllers/externalwfs.go @ 402:20d2edbc863d
Add install target for backend and use in Dockerfile
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Tue, 14 Aug 2018 19:37:36 +0200 |
parents | 15369b41be74 |
children | ac23905e64b1 |
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" |
335
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 "encoding/xml" |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 "io" |
352
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
8 "io/ioutil" |
335
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 "log" |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 "net/http" |
346
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
11 "net/url" |
335
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 "strings" |
369
a43495c91856
Show timing of writing in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
365
diff
changeset
|
13 "time" |
335
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 "github.com/gorilla/mux" |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 "golang.org/x/net/html/charset" |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
17 |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 "gemma.intevation.de/gemma/config" |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 ) |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 |
359
ac7e0f980456
Unexport [Rr]oundTripFunc type.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
358
diff
changeset
|
21 // roundTripFunc is a helper type to make externalWFSDirector a http.RoundTripper. |
ac7e0f980456
Unexport [Rr]oundTripFunc type.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
358
diff
changeset
|
22 type roundTripFunc func(*http.Request) (*http.Response, error) |
346
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
23 |
359
ac7e0f980456
Unexport [Rr]oundTripFunc type.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
358
diff
changeset
|
24 func (rtf roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { |
346
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
25 return rtf(req) |
344
e98033e3683a
Be more precise with HTTP headers in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
335
diff
changeset
|
26 } |
e98033e3683a
Be more precise with HTTP headers in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
335
diff
changeset
|
27 |
346
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
28 func externalWFSDirector(req *http.Request) { |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
29 |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
30 abort := func(format string, args ...interface{}) { |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
31 log.Printf(format, args...) |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
32 panic(http.ErrAbortHandler) |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
33 } |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
34 |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
35 external := config.ExternalWFSs() |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
36 if external == nil || len(external) == 0 { |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
37 abort("No external WFS proxy config found\n") |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
38 } |
350
1ea90a22bd15
Better replacements for WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
349
diff
changeset
|
39 vars := mux.Vars(req) |
1ea90a22bd15
Better replacements for WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
349
diff
changeset
|
40 wfs := vars["wfs"] |
1ea90a22bd15
Better replacements for WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
349
diff
changeset
|
41 rest := vars["rest"] |
1ea90a22bd15
Better replacements for WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
349
diff
changeset
|
42 |
1ea90a22bd15
Better replacements for WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
349
diff
changeset
|
43 log.Printf("rest: %s\n", rest) |
346
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
44 |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
45 alias, found := external[wfs] |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
46 if !found { |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
47 abort("No config found for %s\n", wfs) |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
48 } |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
49 data, ok := alias.(map[string]interface{}) |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
50 if !ok { |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
51 abort("error: badly configured external WFS %s\n", wfs) |
344
e98033e3683a
Be more precise with HTTP headers in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
335
diff
changeset
|
52 } |
346
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
53 |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
54 urlS, found := data["url"] |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
55 if !found { |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
56 abort("error: missing url for external WFS %s\n", wfs) |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
57 } |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
58 |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
59 prefix, ok := urlS.(string) |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
60 if !ok { |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
61 abort("error: badly configured url for external WFS %s\n", wfs) |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
62 } |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
63 |
358
55388227e7da
Rewrite XML attributes in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
355
diff
changeset
|
64 https := useHTTPS(req) |
55388227e7da
Rewrite XML attributes in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
355
diff
changeset
|
65 |
346
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
66 log.Printf("%v\n", prefix) |
350
1ea90a22bd15
Better replacements for WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
349
diff
changeset
|
67 nURL := prefix + "/" + rest + "?" + req.URL.RawQuery |
346
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
68 log.Printf("%v\n", nURL) |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
69 |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
70 u, err := url.Parse(nURL) |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
71 if err != nil { |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
72 abort("Invalid url: %v\n", err) |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
73 } |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
74 req.URL = u |
349
56f6c5ab0f3d
Made WFS proxy rewriting work (to some degrees).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
348
diff
changeset
|
75 req.Header.Set("X-Gemma-From", prefix) |
358
55388227e7da
Rewrite XML attributes in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
355
diff
changeset
|
76 to := https + "://" + req.Host + "/api/externalwfs/" + wfs |
349
56f6c5ab0f3d
Made WFS proxy rewriting work (to some degrees).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
348
diff
changeset
|
77 req.Header.Set("X-Gemma-To", to) |
56f6c5ab0f3d
Made WFS proxy rewriting work (to some degrees).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
348
diff
changeset
|
78 |
346
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
79 req.Host = u.Host |
349
56f6c5ab0f3d
Made WFS proxy rewriting work (to some degrees).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
348
diff
changeset
|
80 |
56f6c5ab0f3d
Made WFS proxy rewriting work (to some degrees).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
348
diff
changeset
|
81 //log.Printf("headers: %v\n", req.Header) |
344
e98033e3683a
Be more precise with HTTP headers in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
335
diff
changeset
|
82 } |
e98033e3683a
Be more precise with HTTP headers in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
335
diff
changeset
|
83 |
346
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
84 func externalWFSTransport(req *http.Request) (*http.Response, error) { |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
85 |
349
56f6c5ab0f3d
Made WFS proxy rewriting work (to some degrees).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
348
diff
changeset
|
86 from := req.Header.Get("X-Gemma-From") |
56f6c5ab0f3d
Made WFS proxy rewriting work (to some degrees).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
348
diff
changeset
|
87 to := req.Header.Get("X-Gemma-To") |
56f6c5ab0f3d
Made WFS proxy rewriting work (to some degrees).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
348
diff
changeset
|
88 req.Header.Del("X-Gemma-From") |
56f6c5ab0f3d
Made WFS proxy rewriting work (to some degrees).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
348
diff
changeset
|
89 req.Header.Del("X-Gemma-To") |
56f6c5ab0f3d
Made WFS proxy rewriting work (to some degrees).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
348
diff
changeset
|
90 |
56f6c5ab0f3d
Made WFS proxy rewriting work (to some degrees).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
348
diff
changeset
|
91 // To prevent some caching effects. |
56f6c5ab0f3d
Made WFS proxy rewriting work (to some degrees).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
348
diff
changeset
|
92 req.Header.Del("If-None-Match") |
346
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
93 |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
94 resp, err := http.DefaultTransport.RoundTrip(req) |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
95 if err != nil { |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
96 return nil, err |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
97 } |
349
56f6c5ab0f3d
Made WFS proxy rewriting work (to some degrees).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
348
diff
changeset
|
98 resp.Header.Set("X-Gemma-From", from) |
346
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
99 resp.Header.Set("X-Gemma-To", to) |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
100 |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
101 return resp, err |
344
e98033e3683a
Be more precise with HTTP headers in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
335
diff
changeset
|
102 } |
e98033e3683a
Be more precise with HTTP headers in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
335
diff
changeset
|
103 |
352
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
104 type nopCloser struct { |
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
105 io.Writer |
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
106 } |
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
107 |
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
108 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
|
109 |
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
110 func encoding(h http.Header) ( |
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
111 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
|
112 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
|
113 ) { |
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
114 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
|
115 case strings.Contains(enc, "gzip"): |
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
116 log.Println("gzip compression") |
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
117 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
|
118 return gzip.NewReader(r) |
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 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
|
121 return gzip.NewWriter(w), nil |
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
122 } |
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
123 case strings.Contains(enc, "deflate"): |
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
124 log.Println("Deflate compression") |
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
125 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
|
126 return flate.NewReader(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 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
|
130 } |
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
131 default: |
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
132 log.Println("No content compression") |
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
133 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
|
134 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
|
135 return r2, nil |
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
136 } |
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
137 return ioutil.NopCloser(r), nil |
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
138 }, |
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
139 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
|
140 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
|
141 return w2, nil |
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
142 } |
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
143 return nopCloser{w}, nil |
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
144 } |
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
145 } |
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
146 } |
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
147 |
346
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
148 func externalWFSModifyResponse(resp *http.Response) error { |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
149 |
349
56f6c5ab0f3d
Made WFS proxy rewriting work (to some degrees).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
348
diff
changeset
|
150 from := resp.Header.Get("X-Gemma-From") |
346
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
151 to := resp.Header.Get("X-Gemma-To") |
349
56f6c5ab0f3d
Made WFS proxy rewriting work (to some degrees).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
348
diff
changeset
|
152 resp.Header.Del("X-Gemma-From") |
346
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
153 resp.Header.Del("X-Gemma-To") |
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
154 |
355
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
155 if !isXML(resp.Header) { |
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
156 return nil |
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
157 } |
348
9543ca97aa70
Re-enable content gzip encoding in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
347
diff
changeset
|
158 |
355
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
159 log.Printf("rewrite from %s to %s\n", from, to) |
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
160 |
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
161 pr, pw := io.Pipe() |
348
9543ca97aa70
Re-enable content gzip encoding in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
347
diff
changeset
|
162 |
355
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
163 var ( |
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
164 r io.ReadCloser |
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
165 w io.WriteCloser |
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
166 err error |
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
167 ) |
352
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
168 |
355
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
169 reader, writer := encoding(resp.Header) |
354
aa24b5691838
Simplified WFS proxy code a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
352
diff
changeset
|
170 |
355
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
171 if r, err = reader(resp.Body); err != nil { |
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
172 return err |
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
173 } |
352
23d4a9104b0c
Support deflate compression in WFS proxy, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
351
diff
changeset
|
174 |
355
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
175 if w, err = writer(pw); err != nil { |
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
176 return err |
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
177 } |
346
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
178 |
355
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
179 go func(force io.ReadCloser) { |
369
a43495c91856
Show timing of writing in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
365
diff
changeset
|
180 start := time.Now() |
355
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
181 defer func() { |
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
182 //r.Close() |
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
183 w.Close() |
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
184 pw.Close() |
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
185 force.Close() |
369
a43495c91856
Show timing of writing in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
365
diff
changeset
|
186 log.Printf("rewrite took %s\n", time.Since(start)) |
355
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
187 }() |
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
188 if err := rewrite(w, r, from, to); err != nil { |
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
189 log.Printf("rewrite failed: %v\n", err) |
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
190 return |
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
191 } |
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
192 log.Println("rewrite successful") |
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
193 }(resp.Body) |
346
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
194 |
355
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
195 resp.Body = pr |
e170075c22ac
Cosmetics: Unindent some code in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
354
diff
changeset
|
196 |
346
ad0e47c1fedf
Use httputil.ReverseProxy for WFS proxying.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
344
diff
changeset
|
197 return nil |
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 |
365
765e056ab4e8
Recognize more XML content types in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
359
diff
changeset
|
200 var xmlContentTypes = []string{ |
765e056ab4e8
Recognize more XML content types in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
359
diff
changeset
|
201 "application/xml", |
765e056ab4e8
Recognize more XML content types in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
359
diff
changeset
|
202 "text/xml", |
765e056ab4e8
Recognize more XML content types in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
359
diff
changeset
|
203 "application/gml+xml", |
765e056ab4e8
Recognize more XML content types in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
359
diff
changeset
|
204 } |
765e056ab4e8
Recognize more XML content types in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
359
diff
changeset
|
205 |
344
e98033e3683a
Be more precise with HTTP headers in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
335
diff
changeset
|
206 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
|
207 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
|
208 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
|
209 for _, ct := range xmlContentTypes { |
765e056ab4e8
Recognize more XML content types in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
359
diff
changeset
|
210 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
|
211 return true |
765e056ab4e8
Recognize more XML content types in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
359
diff
changeset
|
212 } |
344
e98033e3683a
Be more precise with HTTP headers in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
335
diff
changeset
|
213 } |
e98033e3683a
Be more precise with HTTP headers in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
335
diff
changeset
|
214 } |
e98033e3683a
Be more precise with HTTP headers in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
335
diff
changeset
|
215 return false |
e98033e3683a
Be more precise with HTTP headers in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
335
diff
changeset
|
216 } |
e98033e3683a
Be more precise with HTTP headers in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
335
diff
changeset
|
217 |
335
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
218 func rewrite(w io.Writer, r io.Reader, from, to string) error { |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
219 |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
220 decoder := xml.NewDecoder(r) |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
221 decoder.CharsetReader = charset.NewReaderLabel |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
222 |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
223 encoder := xml.NewEncoder(w) |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
224 |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
225 replace := func(s string) string { |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
226 return strings.Replace(s, from, to, -1) |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
227 } |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
228 |
372
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
229 var n nsdef |
335
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
230 |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
231 tokens: |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
232 for { |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
233 tok, err := decoder.Token() |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
234 switch { |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
235 case tok == nil && err == io.EOF: |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
236 break tokens |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
237 case err != nil: |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
238 return err |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
239 } |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
240 |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
241 switch t := tok.(type) { |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
242 case xml.StartElement: |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
243 t = t.Copy() |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
244 |
372
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
245 isDef := n.isDef(t.Name.Space) |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
246 n = n.push() |
335
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 for i := range t.Attr { |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
249 t.Attr[i].Value = replace(t.Attr[i].Value) |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
250 n.checkDef(&t.Attr[i]) |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
251 } |
351
b89138a25f9e
Fixed namespace compression in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
350
diff
changeset
|
252 |
372
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
253 for i := range t.Attr { |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
254 n.adjust(&t.Attr[i]) |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
255 } |
351
b89138a25f9e
Fixed namespace compression in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
350
diff
changeset
|
256 |
372
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
257 switch { |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
258 case isDef: |
351
b89138a25f9e
Fixed namespace compression in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
350
diff
changeset
|
259 t.Name.Space = "" |
372
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
260 default: |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
261 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
|
262 t.Name.Space = "" |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
263 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
|
264 } |
351
b89138a25f9e
Fixed namespace compression in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
350
diff
changeset
|
265 } |
335
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
266 tok = t |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
267 |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
268 case xml.CharData: |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
269 tok = xml.CharData(replace(string(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.EndElement: |
372
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
272 s := n.lookup(t.Name.Space) |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
273 |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
274 n = n.pop() |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
275 |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
276 if n.isDef(t.Name.Space) { |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
277 t.Name.Space = "" |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
278 } else if s != "" { |
351
b89138a25f9e
Fixed namespace compression in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
350
diff
changeset
|
279 t.Name.Space = "" |
b89138a25f9e
Fixed namespace compression in WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
350
diff
changeset
|
280 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
|
281 } |
372
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
282 tok = t |
335
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
283 } |
372
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
284 |
335
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
285 if err := encoder.EncodeToken(tok); err != nil { |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
286 return err |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
287 } |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
288 } |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
289 |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
290 return encoder.Flush() |
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 |
372
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
293 type nsframe struct { |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
294 def string |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
295 ns map[string]string |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
296 } |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
297 |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
298 type nsdef []nsframe |
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 func (n nsdef) setDef(def string) { |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
301 if l := len(n); l > 0 { |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
302 n[l-1].def = def |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
303 } |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
304 } |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
305 |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
306 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
|
307 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
|
308 if x := n[i].def; x != "" { |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
309 return s == x |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
310 } |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
311 } |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
312 return false |
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 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
|
316 if l := len(n); l > 0 { |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
317 n[l-1].ns[ns] = s |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
318 } |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
319 } |
335
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
320 |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
321 func (n nsdef) lookup(ns string) string { |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
322 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
|
323 if s := n[i].ns[ns]; s != "" { |
335
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
324 return s |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
325 } |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
326 } |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
327 return "" |
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 |
372
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
330 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
|
331 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
|
332 n.setDef(at.Value) |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
333 } |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
334 } |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
335 |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
336 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
|
337 switch { |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
338 case at.Name.Space == "xmlns": |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
339 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
|
340 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
|
341 at.Name.Space = "" |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
342 |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
343 case at.Name.Space != "": |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
344 if n.isDef(at.Name.Space) { |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
345 at.Name.Space = "" |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
346 } 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
|
347 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
|
348 at.Name.Space = "" |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
349 } |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
350 } |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
351 } |
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
352 |
335
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
353 func (n nsdef) push() nsdef { |
372
15369b41be74
Teach WFS proxy about default namespaces.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
369
diff
changeset
|
354 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
|
355 } |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
356 |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
357 func (n nsdef) pop() nsdef { |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
358 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
|
359 n[l-1] = nsframe{} |
335
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
360 n = n[:l-1] |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
361 } |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
362 return n |
bd292a554b6e
Made gemma a WFS proxy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
363 } |