@@ -7,6 +7,7 @@ var glob = require('glob');
77var path = require ( 'path' ) ;
88var $rdf = require ( 'rdflib' ) ;
99var S = require ( 'string' ) ;
10+ var async = require ( 'async' ) ;
1011
1112var debug = require ( '../logging' ) . handlers ;
1213var acl = require ( '../acl.js' ) ;
@@ -67,8 +68,9 @@ function get(req, res, includeBody) {
6768 header . addLink ( res , metaLink , 'describedBy' ) ;
6869
6970 ldp . stat ( filename , function ( err , stats ) {
71+ // File does not exist
7072 if ( err ) {
71- // File does not exist
73+ // Check in case it is a pattern, e.g. /*.js
7274 if ( glob . hasMagic ( filename ) ) {
7375 debug ( "GET/HEAD -- Glob request" ) ;
7476 return globHandler ( ) ;
@@ -95,6 +97,7 @@ function get(req, res, includeBody) {
9597 }
9698 ldp . listContainer ( filename , uri , data , function ( err , data ) {
9799 if ( err ) {
100+ debug ( 'GET/HEAD -- Read error:' + err ) ;
98101 return res
99102 . status ( err . status )
100103 . send ( err . message ) ;
@@ -137,7 +140,9 @@ function get(req, res, includeBody) {
137140 }
138141
139142 // Otherwise, just send the data
140- res . status ( 200 ) . send ( data ) ;
143+ return res
144+ . status ( 200 )
145+ . send ( data ) ;
141146 } ) ;
142147 }
143148 } ) ;
@@ -151,34 +156,37 @@ function get(req, res, includeBody) {
151156
152157 // Matches found
153158 var globGraph = $rdf . graph ( ) ;
154- matches . forEach ( function ( match ) {
155- try {
156- var baseUri = file . filenameToBaseUri ( match , uri , ldp . root ) ;
157- var fileData = fs . readFileSync (
158- match ,
159- { encoding : "utf8" } ) ;
160159
160+ async . each ( matches , function ( match , done ) {
161+ var baseUri = file . filenameToBaseUri ( match , uri , ldp . root ) ;
162+ fs . readFile ( match , { encoding : "utf8" } , function ( err , fileData ) {
163+ if ( err ) {
164+ debug ( 'GET -- Error in globHandler' + err ) ;
165+ return done ( null ) ;
166+ }
161167 if ( S ( match ) . endsWith ( ".ttl" ) && aclAllow ( match ) ) {
162- $rdf . parse (
163- fileData ,
164- globGraph ,
165- baseUri ,
166- 'text/turtle' ) ;
168+ try {
169+ $rdf . parse (
170+ fileData ,
171+ globGraph ,
172+ baseUri ,
173+ 'text/turtle' ) ;
174+ } catch ( parseErr ) {
175+ debug ( 'GET -- Error in globHandler' + parseErr ) ;
176+ }
167177 }
168- } catch ( readErr ) {
169- debug ( 'GET -- Error in globHandler' + readErr ) ;
170- return ;
171- }
178+ return done ( null ) ;
179+ } ) ;
180+ } , function ( ) {
181+ var data = $rdf . serialize (
182+ undefined ,
183+ globGraph ,
184+ null ,
185+ 'text/turtle' ) ;
186+ // TODO this should be added as a middleware in the routes
187+ res . locals . turtleData = data ;
188+ return parseLinkedData ( req , res ) ;
172189 } ) ;
173-
174- var data = $rdf . serialize (
175- undefined ,
176- globGraph ,
177- null ,
178- 'text/turtle' ) ;
179- // TODO this should be added as a middleware in the routes
180- res . locals . turtleData = data ;
181- return parseLinkedData ( req , res ) ;
182190 } ) ;
183191 }
184192
@@ -190,7 +198,7 @@ function get(req, res, includeBody) {
190198 var relativePath = '/' +
191199 path . relative ( ldp . root , match ) ;
192200
193- req . path = relativePath ;
201+ res . locals . path = relativePath ;
194202 var allow = acl . allow ( "Read" , req , res ) ;
195203
196204 if ( allow . status === 200 ) {
0 commit comments