Skip to content

Commit d949004

Browse files
Add parseTargetUrl() to LdpOperation
1 parent dbdcd43 commit d949004

2 files changed

Lines changed: 40 additions & 7 deletions

File tree

lib/api/ldp/api.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class LdpHttpHandler {
2020
constructor (options) {
2121
this.store = options.store
2222
this.acl = options.acl
23+
this.host = options.host
2324
}
2425

2526
/**
@@ -54,13 +55,15 @@ class LdpHttpHandler {
5455
async middleware (req, res, next) {
5556
try {
5657
// parse operation
57-
const operation = LdpOperation.from(req)
58+
const operation = LdpOperation.from({req, host: this.host})
5859

5960
// check that operation is permitted (throws error if not)
6061
const permissions = await this.acl.allow(operation)
6162

6263
// perform the operation and return a response
63-
await operation.perform({res, permissions, store: this.store})
64+
const result = await operation.perform({res, store: this.store})
65+
operation.writeHeaders({res, result, permissions})
66+
operation.writeResponse({res, result})
6467
} catch (error) {
6568
next(error)
6669
}

lib/api/ldp/ldp-operation.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const url = require('url')
34
const HttpError = require('standard-http-error')
45

56
const OPERATIONS = {
@@ -36,9 +37,9 @@ class LdpOperation {
3637
this.bodyStream = options.bodyStream
3738
}
3839

39-
static async from ({req}) {
40+
static async from ({req, host}) {
4041
const { method, headers, session: { authentication } } = req
41-
const target = LdpOperation.parseTargetUrl(req)
42+
const target = LdpOperation.parseTargetUrl({req, host})
4243
const bodyStream = req
4344

4445
const Operation = OPERATIONS[method]
@@ -48,19 +49,48 @@ class LdpOperation {
4849

4950
return Operation.from({headers, target, bodyStream, authentication})
5051
}
52+
53+
/**
54+
* Taken from utils.getBaseUri()
55+
*
56+
* @param req {IncomingRequest}
57+
* @param host {SolidHost}
58+
*
59+
* @returns {URL}
60+
*/
61+
static parseTargetUrl ({req, host}) {
62+
const protocol = host.serverUri.replace(/:.*/, '')
63+
64+
return url.format({
65+
protocol: protocol || req.protocol,
66+
host: req.get('host'),
67+
pathname: url.resolve(req.baseUrl, req.path),
68+
query: req.query
69+
})
70+
}
71+
72+
writeCommonHeaders ({res}) {
73+
// set headers in common to all LDP responses
74+
}
5175
}
5276

5377
/**
5478
* Checks to see if target exists
5579
*/
56-
class LdpHeadOperation extends LdpOperation {}
80+
class LdpHeadOperation extends LdpOperation {
81+
writeHeaders ({res, result, permissions}) {
82+
this.writeCommonHeaders({res})
83+
// write HEAD-specific headers
84+
}
85+
}
5786

5887
/**
5988
* Use cases to handle:
60-
* - LdpListContainerRequest, if target is an ldp container
89+
* - "List Container" request, if target is an ldp container
6190
* - However, if Accept: header is HTML, check if index.html exists
6291
* - Need to also support similar use case if index.ttl exists
63-
* - Otherwise, plain LdpGetRequest
92+
* - "Glob pattern request" if glob magic chars are detected (ie `*`)
93+
* - Otherwise, plain Get request
6494
*/
6595
class LdpGetOperation extends LdpOperation {}
6696

0 commit comments

Comments
 (0)