Replaced tabs with 4 spaces.
This commit is contained in:
parent
802e5366b2
commit
66b4735acd
File diff suppressed because it is too large
Load Diff
@ -27,14 +27,15 @@ import org.jboss.netty.logging.InternalLoggerFactory;
|
||||
import org.jboss.netty.util.CharsetUtil;
|
||||
|
||||
public class HttpUploadClientHandler extends SimpleChannelUpstreamHandler {
|
||||
|
||||
private static final InternalLogger logger =
|
||||
InternalLoggerFactory.getInstance(HttpUploadClientHandler.class);
|
||||
|
||||
private static final InternalLogger logger = InternalLoggerFactory
|
||||
.getInstance(HttpUploadClientHandler.class);
|
||||
|
||||
private volatile boolean readingChunks;
|
||||
|
||||
@Override
|
||||
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
|
||||
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
|
||||
throws Exception {
|
||||
if (!readingChunks) {
|
||||
HttpResponse response = (HttpResponse) e.getMessage();
|
||||
|
||||
@ -42,8 +43,8 @@ public class HttpUploadClientHandler extends SimpleChannelUpstreamHandler {
|
||||
logger.info("VERSION: " + response.getProtocolVersion());
|
||||
|
||||
if (!response.getHeaderNames().isEmpty()) {
|
||||
for (String name: response.getHeaderNames()) {
|
||||
for (String value: response.getHeaders(name)) {
|
||||
for (String name : response.getHeaderNames()) {
|
||||
for (String value : response.getHeaders(name)) {
|
||||
logger.info("HEADER: " + name + " = " + value);
|
||||
}
|
||||
}
|
||||
|
@ -41,8 +41,8 @@ public class HttpUploadClientPipelineFactory implements ChannelPipelineFactory {
|
||||
|
||||
// Enable HTTPS if necessary.
|
||||
if (ssl) {
|
||||
SSLEngine engine =
|
||||
SecureChatSslContextFactory.getClientContext().createSSLEngine();
|
||||
SSLEngine engine = SecureChatSslContextFactory.getClientContext()
|
||||
.createSSLEngine();
|
||||
engine.setUseClientMode(true);
|
||||
|
||||
pipeline.addLast("ssl", new SslHandler(engine));
|
||||
@ -50,12 +50,13 @@ public class HttpUploadClientPipelineFactory implements ChannelPipelineFactory {
|
||||
|
||||
pipeline.addLast("codec", new HttpClientCodec());
|
||||
|
||||
// Remove the following line if you don't want automatic content decompression.
|
||||
// Remove the following line if you don't want automatic content
|
||||
// decompression.
|
||||
pipeline.addLast("inflater", new HttpContentDecompressor());
|
||||
|
||||
// to be used since huge file transfer
|
||||
pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
|
||||
|
||||
|
||||
pipeline.addLast("handler", new HttpUploadClientHandler());
|
||||
return pipeline;
|
||||
}
|
||||
|
@ -23,33 +23,33 @@ import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
|
||||
|
||||
public class HttpUploadServer {
|
||||
|
||||
private final int port;
|
||||
private final int port;
|
||||
|
||||
public HttpUploadServer(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
public HttpUploadServer(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
// Configure the server.
|
||||
ServerBootstrap bootstrap = new ServerBootstrap(
|
||||
new NioServerSocketChannelFactory(
|
||||
Executors.newCachedThreadPool(),
|
||||
Executors.newCachedThreadPool()));
|
||||
public void run() {
|
||||
// Configure the server.
|
||||
ServerBootstrap bootstrap = new ServerBootstrap(
|
||||
new NioServerSocketChannelFactory(
|
||||
Executors.newCachedThreadPool(),
|
||||
Executors.newCachedThreadPool()));
|
||||
|
||||
// Set up the event pipeline factory.
|
||||
bootstrap.setPipelineFactory(new HttpUploadServerPipelineFactory());
|
||||
// Set up the event pipeline factory.
|
||||
bootstrap.setPipelineFactory(new HttpUploadServerPipelineFactory());
|
||||
|
||||
// Bind and start to accept incoming connections.
|
||||
bootstrap.bind(new InetSocketAddress(port));
|
||||
}
|
||||
// Bind and start to accept incoming connections.
|
||||
bootstrap.bind(new InetSocketAddress(port));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int port;
|
||||
if (args.length > 0) {
|
||||
port = Integer.parseInt(args[0]);
|
||||
} else {
|
||||
port = 8080;
|
||||
}
|
||||
new HttpUploadServer(port).run();
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
int port;
|
||||
if (args.length > 0) {
|
||||
port = Integer.parseInt(args[0]);
|
||||
} else {
|
||||
port = 8080;
|
||||
}
|
||||
new HttpUploadServer(port).run();
|
||||
}
|
||||
}
|
||||
|
@ -63,9 +63,9 @@ import org.jboss.netty.logging.InternalLoggerFactory;
|
||||
import org.jboss.netty.util.CharsetUtil;
|
||||
|
||||
public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||
|
||||
private static final InternalLogger logger =
|
||||
InternalLoggerFactory.getInstance(HttpUploadServerHandler.class);
|
||||
|
||||
private static final InternalLogger logger = InternalLoggerFactory
|
||||
.getInstance(HttpUploadServerHandler.class);
|
||||
|
||||
private volatile HttpRequest request;
|
||||
|
||||
@ -96,7 +96,8 @@ public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
|
||||
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
|
||||
throws Exception {
|
||||
if (!readingChunks) {
|
||||
// clean previous FileUpload if Any
|
||||
if (decoder != null) {
|
||||
@ -115,18 +116,18 @@ public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||
responseContent.append("WELCOME TO THE WILD WILD WEB SERVER\r\n");
|
||||
responseContent.append("===================================\r\n");
|
||||
|
||||
responseContent.append("VERSION: " +
|
||||
request.getProtocolVersion().getText() + "\r\n");
|
||||
responseContent.append("VERSION: "
|
||||
+ request.getProtocolVersion().getText() + "\r\n");
|
||||
|
||||
responseContent.append("REQUEST_URI: " + request.getUri() +
|
||||
"\r\n\r\n");
|
||||
responseContent.append("REQUEST_URI: " + request.getUri()
|
||||
+ "\r\n\r\n");
|
||||
responseContent.append("\r\n\r\n");
|
||||
|
||||
// new method
|
||||
List<Entry<String, String>> headers = request.getHeaders();
|
||||
for (Entry<String, String> entry: headers) {
|
||||
responseContent.append("HEADER: " + entry.getKey() + "=" +
|
||||
entry.getValue() + "\r\n");
|
||||
for (Entry<String, String> entry : headers) {
|
||||
responseContent.append("HEADER: " + entry.getKey() + "="
|
||||
+ entry.getValue() + "\r\n");
|
||||
}
|
||||
responseContent.append("\r\n\r\n");
|
||||
|
||||
@ -139,19 +140,19 @@ public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||
CookieDecoder decoder = new CookieDecoder();
|
||||
cookies = decoder.decode(value);
|
||||
}
|
||||
for (Cookie cookie: cookies) {
|
||||
for (Cookie cookie : cookies) {
|
||||
responseContent.append("COOKIE: " + cookie.toString() + "\r\n");
|
||||
}
|
||||
responseContent.append("\r\n\r\n");
|
||||
|
||||
QueryStringDecoder decoderQuery = new QueryStringDecoder(request
|
||||
.getUri());
|
||||
QueryStringDecoder decoderQuery = new QueryStringDecoder(
|
||||
request.getUri());
|
||||
Map<String, List<String>> uriAttributes = decoderQuery
|
||||
.getParameters();
|
||||
for (String key: uriAttributes.keySet()) {
|
||||
for (String valuen: uriAttributes.get(key)) {
|
||||
responseContent.append("URI: " + key + "=" + valuen +
|
||||
"\r\n");
|
||||
for (String key : uriAttributes.keySet()) {
|
||||
for (String valuen : uriAttributes.get(key)) {
|
||||
responseContent.append("URI: " + key + "=" + valuen
|
||||
+ "\r\n");
|
||||
}
|
||||
}
|
||||
responseContent.append("\r\n\r\n");
|
||||
@ -174,10 +175,10 @@ public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||
return;
|
||||
}
|
||||
|
||||
responseContent.append("Is Chunked: " + request.isChunked() +
|
||||
"\r\n");
|
||||
responseContent.append("IsMultipart: " + decoder.isMultipart() +
|
||||
"\r\n");
|
||||
responseContent.append("Is Chunked: " + request.isChunked()
|
||||
+ "\r\n");
|
||||
responseContent.append("IsMultipart: " + decoder.isMultipart()
|
||||
+ "\r\n");
|
||||
if (request.isChunked()) {
|
||||
// Chunk version
|
||||
responseContent.append("Chunks: ");
|
||||
@ -202,7 +203,8 @@ public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||
return;
|
||||
}
|
||||
responseContent.append("o");
|
||||
// example of reading chunk by chunk (minimize memory usage due to Factory)
|
||||
// example of reading chunk by chunk (minimize memory usage due to
|
||||
// Factory)
|
||||
readHttpDataChunkByChunk(e.getChannel());
|
||||
// example of reading only if at the end
|
||||
if (chunk.isLast()) {
|
||||
@ -215,7 +217,7 @@ public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||
|
||||
/**
|
||||
* Example of reading all InterfaceHttpData from finished transfer
|
||||
*
|
||||
*
|
||||
* @param channel
|
||||
*/
|
||||
private void readHttpDataAllReceive(Channel channel) {
|
||||
@ -230,7 +232,7 @@ public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||
Channels.close(channel);
|
||||
return;
|
||||
}
|
||||
for (InterfaceHttpData data: datas) {
|
||||
for (InterfaceHttpData data : datas) {
|
||||
writeHttpData(data);
|
||||
}
|
||||
responseContent.append("\r\n\r\nEND OF CONTENT AT FINAL END\r\n");
|
||||
@ -239,7 +241,7 @@ public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||
/**
|
||||
* Example of reading request by chunk and getting values from chunk to
|
||||
* chunk
|
||||
*
|
||||
*
|
||||
* @param channel
|
||||
*/
|
||||
private void readHttpDataChunkByChunk(Channel channel) {
|
||||
@ -267,25 +269,25 @@ public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||
} catch (IOException e1) {
|
||||
// Error while reading data from File, only print name and error
|
||||
e1.printStackTrace();
|
||||
responseContent.append("\r\nBODY Attribute: " +
|
||||
attribute.getHttpDataType().name() + ": " +
|
||||
attribute.getName() + " Error while reading value: " +
|
||||
e1.getMessage() + "\r\n");
|
||||
responseContent.append("\r\nBODY Attribute: "
|
||||
+ attribute.getHttpDataType().name() + ": "
|
||||
+ attribute.getName() + " Error while reading value: "
|
||||
+ e1.getMessage() + "\r\n");
|
||||
return;
|
||||
}
|
||||
if (value.length() > 100) {
|
||||
responseContent.append("\r\nBODY Attribute: " +
|
||||
attribute.getHttpDataType().name() + ": " +
|
||||
attribute.getName() + " data too long\r\n");
|
||||
responseContent.append("\r\nBODY Attribute: "
|
||||
+ attribute.getHttpDataType().name() + ": "
|
||||
+ attribute.getName() + " data too long\r\n");
|
||||
} else {
|
||||
responseContent.append("\r\nBODY Attribute: " +
|
||||
attribute.getHttpDataType().name() + ": " +
|
||||
attribute.toString() + "\r\n");
|
||||
responseContent.append("\r\nBODY Attribute: "
|
||||
+ attribute.getHttpDataType().name() + ": "
|
||||
+ attribute.toString() + "\r\n");
|
||||
}
|
||||
} else {
|
||||
responseContent.append("\r\nBODY FileUpload: " +
|
||||
data.getHttpDataType().name() + ": " + data.toString() +
|
||||
"\r\n");
|
||||
responseContent.append("\r\nBODY FileUpload: "
|
||||
+ data.getHttpDataType().name() + ": " + data.toString()
|
||||
+ "\r\n");
|
||||
if (data.getHttpDataType() == HttpDataType.FileUpload) {
|
||||
FileUpload fileUpload = (FileUpload) data;
|
||||
if (fileUpload.isCompleted()) {
|
||||
@ -303,8 +305,8 @@ public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||
responseContent.append("\r\n");
|
||||
} else {
|
||||
responseContent
|
||||
.append("\tFile too long to be printed out:" +
|
||||
fileUpload.length() + "\r\n");
|
||||
.append("\tFile too long to be printed out:"
|
||||
+ fileUpload.length() + "\r\n");
|
||||
}
|
||||
// fileUpload.isInMemory();// tells if the file is in Memory
|
||||
// or on File
|
||||
@ -322,15 +324,15 @@ public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||
|
||||
private void writeResponse(Channel channel) {
|
||||
// Convert the response content to a ChannelBuffer.
|
||||
ChannelBuffer buf = ChannelBuffers.copiedBuffer(responseContent
|
||||
.toString(), CharsetUtil.UTF_8);
|
||||
ChannelBuffer buf = ChannelBuffers.copiedBuffer(
|
||||
responseContent.toString(), CharsetUtil.UTF_8);
|
||||
responseContent.setLength(0);
|
||||
|
||||
// Decide whether to close the connection or not.
|
||||
boolean close = HttpHeaders.Values.CLOSE.equalsIgnoreCase(request
|
||||
.getHeader(HttpHeaders.Names.CONNECTION)) ||
|
||||
request.getProtocolVersion().equals(HttpVersion.HTTP_1_0) &&
|
||||
!HttpHeaders.Values.KEEP_ALIVE.equalsIgnoreCase(request
|
||||
.getHeader(HttpHeaders.Names.CONNECTION))
|
||||
|| request.getProtocolVersion().equals(HttpVersion.HTTP_1_0)
|
||||
&& !HttpHeaders.Values.KEEP_ALIVE.equalsIgnoreCase(request
|
||||
.getHeader(HttpHeaders.Names.CONNECTION));
|
||||
|
||||
// Build the response object.
|
||||
@ -343,8 +345,8 @@ public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||
if (!close) {
|
||||
// There's no need to add 'Content-Length' header
|
||||
// if this is the last response.
|
||||
response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String
|
||||
.valueOf(buf.readableBytes()));
|
||||
response.setHeader(HttpHeaders.Names.CONTENT_LENGTH,
|
||||
String.valueOf(buf.readableBytes()));
|
||||
}
|
||||
|
||||
Set<Cookie> cookies;
|
||||
@ -358,11 +360,11 @@ public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||
if (!cookies.isEmpty()) {
|
||||
// Reset the cookies if necessary.
|
||||
CookieEncoder cookieEncoder = new CookieEncoder(true);
|
||||
for (Cookie cookie: cookies) {
|
||||
for (Cookie cookie : cookies) {
|
||||
cookieEncoder.addCookie(cookie);
|
||||
}
|
||||
response.addHeader(HttpHeaders.Names.SET_COOKIE, cookieEncoder
|
||||
.encode());
|
||||
response.addHeader(HttpHeaders.Names.SET_COOKIE,
|
||||
cookieEncoder.encode());
|
||||
}
|
||||
// Write the response.
|
||||
ChannelFuture future = channel.write(response);
|
||||
@ -468,16 +470,16 @@ public class HttpUploadServerHandler extends SimpleChannelUpstreamHandler {
|
||||
responseContent.append("</body>");
|
||||
responseContent.append("</html>");
|
||||
|
||||
ChannelBuffer buf = ChannelBuffers.copiedBuffer(responseContent
|
||||
.toString(), CharsetUtil.UTF_8);
|
||||
ChannelBuffer buf = ChannelBuffers.copiedBuffer(
|
||||
responseContent.toString(), CharsetUtil.UTF_8);
|
||||
// Build the response object.
|
||||
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
|
||||
HttpResponseStatus.OK);
|
||||
response.setContent(buf);
|
||||
response.setHeader(HttpHeaders.Names.CONTENT_TYPE,
|
||||
"text/html; charset=UTF-8");
|
||||
response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(buf
|
||||
.readableBytes()));
|
||||
response.setHeader(HttpHeaders.Names.CONTENT_LENGTH,
|
||||
String.valueOf(buf.readableBytes()));
|
||||
// Write the response.
|
||||
e.getChannel().write(response);
|
||||
}
|
||||
|
@ -30,15 +30,17 @@ public class HttpUploadServerPipelineFactory implements ChannelPipelineFactory {
|
||||
ChannelPipeline pipeline = pipeline();
|
||||
|
||||
// Uncomment the following line if you want HTTPS
|
||||
//SSLEngine engine = SecureChatSslContextFactory.getServerContext().createSSLEngine();
|
||||
//engine.setUseClientMode(false);
|
||||
//pipeline.addLast("ssl", new SslHandler(engine));
|
||||
// SSLEngine engine =
|
||||
// SecureChatSslContextFactory.getServerContext().createSSLEngine();
|
||||
// engine.setUseClientMode(false);
|
||||
// pipeline.addLast("ssl", new SslHandler(engine));
|
||||
|
||||
pipeline.addLast("decoder", new HttpRequestDecoder());
|
||||
|
||||
pipeline.addLast("encoder", new HttpResponseEncoder());
|
||||
|
||||
// Remove the following line if you don't want automatic content compression.
|
||||
// Remove the following line if you don't want automatic content
|
||||
// compression.
|
||||
pipeline.addLast("deflater", new HttpContentCompressor());
|
||||
|
||||
pipeline.addLast("handler", new HttpUploadServerHandler());
|
||||
|
@ -344,4 +344,4 @@ public abstract class AbstractDiskHttpData extends AbstractHttpData {
|
||||
return file;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -94,4 +94,4 @@ public abstract class AbstractHttpData implements HttpData {
|
||||
public long length() {
|
||||
return size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -223,4 +223,4 @@ public abstract class AbstractMemoryHttpData extends AbstractHttpData {
|
||||
public File getFile() throws IOException {
|
||||
throw new IOException("Not represented by a file");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -31,4 +31,4 @@ public interface Attribute extends HttpData {
|
||||
* @param value
|
||||
*/
|
||||
void setValue(String value) throws IOException;
|
||||
}
|
||||
}
|
||||
|
@ -21,8 +21,6 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.jboss.netty.handler.codec.http.HttpRequest;
|
||||
|
||||
/**
|
||||
* Default factory giving Attribute and FileUpload according to constructor
|
||||
*
|
||||
@ -187,4 +185,4 @@ public class DefaultHttpDataFactory implements HttpDataFactory {
|
||||
requestFileDeleteMap.remove(request);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -144,4 +144,4 @@ public class DiskAttribute extends AbstractDiskHttpData implements Attribute {
|
||||
protected String getPrefix() {
|
||||
return prefix;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -18,8 +18,6 @@ package org.jboss.netty.handler.codec.http;
|
||||
import java.io.File;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.jboss.netty.handler.codec.http.HttpHeaders;
|
||||
|
||||
/**
|
||||
* Disk FileUpload implementation that stores file into real files
|
||||
*/
|
||||
@ -159,4 +157,4 @@ public class DiskFileUpload extends AbstractDiskHttpData implements FileUpload {
|
||||
protected String getPrefix() {
|
||||
return prefix;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -178,4 +178,4 @@ final class HttpPostBodyUtil {
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -33,4 +33,4 @@ public interface InterfaceHttpData extends Comparable<InterfaceHttpData> {
|
||||
* @return The HttpDataType
|
||||
*/
|
||||
HttpDataType getHttpDataType();
|
||||
}
|
||||
}
|
||||
|
@ -105,4 +105,4 @@ public class MemoryAttribute extends AbstractMemoryHttpData implements Attribute
|
||||
return getName() + "=" + getValue();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -17,8 +17,6 @@ package org.jboss.netty.handler.codec.http;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.jboss.netty.handler.codec.http.HttpHeaders;
|
||||
|
||||
/**
|
||||
* Default FileUpload implementation that stores file into memory.<br><br>
|
||||
*
|
||||
@ -125,4 +123,4 @@ public class MemoryFileUpload extends AbstractMemoryHttpData implements FileUplo
|
||||
"Completed: " + isCompleted() +
|
||||
"\r\nIsInMemory: " + isInMemory();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -199,4 +199,4 @@ public class MixedAttribute implements Attribute {
|
||||
return attribute.getFile();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -224,4 +224,4 @@ public class MixedFileUpload implements FileUpload {
|
||||
return fileUpload.getFile();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user