Skip to content

Commit

Permalink
Don't cache HTTP.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Dec 10, 2018
1 parent 240bc0c commit 676ba6a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 24 deletions.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
"name": "Cacao Server",
"program": "bin/cacao.dart",
"args": ["-p", "9000",
"-h", "0.0.0.0",
"http://www.google.ca",
"/stewart=file:///Users/stewart/cacao/static",
"/michael=file:///Users/michael/src/cacao/static",
"/mjpeg=http://oviso.axiscam.net/axis-cgi/mjpg/video.cgi"],
"/mjpeg=http://216.8.159.21/mjpg/video.mjpg"],
"request": "launch",
"type": "dart"
}
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.2

- Prevent caching of HTTP requests.

## 0.3.1

- Fix HTTP proxy connection leak.
Expand Down
31 changes: 21 additions & 10 deletions lib/src/io/serveHttp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,32 @@ Future<void> serveHttp(HttpRequest request, Uri uri) async {
final targetRequest = await client.openUrl(request.method, uri);
bool clientClosed = false;
request.response.done
.then((_) => clientClosed = true);
.then((_) => clientClosed = true)
.catchError((_) => clientClosed = true);

// Add the required headers to the request.
request.headers.forEach((hdr, val) =>
targetRequest.headers.set(hdr, val));
request.headers.forEach((hdr, val) {
// print('$hdr: $val');
if (hdr.toLowerCase() == 'connection') {
val = val.first.toLowerCase() == 'upgrade' ? val : ['close'];
}
targetRequest.headers.set(hdr, val);
});

// Make sure we request the right host.
targetRequest.headers.set('Host', uri.host);
await targetRequest.flush();

// Stream the request.
targetRequest
..bufferOutput = false
..addStream(request)
.then((_) => targetRequest.close());
if (true) {
// Stream the request.
targetRequest
..bufferOutput = false
..addStream(request)
.then((_) => targetRequest.close());
}
else {
targetRequest.close();
}

final targetResponse = await targetRequest.done;

Expand All @@ -39,9 +50,9 @@ Future<void> serveHttp(HttpRequest request, Uri uri) async {
request.response.headers.contentType = targetResponse.headers.contentType;

// Do the magic: add the allow origin.
request.response.headers.remove('X-Content-Type-Options', 'nosniff');
request.response.headers.remove('X-Frame-Options', 'SAMEORIGIN');
request.response.headers.set('Access-Control-Allow-Origin', '*');
request.response.headers.set('Cache-Control', 'no-cache');
await request.response.flush();
}
request.response.add(event);
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: cacao
description: Cacao (CORS Access-Control-Allow-Origin) Proxy
version: 0.3.1
version: 0.3.2-a1
homepage: https://github.com/michaelfig/cacao
author: Michael FIG <[email protected]>

Expand Down
31 changes: 19 additions & 12 deletions static/mjpeg.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
<head><title>Cacao - MJPEG to MediaStream</title></head>
<body>
<h1>Cacao - MJPEG to MediaStream</h1>
<p>First, run "cacao -p9000 /mjpeg=SOME-MJPEG-URL". Then click
<button onclick="go('http://localhost:9000/mjpeg')">Go</button>. After a few seconds, you should see the image, canvas, and video all populate and synchronize.
<p>First, run "cacao -p9000 /mjpeg=SOME-MJPEG-URL". Then:
<input type="text" id="url" value="/mjpeg" /><button onclick="go(url.value)">Go</button>. After a few seconds, you should see the image, canvas, and video all populate and synchronize.
</p>
<table>
<tr><th>Video</th> <td><video id="video" autoplay playsinline /></td></tr>
<tr><th>Video</th> <td><video style="background: black" id="video" autoplay playsinline /></td></tr>
<tr><th>Image</th> <td><img id="image"/></td></tr>
<tr><th>Canvas</th> <td><canvas id="canvas"></canvas></td></tr>
</table>
Expand All @@ -16,26 +16,33 @@ <h1>Cacao - MJPEG to MediaStream</h1>
var ctx = canvas.getContext('2d');

var TYPE_JPEG = 'image/jpeg';
function go(url) {
function go(src) {
// This function adapted from: https://github.com/aruntj/mjpeg-readable-stream/blob/master/index.html
// MIT License.
fetch(url, {mode: 'cors'})
fetch(src, {mode: 'cors'})
.then(function fetchComplete(response) {
if (!response.ok) {
throw Error(response.status+' '+response.statusText)
var reason = response.status+' '+response.statusText;
alert('Cannot fetch ' + src + ': ' + reason);
throw Error(reason);
}
if (!response.body) {
alert('ReadableStream not supported.\n' +
'If you are using Firefox, go to about:config and set:\n' +
'dom.streams.enabled and javascript.options.streams');
throw Error('ReadableStream not yet supported in this browser.')
throw Error('ReadableStream not yet supported in this browser.');
}

var stream = canvas.captureStream();
whenStreamIsActive(stream, setSrc);
function setSrc() {
console.log('setting active video stream');
video.srcObject = stream;
try {
var stream = canvas.captureStream();
whenStreamIsActive(stream, setSrc);
function setSrc() {
console.log('setting active video stream');
video.srcObject = stream;
}
}
catch (e) {
console.error('Cannot captureStream', e);
}

var reader = response.body.getReader();
Expand Down

0 comments on commit 676ba6a

Please sign in to comment.