Skip to content

Commit

Permalink
Implemented the callback on the start method + Made the arguments of …
Browse files Browse the repository at this point in the history
…the start method optional
  • Loading branch information
AdrienCastex committed May 24, 2017
1 parent 516a7fb commit 880dd8a
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 86 deletions.
5 changes: 4 additions & 1 deletion lib/server/WebDAVServer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
99 changes: 60 additions & 39 deletions lib/server/WebDAVServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
126 changes: 80 additions & 46 deletions src/server/WebDAVServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 880dd8a

Please sign in to comment.