From bf243244ce3b0c88170484444ce340129b984dee Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Mon, 21 Aug 2017 23:35:04 +0200 Subject: [PATCH] [web] make it possible to run static viewer in subdirectories --- mitmproxy/tools/web/app.py | 2 +- mitmproxy/tools/web/static_viewer.py | 2 +- test/mitmproxy/tools/web/test_app.py | 10 +++++----- test/mitmproxy/tools/web/test_static_viewer.py | 4 ++-- .../__snapshots__/ContentViewOptionsSpec.js.snap | 2 +- .../ContentView/__snapshots__/ContentViewSpec.js.snap | 2 +- .../__snapshots__/DownloadContentButtonSpec.js.snap | 2 +- .../ContentView/__snapshots__/MetaViewsSpec.js.snap | 2 +- .../FlowView/__snapshots__/MessagesSpec.js.snap | 4 ++-- .../components/__snapshots__/ContentViewSpec.js.snap | 2 +- web/src/js/__tests__/flow/utilsSpec.js | 6 +++--- web/src/js/backends/static.js | 2 +- web/src/js/backends/websocket.js | 2 +- web/src/js/ducks/ui/keyboard.js | 2 +- web/src/js/flow/utils.js | 2 +- web/src/js/urlState.js | 6 +++++- 16 files changed, 28 insertions(+), 24 deletions(-) diff --git a/mitmproxy/tools/web/app.py b/mitmproxy/tools/web/app.py index 9c6f7583a..77695515c 100644 --- a/mitmproxy/tools/web/app.py +++ b/mitmproxy/tools/web/app.py @@ -478,7 +478,7 @@ class Application(tornado.web.Application): (r"/flows/(?P[0-9a-f\-]+)/duplicate", DuplicateFlow), (r"/flows/(?P[0-9a-f\-]+)/replay", ReplayFlow), (r"/flows/(?P[0-9a-f\-]+)/revert", RevertFlow), - (r"/flows/(?P[0-9a-f\-]+)/(?Prequest|response)/_content", FlowContent), + (r"/flows/(?P[0-9a-f\-]+)/(?Prequest|response)/content.data", FlowContent), ( r"/flows/(?P[0-9a-f\-]+)/(?Prequest|response)/content/(?P[0-9a-zA-Z\-\_]+)(?:\.json)?", FlowContentView), diff --git a/mitmproxy/tools/web/static_viewer.py b/mitmproxy/tools/web/static_viewer.py index c4f8b1397..416c2539e 100644 --- a/mitmproxy/tools/web/static_viewer.py +++ b/mitmproxy/tools/web/static_viewer.py @@ -48,7 +48,7 @@ def save_flows_content(path: pathlib.Path, flows: typing.Iterable[flow.Flow]) -> message_path = path / "flows" / f.id / m os.makedirs(str(message_path / "content"), exist_ok=True) - with open(str(message_path / '_content'), 'wb') as content_file: + with open(str(message_path / 'content.data'), 'wb') as content_file: # don't use raw_content here as this is served with a default content type if message: content_file.write(message.content) diff --git a/test/mitmproxy/tools/web/test_app.py b/test/mitmproxy/tools/web/test_app.py index aaf949a86..6b88823c8 100644 --- a/test/mitmproxy/tools/web/test_app.py +++ b/test/mitmproxy/tools/web/test_app.py @@ -186,7 +186,7 @@ class TestApp(tornado.testing.AsyncHTTPTestCase): f.response.headers["Content-Encoding"] = "ran\x00dom" f.response.headers["Content-Disposition"] = 'inline; filename="filename.jpg"' - r = self.fetch("/flows/42/response/_content") + r = self.fetch("/flows/42/response/content.data") assert r.body == b"message" assert r.headers["Content-Encoding"] == "random" assert r.headers["Content-Disposition"] == 'attachment; filename="filename.jpg"' @@ -194,17 +194,17 @@ class TestApp(tornado.testing.AsyncHTTPTestCase): del f.response.headers["Content-Disposition"] f.request.path = "/foo/bar.jpg" assert self.fetch( - "/flows/42/response/_content" + "/flows/42/response/content.data" ).headers["Content-Disposition"] == 'attachment; filename=bar.jpg' f.response.content = b"" - assert self.fetch("/flows/42/response/_content").code == 400 + assert self.fetch("/flows/42/response/content.data").code == 400 f.revert() def test_update_flow_content(self): assert self.fetch( - "/flows/42/request/_content", + "/flows/42/request/content.data", method="POST", body="new" ).code == 200 @@ -222,7 +222,7 @@ class TestApp(tornado.testing.AsyncHTTPTestCase): b'--somefancyboundary--\r\n' ) assert self.fetch( - "/flows/42/request/_content", + "/flows/42/request/content.data", method="POST", headers={"Content-Type": 'multipart/form-data; boundary="somefancyboundary"'}, body=body diff --git a/test/mitmproxy/tools/web/test_static_viewer.py b/test/mitmproxy/tools/web/test_static_viewer.py index 138c3add1..5b7ddfff6 100644 --- a/test/mitmproxy/tools/web/test_static_viewer.py +++ b/test/mitmproxy/tools/web/test_static_viewer.py @@ -43,9 +43,9 @@ def test_save_flows_content(ctx, tmpdir): for p in flows_path.listdir(): assert p.join('request').check(dir=1) assert p.join('response').check(dir=1) - assert p.join('request/_content').check(file=1) + assert p.join('request/content.data').check(file=1) assert p.join('request/content').check(dir=1) - assert p.join('response/_content').check(file=1) + assert p.join('response/content.data').check(file=1) assert p.join('response/content').check(dir=1) assert p.join('request/content/Auto.json').check(file=1) assert p.join('response/content/Auto.json').check(file=1) diff --git a/web/src/js/__tests__/components/ContentView/__snapshots__/ContentViewOptionsSpec.js.snap b/web/src/js/__tests__/components/ContentView/__snapshots__/ContentViewOptionsSpec.js.snap index 01fab0a7e..57368de2b 100644 --- a/web/src/js/__tests__/components/ContentView/__snapshots__/ContentViewOptionsSpec.js.snap +++ b/web/src/js/__tests__/components/ContentView/__snapshots__/ContentViewOptionsSpec.js.snap @@ -13,7 +13,7 @@ exports[`ContentViewOptions Component should render correctly 1`] = `   `; diff --git a/web/src/js/__tests__/components/ContentView/__snapshots__/DownloadContentButtonSpec.js.snap b/web/src/js/__tests__/components/ContentView/__snapshots__/DownloadContentButtonSpec.js.snap index 4c578a0c2..76f21ce15 100644 --- a/web/src/js/__tests__/components/ContentView/__snapshots__/DownloadContentButtonSpec.js.snap +++ b/web/src/js/__tests__/components/ContentView/__snapshots__/DownloadContentButtonSpec.js.snap @@ -3,7 +3,7 @@ exports[`DownloadContentButton Component should render correctly 1`] = ` { let msg = "foo", view = "bar", flow = { request: msg, id: 1} expect(utils.MessageUtils.getContentURL(flow, msg, view)).toEqual( - "/flows/1/request/content/bar.json" + "./flows/1/request/content/bar.json" ) expect(utils.MessageUtils.getContentURL(flow, msg, '')).toEqual( - "/flows/1/request/_content" + "./flows/1/request/content.data" ) // response flow = {response: msg, id: 2} expect(utils.MessageUtils.getContentURL(flow, msg, view)).toEqual( - "/flows/2/response/content/bar.json" + "./flows/2/response/content/bar.json" ) }) }) diff --git a/web/src/js/backends/static.js b/web/src/js/backends/static.js index 7e87a5eda..d2c8d3fe3 100644 --- a/web/src/js/backends/static.js +++ b/web/src/js/backends/static.js @@ -16,7 +16,7 @@ export default class StaticBackend { } fetchData(resource) { - fetchApi(`/${resource}`) + fetchApi(`./${resource}`) .then(res => res.json()) .then(json => { this.receive(resource, json) diff --git a/web/src/js/backends/websocket.js b/web/src/js/backends/websocket.js index d7e13bb2a..081366178 100644 --- a/web/src/js/backends/websocket.js +++ b/web/src/js/backends/websocket.js @@ -34,7 +34,7 @@ export default class WebsocketBackend { fetchData(resource) { let queue = [] this.activeFetches[resource] = queue - fetchApi(`/${resource}`) + fetchApi(`./${resource}`) .then(res => res.json()) .then(json => { // Make sure that we are not superseded yet by the server sending a RESET. diff --git a/web/src/js/ducks/ui/keyboard.js b/web/src/js/ducks/ui/keyboard.js index e3f8c33c7..ed4dbba54 100644 --- a/web/src/js/ducks/ui/keyboard.js +++ b/web/src/js/ducks/ui/keyboard.js @@ -5,7 +5,7 @@ import * as modalActions from "./modal" export function onKeyDown(e) { - console.debug("onKeyDown", e) + //console.debug("onKeyDown", e) if (e.ctrlKey) { return () => { } diff --git a/web/src/js/flow/utils.js b/web/src/js/flow/utils.js index 9915a639a..d38f4a860 100644 --- a/web/src/js/flow/utils.js +++ b/web/src/js/flow/utils.js @@ -49,7 +49,7 @@ export var MessageUtils = { } else if (message === flow.response) { message = "response"; } - return `/flows/${flow.id}/${message}/` + (view ? `content/${view}.json` : '_content'); + return `./flows/${flow.id}/${message}/` + (view ? `content/${view}.json` : 'content.data'); } }; diff --git a/web/src/js/urlState.js b/web/src/js/urlState.js index 7802bdb86..d9c50c038 100644 --- a/web/src/js/urlState.js +++ b/web/src/js/urlState.js @@ -72,8 +72,12 @@ export function updateUrlFromStore(store) { if (queryStr) { url += "?" + queryStr } + let pathname = window.location.pathname + if(pathname === "blank") { + pathname = "/" // this happens in tests... + } if (window.location.hash.substr(1) !== url) { - history.replaceState(undefined, "", `/#${url}`) + history.replaceState(undefined, "", `${pathname}#${url}`) } }