|
| 1 | +import { HTTPCodes, MethodCallArgs, WebDAVRequest, ChunkOnDataCallback } from './WebDAVRequest' |
1 | 2 | import { WebDAVServerOptions, setDefaultServerOptions } from './WebDAVServerOptions' |
2 | 3 | import { SerializedObject, unserialize, serialize } from '../manager/ISerializer' |
3 | | -import { HTTPCodes, MethodCallArgs, WebDAVRequest } from './WebDAVRequest' |
4 | 4 | import { IResource, ReturnCallback } from '../resource/IResource' |
5 | 5 | import { FakePrivilegeManager } from '../user/privilege/FakePrivilegeManager' |
6 | 6 | import { HTTPAuthentication } from '../user/authentication/HTTPAuthentication' |
7 | 7 | import { IPrivilegeManager } from '../user/privilege/IPrivilegeManager' |
8 | 8 | import { SimpleUserManager } from '../user/simple/SimpleUserManager' |
9 | 9 | import { FSManager, FSPath } from '../manager/FSManager' |
| 10 | +import { Errors, HTTPError } from '../Errors' |
10 | 11 | import { RootResource } from '../resource/std/RootResource' |
11 | 12 | import { IUserManager } from '../user/IUserManager' |
12 | | -import { Errors } from '../Errors' |
13 | 13 | import Commands from './commands/Commands' |
14 | 14 | import * as http from 'http' |
15 | 15 |
|
@@ -273,39 +273,75 @@ export class WebDAVServer |
273 | 273 | return; |
274 | 274 | } |
275 | 275 |
|
276 | | - if(!method.chunked) |
| 276 | + base.exit = () => |
| 277 | + { |
| 278 | + base.response.end(); |
| 279 | + this.invokeAfterRequest(base, null); |
| 280 | + }; |
| 281 | + |
| 282 | + if(!this.options.canChunk || !method.startChunked || base.contentLength <= 0) |
277 | 283 | { |
278 | | - let data = ''; |
279 | 284 | const go = () => |
280 | 285 | { |
281 | | - base.data = data; |
282 | 286 | this.invokeBeforeRequest(base, () => { |
283 | | - base.exit = () => |
284 | | - { |
285 | | - res.end(); |
286 | | - this.invokeAfterRequest(base, null); |
287 | | - }; |
288 | 287 | method(base, base.exit); |
289 | 288 | }) |
290 | 289 | } |
291 | | - |
292 | | - if(base.contentLength === 0) |
| 290 | + |
| 291 | + if(base.contentLength <= 0) |
293 | 292 | { |
| 293 | + base.data = new Buffer(0); |
294 | 294 | go(); |
295 | 295 | } |
296 | 296 | else |
297 | 297 | { |
| 298 | + const data = new Buffer(base.contentLength); |
| 299 | + let index = 0; |
298 | 300 | req.on('data', (chunk) => { |
299 | | - data += chunk.toString(); |
300 | | - if(data.length >= base.contentLength) |
| 301 | + if(chunk.constructor === String) |
| 302 | + chunk = new Buffer(chunk as string); |
| 303 | + |
| 304 | + for(let i = 0; i < chunk.length && index < data.length; ++i, ++index) |
| 305 | + data[index] = (chunk as Buffer)[i]; |
| 306 | + |
| 307 | + if(index >= base.contentLength) |
301 | 308 | { |
302 | | - if(data.length > base.contentLength) |
303 | | - data = data.substring(0, base.contentLength); |
| 309 | + base.data = data; |
304 | 310 | go(); |
305 | 311 | } |
306 | 312 | }); |
307 | 313 | } |
308 | 314 | } |
| 315 | + else |
| 316 | + { |
| 317 | + this.invokeBeforeRequest(base, () => { |
| 318 | + this.invokeBeforeRequest(base, () => { |
| 319 | + method.startChunked(base, (error : HTTPError, onData : ChunkOnDataCallback) => { |
| 320 | + if(error) |
| 321 | + { |
| 322 | + base.setCode(error.HTTPCode); |
| 323 | + base.exit(); |
| 324 | + return; |
| 325 | + } |
| 326 | + |
| 327 | + if(!onData) |
| 328 | + { |
| 329 | + base.exit(); |
| 330 | + return; |
| 331 | + } |
| 332 | + |
| 333 | + let size = 0; |
| 334 | + req.on('data', (chunk) => { |
| 335 | + if(chunk.constructor === String) |
| 336 | + chunk = new Buffer(chunk as string); |
| 337 | + size += chunk.length; |
| 338 | + |
| 339 | + onData(chunk as Buffer, size === chunk.length, size >= base.contentLength); |
| 340 | + }); |
| 341 | + }); |
| 342 | + }) |
| 343 | + }) |
| 344 | + } |
309 | 345 | }) |
310 | 346 | }) |
311 | 347 | } |
|
0 commit comments