Permalink
Browse files

Updated crimson to work with the latest api

  • Loading branch information...
1 parent d683b26 commit 24ad84b77338e43ac28e8e7e092460f1b28ac849 @chrisbu committed Apr 28, 2012
View
19 core/CrimsonLib.dart
@@ -0,0 +1,19 @@
+// Copyright (C) 2012 Chris Buckett
+// Use of this source code is governed by a MIT style licence
+// can be found in the LICENSE file.
+// website:
+
+
+#library("crimson:core");
+
+#import('../../log4dart/LogLib.dart');
+#import("dart:io");
+#import("dart:uri");
+#import("dart:isolate", prefix:"isolate");
+
+#source('crimson.dart');
+#source('crimson_impl.dart');
+#source('crimson_utils.dart');
+#source('crimsonPrivate.dart');
+#source('crimsonHttpServer.dart');
+#source('crimsonModule.dart');
View
19 core/crimson.dart
@@ -4,23 +4,6 @@
// website:
-#library("crimson:core");
-
-#import('../../log4dart/LogLib.dart');
-#import("dart:io");
-#import("dart:uri");
-#import("dart:isolate", prefix:"isolate");
-#source('crimson_impl.dart');
-#source('crimson_utils.dart');
-#source('crimsonPrivate.dart');
-#source('crimsonHttpServer.dart');
-#source('crimsonModule.dart');
-
-
-
-//#source('endpoints/favicon.dart');
-//#source('endpoints/staticFile.dart');
-//#source('filters/cookieSession.dart');
/// Crimson is a set of HTTP middleware for Dart, loosely based upon
@@ -189,5 +172,7 @@ class CrimsonHttpException extends HttpException {
}
interface Session extends Map<String, Object> default SessionImpl {
+ Session();
+ void addOrReplace(String key, Object value);
}
View
2 core/crimsonHttpServer.dart
@@ -16,7 +16,7 @@ class _CrimsonHttpServer implements CrimsonHttpServer {
{
//setup the error and request handlers
_httpServer.onError = _httpErrorHandler;
- _httpServer.onRequest = _onRequestHandler;
+ _httpServer.defaultRequestHandler = _onRequestHandler;
}
/// Start listening on the [host] and [port] provided.
View
15 core/crimson_impl.dart
@@ -8,15 +8,26 @@
class SessionImpl implements Session {
- Map<String, Object> _internalList;
+ SessionImpl():
+ _internalList = new Map<String,Object>()
+ {
+ print("Created session object");
+
+ }
+
+ void addOrReplace(String key, String value) {
+ _internalList[key] = value;
+ }
+
+ final Map<String, Object> _internalList;
bool containsValue(Object value) => _internalList.containsValue(value);
bool containsKey(String key) => _internalList.containsKey(key);
Object operator[](String key) => _internalList[key];
- void operator[]=(String key, Object value) {
+ void operator[]=(String key, String value) {
_internalList[key] = value;
}
View
2 handlers/handlers.dart → handlers/HandlersLib.dart
@@ -1,5 +1,5 @@
#library("crimson:handlers");
-#import("../core/crimson.dart");
+#import("../core/CrimsonLib.dart");
#import('../../log4dart/LogLib.dart');
#import('../../dart-crypto-lib/src/md5.dart');
#import("dart:io");
View
6 handlers/endpoints/favicon.dart
@@ -32,9 +32,9 @@ class Favicon implements CrimsonEndpoint {
//otherwise, this request is for the favicon.
onSuccess(List filedata) {
- response.setHeader("Content-Type", "image/x-icon");
- response.setHeader("Content-Length", "${filedata.length}");
- response.setHeader("Cache-Control", "public, max-age=86400"); //1 day
+ response.headers.add(HttpHeaders.CONTENT_TYPE, "image/x-icon");
+ response.headers.add(HttpHeaders.CONTENT_LENGTH, "${filedata.length}");
+ response.headers.add(HttpHeaders.CACHE_CONTROL, "public, max-age=86400"); //1 day
response.outputStream.write(filedata);
completer.complete(data);
};
View
2 handlers/endpoints/staticFile.dart
@@ -23,7 +23,7 @@ class StaticFile implements CrimsonEndpoint {
onSuccess(List filedata) {
logger.debug("Read file: ${fileToLoad}");
//response.setHeader("Content-Type", "image/x-icon"); //todo - this properly
- response.setHeader("Content-Length", "${filedata.length}");
+ response.headers.add(HttpHeaders.CONTENT_LENGTH, "${filedata.length}");
//TODO: add other headers
response.outputStream.write(filedata);
completer.complete(data);
View
22 handlers/filters/cookieSession.dart
@@ -58,10 +58,10 @@ class CookieSession implements CrimsonFilter {
//TODO - CJB: Fix this - it's fragile.
logger.debug("in _getSession");
//tempcookie takes precdence, as it's been added.
- String cookieHeader = req.headers["tempcookie"];
+ String cookieHeader = req.headers.value("tempcookie");
if (cookieHeader == null) {
//otherwise, look for a real cookie header.
- cookieHeader = req.headers["cookie"];
+ cookieHeader = req.headers.value("cookie");
if (cookieHeader != null) {
logger.debug("found real cookie header: " + cookieHeader);
}
@@ -111,7 +111,7 @@ class CookieSession implements CrimsonFilter {
//is there an existing cookie header?
//if so, re-use the session cookie id...
- String cookieHeader = req.headers["cookie"];
+ String cookieHeader = req.headers.value("cookie");
if (cookieHeader != null) {
sessionid = _extractSessionCookieId(cookieHeader);
}
@@ -130,14 +130,14 @@ class CookieSession implements CrimsonFilter {
//add a new session cookie.
//no expiry means it will go when the browser session ends.
- res.setHeader("Set-Cookie","${SESSION_COOKIE}=${sessionid}; Path=/;");
+ res.headers.add("Set-Cookie","${SESSION_COOKIE}=${sessionid}; Path=/;");
//add it into the request, too, as this is used later by the getSession()
//on the first pass, and it should take precedence over the cookie on the request.
- //TODO: change the cookie ID on the request.
- req.headers.putIfAbsent("tempcookie", () => "${SESSION_COOKIE}=${sessionid}; Path=/;");
- //create somewhere to store stuff
- _sessions[sessionid] = new Map<String,Object>();
+ //TODO: change the cookie ID on the request.
+ res.headers.set("tempcookie", "${SESSION_COOKIE}=${sessionid}; Path=/;");
+ //create somewhere to store stuff and add to the list of sessions
+ _sessions[sessionid] = session = new Session();
//also store the session id in the session
//this allows callers to get the session id.
@@ -152,13 +152,11 @@ class CookieSession implements CrimsonFilter {
logger.debug("there is already a session cookie");
}
-
-
//add the time the last accessed the session (ie, now).
- session = _getSession(req);
+ logger.debug("session is null?=${session==null}");
session["last-accessed"] = new Date.now();
- if (req.headers.containsKey("cookie")) {
+ if (req.headers.value("cookie") != null) {
logger.debug("Header: cookie=${req.headers['cookie']}");
}
View
4 test/crimsonTest.dart
@@ -1,7 +1,7 @@
#library('crimsonTest');
-#import("../core/crimson.dart");
-#import("../handlers/handlers.dart");
+#import("../core/CrimsonLib.dart");
+#import("../handlers/HandlersLib.dart");
#import("dart:io");
///Simple test server

0 comments on commit 24ad84b

Please sign in to comment.