Use package-local fields instead of reflection / Reapply 4dc78c10ee / Inspector warnings

This commit is contained in:
Trustin Lee 2014-01-19 14:58:09 +09:00
parent 709be30442
commit f97f6b938e
2 changed files with 63 additions and 45 deletions

View File

@ -15,7 +15,6 @@
*/ */
package io.netty.handler.codec.http.multipart; package io.netty.handler.codec.http.multipart;
import static io.netty.buffer.Unpooled.wrappedBuffer;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.DecoderResult; import io.netty.handler.codec.DecoderResult;
@ -44,6 +43,8 @@ import java.util.ListIterator;
import java.util.Map; import java.util.Map;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static io.netty.buffer.Unpooled.*;
/** /**
* This encoder will help to encode Request for a FORM as POST. * This encoder will help to encode Request for a FORM as POST.
*/ */
@ -111,7 +112,7 @@ public class HttpPostRequestEncoder implements ChunkedInput<HttpContent> {
/** /**
* The final Multipart List of InterfaceHttpData including encoding * The final Multipart List of InterfaceHttpData including encoding
*/ */
private final List<InterfaceHttpData> multipartHttpDatas; final List<InterfaceHttpData> multipartHttpDatas;
/** /**
* Does this request is a Multipart request * Does this request is a Multipart request
@ -121,12 +122,12 @@ public class HttpPostRequestEncoder implements ChunkedInput<HttpContent> {
/** /**
* If multipart, this is the boundary for the flobal multipart * If multipart, this is the boundary for the flobal multipart
*/ */
private String multipartDataBoundary; String multipartDataBoundary;
/** /**
* If multipart, there could be internal multiparts (mixed) to the global multipart. Only one level is allowed. * If multipart, there could be internal multiparts (mixed) to the global multipart. Only one level is allowed.
*/ */
private String multipartMixedBoundary; String multipartMixedBoundary;
/** /**
* To check if the header has been finalized * To check if the header has been finalized
*/ */
@ -571,16 +572,48 @@ public class HttpPostRequestEncoder implements ChunkedInput<HttpContent> {
.size() - 2); .size() - 2);
// remove past size // remove past size
globalBodySize -= pastAttribute.size(); globalBodySize -= pastAttribute.size();
String replacement = "--" + multipartDataBoundary + "\r\n"; StringBuilder replacement = new StringBuilder(
replacement += HttpPostBodyUtil.CONTENT_DISPOSITION + ": " + HttpPostBodyUtil.FORM_DATA 139 + multipartDataBoundary.length() + multipartMixedBoundary.length() * 2 +
+ "; " + HttpPostBodyUtil.NAME + "=\"" + fileUpload.getName() + "\"\r\n"; fileUpload.getFilename().length() + fileUpload.getName().length());
replacement += HttpHeaders.Names.CONTENT_TYPE + ": " + HttpPostBodyUtil.MULTIPART_MIXED + "; "
+ HttpHeaders.Values.BOUNDARY + '=' + multipartMixedBoundary + "\r\n\r\n"; replacement.append("--");
replacement += "--" + multipartMixedBoundary + "\r\n"; replacement.append(multipartDataBoundary);
replacement += HttpPostBodyUtil.CONTENT_DISPOSITION + ": " + HttpPostBodyUtil.ATTACHMENT + "; " replacement.append("\r\n");
+ HttpPostBodyUtil.FILENAME + "=\"" + fileUpload.getFilename() + "\"\r\n";
pastAttribute.setValue(replacement, 1); replacement.append(HttpPostBodyUtil.CONTENT_DISPOSITION);
replacement.append(": ");
replacement.append(HttpPostBodyUtil.FORM_DATA);
replacement.append("; ");
replacement.append(HttpPostBodyUtil.NAME);
replacement.append("=\"");
replacement.append(fileUpload.getName());
replacement.append("\"\r\n");
replacement.append(HttpHeaders.Names.CONTENT_TYPE);
replacement.append(": ");
replacement.append(HttpPostBodyUtil.MULTIPART_MIXED);
replacement.append("; ");
replacement.append(HttpHeaders.Values.BOUNDARY);
replacement.append('=');
replacement.append(multipartMixedBoundary);
replacement.append("\r\n\r\n");
replacement.append("--");
replacement.append(multipartMixedBoundary);
replacement.append("\r\n");
replacement.append(HttpPostBodyUtil.CONTENT_DISPOSITION);
replacement.append(": ");
replacement.append(HttpPostBodyUtil.ATTACHMENT);
replacement.append("; ");
replacement.append(HttpPostBodyUtil.FILENAME);
replacement.append("=\"");
replacement.append(fileUpload.getFilename());
replacement.append("\"\r\n");
pastAttribute.setValue(replacement.toString(), 1);
pastAttribute.setValue("", 2); pastAttribute.setValue("", 2);
// update past size // update past size
globalBodySize += pastAttribute.size(); globalBodySize += pastAttribute.size();
@ -677,10 +710,9 @@ public class HttpPostRequestEncoder implements ChunkedInput<HttpContent> {
headers.remove(HttpHeaders.Names.CONTENT_TYPE); headers.remove(HttpHeaders.Names.CONTENT_TYPE);
for (String contentType : contentTypes) { for (String contentType : contentTypes) {
// "multipart/form-data; boundary=--89421926422648" // "multipart/form-data; boundary=--89421926422648"
if (contentType.toLowerCase().startsWith(HttpHeaders.Values.MULTIPART_FORM_DATA.toString())) { String lowercased = contentType.toLowerCase();
// ignore if (lowercased.startsWith(HttpHeaders.Values.MULTIPART_FORM_DATA.toString()) ||
} else if (contentType.toLowerCase().startsWith( lowercased.startsWith(HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED.toString())) {
HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED.toString())) {
// ignore // ignore
} else { } else {
headers.add(HttpHeaders.Names.CONTENT_TYPE, contentType); headers.add(HttpHeaders.Names.CONTENT_TYPE, contentType);

View File

@ -15,22 +15,19 @@
*/ */
package io.netty.handler.codec.http.multipart; package io.netty.handler.codec.http.multipart;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import java.io.File;
import java.lang.reflect.Field;
import java.util.List;
import io.netty.handler.codec.http.multipart.HttpPostRequestEncoder.EncoderMode;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.DefaultFullHttpRequest; import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpVersion; import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.multipart.HttpPostRequestEncoder.EncoderMode;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import org.junit.Test;
import java.io.File;
import java.util.List;
import static org.junit.Assert.*;
/** {@link HttpPostRequestEncoder} test case. */ /** {@link HttpPostRequestEncoder} test case. */
public class HttpPostRequestEncoderTest { public class HttpPostRequestEncoderTest {
@ -45,7 +42,7 @@ public class HttpPostRequestEncoderTest {
encoder.addBodyAttribute("foo", "bar"); encoder.addBodyAttribute("foo", "bar");
encoder.addBodyFileUpload("quux", file1, "text/plain", false); encoder.addBodyFileUpload("quux", file1, "text/plain", false);
String multipartDataBoundary = getEncoderField(encoder, "multipartDataBoundary"); String multipartDataBoundary = encoder.multipartDataBoundary;
String content = getRequestBody(encoder); String content = getRequestBody(encoder);
String expected = "--" + multipartDataBoundary + "\r\n" + String expected = "--" + multipartDataBoundary + "\r\n" +
@ -80,8 +77,8 @@ public class HttpPostRequestEncoderTest {
// We have to query the value of these two fields before finalizing // We have to query the value of these two fields before finalizing
// the request, which unsets one of them. // the request, which unsets one of them.
String multipartDataBoundary = getEncoderField(encoder, "multipartDataBoundary"); String multipartDataBoundary = encoder.multipartDataBoundary;
String multipartMixedBoundary = getEncoderField(encoder, "multipartMixedBoundary"); String multipartMixedBoundary = encoder.multipartMixedBoundary;
String content = getRequestBody(encoder); String content = getRequestBody(encoder);
String expected = "--" + multipartDataBoundary + "\r\n" + String expected = "--" + multipartDataBoundary + "\r\n" +
@ -128,7 +125,7 @@ public class HttpPostRequestEncoderTest {
encoder.addBodyFileUpload("quux", file1, "text/plain", false); encoder.addBodyFileUpload("quux", file1, "text/plain", false);
encoder.addBodyFileUpload("quux", file2, "text/plain", false); encoder.addBodyFileUpload("quux", file2, "text/plain", false);
String multipartDataBoundary = getEncoderField(encoder, "multipartDataBoundary"); String multipartDataBoundary = encoder.multipartDataBoundary;
String content = getRequestBody(encoder); String content = getRequestBody(encoder);
String expected = "--" + multipartDataBoundary + "\r\n" + String expected = "--" + multipartDataBoundary + "\r\n" +
@ -167,7 +164,7 @@ public class HttpPostRequestEncoderTest {
encoder.addBodyAttribute("foo", "bar"); encoder.addBodyAttribute("foo", "bar");
encoder.addBodyFileUpload("quux", file1, "text/plain", false); encoder.addBodyFileUpload("quux", file1, "text/plain", false);
String multipartDataBoundary = getEncoderField(encoder, "multipartDataBoundary"); String multipartDataBoundary = encoder.multipartDataBoundary;
String content = getRequestBody(encoder); String content = getRequestBody(encoder);
String expected = "--" + multipartDataBoundary + "\r\n" + String expected = "--" + multipartDataBoundary + "\r\n" +
@ -188,10 +185,10 @@ public class HttpPostRequestEncoderTest {
assertEquals(expected, content); assertEquals(expected, content);
} }
private String getRequestBody(HttpPostRequestEncoder encoder) throws Exception { private static String getRequestBody(HttpPostRequestEncoder encoder) throws Exception {
encoder.finalizeRequest(); encoder.finalizeRequest();
List<InterfaceHttpData> chunks = getEncoderField(encoder, "multipartHttpDatas"); List<InterfaceHttpData> chunks = encoder.multipartHttpDatas;
ByteBuf[] buffers = new ByteBuf[chunks.size()]; ByteBuf[] buffers = new ByteBuf[chunks.size()];
for (int i = 0; i < buffers.length; i++) { for (int i = 0; i < buffers.length; i++) {
@ -205,15 +202,4 @@ public class HttpPostRequestEncoderTest {
return Unpooled.wrappedBuffer(buffers).toString(CharsetUtil.UTF_8); return Unpooled.wrappedBuffer(buffers).toString(CharsetUtil.UTF_8);
} }
private <A> A getEncoderField(HttpPostRequestEncoder encoder, String fieldName) throws Exception {
return this.<A, HttpPostRequestEncoder>getField(encoder, HttpPostRequestEncoder.class, fieldName);
}
@SuppressWarnings("unchecked")
private <A, T> A getField(T instance, Class<T> klass, String fieldName) throws Exception {
Field requestField = klass.getDeclaredField(fieldName);
requestField.setAccessible(true);
return (A) requestField.get(instance);
}
} }