Simplified HTTP snoop example
This commit is contained in:
parent
140aabef68
commit
98ccc7a784
@ -15,6 +15,9 @@
|
|||||||
*/
|
*/
|
||||||
package org.jboss.netty.example.http.snoop;
|
package org.jboss.netty.example.http.snoop;
|
||||||
|
|
||||||
|
import static org.jboss.netty.handler.codec.http.HttpHeaders.*;
|
||||||
|
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.*;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@ -61,15 +64,12 @@ public class HttpRequestHandler extends SimpleChannelUpstreamHandler {
|
|||||||
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
|
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
|
||||||
if (!readingChunks) {
|
if (!readingChunks) {
|
||||||
HttpRequest request = this.request = (HttpRequest) e.getMessage();
|
HttpRequest request = this.request = (HttpRequest) e.getMessage();
|
||||||
|
responseContent.setLength(0);
|
||||||
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: " + request.getProtocolVersion().getText() + "\r\n");
|
responseContent.append("VERSION: " + request.getProtocolVersion().getText() + "\r\n");
|
||||||
|
responseContent.append("HOSTNAME: " + getHost(request, "unknown") + "\r\n");
|
||||||
if (request.containsHeader(HttpHeaders.Names.HOST)) {
|
|
||||||
responseContent.append("HOSTNAME: " + request.getHeader(HttpHeaders.Names.HOST) + "\r\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
responseContent.append("REQUEST_URI: " + request.getUri() + "\r\n\r\n");
|
responseContent.append("REQUEST_URI: " + request.getUri() + "\r\n\r\n");
|
||||||
|
|
||||||
for (Map.Entry<String, String> h: request.getHeaders()) {
|
for (Map.Entry<String, String> h: request.getHeaders()) {
|
||||||
@ -124,24 +124,20 @@ public class HttpRequestHandler extends SimpleChannelUpstreamHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void writeResponse(MessageEvent e) {
|
private void writeResponse(MessageEvent e) {
|
||||||
// Convert the response content to a ChannelBuffer.
|
|
||||||
ChannelBuffer buf = ChannelBuffers.copiedBuffer(responseContent.toString(), CharsetUtil.UTF_8);
|
|
||||||
responseContent.setLength(0);
|
|
||||||
|
|
||||||
// Decide whether to close the connection or not.
|
// Decide whether to close the connection or not.
|
||||||
boolean close = !request.isKeepAlive();
|
boolean keepAlive = request.isKeepAlive();
|
||||||
|
|
||||||
// Build the response object.
|
// Build the response object.
|
||||||
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
|
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
|
||||||
response.setContent(buf);
|
response.setContent(ChannelBuffers.copiedBuffer(responseContent.toString(), CharsetUtil.UTF_8));
|
||||||
response.setHeader(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8");
|
response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8");
|
||||||
|
|
||||||
if (!close) {
|
if (keepAlive) {
|
||||||
// There's no need to add 'Content-Length' header
|
// Add 'Content-Length' header only for a keep-alive connection.
|
||||||
// if this is the last response.
|
response.setHeader(CONTENT_LENGTH, response.getContent().readableBytes());
|
||||||
response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(buf.readableBytes()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Encode the cookie.
|
||||||
String cookieString = request.getHeader(HttpHeaders.Names.COOKIE);
|
String cookieString = request.getHeader(HttpHeaders.Names.COOKIE);
|
||||||
if (cookieString != null) {
|
if (cookieString != null) {
|
||||||
CookieDecoder cookieDecoder = new CookieDecoder();
|
CookieDecoder cookieDecoder = new CookieDecoder();
|
||||||
@ -152,15 +148,15 @@ public class HttpRequestHandler extends SimpleChannelUpstreamHandler {
|
|||||||
for (Cookie cookie : cookies) {
|
for (Cookie cookie : cookies) {
|
||||||
cookieEncoder.addCookie(cookie);
|
cookieEncoder.addCookie(cookie);
|
||||||
}
|
}
|
||||||
response.addHeader(HttpHeaders.Names.SET_COOKIE, cookieEncoder.encode());
|
response.addHeader(SET_COOKIE, cookieEncoder.encode());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write the response.
|
// Write the response.
|
||||||
ChannelFuture future = e.getChannel().write(response);
|
ChannelFuture future = e.getChannel().write(response);
|
||||||
|
|
||||||
// Close the connection after the write operation is done if necessary.
|
// Close the non-keep-alive connection after the write operation is done.
|
||||||
if (close) {
|
if (!keepAlive) {
|
||||||
future.addListener(ChannelFutureListener.CLOSE);
|
future.addListener(ChannelFutureListener.CLOSE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user