Skip to content

Commit

Permalink
Exported the function/method 'executeRequest' to execute requests wit…
Browse files Browse the repository at this point in the history
…hout the server started
  • Loading branch information
AdrienCastex committed Nov 18, 2017
1 parent 209bcef commit fc22c3b
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 107 deletions.
3 changes: 3 additions & 0 deletions lib/server/v2/webDAVServer/StartStop.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +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 start(port?: number | WebDAVServerStartCallback, callback?: WebDAVServerStartCallback): void;
export declare function stop(callback: () => void): void;
95 changes: 49 additions & 46 deletions lib/server/v2/webDAVServer/StartStop.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,54 @@ var WebDAVRequest_1 = require("../WebDAVRequest");
var Errors_1 = require("../../../Errors");
var https = require("https");
var http = require("http");
function executeRequest(req, res) {
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) {
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);
else
base.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
res.end();
return;
}
base.exit = function () {
base.response.end();
_this.invokeAfterRequest(base, null);
};
if (!method.chunked) {
var go_1 = function (data) {
_this.invokeBeforeRequest(base, function () {
method.unchunked(base, data, base.exit);
});
};
if (base.headers.contentLength <= 0) {
go_1(new Buffer(0));
}
else {
var data_1 = new Buffer(base.headers.contentLength);
var index_1 = 0;
req.on('data', function (chunk) {
if (chunk.constructor === String)
chunk = new Buffer(chunk);
for (var i = 0; i < chunk.length && index_1 < data_1.length; ++i, ++index_1)
data_1[index_1] = chunk[i];
if (index_1 >= base.headers.contentLength)
go_1(data_1);
});
}
}
else {
_this.invokeBeforeRequest(base, function () {
method.chunked(base, req, base.exit);
});
}
});
}
exports.executeRequest = executeRequest;
function start(port, callback) {
var _this = this;
var _port = this.options.port;
Expand All @@ -25,52 +73,7 @@ function start(port, callback) {
}
if (!this.server) {
var serverCreator = this.options.https ? function (c) { return https.createServer(_this.options.https, c); } : function (c) { return http.createServer(c); };
this.server = serverCreator(function (req, res) {
var method = _this.methods[_this.normalizeMethodName(req.method)];
if (!method)
method = _this.unknownMethod;
WebDAVRequest_1.HTTPRequestContext.create(_this, req, res, 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);
else
base.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
res.end();
return;
}
base.exit = function () {
base.response.end();
_this.invokeAfterRequest(base, null);
};
if (!method.chunked) {
var go_1 = function (data) {
_this.invokeBeforeRequest(base, function () {
method.unchunked(base, data, base.exit);
});
};
if (base.headers.contentLength <= 0) {
go_1(new Buffer(0));
}
else {
var data_1 = new Buffer(base.headers.contentLength);
var index_1 = 0;
req.on('data', function (chunk) {
if (chunk.constructor === String)
chunk = new Buffer(chunk);
for (var i = 0; i < chunk.length && index_1 < data_1.length; ++i, ++index_1)
data_1[index_1] = chunk[i];
if (index_1 >= base.headers.contentLength)
go_1(data_1);
});
}
}
else {
_this.invokeBeforeRequest(base, function () {
method.chunked(base, req, base.exit);
});
}
});
});
this.server = serverCreator(executeRequest.bind(this));
this.autoSave();
}
this.server.listen(_port, this.options.hostname, function () {
Expand Down
1 change: 1 addition & 0 deletions lib/server/v2/webDAVServer/WebDAVServer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export declare class WebDAVServer {
start(callback: WebDAVServerStartCallback): any;
start(port: number, callback: WebDAVServerStartCallback): any;
stop: typeof startStop.stop;
executeRequest: any;
/**
* Start the auto-save feature of the server. Use the server's options as settings.
*/
Expand Down
2 changes: 2 additions & 0 deletions lib/server/v2/webDAVServer/WebDAVServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ var startStop = require("./StartStop");
var WebDAVServer = (function () {
function WebDAVServer(options) {
this.stop = startStop.stop;
// Execute request
this.executeRequest = startStop.executeRequest.bind(this);
this.autoLoad = persistence.autoLoad;
this.load = persistence.load;
this.save = persistence.save;
Expand Down
122 changes: 62 additions & 60 deletions src/server/v2/webDAVServer/StartStop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,67 @@ 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
{
let method : HTTPMethod = this.methods[this.normalizeMethodName(req.method)];
if(!method)
method = this.unknownMethod;

HTTPRequestContext.create(this, req, res, (e, base) => {
if(e)
{
if(e === Errors.AuenticationPropertyMissing || e === Errors.MissingAuthorisationHeader || e === Errors.BadAuthentication || e === Errors.WrongHeaderFormat)
base.setCode(HTTPCodes.Unauthorized);
else
base.setCode(HTTPCodes.InternalServerError);
res.end();
return;
}

base.exit = () =>
{
base.response.end();
this.invokeAfterRequest(base, null);
};

if(!method.chunked)
{
const go = (data : Buffer) =>
{
this.invokeBeforeRequest(base, () => {
method.unchunked(base, data, base.exit);
})
}

if(base.headers.contentLength <= 0)
{
go(new Buffer(0));
}
else
{
const data = new Buffer(base.headers.contentLength);
let index = 0;
req.on('data', (chunk) => {
if(chunk.constructor === String)
chunk = new Buffer(chunk as string);

for(let i = 0; i < chunk.length && index < data.length; ++i, ++index)
data[index] = (chunk as Buffer)[i];

if(index >= base.headers.contentLength)
go(data);
});
}
}
else
{
this.invokeBeforeRequest(base, () => {
method.chunked(base, req, base.exit);
})
}
})
}

export function start(port ?: number | WebDAVServerStartCallback, callback ?: WebDAVServerStartCallback)
{
let _port : number = this.options.port;
Expand Down Expand Up @@ -37,66 +98,7 @@ export function start(port ?: number | WebDAVServerStartCallback, callback ?: We
if(!this.server)
{
const serverCreator = this.options.https ? (c) => https.createServer(this.options.https, c) : (c) => http.createServer(c);
this.server = serverCreator((req : http.IncomingMessage, res : http.ServerResponse) =>
{
let method : HTTPMethod = this.methods[this.normalizeMethodName(req.method)];
if(!method)
method = this.unknownMethod;

HTTPRequestContext.create(this, req, res, (e, base) => {
if(e)
{
if(e === Errors.AuenticationPropertyMissing || e === Errors.MissingAuthorisationHeader || e === Errors.BadAuthentication || e === Errors.WrongHeaderFormat)
base.setCode(HTTPCodes.Unauthorized);
else
base.setCode(HTTPCodes.InternalServerError);
res.end();
return;
}

base.exit = () =>
{
base.response.end();
this.invokeAfterRequest(base, null);
};

if(!method.chunked)
{
const go = (data : Buffer) =>
{
this.invokeBeforeRequest(base, () => {
method.unchunked(base, data, base.exit);
})
}

if(base.headers.contentLength <= 0)
{
go(new Buffer(0));
}
else
{
const data = new Buffer(base.headers.contentLength);
let index = 0;
req.on('data', (chunk) => {
if(chunk.constructor === String)
chunk = new Buffer(chunk as string);

for(let i = 0; i < chunk.length && index < data.length; ++i, ++index)
data[index] = (chunk as Buffer)[i];

if(index >= base.headers.contentLength)
go(data);
});
}
}
else
{
this.invokeBeforeRequest(base, () => {
method.chunked(base, req, base.exit);
})
}
})
})
this.server = serverCreator(executeRequest.bind(this));

this.autoSave();
}
Expand Down
5 changes: 4 additions & 1 deletion src/server/v2/webDAVServer/WebDAVServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class WebDAVServer
createExternalContext(options : RequestContextExternalOptions, callback : (error : Error, ctx : ExternalRequestContext) => void) : ExternalRequestContext
createExternalContext(_options ?: RequestContextExternalOptions | ((error : Error, ctx : ExternalRequestContext) => void), _callback ?: (error : Error, ctx : ExternalRequestContext) => void) : ExternalRequestContext
{
return ExternalRequestContext.create(this, _options, _callback);
return ExternalRequestContext.create(this, _options as any, _callback);
}

rootFileSystem() : FileSystem
Expand Down Expand Up @@ -292,6 +292,9 @@ export class WebDAVServer
}
stop = startStop.stop

// Execute request
executeRequest = startStop.executeRequest.bind(this)

// Persistence
/**
* Start the auto-save feature of the server. Use the server's options as settings.
Expand Down

0 comments on commit fc22c3b

Please sign in to comment.