22
33const url = require ( 'url' )
44const glob = require ( 'glob' )
5- const HttpError = require ( 'standard-http-error' )
5+ // const HttpError = require('standard-http-error')
66
77const BY_METHOD = {
88 'head' : LdpHeadOperation ,
@@ -39,24 +39,19 @@ class LdpOperation {
3939 }
4040
4141 /**
42- * Taken from utils.getBaseUri()
42+ * General construction method, overridden where needed (such as in
43+ * LdpGetOperation).
4344 *
44- * @param req {IncomingRequest}
45- * @param host {SolidHost}
45+ * @param options {object}
4646 *
47- * @returns {URL }
47+ * @returns {LdpOperation }
4848 */
49- static parseTargetUrl ( { req, host} ) {
50- const protocol = host . serverUri . replace ( / : .* / , '' )
51-
52- return url . format ( {
53- protocol : protocol || req . protocol ,
54- host : req . get ( 'host' ) ,
55- pathname : url . resolve ( req . baseUrl , req . path ) ,
56- query : req . query
57- } )
49+ static from ( options ) {
50+ const Operation = this
51+ return new Operation ( options )
5852 }
5953
54+
6055 writeCommonHeaders ( { res} ) {
6156 // set headers in common to all LDP responses
6257 }
@@ -81,28 +76,40 @@ class LdpHeadOperation extends LdpOperation {
8176 * - Need to also support similar use case if index.ttl exists
8277 * - "Glob pattern request" if glob magic chars are detected (ie `*`)
8378 * - Otherwise, plain Get request
79+ *
80+ * @return {LdpGlobOperation|LdpListContainerOperation|LdpGetOperation }
8481 */
8582class LdpGetOperation extends LdpOperation {
86- static from ( { headers, target, bodyStream, authentication, store} ) {
83+ static from ( options ) {
84+ const { store, target } = options
8785 const targetUrl = url . format ( target )
8886
8987 if ( glob . hasMagic ( targetUrl ) ) {
90- // this is a Ldp Glob operation, return that
88+ return new LdpGlobOperation ( options )
9189 }
9290
93- // check to see if target is a container (based on Link: type rel)
94- // ...
95- // if it is a container, check to see if index.html exists
96- const indexFile = url . resolve ( targetUrl , '/index.html' )
97- if ( store . exists ( indexFile ) ) {
98- // this is an implicit Get operation to the index file
99- // return it
91+ const isContainer = targetUrl . endsWith ( '/' )
92+
93+ if ( isContainer ) {
94+ // if it is a container, check to see if index.html exists
95+ const indexFile = url . resolve ( targetUrl , '/index.html' )
96+
97+ if ( store . exists ( indexFile ) ) {
98+ return new LdpGetOperation ( { target : indexFile , ...options } )
99+ }
100+
101+ return new LdpListContainerOperation ( options )
100102 }
101103
102- // return plain get operation
104+ // plain get operation
105+ return new LdpGetOperation ( options )
103106 }
104107}
105108
109+ class LdpListContainerOperation extends LdpGetOperation { }
110+
111+ class LdpGlobOperation extends LdpGetOperation { }
112+
106113/**
107114 * Creates a resource or container, and any necessary containers above it in
108115 * the hierarchy. Idempotent.
0 commit comments