-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added BBtoHttpResponse Utility class for DRY
- Loading branch information
Jeff Rose
committed
Sep 25, 2015
1 parent
4aa7021
commit d1fe682
Showing
8 changed files
with
147 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/java/com/xjeffrose/xio/core/BBtoHttpResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.xjeffrose.xio.core; | ||
|
||
import com.google.common.base.Joiner; | ||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.handler.codec.http.DefaultFullHttpResponse; | ||
import io.netty.handler.codec.http.HttpResponseStatus; | ||
import io.netty.handler.codec.http.HttpVersion; | ||
import java.nio.charset.Charset; | ||
import java.util.Arrays; | ||
import javax.annotation.Nullable; | ||
|
||
public class BBtoHttpResponse { | ||
|
||
private BBtoHttpResponse() { } | ||
|
||
public static DefaultFullHttpResponse getResponse(ByteBuf byteBuf) { | ||
return BBtoHttpResponse.getResponse(null, byteBuf); | ||
} | ||
|
||
public static DefaultFullHttpResponse getResponse(@Nullable ChannelHandlerContext ctx, ByteBuf byteBuf) { | ||
// Lets make a HTTP parser cause apparently that's a good idea... | ||
final ByteBuf response = byteBuf.duplicate(); | ||
final Joiner joiner = Joiner.on(" ").skipNulls(); | ||
DefaultFullHttpResponse httpResponse; | ||
|
||
String[] headerBody = response.toString(Charset.defaultCharset()).split("\r\n\r\n"); | ||
String[] headers = headerBody[0].split("\r\n"); | ||
String[] firstLine = headers[0].split("\\s"); | ||
|
||
// Lets make a HTTP Response object now | ||
if (ctx == null) { | ||
httpResponse = new DefaultFullHttpResponse( | ||
HttpVersion.valueOf(firstLine[0]), | ||
new HttpResponseStatus(Integer.parseInt(firstLine[1]), joiner.join(Arrays.copyOfRange(firstLine, 2, 5))), | ||
Unpooled.wrappedBuffer(headerBody[1].getBytes())); | ||
} else { | ||
httpResponse = new DefaultFullHttpResponse( | ||
HttpVersion.valueOf(firstLine[0]), | ||
new HttpResponseStatus(Integer.parseInt(firstLine[1]), joiner.join(Arrays.copyOfRange(firstLine, 2 , 5))), | ||
ctx.alloc().buffer().writeBytes(headerBody[1].getBytes())); | ||
} | ||
|
||
for (int i = 1; i < headers.length; i++) { | ||
String[] xs = headers[i].split(":"); | ||
httpResponse.headers().add(xs[0].trim(), xs[1].trim()); | ||
} | ||
|
||
return httpResponse; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/test/java/com/xjeffrose/xio/core/BBtoHttpResponseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.xjeffrose.xio.core; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
import io.netty.handler.codec.http.DefaultFullHttpResponse; | ||
import io.netty.handler.codec.http.HttpResponseStatus; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class BBtoHttpResponseTest { | ||
|
||
@Test | ||
public void testGetResponseOK() throws Exception { | ||
ByteBuf bb = Unpooled.wrappedBuffer("HTTP/1.1 200 OK\r\nServer: xio\r\n\r\n\r\n".getBytes()); | ||
DefaultFullHttpResponse response = BBtoHttpResponse.getResponse(bb); | ||
|
||
assertEquals(HttpResponseStatus.OK, response.getStatus()); | ||
assertEquals("xio", response.headers().get("Server")); | ||
|
||
} | ||
|
||
@Test | ||
public void testGetResponseServerError() throws Exception { | ||
ByteBuf bb = Unpooled.wrappedBuffer("HTTP/1.1 500 Internal Server Error\r\nServer: xio\r\n\r\n\r\n".getBytes()); | ||
DefaultFullHttpResponse response = BBtoHttpResponse.getResponse(bb); | ||
|
||
assertEquals(HttpResponseStatus.INTERNAL_SERVER_ERROR, response.getStatus()); | ||
assertEquals("xio", response.headers().get("Server")); | ||
} | ||
} |
Oops, something went wrong.