diff --git a/lib/server/WebDAVServer.d.ts b/lib/server/WebDAVServer.d.ts index d84ebc2a..afb06c3f 100644 --- a/lib/server/WebDAVServer.d.ts +++ b/lib/server/WebDAVServer.d.ts @@ -9,6 +9,7 @@ import { FSManager, FSPath } from '../manager/FSManager'; import { IUserManager } from '../user/IUserManager'; import * as http from 'http'; export { WebDAVServerOptions } from './WebDAVServerOptions'; +export declare type WebDAVServerStartCallback = (server?: http.Server) => void; export interface IResourceTreeNode { r?: IResource; resource?: IResource; @@ -33,7 +34,9 @@ export declare class WebDAVServer { addResourceTree(resoureceTree: ResourceTreeNode, callback: (e: Error) => void): any; addResourceTree(rootResource: IResource, resoureceTree: ResourceTreeNode, callback: (e: Error) => void): any; onUnknownMethod(unknownMethod: WebDAVRequest): void; - start(port?: number, callback?: () => void): void; + start(port: number): any; + start(callback: WebDAVServerStartCallback): any; + start(port: number, callback: WebDAVServerStartCallback): any; stop(callback: () => void): void; load(obj: SerializedObject, managers: FSManager[], callback: (error: Error) => void): void; save(callback: (error: Error, obj: any) => void): void; diff --git a/lib/server/WebDAVServer.js b/lib/server/WebDAVServer.js index de3d3461..6587a5c2 100644 --- a/lib/server/WebDAVServer.js +++ b/lib/server/WebDAVServer.js @@ -151,49 +151,70 @@ var WebDAVServer = (function () { }; WebDAVServer.prototype.start = function (port, callback) { var _this = this; - if (port === void 0) { port = this.options.port; } - this.server = http.createServer(function (req, res) { - var method = _this.methods[_this.normalizeMethodName(req.method)]; - if (!method) - method = _this.unknownMethod; - WebDAVRequest_1.MethodCallArgs.create(_this, req, res, function (e, base) { - if (e) { - if (e === Errors_1.Errors.AuenticationPropertyMissing) - base.setCode(WebDAVRequest_1.HTTPCodes.Forbidden); - else - base.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError); - res.end(); - return; - } - if (!method.chunked) { - var data_1 = ''; - var go_1 = function () { - base.data = data_1; - _this.invokeBeforeRequest(base, function () { - base.exit = function () { - res.end(); - _this.invokeAfterRequest(base, null); - }; - method(base, base.exit); - }); - }; - if (base.contentLength === 0) { - go_1(); + var _port = this.options.port; + var _callback; + if (port && port.constructor === Number) { + _port = port; + if (callback) { + if (callback instanceof Function) + _callback = callback; + else + throw new Error('Illegal arguments'); + } + } + else if (port && port.constructor === Function) { + _port = this.options.port; + _callback = port; + if (callback) + throw new Error('Illegal arguments'); + } + if (!this.server) { + this.server = http.createServer(function (req, res) { + var method = _this.methods[_this.normalizeMethodName(req.method)]; + if (!method) + method = _this.unknownMethod; + WebDAVRequest_1.MethodCallArgs.create(_this, req, res, function (e, base) { + if (e) { + if (e === Errors_1.Errors.AuenticationPropertyMissing) + base.setCode(WebDAVRequest_1.HTTPCodes.Forbidden); + else + base.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError); + res.end(); + return; } - else { - req.on('data', function (chunk) { - data_1 += chunk.toString(); - if (data_1.length >= base.contentLength) { - if (data_1.length > base.contentLength) - data_1 = data_1.substring(0, base.contentLength); - go_1(); - } - }); + if (!method.chunked) { + var data_1 = ''; + var go_1 = function () { + base.data = data_1; + _this.invokeBeforeRequest(base, function () { + base.exit = function () { + res.end(); + _this.invokeAfterRequest(base, null); + }; + method(base, base.exit); + }); + }; + if (base.contentLength === 0) { + go_1(); + } + else { + req.on('data', function (chunk) { + data_1 += chunk.toString(); + if (data_1.length >= base.contentLength) { + if (data_1.length > base.contentLength) + data_1 = data_1.substring(0, base.contentLength); + go_1(); + } + }); + } } - } + }); }); + } + this.server.listen(_port, this.options.hostname, function () { + if (_callback) + _callback(_this.server); }); - this.server.listen(port, this.options.hostname, callback); }; WebDAVServer.prototype.stop = function (callback) { if (this.server) { diff --git a/src/server/WebDAVServer.ts b/src/server/WebDAVServer.ts index 134be9c3..dd63c512 100644 --- a/src/server/WebDAVServer.ts +++ b/src/server/WebDAVServer.ts @@ -15,6 +15,8 @@ import * as http from 'http' export { WebDAVServerOptions } from './WebDAVServerOptions' +export type WebDAVServerStartCallback = (server ?: http.Server) => void; + export interface IResourceTreeNode { r ?: IResource @@ -225,61 +227,93 @@ export class WebDAVServer this.unknownMethod = unknownMethod; } - start(port : number = this.options.port, callback ?: () => void) + start(port : number) + start(callback : WebDAVServerStartCallback) + start(port : number, callback : WebDAVServerStartCallback) + start(port ?: number | WebDAVServerStartCallback, callback ?: WebDAVServerStartCallback) { - this.server = http.createServer((req : http.IncomingMessage, res : http.ServerResponse) => + let _port : number = this.options.port; + let _callback : WebDAVServerStartCallback; + + if(port && port.constructor === Number) + { + _port = port as number; + if(callback) + { + if(callback instanceof Function) + _callback = callback; + else + throw new Error('Illegal arguments'); + } + } + else if(port && port.constructor === Function) { - let method : WebDAVRequest = this.methods[this.normalizeMethodName(req.method)]; - if(!method) - method = this.unknownMethod; + _port = this.options.port; + _callback = port as WebDAVServerStartCallback; + if(callback) + throw new Error('Illegal arguments'); + } - MethodCallArgs.create(this, req, res, (e, base) => { - if(e) - { - if(e === Errors.AuenticationPropertyMissing) - base.setCode(HTTPCodes.Forbidden); - else - base.setCode(HTTPCodes.InternalServerError); - res.end(); - return; - } + if(!this.server) + { + this.server = http.createServer((req : http.IncomingMessage, res : http.ServerResponse) => + { + let method : WebDAVRequest = this.methods[this.normalizeMethodName(req.method)]; + if(!method) + method = this.unknownMethod; - if(!method.chunked) - { - let data = ''; - const go = () => - { - base.data = data; - this.invokeBeforeRequest(base, () => { - base.exit = () => - { - res.end(); - this.invokeAfterRequest(base, null); - }; - method(base, base.exit); - }) - } - - if(base.contentLength === 0) + MethodCallArgs.create(this, req, res, (e, base) => { + if(e) { - go(); + if(e === Errors.AuenticationPropertyMissing) + base.setCode(HTTPCodes.Forbidden); + else + base.setCode(HTTPCodes.InternalServerError); + res.end(); + return; } - else + + if(!method.chunked) { - req.on('data', (chunk) => { - data += chunk.toString(); - if(data.length >= base.contentLength) - { - if(data.length > base.contentLength) - data = data.substring(0, base.contentLength); - go(); - } - }); + let data = ''; + const go = () => + { + base.data = data; + this.invokeBeforeRequest(base, () => { + base.exit = () => + { + res.end(); + this.invokeAfterRequest(base, null); + }; + method(base, base.exit); + }) + } + + if(base.contentLength === 0) + { + go(); + } + else + { + req.on('data', (chunk) => { + data += chunk.toString(); + if(data.length >= base.contentLength) + { + if(data.length > base.contentLength) + data = data.substring(0, base.contentLength); + go(); + } + }); + } } - } + }) }) - }) - this.server.listen(port, this.options.hostname, callback); + } + + this.server.listen(_port, this.options.hostname, () => { + if(_callback) + _callback(this.server); + }); } stop(callback : () => void)