@@ -24,20 +24,23 @@ function get(req, res, includeBody) {
2424 var uri = file . uriAbs ( req ) ;
2525
2626 // Add request to subscription service
27- if ( ( '' + req . path ) . slice ( - options . suffixChanges . length ) ===
27+ if ( req . path . slice ( - options . suffixChanges . length ) ===
2828 options . suffixChanges ) {
2929 debug ( "GET -- Subscribed to " , req . path ) ;
3030 return subscription . subscribeToChanges ( req , res ) ;
3131 }
32+
3233 // Set headers
3334 res . header ( 'MS-Author-Via' , 'SPARQL' ) ;
35+
36+ // Set live updates
3437 if ( options . live ) {
3538 // Note not yet in
3639 // http://www.iana.org/assignments/link-relations/link-relations.xhtml
3740 header . addLink ( res , req . path + options . suffixChanges , 'changes' ) ;
3841 // res.header('Link' , '' + req.path + options.suffixSSE + ' ; rel=events' );
3942 // overwrites the pevious
40- res . header ( 'Updates-Via' , '' + req . path + options . suffixChanges ) ;
43+ res . header ( 'Updates-Via' , req . path + options . suffixChanges ) ;
4144 }
4245
4346 if ( includeBody ) {
@@ -47,107 +50,131 @@ function get(req, res, includeBody) {
4750 }
4851
4952 var filename = file . uriToFilename ( req . path , options . root ) ;
53+
54+ // Check if file exists
5055 fs . stat ( filename , function ( err , stats ) {
5156 if ( err ) {
57+ // File does not exist
5258 if ( glob . hasMagic ( filename ) ) {
5359 debug ( "GET/HEAD -- Glob request" ) ;
54- globHandler ( ) ;
55- } else {
56- debug ( 'GET/HEAD -- Read error: ' + err ) ;
57- return res . status ( 404 ) . send ( "Can't read file: " + err ) ;
60+ return globHandler ( ) ;
5861 }
59- } else if ( stats . isDirectory ( ) ) {
62+ debug ( 'GET/HEAD -- Read error: ' + err ) ;
63+ return res . status ( 404 ) . send ( "Can't read file: " + err ) ;
64+ }
65+
66+ if ( stats . isDirectory ( ) ) {
67+ // Found a container
6068 if ( includeBody ) {
61- metadata . readContainerMetadata ( filename , containerHandler ) ;
62- } else {
63- res . status ( 200 ) . send ( ) ;
64- res . end ( ) ;
69+ return metadata . readContainerMetadata ( filename , containerHandler ) ;
6570 }
71+ res . status ( 200 ) . send ( ) ;
72+ res . end ( ) ;
6673 } else {
67- if ( includeBody )
68- fs . readFile ( filename , {
69- encoding : "utf8"
70- } , fileHandler ) ;
71- else {
72- res . status ( 200 ) . send ( ) ;
73- res . end ( ) ;
74+ // Found a resource
75+ if ( includeBody ) {
76+ return fs . readFile ( filename , { encoding : "utf8" } , fileHandler ) ;
7477 }
78+ res . status ( 200 ) . send ( ) ;
79+ res . end ( ) ;
7580 }
7681 } ) ;
7782
7883 function fileHandler ( err , data ) {
7984 if ( err ) {
8085 debug ( 'GET/HEAD -- Read error:' + err ) ;
81- res . status ( 404 ) . send ( "Can't read file: " + err ) ;
82- } else {
83- debug ( 'GET/HEAD -- Read Ok. Bytes read: ' + data . length ) ;
84- var ct = mime . lookup ( filename ) ;
85- res . set ( 'content-type' , ct ) ;
86- debug ( 'GET/HEAD -- content-type: ' + ct ) ;
87- if ( path . extname ( filename ) === options . suffixAcl ||
88- path . basename ( filename ) === options . suffixAcl ||
89- path . basename ( filename ) === metaExtension ) {
90- ct = 'text/turtle' ;
91- }
92- if ( ct === 'text/turtle' ) {
93- parseLinkedData ( data ) ;
94- } else {
95- res . status ( 200 ) . send ( data ) ;
96- }
86+ return res . status ( 404 )
87+ . send ( "Can't read file: " + err ) ;
88+ }
89+
90+ debug ( 'GET/HEAD -- Read Ok. Bytes read: ' + data . length ) ;
91+ var contentType = mime . lookup ( filename ) ;
92+ res . set ( 'content-type' , contentType ) ;
93+ debug ( 'GET/HEAD -- content-type: ' + contentType ) ;
94+
95+ // Consider acl and meta files text/turtle
96+ if ( path . extname ( filename ) === options . suffixAcl ||
97+ path . basename ( filename ) === options . suffixAcl ||
98+ path . basename ( filename ) === metaExtension ) {
99+ contentType = 'text/turtle' ;
97100 }
101+
102+ // if data is text/turtle, parse it
103+ if ( contentType === 'text/turtle' ) {
104+ return parseLinkedData ( data ) ;
105+ }
106+
107+ // Otherwise, just send the data
108+ res . status ( 200 ) . send ( data ) ;
109+
98110 }
99111
100112 function containerHandler ( err , rawContainer ) {
101113 if ( err ) {
102114 debug ( "GET/HEAD -- Not a valid container" ) ;
103- res . status ( 404 ) . send ( "Not a container" ) ;
104- } else {
105- parseContainer ( rawContainer ) ;
115+ return res . status ( 404 )
116+ . send ( "Not a container" ) ;
106117 }
118+
119+ // Parse the container
120+ parseContainer ( rawContainer ) ;
107121 }
108122
109123 function globHandler ( ) {
110124 glob ( filename , globOptions , function ( err , matches ) {
111- if ( err || matches . length === 0 ) {
125+ if ( err || matches . length === 0 ) {
112126 debug ( "GET/HEAD -- No files matching the pattern" ) ;
113127 return res . sendStatus ( 404 ) ;
114- } else {
115- debug ( "matches " + matches ) ;
116- var globGraph = $rdf . graph ( ) ;
117- matches . forEach ( function ( match ) {
118- try {
119- var fileData = fs . readFileSync ( match ,
120- { encoding : "utf8" } ) ;
121- var baseUri = file . filenameToBaseUri ( match , uri , options . root ) ;
122- //TODO integrate ACL
123- if ( S ( match ) . endsWith ( ".ttl" ) && aclAllow ( match ) ) {
124- $rdf . parse ( fileData , globGraph , baseUri ,
125- 'text/turtle' ) ;
126- }
127- } catch ( readErr ) {
128- return ;
129- }
130- } ) ;
131- var turtleData = $rdf . serialize ( undefined , globGraph ,
132- null , 'text/turtle' ) ;
133- parseLinkedData ( turtleData ) ;
134128 }
129+
130+ // Matches found
131+ debug ( "matches " + matches ) ;
132+ var globGraph = $rdf . graph ( ) ;
133+ matches . forEach ( function ( match ) {
134+ try {
135+ var baseUri = file . filenameToBaseUri ( match , uri , options . root ) ;
136+ var fileData = fs . readFileSync (
137+ match ,
138+ { encoding : "utf8" } ) ;
139+
140+ //TODO integrate ACL
141+ if ( S ( match ) . endsWith ( ".ttl" ) && aclAllow ( match ) ) {
142+ $rdf . parse (
143+ fileData ,
144+ globGraph ,
145+ baseUri ,
146+ 'text/turtle' ) ;
147+ }
148+ } catch ( readErr ) {
149+ debug ( 'GET -- Error in globHandler' + readErr ) ;
150+ return ;
151+ }
152+ } ) ;
153+
154+ var turtleData = $rdf . serialize (
155+ undefined ,
156+ globGraph ,
157+ null ,
158+ 'text/turtle' ) ;
159+ parseLinkedData ( turtleData ) ;
135160 } ) ;
136161 }
137162
138163 function aclAllow ( match ) {
139164 if ( ! options . webid ) {
140165 return true ;
166+ }
167+
168+ var relativePath = '/' +
169+ path . relative ( options . root , match ) ;
170+
171+ req . path = relativePath ;
172+ var allow = acl . allow ( "Read" , req , res ) ;
173+
174+ if ( allow . status === 200 ) {
175+ return true ;
141176 } else {
142- var relativePath = '/' +
143- path . relative ( options . root , match ) ;
144- req . path = relativePath ;
145- var allow = acl . allow ( "Read" , req , res ) ;
146- if ( allow . status === 200 ) {
147- return true ;
148- } else {
149- return false ;
150- }
177+ return false ;
151178 }
152179 }
153180
@@ -159,8 +186,10 @@ function get(req, res, includeBody) {
159186 if ( accept === undefined || accept === null ) {
160187 accept = 'text/turtle' ;
161188 }
162- if ( accept === 'text/turtle' || accept === 'text/n3' ||
163- accept == 'application/turtle' || accept === 'application/n3' ) {
189+ if ( accept === 'text/turtle' ||
190+ accept === 'text/n3' ||
191+ accept === 'application/turtle' ||
192+ accept === 'application/n3' ) {
164193 return res . status ( 200 )
165194 . set ( 'content-type' , accept )
166195 . send ( turtleData ) ;
@@ -176,8 +205,12 @@ function get(req, res, includeBody) {
176205 }
177206
178207 //TODO rdflib callbacks
179- $rdf . serialize ( undefined , resourceGraph , null ,
180- accept , function ( err , result ) {
208+ $rdf . serialize (
209+ undefined ,
210+ resourceGraph ,
211+ null ,
212+ accept ,
213+ function ( err , result ) {
181214 if ( result === undefined || err ) {
182215 debug ( "GET/HEAD -- Serialization error: " + err ) ;
183216 return res . sendStatus ( 500 ) ;
@@ -198,6 +231,7 @@ function get(req, res, includeBody) {
198231 debug ( "GET/HEAD -- Error parsing data: " + err ) ;
199232 return res . status ( 500 ) . send ( err ) ;
200233 }
234+
201235 debug ( "GET/HEAD -- Reading directory" ) ;
202236 fs . readdir ( filename , readdirCallback ) ;
203237
@@ -221,8 +255,11 @@ function get(req, res, includeBody) {
221255 }
222256 }
223257 try {
224- var turtleData = $rdf . serialize ( undefined , resourceGraph ,
225- null , 'text/turtle' ) ;
258+ var turtleData = $rdf . serialize (
259+ undefined ,
260+ resourceGraph ,
261+ null ,
262+ 'text/turtle' ) ;
226263 parseLinkedData ( turtleData ) ;
227264 } catch ( parseErr ) {
228265 debug ( "GET/HEAD -- Error serializing container: " + parseErr ) ;
@@ -233,7 +270,10 @@ function get(req, res, includeBody) {
233270 }
234271}
235272
236- var globOptions = { noext : true , nobrace : true } ;
273+ var globOptions = {
274+ noext : true ,
275+ nobrace : true
276+ } ;
237277
238278function getHandler ( req , res ) {
239279 get ( req , res , true ) ;
0 commit comments