This commit is contained in:
RedKale
2016-02-23 11:16:24 +08:00
parent 10f7f3e4fd
commit 82af35c0fe
3 changed files with 32 additions and 28 deletions

View File

@@ -109,15 +109,15 @@ public class HttpRequest extends Request<HttpContext> {
}
if (this.requestURI.contains("../")) return -1;
index = ++offset;
this.protocol = array.toString(index, array.count() - index, charset).trim();
this.protocol = array.toString(index, array.size() - index, charset).trim();
while (readLine(buffer, array)) {
if (array.count() < 2) break;
if (array.size() < 2) break;
index = 0;
offset = array.find(index, ':');
if (offset <= 0) return -1;
String name = array.toString(index, offset, charset).trim();
index = offset + 1;
String value = array.toString(index, array.count() - index, charset).trim();
String value = array.toString(index, array.size() - index, charset).trim();
switch (name) {
case "Content-Type":
this.contentType = value;
@@ -143,15 +143,15 @@ public class HttpRequest extends Request<HttpContext> {
header.addValue(name, value);
}
}
array.clear();
if (buffer.hasRemaining()) array.add(buffer, buffer.remaining());
array.reset();
if (buffer.hasRemaining()) array.write(buffer, buffer.remaining());
if (this.contentType != null && this.contentType.contains("boundary=")) {
this.boundary = true;
}
if(this.boundary) this.keepAlive = false; //文件上传必须设置keepAlive为false因为文件过大时用户不一定会skip掉多余的数据
if (this.boundary) this.keepAlive = false; //文件上传必须设置keepAlive为false因为文件过大时用户不一定会skip掉多余的数据
if (this.contentLength > 0 && (this.contentType == null || !this.boundary)) {
if (this.contentLength > context.getMaxbody()) return -1;
int lr = (int) this.contentLength - array.count();
int lr = (int) this.contentLength - array.size();
return lr > 0 ? lr : 0;
}
return 0;
@@ -160,7 +160,7 @@ public class HttpRequest extends Request<HttpContext> {
@Override
protected int readBody(ByteBuffer buffer) {
int len = buffer.remaining();
array.add(buffer, len);
array.write(buffer, len);
return len;
}
@@ -170,7 +170,7 @@ public class HttpRequest extends Request<HttpContext> {
private void parseBody() {
if (this.boundary || bodyparsed) return;
addParameter(array, 0, array.count());
addParameter(array, 0, array.size());
bodyparsed = true;
}
@@ -195,15 +195,15 @@ public class HttpRequest extends Request<HttpContext> {
private boolean readLine(ByteBuffer buffer, ByteArray bytes) {
byte lasted = '\r';
bytes.clear();
bytes.size();
for (;;) {
if (!buffer.hasRemaining()) {
if (lasted != '\r') bytes.add(lasted);
if (lasted != '\r') bytes.write(lasted);
return false;
}
byte b = buffer.get();
if (b == -1 || (lasted == '\r' && b == '\n')) break;
if (lasted != '\r') bytes.add(lasted);
if (lasted != '\r') bytes.write(lasted);
lasted = b;
}
return true;
@@ -286,10 +286,10 @@ public class HttpRequest extends Request<HttpContext> {
*/
public final MultiContext getMultiContext() {
return new MultiContext(context.getCharset(), this.getContentType(), this.params,
new BufferedInputStream(Channels.newInputStream(this.channel), Math.max(array.count(), 8192)) {
new BufferedInputStream(Channels.newInputStream(this.channel), Math.max(array.size(), 8192)) {
{
array.write(this.buf);
this.count = array.count();
array.copyTo(this.buf);
this.count = array.size();
}
}, null);
}
@@ -321,7 +321,7 @@ public class HttpRequest extends Request<HttpContext> {
this.header.clear();
this.params.clear();
this.array.clear();
this.array.reset();
super.recycle();
}

View File

@@ -214,22 +214,22 @@ public final class MultiContext {
private String readLine(boolean bd) throws IOException { // bd : 是否是读取boundary
byte lasted = '\r';
buf.clear();
buf.reset();
final int bc = this.endboundarray.length;
int c = 0;
for (;;) {
int b = in.read();
c++;
if (b == -1 || (lasted == '\r' && b == '\n')) break;
if (lasted != '\r') buf.add(lasted);
if (lasted != '\r') buf.write(lasted);
lasted = (byte) b;
if (bd && bc == c) {
buf.add(lasted);
buf.write(lasted);
if (buf.equal(this.endboundarray)) break;
buf.removeLastByte();
}
}
if (buf.count() == 0) return "";
if (buf.size() == 0) return "";
return buf.toString(this.charset).trim();
}

View File

@@ -30,7 +30,7 @@ public final class ByteArray {
content = new byte[Math.max(128, size)];
}
public void clear() {
public void reset() {
this.count = 0;
}
@@ -50,7 +50,7 @@ public final class ByteArray {
return count == 0;
}
public int count() {
public int size() {
return count;
}
@@ -58,7 +58,11 @@ public final class ByteArray {
return content[index];
}
public void write(byte[] buf) {
public byte getLastByte() {
return content[count - 1];
}
public void copyTo(byte[] buf) {
System.arraycopy(this.content, 0, buf, 0, count);
}
@@ -91,11 +95,11 @@ public final class ByteArray {
if (count > 0) count--;
}
public void addInt(int value) {
add((byte) (value >> 24 & 0xFF), (byte) (value >> 16 & 0xFF), (byte) (value >> 8 & 0xFF), (byte) (value & 0xFF));
public void writeInt(int value) {
write((byte) (value >> 24 & 0xFF), (byte) (value >> 16 & 0xFF), (byte) (value >> 8 & 0xFF), (byte) (value & 0xFF));
}
public void add(byte value) {
public void write(byte value) {
if (count >= content.length - 1) {
byte[] ns = new byte[content.length + 8];
System.arraycopy(content, 0, ns, 0, count);
@@ -104,7 +108,7 @@ public final class ByteArray {
content[count++] = value;
}
public void add(byte... values) {
public void write(byte... values) {
if (count >= content.length - values.length) {
byte[] ns = new byte[content.length + values.length];
System.arraycopy(content, 0, ns, 0, count);
@@ -114,7 +118,7 @@ public final class ByteArray {
count += values.length;
}
public void add(ByteBuffer buffer, int len) {
public void write(ByteBuffer buffer, int len) {
if (len < 1) return;
if (count >= content.length - len) {
byte[] ns = new byte[content.length + len];