Reapply files form master to keep formatting.
This commit is contained in:
parent
e8b016461c
commit
e469980236
File diff suppressed because it is too large
Load Diff
|
@ -27,15 +27,14 @@ import org.jboss.netty.logging.InternalLoggerFactory;
|
||||||
import org.jboss.netty.util.CharsetUtil;
|
import org.jboss.netty.util.CharsetUtil;
|
||||||
|
|
||||||
public class HttpUploadClientHandler extends SimpleChannelUpstreamHandler {
|
public class HttpUploadClientHandler extends SimpleChannelUpstreamHandler {
|
||||||
|
|
||||||
private static final InternalLogger logger = InternalLoggerFactory
|
private static final InternalLogger logger =
|
||||||
.getInstance(HttpUploadClientHandler.class);
|
InternalLoggerFactory.getInstance(HttpUploadClientHandler.class);
|
||||||
|
|
||||||
private volatile boolean readingChunks;
|
private volatile boolean readingChunks;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
|
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
|
||||||
throws Exception {
|
|
||||||
if (!readingChunks) {
|
if (!readingChunks) {
|
||||||
HttpResponse response = (HttpResponse) e.getMessage();
|
HttpResponse response = (HttpResponse) e.getMessage();
|
||||||
|
|
||||||
|
@ -43,8 +42,8 @@ public class HttpUploadClientHandler extends SimpleChannelUpstreamHandler {
|
||||||
logger.info("VERSION: " + response.getProtocolVersion());
|
logger.info("VERSION: " + response.getProtocolVersion());
|
||||||
|
|
||||||
if (!response.getHeaderNames().isEmpty()) {
|
if (!response.getHeaderNames().isEmpty()) {
|
||||||
for (String name : response.getHeaderNames()) {
|
for (String name: response.getHeaderNames()) {
|
||||||
for (String value : response.getHeaders(name)) {
|
for (String value: response.getHeaders(name)) {
|
||||||
logger.info("HEADER: " + name + " = " + value);
|
logger.info("HEADER: " + name + " = " + value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,8 +41,8 @@ public class HttpUploadClientPipelineFactory implements ChannelPipelineFactory {
|
||||||
|
|
||||||
// Enable HTTPS if necessary.
|
// Enable HTTPS if necessary.
|
||||||
if (ssl) {
|
if (ssl) {
|
||||||
SSLEngine engine = SecureChatSslContextFactory.getClientContext()
|
SSLEngine engine =
|
||||||
.createSSLEngine();
|
SecureChatSslContextFactory.getClientContext().createSSLEngine();
|
||||||
engine.setUseClientMode(true);
|
engine.setUseClientMode(true);
|
||||||
|
|
||||||
pipeline.addLast("ssl", new SslHandler(engine));
|
pipeline.addLast("ssl", new SslHandler(engine));
|
||||||
|
@ -50,13 +50,12 @@ public class HttpUploadClientPipelineFactory implements ChannelPipelineFactory {
|
||||||
|
|
||||||
pipeline.addLast("codec", new HttpClientCodec());
|
pipeline.addLast("codec", new HttpClientCodec());
|
||||||
|
|
||||||
// Remove the following line if you don't want automatic content
|
// Remove the following line if you don't want automatic content decompression.
|
||||||
// decompression.
|
|
||||||
pipeline.addLast("inflater", new HttpContentDecompressor());
|
pipeline.addLast("inflater", new HttpContentDecompressor());
|
||||||
|
|
||||||
// to be used since huge file transfer
|
// to be used since huge file transfer
|
||||||
pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
|
pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
|
||||||
|
|
||||||
pipeline.addLast("handler", new HttpUploadClientHandler());
|
pipeline.addLast("handler", new HttpUploadClientHandler());
|
||||||
return pipeline;
|
return pipeline;
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,13 +22,13 @@ import org.jboss.netty.bootstrap.ServerBootstrap;
|
||||||
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
|
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
|
||||||
|
|
||||||
public class HttpUploadServer {
|
public class HttpUploadServer {
|
||||||
|
|
||||||
private final int port;
|
private final int port;
|
||||||
|
|
||||||
public HttpUploadServer(int port) {
|
public HttpUploadServer(int port) {
|
||||||
this.port = port;
|
this.port = port;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
// Configure the server.
|
// Configure the server.
|
||||||
ServerBootstrap bootstrap = new ServerBootstrap(
|
ServerBootstrap bootstrap = new ServerBootstrap(
|
||||||
|
|
|
@ -63,9 +63,9 @@ import org.jboss.netty.logging.InternalLoggerFactory;
|
||||||
import org.jboss.netty.util.CharsetUtil;
|
import org.jboss.netty.util.CharsetUtil;
|
||||||
|
|
||||||
public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||||
|
|
||||||
private static final InternalLogger logger = InternalLoggerFactory
|
private static final InternalLogger logger =
|
||||||
.getInstance(HttpUploadServerHandler.class);
|
InternalLoggerFactory.getInstance(HttpUploadServerHandler.class);
|
||||||
|
|
||||||
private volatile HttpRequest request;
|
private volatile HttpRequest request;
|
||||||
|
|
||||||
|
@ -96,8 +96,7 @@ public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
|
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
|
||||||
throws Exception {
|
|
||||||
if (!readingChunks) {
|
if (!readingChunks) {
|
||||||
// clean previous FileUpload if Any
|
// clean previous FileUpload if Any
|
||||||
if (decoder != null) {
|
if (decoder != null) {
|
||||||
|
@ -116,18 +115,18 @@ public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||||
responseContent.append("WELCOME TO THE WILD WILD WEB SERVER\r\n");
|
responseContent.append("WELCOME TO THE WILD WILD WEB SERVER\r\n");
|
||||||
responseContent.append("===================================\r\n");
|
responseContent.append("===================================\r\n");
|
||||||
|
|
||||||
responseContent.append("VERSION: "
|
responseContent.append("VERSION: " +
|
||||||
+ request.getProtocolVersion().getText() + "\r\n");
|
request.getProtocolVersion().getText() + "\r\n");
|
||||||
|
|
||||||
responseContent.append("REQUEST_URI: " + request.getUri()
|
responseContent.append("REQUEST_URI: " + request.getUri() +
|
||||||
+ "\r\n\r\n");
|
"\r\n\r\n");
|
||||||
responseContent.append("\r\n\r\n");
|
responseContent.append("\r\n\r\n");
|
||||||
|
|
||||||
// new method
|
// new method
|
||||||
List<Entry<String, String>> headers = request.getHeaders();
|
List<Entry<String, String>> headers = request.getHeaders();
|
||||||
for (Entry<String, String> entry : headers) {
|
for (Entry<String, String> entry: headers) {
|
||||||
responseContent.append("HEADER: " + entry.getKey() + "="
|
responseContent.append("HEADER: " + entry.getKey() + "=" +
|
||||||
+ entry.getValue() + "\r\n");
|
entry.getValue() + "\r\n");
|
||||||
}
|
}
|
||||||
responseContent.append("\r\n\r\n");
|
responseContent.append("\r\n\r\n");
|
||||||
|
|
||||||
|
@ -140,19 +139,19 @@ public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||||
CookieDecoder decoder = new CookieDecoder();
|
CookieDecoder decoder = new CookieDecoder();
|
||||||
cookies = decoder.decode(value);
|
cookies = decoder.decode(value);
|
||||||
}
|
}
|
||||||
for (Cookie cookie : cookies) {
|
for (Cookie cookie: cookies) {
|
||||||
responseContent.append("COOKIE: " + cookie.toString() + "\r\n");
|
responseContent.append("COOKIE: " + cookie.toString() + "\r\n");
|
||||||
}
|
}
|
||||||
responseContent.append("\r\n\r\n");
|
responseContent.append("\r\n\r\n");
|
||||||
|
|
||||||
QueryStringDecoder decoderQuery = new QueryStringDecoder(
|
QueryStringDecoder decoderQuery = new QueryStringDecoder(request
|
||||||
request.getUri());
|
.getUri());
|
||||||
Map<String, List<String>> uriAttributes = decoderQuery
|
Map<String, List<String>> uriAttributes = decoderQuery
|
||||||
.getParameters();
|
.getParameters();
|
||||||
for (String key : uriAttributes.keySet()) {
|
for (String key: uriAttributes.keySet()) {
|
||||||
for (String valuen : uriAttributes.get(key)) {
|
for (String valuen: uriAttributes.get(key)) {
|
||||||
responseContent.append("URI: " + key + "=" + valuen
|
responseContent.append("URI: " + key + "=" + valuen +
|
||||||
+ "\r\n");
|
"\r\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
responseContent.append("\r\n\r\n");
|
responseContent.append("\r\n\r\n");
|
||||||
|
@ -175,10 +174,10 @@ public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
responseContent.append("Is Chunked: " + request.isChunked()
|
responseContent.append("Is Chunked: " + request.isChunked() +
|
||||||
+ "\r\n");
|
"\r\n");
|
||||||
responseContent.append("IsMultipart: " + decoder.isMultipart()
|
responseContent.append("IsMultipart: " + decoder.isMultipart() +
|
||||||
+ "\r\n");
|
"\r\n");
|
||||||
if (request.isChunked()) {
|
if (request.isChunked()) {
|
||||||
// Chunk version
|
// Chunk version
|
||||||
responseContent.append("Chunks: ");
|
responseContent.append("Chunks: ");
|
||||||
|
@ -203,8 +202,7 @@ public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
responseContent.append("o");
|
responseContent.append("o");
|
||||||
// example of reading chunk by chunk (minimize memory usage due to
|
// example of reading chunk by chunk (minimize memory usage due to Factory)
|
||||||
// Factory)
|
|
||||||
readHttpDataChunkByChunk(e.getChannel());
|
readHttpDataChunkByChunk(e.getChannel());
|
||||||
// example of reading only if at the end
|
// example of reading only if at the end
|
||||||
if (chunk.isLast()) {
|
if (chunk.isLast()) {
|
||||||
|
@ -217,7 +215,7 @@ public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Example of reading all InterfaceHttpData from finished transfer
|
* Example of reading all InterfaceHttpData from finished transfer
|
||||||
*
|
*
|
||||||
* @param channel
|
* @param channel
|
||||||
*/
|
*/
|
||||||
private void readHttpDataAllReceive(Channel channel) {
|
private void readHttpDataAllReceive(Channel channel) {
|
||||||
|
@ -232,7 +230,7 @@ public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||||
Channels.close(channel);
|
Channels.close(channel);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (InterfaceHttpData data : datas) {
|
for (InterfaceHttpData data: datas) {
|
||||||
writeHttpData(data);
|
writeHttpData(data);
|
||||||
}
|
}
|
||||||
responseContent.append("\r\n\r\nEND OF CONTENT AT FINAL END\r\n");
|
responseContent.append("\r\n\r\nEND OF CONTENT AT FINAL END\r\n");
|
||||||
|
@ -241,7 +239,7 @@ public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||||
/**
|
/**
|
||||||
* Example of reading request by chunk and getting values from chunk to
|
* Example of reading request by chunk and getting values from chunk to
|
||||||
* chunk
|
* chunk
|
||||||
*
|
*
|
||||||
* @param channel
|
* @param channel
|
||||||
*/
|
*/
|
||||||
private void readHttpDataChunkByChunk(Channel channel) {
|
private void readHttpDataChunkByChunk(Channel channel) {
|
||||||
|
@ -269,25 +267,25 @@ public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||||
} catch (IOException e1) {
|
} catch (IOException e1) {
|
||||||
// Error while reading data from File, only print name and error
|
// Error while reading data from File, only print name and error
|
||||||
e1.printStackTrace();
|
e1.printStackTrace();
|
||||||
responseContent.append("\r\nBODY Attribute: "
|
responseContent.append("\r\nBODY Attribute: " +
|
||||||
+ attribute.getHttpDataType().name() + ": "
|
attribute.getHttpDataType().name() + ": " +
|
||||||
+ attribute.getName() + " Error while reading value: "
|
attribute.getName() + " Error while reading value: " +
|
||||||
+ e1.getMessage() + "\r\n");
|
e1.getMessage() + "\r\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (value.length() > 100) {
|
if (value.length() > 100) {
|
||||||
responseContent.append("\r\nBODY Attribute: "
|
responseContent.append("\r\nBODY Attribute: " +
|
||||||
+ attribute.getHttpDataType().name() + ": "
|
attribute.getHttpDataType().name() + ": " +
|
||||||
+ attribute.getName() + " data too long\r\n");
|
attribute.getName() + " data too long\r\n");
|
||||||
} else {
|
} else {
|
||||||
responseContent.append("\r\nBODY Attribute: "
|
responseContent.append("\r\nBODY Attribute: " +
|
||||||
+ attribute.getHttpDataType().name() + ": "
|
attribute.getHttpDataType().name() + ": " +
|
||||||
+ attribute.toString() + "\r\n");
|
attribute.toString() + "\r\n");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
responseContent.append("\r\nBODY FileUpload: "
|
responseContent.append("\r\nBODY FileUpload: " +
|
||||||
+ data.getHttpDataType().name() + ": " + data.toString()
|
data.getHttpDataType().name() + ": " + data.toString() +
|
||||||
+ "\r\n");
|
"\r\n");
|
||||||
if (data.getHttpDataType() == HttpDataType.FileUpload) {
|
if (data.getHttpDataType() == HttpDataType.FileUpload) {
|
||||||
FileUpload fileUpload = (FileUpload) data;
|
FileUpload fileUpload = (FileUpload) data;
|
||||||
if (fileUpload.isCompleted()) {
|
if (fileUpload.isCompleted()) {
|
||||||
|
@ -305,8 +303,8 @@ public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||||
responseContent.append("\r\n");
|
responseContent.append("\r\n");
|
||||||
} else {
|
} else {
|
||||||
responseContent
|
responseContent
|
||||||
.append("\tFile too long to be printed out:"
|
.append("\tFile too long to be printed out:" +
|
||||||
+ fileUpload.length() + "\r\n");
|
fileUpload.length() + "\r\n");
|
||||||
}
|
}
|
||||||
// fileUpload.isInMemory();// tells if the file is in Memory
|
// fileUpload.isInMemory();// tells if the file is in Memory
|
||||||
// or on File
|
// or on File
|
||||||
|
@ -324,15 +322,15 @@ public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||||
|
|
||||||
private void writeResponse(Channel channel) {
|
private void writeResponse(Channel channel) {
|
||||||
// Convert the response content to a ChannelBuffer.
|
// Convert the response content to a ChannelBuffer.
|
||||||
ChannelBuffer buf = ChannelBuffers.copiedBuffer(
|
ChannelBuffer buf = ChannelBuffers.copiedBuffer(responseContent
|
||||||
responseContent.toString(), CharsetUtil.UTF_8);
|
.toString(), CharsetUtil.UTF_8);
|
||||||
responseContent.setLength(0);
|
responseContent.setLength(0);
|
||||||
|
|
||||||
// Decide whether to close the connection or not.
|
// Decide whether to close the connection or not.
|
||||||
boolean close = HttpHeaders.Values.CLOSE.equalsIgnoreCase(request
|
boolean close = HttpHeaders.Values.CLOSE.equalsIgnoreCase(request
|
||||||
.getHeader(HttpHeaders.Names.CONNECTION))
|
.getHeader(HttpHeaders.Names.CONNECTION)) ||
|
||||||
|| request.getProtocolVersion().equals(HttpVersion.HTTP_1_0)
|
request.getProtocolVersion().equals(HttpVersion.HTTP_1_0) &&
|
||||||
&& !HttpHeaders.Values.KEEP_ALIVE.equalsIgnoreCase(request
|
!HttpHeaders.Values.KEEP_ALIVE.equalsIgnoreCase(request
|
||||||
.getHeader(HttpHeaders.Names.CONNECTION));
|
.getHeader(HttpHeaders.Names.CONNECTION));
|
||||||
|
|
||||||
// Build the response object.
|
// Build the response object.
|
||||||
|
@ -345,8 +343,8 @@ public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||||
if (!close) {
|
if (!close) {
|
||||||
// There's no need to add 'Content-Length' header
|
// There's no need to add 'Content-Length' header
|
||||||
// if this is the last response.
|
// if this is the last response.
|
||||||
response.setHeader(HttpHeaders.Names.CONTENT_LENGTH,
|
response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String
|
||||||
String.valueOf(buf.readableBytes()));
|
.valueOf(buf.readableBytes()));
|
||||||
}
|
}
|
||||||
|
|
||||||
Set<Cookie> cookies;
|
Set<Cookie> cookies;
|
||||||
|
@ -360,11 +358,11 @@ public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||||
if (!cookies.isEmpty()) {
|
if (!cookies.isEmpty()) {
|
||||||
// Reset the cookies if necessary.
|
// Reset the cookies if necessary.
|
||||||
CookieEncoder cookieEncoder = new CookieEncoder(true);
|
CookieEncoder cookieEncoder = new CookieEncoder(true);
|
||||||
for (Cookie cookie : cookies) {
|
for (Cookie cookie: cookies) {
|
||||||
cookieEncoder.addCookie(cookie);
|
cookieEncoder.addCookie(cookie);
|
||||||
}
|
}
|
||||||
response.addHeader(HttpHeaders.Names.SET_COOKIE,
|
response.addHeader(HttpHeaders.Names.SET_COOKIE, cookieEncoder
|
||||||
cookieEncoder.encode());
|
.encode());
|
||||||
}
|
}
|
||||||
// Write the response.
|
// Write the response.
|
||||||
ChannelFuture future = channel.write(response);
|
ChannelFuture future = channel.write(response);
|
||||||
|
@ -470,16 +468,16 @@ public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||||
responseContent.append("</body>");
|
responseContent.append("</body>");
|
||||||
responseContent.append("</html>");
|
responseContent.append("</html>");
|
||||||
|
|
||||||
ChannelBuffer buf = ChannelBuffers.copiedBuffer(
|
ChannelBuffer buf = ChannelBuffers.copiedBuffer(responseContent
|
||||||
responseContent.toString(), CharsetUtil.UTF_8);
|
.toString(), CharsetUtil.UTF_8);
|
||||||
// Build the response object.
|
// Build the response object.
|
||||||
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
|
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
|
||||||
HttpResponseStatus.OK);
|
HttpResponseStatus.OK);
|
||||||
response.setContent(buf);
|
response.setContent(buf);
|
||||||
response.setHeader(HttpHeaders.Names.CONTENT_TYPE,
|
response.setHeader(HttpHeaders.Names.CONTENT_TYPE,
|
||||||
"text/html; charset=UTF-8");
|
"text/html; charset=UTF-8");
|
||||||
response.setHeader(HttpHeaders.Names.CONTENT_LENGTH,
|
response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(buf
|
||||||
String.valueOf(buf.readableBytes()));
|
.readableBytes()));
|
||||||
// Write the response.
|
// Write the response.
|
||||||
e.getChannel().write(response);
|
e.getChannel().write(response);
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,17 +30,15 @@ public class HttpUploadServerPipelineFactory implements ChannelPipelineFactory {
|
||||||
ChannelPipeline pipeline = pipeline();
|
ChannelPipeline pipeline = pipeline();
|
||||||
|
|
||||||
// Uncomment the following line if you want HTTPS
|
// Uncomment the following line if you want HTTPS
|
||||||
// SSLEngine engine =
|
//SSLEngine engine = SecureChatSslContextFactory.getServerContext().createSSLEngine();
|
||||||
// SecureChatSslContextFactory.getServerContext().createSSLEngine();
|
//engine.setUseClientMode(false);
|
||||||
// engine.setUseClientMode(false);
|
//pipeline.addLast("ssl", new SslHandler(engine));
|
||||||
// pipeline.addLast("ssl", new SslHandler(engine));
|
|
||||||
|
|
||||||
pipeline.addLast("decoder", new HttpRequestDecoder());
|
pipeline.addLast("decoder", new HttpRequestDecoder());
|
||||||
|
|
||||||
pipeline.addLast("encoder", new HttpResponseEncoder());
|
pipeline.addLast("encoder", new HttpResponseEncoder());
|
||||||
|
|
||||||
// Remove the following line if you don't want automatic content
|
// Remove the following line if you don't want automatic content compression.
|
||||||
// compression.
|
|
||||||
pipeline.addLast("deflater", new HttpContentCompressor());
|
pipeline.addLast("deflater", new HttpContentCompressor());
|
||||||
|
|
||||||
pipeline.addLast("handler", new HttpUploadServerHandler());
|
pipeline.addLast("handler", new HttpUploadServerHandler());
|
||||||
|
|
Loading…
Reference in New Issue
Block a user