Commit Graph

11 Commits (cc7e695f37c0032cbd9eea3348e0fa4126c588bf)

Author SHA1 Message Date
Tim Raymond 206a6bba33 Bypass URLPrefixer if http.Flusher is unavailable
In certain situations, the http.ResponseWriter passed to the URLPrefixer
may not be an http.Flusher. A simple case where this may occur is if the
Prefixer has been wrapped up in a middleware where the above middleware
wraps the ResponseWriter in a ResponseWriter that doesn't implement the
Flush method.

Previously, the Prefixer would error, which would cause the request to
fail with a 500. Instead, the condition is logged and the request is
passed unmodified to the next middleware in the chain. This effectively
disables prefixing for requests where the above ResponseWriter is not an
http.Flusher.

Misc. Changes
=============

- Some tests for "builders" were moved to server/builders_test.go to
  follow with convention. We've been naming files after different things
  under test and leaving the file matching the package name for support
  objects-in this case a mock logger was added to that file.
2017-04-04 10:33:14 -04:00
Chris Goller 49d8ece940 Add structured logging to underlying http server 2017-03-06 15:16:45 -06:00
Chris Goller 9ac300cf19 Update to fix go linter issues 2017-03-06 10:11:52 -06:00
Chris Goller ce5e104a59 Fix url_prefixer to write asset headers 2017-02-09 17:13:34 -06:00
Tim Raymond c7f31a5074 Fix typo
Spelling is hard.
2017-01-27 19:31:57 -05:00
Tim Raymond 99bd4f369a 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 0c02aeedc0 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 2b6a05cf6f 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 2dfc52069e 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 14437af28d 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 4064817ac8 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