Commit Graph

10 Commits (7ef85b6d54d0dc26f07da0cc77e4d2bf31b1aecf)

Author SHA1 Message Date
Chris Goller b60901e766 Add structured logging to underlying http server 2017-03-06 15:16:45 -06:00
Chris Goller 8dc012aeac Update to fix go linter issues 2017-03-06 10:11:52 -06:00
Chris Goller f37bed5403 Fix url_prefixer to write asset headers 2017-02-09 17:13:34 -06:00
Tim Raymond ee8b9345e9 Fix typo
Spelling is hard.
2017-01-27 19:31:57 -05:00
Tim Raymond 3752affedf Remove extraneous lines
One of these was from experimental work during working on this feature.
The other is just... well, extra.
2017-01-27 19:27:59 -05:00
Tim Raymond 31621b460d Make URLPrefixer use the chronograf.Logger
We have a unified structured logging package in Chronograf, and this
should use it.
2017-01-27 19:24:51 -05:00
Tim Raymond e3186479cd Move URLPrefixer setup to mux, add default
URLPrefixer had nothing to do with assets, so it actually belongs up in
the mux, where we're assembling handlers together across the
application.

Also, the setup was painful to look at, and others will probably use the
same `Attrs`, so a `NewDefaultURLPrefixer` was added to spawn a prefixer
with only a prefix and a next handler.
2017-01-27 19:14:21 -05:00
Tim Raymond cf243794af Improve docs on URL Prefixer
These were a little too brief. Hopefully these make it clearer to others
2017-01-27 17:00:08 -05:00
Tim Raymond 9d2e9111ef Make prefixer support multiple tags, chunked enc
The prefixer needs to not only replace `src="` attributes as it
currently does because that is not the only place a relative URL can
appear. It needs to also prefix URLs found in CSS which can also come
from the downstream http.ResponseWriter.

This adds support for an arbitrary list of patterns that will cause the
prefixer to insert its configured prefix. This is currently set to look
for `src`, `href`, and `url()` attributes.

Also, because we are modifying the stream, we need to suppress the
Content-Length generated by any downstream http.Handlers and instead
enable Transfer-Encoding: chunked so that we can stream the modified
response (we don't know apriori how many times we'll perform a
prefixing, so we can't calculate a final Content-Length). This is
accomplished by duplicating the Headers in the wrapResponseWriter that
is handed to the `Next` handler. We also handle the chunking and
Flushing that needs to happen as a result of using chunked transfer
encoding.
2017-01-27 17:00:08 -05:00
Tim Raymond 33256914b3 Add URL Prefixer
In order to support hosting chronograf under an arbitrary path[1], we
need to be able to rewrite all the URLs that are served in HTML and CSS.
Take, for example, the scenario where Chronograf is to be hosted under
`/chronograf` using Caddy and this example Caddyfile:

```
localhost:2020
gzip
proxy /chronograf localhost:8888 {
  without /chronograf
}
```

Chronograf will not load properly when visiting
`http://localhost:2020/chronograf` because the requests for CSS, and
fonts will go to `http://localhost:2020/app-somegianthash.js` when they
should go to `http://localhost:2020/chronograf/app-somegianthash.js`.
This is the essence of issue #721.

To solve this, we add a URLPrefixer http.Handler, that acts as a
middleware. It inserts itself between any upstream handlers, and the
handler that was passed to it as its `Next` parameter and searches for
`src="` attributes. Upon discovering one of these attributes, it writes
the detected attribute and then the configured prefix. It then continues
writing the stream to the upstream http.ResponseWriter until
encountering another attribute until EOF.
2017-01-27 17:00:08 -05:00