Permalink
Browse files

Common middleware now working pretty well

  • Loading branch information...
brokenseal committed Jul 9, 2010
1 parent 42426c8 commit 47c6071c4147fee9aee1aef274c6401693ce666c
Showing with 116 additions and 12 deletions.
  1. +74 −2 broke/core/handlers/node.js
  2. +12 −1 broke/core/utils.js
  3. +10 −3 broke/http/http.js
  4. +19 −6 broke/middleware/common.js
  5. +1 −0 examples/node/settings.js
@@ -6,6 +6,8 @@
,url= require('url')
,sys= require('sys')
,utils= require('broke/core/utils')
+ ,encoding= require('broke/utils/encoding')
+ ,iriToUri= encoding.iriToUri
;
HttpRequest.create({
@@ -17,13 +19,13 @@
;
this._super(nodeRequest);
+ this.nodeRequest= nodeRequest;
this.initMeta(nodeRequest);
this.method= nodeRequest.method;
+ this.parsedUrl= parsedUrl;
this.path= parsedUrl.pathname;
this.pathInfo= this.path;
-
- this._isSecure= nodeRequest.connection.secure;
}
,initMeta: function(nodeRequest){
var
@@ -45,6 +47,76 @@
_this.META[key] = this;
});
}
+ ,isSecure: function(){
+ return this.nodeRequest.connection.secure;
+ }
+ ,getFullPath: function(){
+ // RFC 3986 requires query string arguments to be in the ASCII range.
+ // Rather than crash if this doesn't happen, we encode defensively.
+ return utils.interpolate('%s%s', this.path, (this.parsedUrl.query || '') ? ('?' + iriToUri((this.parsedUrl.query || ''))) : '');
+ }
+ ,_loadPostAndFiles: function(){
+ // Populates self._post and self._files
+ if(this.method == 'POST') {
+ if(0) {
+ // TODO
+ } else {
+
+ }
+ } else {
+ [ this._post, this._files ] = [ http.QueryDict(''), datastructures.MultiValueDict() ];
+ }
+ }
+ /*,get GET() {
+ if(!('_get' in this)) {
+ this._get= http.QueryDict(this.parsedUrl.query, this._encoding);
+ }
+
+ return this._get;
+ }
+ ,set GET(get) {
+ this._get= get;
+ }
+ ,get POST() {
+ if(!('_post' in this)) {
+ this._loadPostAndFiles();
+ }
+
+ return this._post;
+ }
+ ,set POST(post) {
+ this._post= post;
+ }
+ ,get FILES() {
+ if(!('_files' in this)) {
+ this._loadPostAndFiles();
+ }
+
+ return this._files;
+ }
+ ,set FILES(files) {
+ this._files= files;
+ }
+ ,get REQUEST() {
+ if(!('_request' in this)) {
+ this._request= datastructures.MergeDict(this.POST, this.GET);
+ }
+
+ return this._request;
+ }
+ ,set REQUEST(request) {
+ this._request= request;
+ }*/
+ /*,get COOKIES() {
+ if(!('_cookies' in this)) {
+ this._cookies= http.parseCookie();
+ }
+
+ return this._cookies;
+ }
+ ,set COOKIES(cookies) {
+ this._cookies= cookies;
+ }*/
});
BaseHandler.create({
View
@@ -564,12 +564,23 @@
return true;
}
+ ,len= function(obj){
+ // does it have a length attribute?
+ if(obj.length !== undefined) {
+ return obj.length;
+ } else if(obj.__len__){
+ return obj.__len__();
+ }
+
+ return undefined;
+ }
;
extend(__global__, {
getCallable: getCallable
,all: all
- ,any: any
+ ,any: any
+ ,len: len
,filter: filter
,map: map
,bool: bool
View
@@ -137,9 +137,6 @@
isSecure: function(){
// TODO
//return os.environ.get("HTTPS") == "on"
- if(this._isSecure !== undefined) {
- return this._isSecure;
- }
return false;
},
isAjax: function(){
@@ -338,6 +335,16 @@
}
});
+ _.HttpResponse.create({
+ __name__: 'HttpResponsePermanentRedirect'
+ ,__parent__: _
+ ,statusCode: 301
+ ,__init__: function(redirectTo){
+ this._super();
+ this.setHeader('Location', iriToUri(redirectTo));
+ }
+ });
+
_.HttpResponse.create({
__name__: 'HttpResponseNotModified'
,__parent__: _
View
@@ -4,13 +4,26 @@
,GenericError= require('broke/core/exceptions').GenericError
,gettext= require('broke/utils/translation').gettext.gettext
,http= require('broke/http/http')
- ,exceptions= require('broke/core/exceptions')
- //,urlquote= require('broke/utils/http').urlquote
+ ,exceptions= require('broke/core/exceptions')
+ ,urlResolvers= require('broke/core/urlresolvers')
+ ,urlquote= require('broke/utils/http').urlquote
//,mailManagers= request('broke/core/mail').mailManagers
,md5Constructor= require('dependencies/md5').hex_md5
,utils= require('broke/core/utils')
- ,Class= require('dependencies/pyjammin/class').Class
+ ,Class= require('dependencies/pyjammin/class').Class
+ ,_isValidPath= function(path, urlConf){
+ // Returns True if the given path resolves against the default URL resolver,
+ // False otherwise.
+ try {
+ urlResolvers.resolve(path, urlConf);
+ return true;
+ } catch(e) {
+ if(e.name == urlResolvers.Resolver404) {
+ return false;
+ }
+ }
+ }
;
/* "Common" middleware for taking care of some basic operations:
@@ -61,14 +74,14 @@
oldUrl= [ host, request.path ];
newUrl= [].concat(oldUrl);
- if(settings.PREPEND_WWW && oldUrl[0] && !utils.startsWith(newUrl[0], 'www.')){
+ if(settings.PREPEND_WWW != false && oldUrl[0] && !utils.startsWith(newUrl[0], 'www.')){
newUrl[0] = 'www.' + oldUrl[0];
}
// Append a slash if APPEND_SLASH is set and the URL doesn't have a
// trailing slash and there is no pattern for the current path
if(settings.APPEND_SLASH && !utils.endsWith(oldUrl[0]), '/'){
- urlConf= utils.getattr(request, 'urlConf', null);
+ urlConf= request.urlConf || null;
if(!_isValidPath(request.pathInfo, urlConf) && _isValidPath(utils.interpolate('%s/', request.pathInfo), urlConf)){
newUrl[1]= newUrl[1] + '/';
@@ -98,7 +111,7 @@
newUrl+= '?' + request.META.QUERY_STRING;
}
- return http.HttpResponsePermanentRedirect(newurl);
+ return http.HttpResponsePermanentRedirect(newUrl);
}
,processResponse: function(request, response){
var
@@ -50,6 +50,7 @@
,ROOT_URLCONF: 'examples/node/urls'
,MEDIA_ROOT: '/var/www/js/broke/examples/node/static'
,APPEND_SLASH: true
+ ,PREPEND_WWW: false
}
,development= utils.extend(utils.clone(production), {
BASE_URL: '/broke'

0 comments on commit 47c6071

Please sign in to comment.