Skip to content

Commit

Permalink
Better file: serving.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Dec 10, 2018
1 parent 676ba6a commit bcc4096
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 13 deletions.
10 changes: 6 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
"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://216.8.159.21/mjpg/video.mjpg"],
"../static",
"/mjpeg=http://216.8.159.21/mjpg/video.mjpg",
"/cam1=http://192.168.100.1/mjpeg",
"/cam2=http://192.168.100.2/-wvhttp-01-/video.cgi",
"/cam3=http://root:[email protected]/-wvhttp-01-/GetOneShot?frame_count=0",
"/cam4=http://192.168.100.4/-wvhttp-01-/video.cgi"],
"request": "launch",
"type": "dart"
}
Expand Down
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## 0.3.2

- Prevent caching of HTTP requests.
- Implement `../PATH` and `./PATH` URLs relative to Cacao's program file.
- Use MJPEG DataLen header if Content-Length doesn't exist.
- Make serveFile send 404's and directory index based on the filesystem contents.
- Send no-cache headers from serveHttp.

## 0.3.1

Expand Down
1 change: 0 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

ISC License

Copyright © 2018, Michael FIG <[email protected]>
Expand Down
19 changes: 14 additions & 5 deletions bin/cacao.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:cacao/cacao_io.dart' as cacao_io;
import 'package:args/args.dart';
import 'package:path/path.dart' as p;
import 'dart:io';
//import 'dart:convert';

Expand Down Expand Up @@ -50,7 +51,15 @@ ${parser.usage}''');
final match = re.firstMatch(pathUrl);
final path = match.group(2);
final url = match.group(3);
pathMap[path] = url;
final furl = p.split(url).first;
if (furl == '.' || furl == '..') {
// Get the path relative to the executing script.
final thisdir = p.canonicalize(p.join(p.dirname(Platform.script.path), url));
pathMap[path] = Uri.file(thisdir).toString();
}
else {
pathMap[path] = url;
}
});

if (pathMap.isEmpty) {
Expand All @@ -59,10 +68,10 @@ ${parser.usage}''');
return;
}

final h = argResults[host];
final p = int.parse(argResults[port]);
print('Listening on http://$h:$p/');
final server = await HttpServer.bind(h, p);
final hst = argResults[host];
final prt = int.parse(argResults[port]);
print('Listening on http://$hst:$prt/');
final server = await HttpServer.bind(hst, prt);

await cacao_io.serve(server, pathMap, cacao_io.DEFAULT_SCHEME_MAP);
}
12 changes: 10 additions & 2 deletions lib/src/io/serveFile.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import "dart:io";
import "dart:async";
import "package:mime/mime.dart";
import "package:path/path.dart" as p;

Future<void> serveFile(HttpRequest request, Uri uri) async {

var path = uri.path;
if (path.substring(path.length-1) == '/'){
path += "index.html";
if (await FileSystemEntity.isDirectory(path)) {
path = p.join(path, 'index.html');
}

if (!await FileSystemEntity.isFile(path)) {
request.response.statusCode = 404;
request.response.writeln('Not Found');
await request.response.close();
return;
}
var file = new File(path).openRead();

Expand Down
5 changes: 5 additions & 0 deletions lib/src/io/serveHttp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ Future<void> serveHttp(HttpRequest request, Uri uri) async {
// Do the magic: add the allow origin.
request.response.headers.set('Access-Control-Allow-Origin', '*');
request.response.headers.set('Cache-Control', 'no-cache');
if (false) {
for (var hdr in ['X-XSS-Protection', 'X-Content-Type-Options', 'X-Frame-Options']) {
request.response.headers.removeAll(hdr);
}
}
await request.response.flush();
}
request.response.add(event);
Expand Down
1 change: 1 addition & 0 deletions static/cacao.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/favicon.ico
Binary file not shown.
6 changes: 6 additions & 0 deletions static/mjpeg.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,16 @@ <h1>Cacao - MJPEG to MediaStream</h1>
}

function getLength(headers) {
// Most MJPEG streams send "Content-Length: 9389986".
var match = headers.match(/^content-length: *(\d+)$/mi);
if (match) {
return parseInt(match[1], 10);
}
// At least Sony SNC CH110 sends "DataLen: 00938696".
var match2 = headers.match(/^datalen: *(\d+)$/mi);
if (match) {
return parseInt(match[1], 10);
}
return -1;
}

Expand Down

0 comments on commit bcc4096

Please sign in to comment.