Skip to content

Commit

Permalink
Updated crimson to work with the latest api
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbu committed Apr 28, 2012
1 parent d683b26 commit 24ad84b
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 39 deletions.
19 changes: 19 additions & 0 deletions 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');
19 changes: 2 additions & 17 deletions core/crimson.dart
Expand Up @@ -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
Expand Down Expand Up @@ -189,5 +172,7 @@ class CrimsonHttpException extends HttpException {
}

interface Session extends Map<String, Object> default SessionImpl {
Session();

void addOrReplace(String key, Object value);
}
2 changes: 1 addition & 1 deletion core/crimsonHttpServer.dart
Expand Up @@ -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.
Expand Down
15 changes: 13 additions & 2 deletions core/crimson_impl.dart
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion 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");
Expand Down
6 changes: 3 additions & 3 deletions handlers/endpoints/favicon.dart
Expand Up @@ -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);
};
Expand Down
2 changes: 1 addition & 1 deletion handlers/endpoints/staticFile.dart
Expand Up @@ -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);
Expand Down
22 changes: 10 additions & 12 deletions handlers/filters/cookieSession.dart
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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.
Expand All @@ -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']}");
}

Expand Down
4 changes: 2 additions & 2 deletions 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
Expand Down

0 comments on commit 24ad84b

Please sign in to comment.