Skip to content

Commit

Permalink
Added support for a new root path in 'executeRequest'
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed Nov 22, 2017
1 parent 7777cb4 commit 876629a
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 26 deletions.
11 changes: 7 additions & 4 deletions lib/server/v2/RequestContext.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface RequestedResource {
uri: string;
}
export interface RequestContextExternalOptions {
rootPath?: string;
headers?: {
[name: string]: string;
};
Expand All @@ -42,14 +43,15 @@ export declare class DefaultRequestContextExternalOptions implements RequestCont
export declare class RequestContext {
overridePrivileges: boolean;
requested: RequestedResource;
rootPath: string;
headers: RequestContextHeaders;
server: WebDAVServer;
user: IUser;
protected constructor(server: WebDAVServer, uri: string, headers: {
[name: string]: string;
});
getResource(callback: ReturnCallback<Resource>): any;
getResource(path: Path | string, callback: ReturnCallback<Resource>): any;
}, rootPath?: string);
getResource(callback: ReturnCallback<Resource>): void;
getResource(path: Path | string, callback: ReturnCallback<Resource>): void;
getResourceSync(path?: Path | string): Resource;
fullUri(uri?: string): string;
prefixUri(): string;
Expand All @@ -65,8 +67,9 @@ export declare class HTTPRequestContext extends RequestContext {
request: http.IncomingMessage;
response: http.ServerResponse;
exit: () => void;
protected constructor(server: WebDAVServer, request: http.IncomingMessage, response: http.ServerResponse, exit: () => void);
protected constructor(server: WebDAVServer, request: http.IncomingMessage, response: http.ServerResponse, exit: () => void, rootPath?: string);
static create(server: WebDAVServer, request: http.IncomingMessage, response: http.ServerResponse, callback: (error: Error, ctx: HTTPRequestContext) => void): void;
static create(server: WebDAVServer, request: http.IncomingMessage, response: http.ServerResponse, rootPath: string, callback: (error: Error, ctx: HTTPRequestContext) => void): void;
static encodeURL(url: string): string;
noBodyExpected(callback: () => void): void;
checkIfHeader(resource: Resource, callback: () => void): any;
Expand Down
20 changes: 14 additions & 6 deletions lib/server/v2/RequestContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ var DefaultRequestContextExternalOptions = (function () {
}());
exports.DefaultRequestContextExternalOptions = DefaultRequestContextExternalOptions;
var RequestContext = (function () {
function RequestContext(server, uri, headers) {
function RequestContext(server, uri, headers, rootPath) {
this.overridePrivileges = false;
this.rootPath = rootPath;
this.headers = new RequestContextHeaders(headers);
this.server = server;
uri = url.parse(uri).pathname;
Expand All @@ -96,6 +97,11 @@ var RequestContext = (function () {
uri: uri,
path: new Path_1.Path(uri)
};
if (this.rootPath) {
this.rootPath = new Path_1.Path(this.rootPath).toString(false);
if (this.rootPath === '/')
this.rootPath = undefined;
}
}
RequestContext.prototype.getResource = function (_path, _callback) {
var path = _callback ? new Path_1.Path(_path) : this.requested.path;
Expand All @@ -113,7 +119,7 @@ var RequestContext = (function () {
return (this.prefixUri() + uri).replace(/([^:])\/\//g, '$1/');
};
RequestContext.prototype.prefixUri = function () {
return 'http://' + this.headers.host.replace('/', '');
return 'http://' + this.headers.host.replace('/', '') + (this.rootPath ? this.rootPath : '');
};
return RequestContext;
}());
Expand Down Expand Up @@ -144,16 +150,18 @@ var ExternalRequestContext = (function (_super) {
exports.ExternalRequestContext = ExternalRequestContext;
var HTTPRequestContext = (function (_super) {
__extends(HTTPRequestContext, _super);
function HTTPRequestContext(server, request, response, exit) {
var _this = _super.call(this, server, request.url, request.headers) || this;
function HTTPRequestContext(server, request, response, exit, rootPath) {
var _this = _super.call(this, server, request.url, request.headers, rootPath) || this;
_this.responseBody = undefined;
_this.response = response;
_this.request = request;
_this.exit = exit;
return _this;
}
HTTPRequestContext.create = function (server, request, response, callback) {
var ctx = new HTTPRequestContext(server, request, response, null);
HTTPRequestContext.create = function (server, request, response, _rootPath, _callback) {
var rootPath = _callback ? _rootPath : undefined;
var callback = _callback ? _callback : _rootPath;
var ctx = new HTTPRequestContext(server, request, response, null, rootPath);
response.setHeader('DAV', '1,2');
response.setHeader('Access-Control-Allow-Origin', '*');
response.setHeader('Access-Control-Allow-Credentials', 'true');
Expand Down
2 changes: 1 addition & 1 deletion lib/server/v2/webDAVServer/StartStop.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference types="node" />
import { WebDAVServerStartCallback } from './WebDAVServer';
import * as http from 'http';
export declare function executeRequest(req: http.IncomingMessage, res: http.ServerResponse): void;
export declare function executeRequest(req: http.IncomingMessage, res: http.ServerResponse, rootPath?: string): void;
export declare function start(port?: number | WebDAVServerStartCallback, callback?: WebDAVServerStartCallback): void;
export declare function stop(callback: () => void): void;
4 changes: 2 additions & 2 deletions lib/server/v2/webDAVServer/StartStop.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ var WebDAVRequest_1 = require("../WebDAVRequest");
var Errors_1 = require("../../../Errors");
var https = require("https");
var http = require("http");
function executeRequest(req, res) {
function executeRequest(req, res, rootPath) {
var _this = this;
var method = this.methods[this.normalizeMethodName(req.method)];
if (!method)
method = this.unknownMethod;
WebDAVRequest_1.HTTPRequestContext.create(this, req, res, function (e, base) {
WebDAVRequest_1.HTTPRequestContext.create(this, req, res, rootPath, function (e, base) {
if (e) {
if (e === Errors_1.Errors.AuenticationPropertyMissing || e === Errors_1.Errors.MissingAuthorisationHeader || e === Errors_1.Errors.BadAuthentication || e === Errors_1.Errors.WrongHeaderFormat)
base.setCode(WebDAVRequest_1.HTTPCodes.Unauthorized);
Expand Down
38 changes: 27 additions & 11 deletions src/server/v2/RequestContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export interface RequestedResource

export interface RequestContextExternalOptions
{
rootPath ?: string
headers ?: { [name : string] : string }
url ?: string
user ?: IUser
Expand All @@ -111,13 +112,15 @@ export class RequestContext
{
overridePrivileges : boolean
requested : RequestedResource
rootPath : string
headers : RequestContextHeaders
server : WebDAVServer
user : IUser

protected constructor(server : WebDAVServer, uri : string, headers : { [name : string] : string })
protected constructor(server : WebDAVServer, uri : string, headers : { [name : string] : string }, rootPath ?: string)
{
this.overridePrivileges = false;
this.rootPath = rootPath;
this.headers = new RequestContextHeaders(headers);
this.server = server;

Expand All @@ -127,11 +130,18 @@ export class RequestContext
uri,
path: new Path(uri)
};

if(this.rootPath)
{
this.rootPath = new Path(this.rootPath).toString(false);
if(this.rootPath === '/')
this.rootPath = undefined;
}
}

getResource(callback : ReturnCallback<Resource>)
getResource(path : Path | string, callback : ReturnCallback<Resource>)
getResource(_path : Path | string | ReturnCallback<Resource>, _callback ?: ReturnCallback<Resource>)
getResource(callback : ReturnCallback<Resource>) : void
getResource(path : Path | string, callback : ReturnCallback<Resource>) : void
getResource(_path : Path | string | ReturnCallback<Resource>, _callback ?: ReturnCallback<Resource>) : void
{
const path = _callback ? new Path(_path as Path | string) : this.requested.path;
const callback = _callback ? _callback : _path as ReturnCallback<Resource>;
Expand All @@ -145,17 +155,17 @@ export class RequestContext
return this.server.getResourceSync(this, path);
}

fullUri(uri : string = null)
fullUri(uri : string = null) : string
{
if(!uri)
uri = this.requested.uri;

return (this.prefixUri() + uri).replace(/([^:])\/\//g, '$1/');
}

prefixUri()
prefixUri() : string
{
return 'http://' + this.headers.host.replace('/', '');
return 'http://' + this.headers.host.replace('/', '') + (this.rootPath ? this.rootPath : '');
}
}

Expand Down Expand Up @@ -202,19 +212,25 @@ export class HTTPRequestContext extends RequestContext
server : WebDAVServer,
request : http.IncomingMessage,
response : http.ServerResponse,
exit : () => void
exit : () => void,
rootPath ?: string
) {
super(server, request.url, request.headers);
super(server, request.url, request.headers, rootPath);

this.responseBody = undefined;
this.response = response;
this.request = request;
this.exit = exit;
}

static create(server : WebDAVServer, request : http.IncomingMessage, response : http.ServerResponse, callback : (error : Error, ctx : HTTPRequestContext) => void)
static create(server : WebDAVServer, request : http.IncomingMessage, response : http.ServerResponse, callback : (error : Error, ctx : HTTPRequestContext) => void) : void
static create(server : WebDAVServer, request : http.IncomingMessage, response : http.ServerResponse, rootPath : string, callback : (error : Error, ctx : HTTPRequestContext) => void) : void
static create(server : WebDAVServer, request : http.IncomingMessage, response : http.ServerResponse, _rootPath : string | ((error : Error, ctx : HTTPRequestContext) => void), _callback ?: (error : Error, ctx : HTTPRequestContext) => void) : void
{
const ctx = new HTTPRequestContext(server, request, response, null);
const rootPath = _callback ? _rootPath as string : undefined;
const callback = _callback ? _callback : _rootPath as ((error : Error, ctx : HTTPRequestContext) => void);

const ctx = new HTTPRequestContext(server, request, response, null, rootPath);
response.setHeader('DAV', '1,2');
response.setHeader('Access-Control-Allow-Origin', '*');
response.setHeader('Access-Control-Allow-Credentials', 'true');
Expand Down
4 changes: 2 additions & 2 deletions src/server/v2/webDAVServer/StartStop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import * as http from 'http'
import * as zlib from 'zlib'
import * as fs from 'fs'

export function executeRequest(req : http.IncomingMessage, res : http.ServerResponse) : void
export function executeRequest(req : http.IncomingMessage, res : http.ServerResponse, rootPath ?: string) : void
{
let method : HTTPMethod = this.methods[this.normalizeMethodName(req.method)];
if(!method)
method = this.unknownMethod;

HTTPRequestContext.create(this, req, res, (e, base) => {
HTTPRequestContext.create(this, req, res, rootPath, (e, base) => {
if(e)
{
if(e === Errors.AuenticationPropertyMissing || e === Errors.MissingAuthorisationHeader || e === Errors.BadAuthentication || e === Errors.WrongHeaderFormat)
Expand Down

0 comments on commit 876629a

Please sign in to comment.