2011-11-22 14:43:54 -05:00
|
|
|
package org.jruby.puma;
|
2007-10-25 23:07:30 -04:00
|
|
|
|
|
|
|
import org.jruby.Ruby;
|
|
|
|
import org.jruby.RubyClass;
|
|
|
|
import org.jruby.RubyHash;
|
|
|
|
import org.jruby.RubyModule;
|
|
|
|
import org.jruby.RubyNumeric;
|
|
|
|
import org.jruby.RubyObject;
|
|
|
|
import org.jruby.RubyString;
|
|
|
|
|
2010-01-22 18:01:10 -05:00
|
|
|
import org.jruby.anno.JRubyMethod;
|
|
|
|
|
2007-10-25 23:07:30 -04:00
|
|
|
import org.jruby.runtime.ObjectAllocator;
|
|
|
|
import org.jruby.runtime.builtin.IRubyObject;
|
|
|
|
|
|
|
|
import org.jruby.exceptions.RaiseException;
|
|
|
|
|
|
|
|
import org.jruby.util.ByteList;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author <a href="mailto:ola.bini@ki.se">Ola Bini</a>
|
2019-10-05 13:33:04 -04:00
|
|
|
* @author <a href="mailto:headius@headius.com">Charles Oliver Nutter</a>
|
2007-10-25 23:07:30 -04:00
|
|
|
*/
|
|
|
|
public class Http11 extends RubyObject {
|
|
|
|
public final static int MAX_FIELD_NAME_LENGTH = 256;
|
|
|
|
public final static String MAX_FIELD_NAME_LENGTH_ERR = "HTTP element FIELD_NAME is longer than the 256 allowed length.";
|
|
|
|
public final static int MAX_FIELD_VALUE_LENGTH = 80 * 1024;
|
|
|
|
public final static String MAX_FIELD_VALUE_LENGTH_ERR = "HTTP element FIELD_VALUE is longer than the 81920 allowed length.";
|
|
|
|
public final static int MAX_REQUEST_URI_LENGTH = 1024 * 12;
|
|
|
|
public final static String MAX_REQUEST_URI_LENGTH_ERR = "HTTP element REQUEST_URI is longer than the 12288 allowed length.";
|
|
|
|
public final static int MAX_FRAGMENT_LENGTH = 1024;
|
|
|
|
public final static String MAX_FRAGMENT_LENGTH_ERR = "HTTP element REQUEST_PATH is longer than the 1024 allowed length.";
|
2020-03-10 13:39:36 -04:00
|
|
|
public final static int MAX_REQUEST_PATH_LENGTH = 8192;
|
|
|
|
public final static String MAX_REQUEST_PATH_LENGTH_ERR = "HTTP element REQUEST_PATH is longer than the 8192 allowed length.";
|
2007-10-25 23:07:30 -04:00
|
|
|
public final static int MAX_QUERY_STRING_LENGTH = 1024 * 10;
|
|
|
|
public final static String MAX_QUERY_STRING_LENGTH_ERR = "HTTP element QUERY_STRING is longer than the 10240 allowed length.";
|
|
|
|
public final static int MAX_HEADER_LENGTH = 1024 * (80 + 32);
|
|
|
|
public final static String MAX_HEADER_LENGTH_ERR = "HTTP element HEADER is longer than the 114688 allowed length.";
|
|
|
|
|
2019-10-05 12:46:08 -04:00
|
|
|
public static final ByteList CONTENT_TYPE_BYTELIST = new ByteList(ByteList.plain("CONTENT_TYPE"));
|
|
|
|
public static final ByteList CONTENT_LENGTH_BYTELIST = new ByteList(ByteList.plain("CONTENT_LENGTH"));
|
|
|
|
public static final ByteList HTTP_PREFIX_BYTELIST = new ByteList(ByteList.plain("HTTP_"));
|
|
|
|
public static final ByteList COMMA_SPACE_BYTELIST = new ByteList(ByteList.plain(", "));
|
|
|
|
public static final ByteList REQUEST_METHOD_BYTELIST = new ByteList(ByteList.plain("REQUEST_METHOD"));
|
|
|
|
public static final ByteList REQUEST_URI_BYTELIST = new ByteList(ByteList.plain("REQUEST_URI"));
|
|
|
|
public static final ByteList FRAGMENT_BYTELIST = new ByteList(ByteList.plain("FRAGMENT"));
|
|
|
|
public static final ByteList REQUEST_PATH_BYTELIST = new ByteList(ByteList.plain("REQUEST_PATH"));
|
|
|
|
public static final ByteList QUERY_STRING_BYTELIST = new ByteList(ByteList.plain("QUERY_STRING"));
|
|
|
|
public static final ByteList HTTP_VERSION_BYTELIST = new ByteList(ByteList.plain("HTTP_VERSION"));
|
2007-10-25 23:07:30 -04:00
|
|
|
|
|
|
|
private static ObjectAllocator ALLOCATOR = new ObjectAllocator() {
|
|
|
|
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
|
|
|
|
return new Http11(runtime, klass);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public static void createHttp11(Ruby runtime) {
|
2011-11-22 14:43:54 -05:00
|
|
|
RubyModule mPuma = runtime.defineModule("Puma");
|
|
|
|
mPuma.defineClassUnder("HttpParserError",runtime.getClass("IOError"),runtime.getClass("IOError").getAllocator());
|
2007-10-25 23:07:30 -04:00
|
|
|
|
2011-11-22 14:43:54 -05:00
|
|
|
RubyClass cHttpParser = mPuma.defineClassUnder("HttpParser",runtime.getObject(),ALLOCATOR);
|
2010-01-22 18:01:10 -05:00
|
|
|
cHttpParser.defineAnnotatedMethods(Http11.class);
|
2007-10-25 23:07:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private Ruby runtime;
|
|
|
|
private Http11Parser hp;
|
2011-11-22 14:43:54 -05:00
|
|
|
private RubyString body;
|
2007-10-25 23:07:30 -04:00
|
|
|
|
|
|
|
public Http11(Ruby runtime, RubyClass clazz) {
|
|
|
|
super(runtime,clazz);
|
|
|
|
this.runtime = runtime;
|
|
|
|
this.hp = new Http11Parser();
|
|
|
|
this.hp.parser.init();
|
|
|
|
}
|
|
|
|
|
2019-10-05 13:42:33 -04:00
|
|
|
public static void validateMaxLength(Ruby runtime, int len, int max, String msg) {
|
2007-10-25 23:07:30 -04:00
|
|
|
if(len>max) {
|
2019-10-05 13:42:33 -04:00
|
|
|
throw newHTTPParserError(runtime, msg);
|
2007-10-25 23:07:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-05 13:42:33 -04:00
|
|
|
private static RaiseException newHTTPParserError(Ruby runtime, String msg) {
|
|
|
|
return runtime.newRaiseException(getHTTPParserError(runtime), msg);
|
2019-10-05 12:46:08 -04:00
|
|
|
}
|
2007-10-25 23:07:30 -04:00
|
|
|
|
2019-10-05 13:42:33 -04:00
|
|
|
private static RubyClass getHTTPParserError(Ruby runtime) {
|
|
|
|
// Cheaper to look this up lazily than cache eagerly and consume a field, since it's rarely encountered
|
|
|
|
return (RubyClass)runtime.getModule("Puma").getConstant("HttpParserError");
|
|
|
|
}
|
2007-10-25 23:07:30 -04:00
|
|
|
|
2019-10-05 17:05:35 -04:00
|
|
|
public static void http_field(Ruby runtime, RubyHash req, ByteList buffer, int field, int flen, int value, int vlen) {
|
|
|
|
RubyString f;
|
|
|
|
IRubyObject v;
|
|
|
|
validateMaxLength(runtime, flen, MAX_FIELD_NAME_LENGTH, MAX_FIELD_NAME_LENGTH_ERR);
|
|
|
|
validateMaxLength(runtime, vlen, MAX_FIELD_VALUE_LENGTH, MAX_FIELD_VALUE_LENGTH_ERR);
|
|
|
|
|
|
|
|
ByteList b = new ByteList(buffer,field,flen);
|
|
|
|
for(int i = 0,j = b.length();i<j;i++) {
|
|
|
|
int bite = b.get(i) & 0xFF;
|
|
|
|
if(bite == '-') {
|
|
|
|
b.set(i, (byte)'_');
|
|
|
|
} else {
|
|
|
|
b.set(i, (byte)Character.toUpperCase(bite));
|
2007-10-25 23:07:30 -04:00
|
|
|
}
|
2019-10-05 17:05:35 -04:00
|
|
|
}
|
2007-10-25 23:07:30 -04:00
|
|
|
|
2019-10-07 09:29:57 -04:00
|
|
|
while (vlen > 0 && Character.isWhitespace(buffer.get(value + vlen - 1))) vlen--;
|
|
|
|
|
2019-10-05 17:05:35 -04:00
|
|
|
if (b.equals(CONTENT_LENGTH_BYTELIST) || b.equals(CONTENT_TYPE_BYTELIST)) {
|
|
|
|
f = RubyString.newString(runtime, b);
|
|
|
|
} else {
|
|
|
|
f = RubyString.newStringShared(runtime, HTTP_PREFIX_BYTELIST);
|
|
|
|
f.cat(b);
|
|
|
|
}
|
2007-10-25 23:07:30 -04:00
|
|
|
|
2019-10-05 17:05:35 -04:00
|
|
|
b = new ByteList(buffer, value, vlen);
|
|
|
|
v = req.fastARef(f);
|
|
|
|
if (v == null || v.isNil()) {
|
|
|
|
req.fastASet(f, RubyString.newString(runtime, b));
|
|
|
|
} else {
|
|
|
|
RubyString vs = v.convertToString();
|
|
|
|
vs.cat(COMMA_SPACE_BYTELIST);
|
|
|
|
vs.cat(b);
|
|
|
|
}
|
|
|
|
}
|
2007-10-25 23:07:30 -04:00
|
|
|
|
2019-10-05 17:05:35 -04:00
|
|
|
public static void request_method(Ruby runtime, RubyHash req, ByteList buffer, int at, int length) {
|
|
|
|
RubyString val = RubyString.newString(runtime,new ByteList(buffer,at,length));
|
|
|
|
req.fastASet(RubyString.newStringShared(runtime, REQUEST_METHOD_BYTELIST),val);
|
|
|
|
}
|
2007-10-25 23:07:30 -04:00
|
|
|
|
2019-10-05 17:05:35 -04:00
|
|
|
public static void request_uri(Ruby runtime, RubyHash req, ByteList buffer, int at, int length) {
|
|
|
|
validateMaxLength(runtime, length, MAX_REQUEST_URI_LENGTH, MAX_REQUEST_URI_LENGTH_ERR);
|
|
|
|
RubyString val = RubyString.newString(runtime,new ByteList(buffer,at,length));
|
|
|
|
req.fastASet(RubyString.newStringShared(runtime, REQUEST_URI_BYTELIST),val);
|
|
|
|
}
|
2007-10-25 23:07:30 -04:00
|
|
|
|
2019-10-05 17:05:35 -04:00
|
|
|
public static void fragment(Ruby runtime, RubyHash req, ByteList buffer, int at, int length) {
|
|
|
|
validateMaxLength(runtime, length, MAX_FRAGMENT_LENGTH, MAX_FRAGMENT_LENGTH_ERR);
|
|
|
|
RubyString val = RubyString.newString(runtime,new ByteList(buffer,at,length));
|
|
|
|
req.fastASet(RubyString.newStringShared(runtime, FRAGMENT_BYTELIST),val);
|
|
|
|
}
|
2007-10-25 23:07:30 -04:00
|
|
|
|
2019-10-05 17:05:35 -04:00
|
|
|
public static void request_path(Ruby runtime, RubyHash req, ByteList buffer, int at, int length) {
|
|
|
|
validateMaxLength(runtime, length, MAX_REQUEST_PATH_LENGTH, MAX_REQUEST_PATH_LENGTH_ERR);
|
|
|
|
RubyString val = RubyString.newString(runtime,new ByteList(buffer,at,length));
|
|
|
|
req.fastASet(RubyString.newStringShared(runtime, REQUEST_PATH_BYTELIST),val);
|
|
|
|
}
|
2007-10-25 23:07:30 -04:00
|
|
|
|
2019-10-05 17:05:35 -04:00
|
|
|
public static void query_string(Ruby runtime, RubyHash req, ByteList buffer, int at, int length) {
|
|
|
|
validateMaxLength(runtime, length, MAX_QUERY_STRING_LENGTH, MAX_QUERY_STRING_LENGTH_ERR);
|
|
|
|
RubyString val = RubyString.newString(runtime,new ByteList(buffer,at,length));
|
|
|
|
req.fastASet(RubyString.newStringShared(runtime, QUERY_STRING_BYTELIST),val);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void http_version(Ruby runtime, RubyHash req, ByteList buffer, int at, int length) {
|
|
|
|
RubyString val = RubyString.newString(runtime,new ByteList(buffer,at,length));
|
|
|
|
req.fastASet(RubyString.newStringShared(runtime, HTTP_VERSION_BYTELIST),val);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void header_done(Ruby runtime, RubyHash req, ByteList buffer, int at, int length) {
|
|
|
|
body = RubyString.newStringShared(runtime, new ByteList(buffer, at, length));
|
|
|
|
}
|
2007-10-25 23:07:30 -04:00
|
|
|
|
2010-01-22 18:01:10 -05:00
|
|
|
@JRubyMethod
|
2007-10-25 23:07:30 -04:00
|
|
|
public IRubyObject initialize() {
|
|
|
|
this.hp.parser.init();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2010-01-22 18:01:10 -05:00
|
|
|
@JRubyMethod
|
2007-10-25 23:07:30 -04:00
|
|
|
public IRubyObject reset() {
|
|
|
|
this.hp.parser.init();
|
|
|
|
return runtime.getNil();
|
|
|
|
}
|
|
|
|
|
2010-01-22 18:01:10 -05:00
|
|
|
@JRubyMethod
|
2007-10-25 23:07:30 -04:00
|
|
|
public IRubyObject finish() {
|
|
|
|
this.hp.finish();
|
|
|
|
return this.hp.is_finished() ? runtime.getTrue() : runtime.getFalse();
|
|
|
|
}
|
|
|
|
|
2010-01-22 18:01:10 -05:00
|
|
|
@JRubyMethod
|
2007-10-25 23:07:30 -04:00
|
|
|
public IRubyObject execute(IRubyObject req_hash, IRubyObject data, IRubyObject start) {
|
2019-10-05 13:33:04 -04:00
|
|
|
int from = RubyNumeric.fix2int(start);
|
2007-10-25 23:07:30 -04:00
|
|
|
ByteList d = ((RubyString)data).getByteList();
|
2010-01-22 18:01:10 -05:00
|
|
|
if(from >= d.length()) {
|
2019-10-05 13:42:33 -04:00
|
|
|
throw newHTTPParserError(runtime, "Requested start is after data buffer end.");
|
2007-10-25 23:07:30 -04:00
|
|
|
} else {
|
2019-10-05 13:33:04 -04:00
|
|
|
Http11Parser hp = this.hp;
|
|
|
|
Http11Parser.HttpParser parser = hp.parser;
|
|
|
|
|
|
|
|
parser.data = (RubyHash) req_hash;
|
|
|
|
|
2019-10-05 17:05:35 -04:00
|
|
|
hp.execute(runtime, this, d,from);
|
2019-10-05 13:33:04 -04:00
|
|
|
|
2019-10-05 13:42:33 -04:00
|
|
|
validateMaxLength(runtime, parser.nread,MAX_HEADER_LENGTH, MAX_HEADER_LENGTH_ERR);
|
2019-10-05 13:33:04 -04:00
|
|
|
|
|
|
|
if(hp.has_error()) {
|
2020-03-05 16:00:13 -05:00
|
|
|
throw newHTTPParserError(runtime, "Invalid HTTP format, parsing fails. Are you trying to open an SSL connection to a non-SSL Puma?");
|
2007-10-25 23:07:30 -04:00
|
|
|
} else {
|
2019-10-05 13:33:04 -04:00
|
|
|
return runtime.newFixnum(parser.nread);
|
2007-10-25 23:07:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-22 18:01:10 -05:00
|
|
|
@JRubyMethod(name = "error?")
|
2007-10-25 23:07:30 -04:00
|
|
|
public IRubyObject has_error() {
|
|
|
|
return this.hp.has_error() ? runtime.getTrue() : runtime.getFalse();
|
|
|
|
}
|
|
|
|
|
2010-01-22 18:01:10 -05:00
|
|
|
@JRubyMethod(name = "finished?")
|
2007-10-25 23:07:30 -04:00
|
|
|
public IRubyObject is_finished() {
|
|
|
|
return this.hp.is_finished() ? runtime.getTrue() : runtime.getFalse();
|
|
|
|
}
|
|
|
|
|
2010-01-22 18:01:10 -05:00
|
|
|
@JRubyMethod
|
2007-10-25 23:07:30 -04:00
|
|
|
public IRubyObject nread() {
|
|
|
|
return runtime.newFixnum(this.hp.parser.nread);
|
|
|
|
}
|
2019-10-07 09:29:57 -04:00
|
|
|
|
2011-11-22 14:43:54 -05:00
|
|
|
@JRubyMethod
|
|
|
|
public IRubyObject body() {
|
|
|
|
return body;
|
|
|
|
}
|
2007-10-25 23:07:30 -04:00
|
|
|
}// Http11
|