Skip to content

Commit

Permalink
Splitted the 'RequestContext' class into 'RequestContext', 'ExternalR…
Browse files Browse the repository at this point in the history
…equestContext' and 'HTTPRequestContext'
  • Loading branch information
AdrienCastex committed Jun 30, 2017
1 parent 29086af commit faf8581
Show file tree
Hide file tree
Showing 61 changed files with 337 additions and 299 deletions.
43 changes: 27 additions & 16 deletions lib/server/v2/RequestContext.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import { Path } from '../../manager/v2/Path';
import { IUser } from '../../user/v2/IUser';
import * as http from 'http';
export declare class RequestContextHeaders {
protected request: http.IncomingMessage;
protected headers: {
[name: string]: string;
};
contentLength: number;
isSource: boolean;
depth: number;
host: string;
constructor(request: http.IncomingMessage);
constructor(headers: {
[name: string]: string;
});
find(name: string, defaultValue?: string): string;
findBestAccept(defaultType?: string): string;
}
Expand All @@ -36,31 +40,38 @@ export declare class DefaultRequestContextExternalOptions implements RequestCont
user: IUser;
}
export declare class RequestContext {
requested: RequestedResource;
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;
getResourceSync(path?: Path | string): Resource;
fullUri(uri?: string): string;
prefixUri(): string;
}
export declare class ExternalRequestContext extends RequestContext {
static create(server: WebDAVServer): ExternalRequestContext;
static create(server: WebDAVServer, callback: (error: Error, ctx: ExternalRequestContext) => void): ExternalRequestContext;
static create(server: WebDAVServer, options: RequestContextExternalOptions): ExternalRequestContext;
static create(server: WebDAVServer, options: RequestContextExternalOptions, callback: (error: Error, ctx: ExternalRequestContext) => void): ExternalRequestContext;
}
export declare class HTTPRequestContext extends RequestContext {
responseBody: string;
request: http.IncomingMessage;
response: http.ServerResponse;
exit: () => void;
responseBody: string;
requested: RequestedResource;
headers: RequestContextHeaders;
user: IUser;
protected constructor(server: WebDAVServer, request: http.IncomingMessage, response: http.ServerResponse, exit: () => void);
static createExternal(server: WebDAVServer): RequestContext;
static createExternal(server: WebDAVServer, callback: (error: Error, ctx: RequestContext) => void): RequestContext;
static createExternal(server: WebDAVServer, options: RequestContextExternalOptions): RequestContext;
static createExternal(server: WebDAVServer, options: RequestContextExternalOptions, callback: (error: Error, ctx: RequestContext) => void): RequestContext;
static create(server: WebDAVServer, request: http.IncomingMessage, response: http.ServerResponse, callback: (error: Error, ctx: RequestContext) => void): void;
static create(server: WebDAVServer, request: http.IncomingMessage, response: http.ServerResponse, callback: (error: Error, ctx: HTTPRequestContext) => void): void;
noBodyExpected(callback: () => void): void;
checkIfHeader(resource: Resource, callback: () => void): any;
checkIfHeader(fs: FileSystem, path: Path, callback: () => void): any;
askForAuthentication(checkForUser: boolean, callback: (error: Error) => void): void;
getResource(callback: ReturnCallback<Resource>): any;
getResource(path: Path | string, callback: ReturnCallback<Resource>): any;
fullUri(uri?: string): string;
prefixUri(): string;
writeBody(xmlObject: XMLElement | object): void;
setCode(code: number, message?: string): void;
static defaultStatusCode(error: Error): number;
setCodeFromError(error: Error): boolean;
}
export default RequestContext;
126 changes: 75 additions & 51 deletions lib/server/v2/RequestContext.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var XML_1 = require("../../helper/XML");
var IfParser_1 = require("../../helper/v2/IfParser");
Expand All @@ -8,8 +18,8 @@ var Errors_1 = require("../../Errors");
var http = require("http");
var url = require("url");
var RequestContextHeaders = (function () {
function RequestContextHeaders(request) {
this.request = request;
function RequestContextHeaders(headers) {
this.headers = headers;
this.isSource = this.find('source', 'F').toUpperCase() === 'T' || this.find('translate', 'T').toUpperCase() === 'F';
this.host = this.find('Host', 'localhost');
var depth = this.find('Depth');
Expand All @@ -32,9 +42,9 @@ var RequestContextHeaders = (function () {
RequestContextHeaders.prototype.find = function (name, defaultValue) {
if (defaultValue === void 0) { defaultValue = null; }
name = name.replace(/(-| )/g, '').toLowerCase();
for (var k in this.request.headers)
for (var k in this.headers)
if (k.replace(/(-| )/g, '').toLowerCase() === name) {
var value = this.request.headers[k].trim();
var value = this.headers[k].trim();
if (value.length !== 0)
return value;
}
Expand Down Expand Up @@ -76,20 +86,42 @@ var DefaultRequestContextExternalOptions = (function () {
}());
exports.DefaultRequestContextExternalOptions = DefaultRequestContextExternalOptions;
var RequestContext = (function () {
function RequestContext(server, request, response, exit) {
function RequestContext(server, uri, headers) {
this.headers = new RequestContextHeaders(headers);
this.server = server;
this.request = request;
this.response = response;
this.exit = exit;
this.responseBody = undefined;
this.headers = new RequestContextHeaders(request);
var uri = url.parse(request.url).pathname;
uri = url.parse(uri).pathname;
this.requested = {
uri: uri,
path: new Path_1.Path(uri)
};
}
RequestContext.createExternal = function (server, _options, _callback) {
RequestContext.prototype.getResource = function (_path, _callback) {
var path = _callback ? new Path_1.Path(_path) : this.requested.path;
var callback = _callback ? _callback : _path;
this.server.getResource(this, path, callback);
};
RequestContext.prototype.getResourceSync = function (path) {
path = path ? path : this.requested.path;
return this.server.getResourceSync(this, path);
};
RequestContext.prototype.fullUri = function (uri) {
if (uri === void 0) { uri = null; }
if (!uri)
uri = this.requested.uri;
return (this.prefixUri() + uri).replace(/([^:])\/\//g, '$1/');
};
RequestContext.prototype.prefixUri = function () {
return 'http://' + this.headers.host.replace('/', '');
};
return RequestContext;
}());
exports.RequestContext = RequestContext;
var ExternalRequestContext = (function (_super) {
__extends(ExternalRequestContext, _super);
function ExternalRequestContext() {
return _super !== null && _super.apply(this, arguments) || this;
}
ExternalRequestContext.create = function (server, _options, _callback) {
var defaultValues = new DefaultRequestContextExternalOptions();
var options = _options && _options.constructor !== Function ? _options : defaultValues;
var callback = _callback ? _callback : _options && _options.constructor === Function ? _options : function () { };
Expand All @@ -98,21 +130,28 @@ var RequestContext = (function () {
if (options[name_2] === undefined)
options[name_2] = defaultValues[name_2];
}
var ctx = new RequestContext(server, {
headers: options.headers,
url: options.url
}, null, null);
if (options.user)
var ctx = new ExternalRequestContext(server, options.url, options.headers);
if (options.user) {
ctx.user = options.user;
else
server.httpAuthentication.getUser(ctx, function (e, user) {
ctx.user = options.user;
callback(e, ctx);
});
process.nextTick(function () { return callback(null, ctx); });
}
return ctx;
};
RequestContext.create = function (server, request, response, callback) {
var ctx = new RequestContext(server, request, response, null);
return ExternalRequestContext;
}(RequestContext));
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;
_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);
response.setHeader('DAV', '1,2');
response.setHeader('Server', server.options.serverName + '/' + server.options.version);
ctx.askForAuthentication(false, function (e) {
Expand Down Expand Up @@ -141,22 +180,22 @@ var RequestContext = (function () {
var allowedMethods = [];
for (var name_3 in server.methods) {
var method = server.methods[name_3];
if (!method.isValidFor || method.isValidFor(type))
if (!method.isValidFor || method.isValidFor(ctx, type))
allowedMethods.push(name_3.toUpperCase());
}
response.setHeader('Allow', allowedMethods.join(','));
callback(null, ctx);
}
};
RequestContext.prototype.noBodyExpected = function (callback) {
HTTPRequestContext.prototype.noBodyExpected = function (callback) {
if (this.server.options.strictMode && this.headers.contentLength !== 0) {
this.setCode(HTTPCodes_1.HTTPCodes.UnsupportedMediaType);
this.exit();
}
else
callback();
};
RequestContext.prototype.checkIfHeader = function (_fs, _path, _callback) {
HTTPRequestContext.prototype.checkIfHeader = function (_fs, _path, _callback) {
var _this = this;
var fs = _callback ? _fs : null;
var path = _callback ? _path : null;
Expand All @@ -183,7 +222,7 @@ var RequestContext = (function () {
callback();
});
};
RequestContext.prototype.askForAuthentication = function (checkForUser, callback) {
HTTPRequestContext.prototype.askForAuthentication = function (checkForUser, callback) {
if (checkForUser && this.user !== null && !this.user.isDefaultUser) {
callback(Errors_1.Errors.AlreadyAuthenticated);
return;
Expand All @@ -193,21 +232,7 @@ var RequestContext = (function () {
this.response.setHeader(name_4, auth[name_4]);
callback(null);
};
RequestContext.prototype.getResource = function (_path, _callback) {
var path = _callback ? new Path_1.Path(_path) : this.requested.path;
var callback = _callback ? _callback : _path;
this.server.getResource(this, path, callback);
};
RequestContext.prototype.fullUri = function (uri) {
if (uri === void 0) { uri = null; }
if (!uri)
uri = this.requested.uri;
return (this.prefixUri() + uri).replace(/([^:])\/\//g, '$1/');
};
RequestContext.prototype.prefixUri = function () {
return 'http://' + this.headers.host.replace('/', '');
};
RequestContext.prototype.writeBody = function (xmlObject) {
HTTPRequestContext.prototype.writeBody = function (xmlObject) {
var content = XML_1.XML.toXML(xmlObject);
switch (this.headers.findBestAccept()) {
default:
Expand All @@ -225,7 +250,7 @@ var RequestContext = (function () {
}
this.responseBody = content;
};
RequestContext.prototype.setCode = function (code, message) {
HTTPRequestContext.prototype.setCode = function (code, message) {
if (!message)
message = http.STATUS_CODES[code];
if (!message) {
Expand All @@ -236,7 +261,7 @@ var RequestContext = (function () {
this.response.statusMessage = message;
}
};
RequestContext.defaultStatusCode = function (error) {
HTTPRequestContext.defaultStatusCode = function (error) {
var code = null;
if (error === Errors_1.Errors.ResourceNotFound)
code = HTTPCodes_1.HTTPCodes.NotFound;
Expand All @@ -258,14 +283,13 @@ var RequestContext = (function () {
code = HTTPCodes_1.HTTPCodes.Forbidden;
return code;
};
RequestContext.prototype.setCodeFromError = function (error) {
var code = RequestContext.defaultStatusCode(error);
HTTPRequestContext.prototype.setCodeFromError = function (error) {
var code = HTTPRequestContext.defaultStatusCode(error);
if (!code)
return false;
this.setCode(code);
return true;
};
return RequestContext;
}());
exports.RequestContext = RequestContext;
exports.default = RequestContext;
return HTTPRequestContext;
}(RequestContext));
exports.HTTPRequestContext = HTTPRequestContext;
10 changes: 5 additions & 5 deletions lib/server/v2/WebDAVRequest.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/// <reference types="node" />
import { ResourceType } from '../../manager/v2/fileSystem/CommonTypes';
import { RequestContext } from './RequestContext';
import { HTTPRequestContext } from './RequestContext';
import { Readable } from 'stream';
export { RequestContext } from './RequestContext';
export { HTTPRequestContext } from './RequestContext';
export { HTTPCodes } from '../HTTPCodes';
export interface HTTPMethod {
unchunked?(ctx: RequestContext, data: Buffer, callback: () => void): void;
chunked?(ctx: RequestContext, inputStream: Readable, callback: () => void): void;
isValidFor?(type?: ResourceType): boolean;
unchunked?(ctx: HTTPRequestContext, data: Buffer, callback: () => void): void;
chunked?(ctx: HTTPRequestContext, inputStream: Readable, callback: () => void): void;
isValidFor?(ctx: HTTPRequestContext, type?: ResourceType): boolean;
}
2 changes: 1 addition & 1 deletion lib/server/v2/WebDAVRequest.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var RequestContext_1 = require("./RequestContext");
exports.RequestContext = RequestContext_1.RequestContext;
exports.HTTPRequestContext = RequestContext_1.HTTPRequestContext;
var HTTPCodes_1 = require("../HTTPCodes");
exports.HTTPCodes = HTTPCodes_1.HTTPCodes;
6 changes: 3 additions & 3 deletions lib/server/v2/commands/Copy.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference types="node" />
import { HTTPMethod, RequestContext } from '../WebDAVRequest';
import { HTTPMethod, HTTPRequestContext } from '../WebDAVRequest';
import { ResourceType } from '../../../manager/v2/fileSystem/CommonTypes';
export default class implements HTTPMethod {
unchunked(ctx: RequestContext, data: Buffer, callback: () => void): void;
isValidFor(type: ResourceType): boolean;
unchunked(ctx: HTTPRequestContext, data: Buffer, callback: () => void): void;
isValidFor(ctx: HTTPRequestContext, type: ResourceType): boolean;
}
2 changes: 1 addition & 1 deletion lib/server/v2/commands/Copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var default_1 = (function () {
default_1.prototype.unchunked = function (ctx, data, callback) {
Move_1.execute(ctx, 'copy', 'canCopy', callback);
};
default_1.prototype.isValidFor = function (type) {
default_1.prototype.isValidFor = function (ctx, type) {
return !!type;
};
return default_1;
Expand Down
6 changes: 3 additions & 3 deletions lib/server/v2/commands/Delete.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference types="node" />
import { RequestContext, HTTPMethod } from '../WebDAVRequest';
import { HTTPRequestContext, HTTPMethod } from '../WebDAVRequest';
import { ResourceType } from '../../../manager/v2/fileSystem/CommonTypes';
export default class implements HTTPMethod {
unchunked(ctx: RequestContext, data: Buffer, callback: () => void): void;
isValidFor(type: ResourceType): boolean;
unchunked(ctx: HTTPRequestContext, data: Buffer, callback: () => void): void;
isValidFor(ctx: HTTPRequestContext, type: ResourceType): boolean;
}
2 changes: 1 addition & 1 deletion lib/server/v2/commands/Delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var default_1 = (function () {
});
});
};
default_1.prototype.isValidFor = function (type) {
default_1.prototype.isValidFor = function (ctx, type) {
return !!type;
};
return default_1;
Expand Down
6 changes: 3 additions & 3 deletions lib/server/v2/commands/Get.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference types="node" />
import { RequestContext, HTTPMethod } from '../WebDAVRequest';
import { HTTPRequestContext, HTTPMethod } from '../WebDAVRequest';
import { ResourceType } from '../../../manager/v2/fileSystem/CommonTypes';
export default class implements HTTPMethod {
unchunked(ctx: RequestContext, data: Buffer, callback: () => void): void;
isValidFor(type: ResourceType): boolean;
unchunked(ctx: HTTPRequestContext, data: Buffer, callback: () => void): void;
isValidFor(ctx: HTTPRequestContext, type: ResourceType): boolean;
}
2 changes: 1 addition & 1 deletion lib/server/v2/commands/Get.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ var default_1 = (function () {
});
});
};
default_1.prototype.isValidFor = function (type) {
default_1.prototype.isValidFor = function (ctx, type) {
return type && type.isFile;
};
return default_1;
Expand Down
6 changes: 3 additions & 3 deletions lib/server/v2/commands/Head.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference types="node" />
import { HTTPMethod, RequestContext } from '../WebDAVRequest';
import { HTTPMethod, HTTPRequestContext } from '../WebDAVRequest';
import { ResourceType } from '../../../manager/v2/fileSystem/CommonTypes';
export default class implements HTTPMethod {
unchunked(ctx: RequestContext, data: Buffer, callback: () => void): void;
isValidFor(type: ResourceType): boolean;
unchunked(ctx: HTTPRequestContext, data: Buffer, callback: () => void): void;
isValidFor(ctx: HTTPRequestContext, type: ResourceType): boolean;
}
Loading

0 comments on commit faf8581

Please sign in to comment.