/
HttpServerFileUploadImpl.java
212 lines (184 loc) · 5.16 KB
/
HttpServerFileUploadImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright (c) 2011-2017 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.core.http.impl;
import io.vertx.core.Context;
import io.vertx.core.Handler;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.file.AsyncFile;
import io.vertx.core.file.OpenOptions;
import io.vertx.core.http.HttpServerFileUpload;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.streams.Pump;
import io.vertx.core.streams.impl.InboundBuffer;
import java.nio.charset.Charset;
/**
* This class is optimised for performance when used on the same event loop that is was passed to the handler with.
* However it can be used safely from other threads.
*
* The internal state is protected using the synchronized keyword. If always used on the same event loop, then
* we benefit from biased locking which makes the overhead of synchronized near zero.
*
* @author <a href="mailto:nmaurer@redhat.com">Norman Maurer</a>
*/
class HttpServerFileUploadImpl implements HttpServerFileUpload {
private final HttpServerRequest req;
private final Context context;
private final String name;
private final String filename;
private final String contentType;
private final String contentTransferEncoding;
private final Charset charset;
private Handler<Void> endHandler;
private AsyncFile file;
private Handler<Throwable> exceptionHandler;
private long size;
private InboundBuffer<Buffer> pending;
private boolean complete;
private boolean lazyCalculateSize;
HttpServerFileUploadImpl(Context context, HttpServerRequest req, String name, String filename, String contentType,
String contentTransferEncoding,
Charset charset, long size) {
this.context = context;
this.req = req;
this.name = name;
this.filename = filename;
this.contentType = contentType;
this.contentTransferEncoding = contentTransferEncoding;
this.charset = charset;
this.size = size;
this.pending = new InboundBuffer<Buffer>(context)
.emptyHandler(v -> {
if (complete) {
handleComplete();
}
});
if (size == 0) {
lazyCalculateSize = true;
}
}
@Override
public String filename() {
return filename;
}
@Override
public String name() {
return name;
}
@Override
public String contentType() {
return contentType;
}
@Override
public String contentTransferEncoding() {
return contentTransferEncoding;
}
@Override
public String charset() {
return charset.toString();
}
@Override
public synchronized long size() {
return size;
}
@Override
public HttpServerFileUpload handler(Handler<Buffer> handler) {
pending.handler(handler);
return this;
}
@Override
public HttpServerFileUpload pause() {
pending.pause();
return this;
}
@Override
public HttpServerFileUpload fetch(long amount) {
pending.resume();
return this;
}
@Override
public HttpServerFileUpload resume() {
pending.resume();
return this;
}
@Override
public synchronized HttpServerFileUpload exceptionHandler(Handler<Throwable> exceptionHandler) {
this.exceptionHandler = exceptionHandler;
return this;
}
@Override
public synchronized HttpServerFileUpload endHandler(Handler<Void> handler) {
this.endHandler = handler;
return this;
}
@Override
public HttpServerFileUpload streamToFileSystem(String filename) {
pause();
context.owner().fileSystem().open(filename, new OpenOptions(), ar -> {
if (ar.succeeded()) {
file = ar.result();
Pump p = Pump.pump(HttpServerFileUploadImpl.this, ar.result());
p.start();
resume();
} else {
notifyExceptionHandler(ar.cause());
}
});
return this;
}
@Override
public synchronized boolean isSizeAvailable() {
return !lazyCalculateSize;
}
synchronized void receiveData(Buffer data) {
if (data.length() != 0) {
// Can sometimes receive zero length packets from Netty!
if (lazyCalculateSize) {
size += data.length();
}
doReceiveData(data);
}
}
synchronized void doReceiveData(Buffer data) {
if (!pending.write(data)) {
req.pause();
}
}
synchronized void complete() {
if (pending.isEmpty()) {
handleComplete();
} else {
complete = true;
}
}
private void handleComplete() {
lazyCalculateSize = false;
if (file == null) {
notifyEndHandler();
} else {
file.close(ar -> {
if (ar.failed()) {
notifyExceptionHandler(ar.cause());
}
notifyEndHandler();
});
}
}
private void notifyEndHandler() {
if (endHandler != null) {
endHandler.handle(null);
}
}
private void notifyExceptionHandler(Throwable cause) {
if (exceptionHandler != null) {
exceptionHandler.handle(cause);
}
}
}