2007-10-25 23:07:30 -04:00
|
|
|
package org.jruby.mongrel;
|
|
|
|
|
|
|
|
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;
|
2010-01-22 18:01:10 -05:00
|
|
|
import org.jruby.runtime.ThreadContext;
|
2007-10-25 23:07:30 -04:00
|
|
|
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>
|
|
|
|
*/
|
|
|
|
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.";
|
|
|
|
public final static int MAX_REQUEST_PATH_LENGTH = 1024;
|
|
|
|
public final static String MAX_REQUEST_PATH_LENGTH_ERR = "HTTP element REQUEST_PATH is longer than the 1024 allowed length.";
|
|
|
|
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.";
|
|
|
|
|
|
|
|
|
|
|
|
private static ObjectAllocator ALLOCATOR = new ObjectAllocator() {
|
|
|
|
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
|
|
|
|
return new Http11(runtime, klass);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public static void createHttp11(Ruby runtime) {
|
|
|
|
RubyModule mMongrel = runtime.defineModule("Mongrel");
|
|
|
|
mMongrel.defineClassUnder("HttpParserError",runtime.getClass("IOError"),runtime.getClass("IOError").getAllocator());
|
|
|
|
|
|
|
|
RubyClass cHttpParser = mMongrel.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 RubyClass eHttpParserError;
|
|
|
|
private Http11Parser hp;
|
|
|
|
|
|
|
|
public Http11(Ruby runtime, RubyClass clazz) {
|
|
|
|
super(runtime,clazz);
|
|
|
|
this.runtime = runtime;
|
|
|
|
this.eHttpParserError = (RubyClass)runtime.getModule("Mongrel").getConstant("HttpParserError");
|
|
|
|
this.hp = new Http11Parser();
|
|
|
|
this.hp.parser.http_field = http_field;
|
|
|
|
this.hp.parser.request_method = request_method;
|
|
|
|
this.hp.parser.request_uri = request_uri;
|
|
|
|
this.hp.parser.fragment = fragment;
|
|
|
|
this.hp.parser.request_path = request_path;
|
|
|
|
this.hp.parser.query_string = query_string;
|
|
|
|
this.hp.parser.http_version = http_version;
|
|
|
|
this.hp.parser.header_done = header_done;
|
|
|
|
this.hp.parser.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void validateMaxLength(int len, int max, String msg) {
|
|
|
|
if(len>max) {
|
|
|
|
throw new RaiseException(runtime, eHttpParserError, msg, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Http11Parser.FieldCB http_field = new Http11Parser.FieldCB() {
|
|
|
|
public void call(Object data, int field, int flen, int value, int vlen) {
|
|
|
|
RubyHash req = (RubyHash)data;
|
|
|
|
RubyString v,f;
|
|
|
|
validateMaxLength(flen, MAX_FIELD_NAME_LENGTH, MAX_FIELD_NAME_LENGTH_ERR);
|
|
|
|
validateMaxLength(vlen, MAX_FIELD_VALUE_LENGTH, MAX_FIELD_VALUE_LENGTH_ERR);
|
|
|
|
|
|
|
|
v = RubyString.newString(runtime, new ByteList(Http11.this.hp.parser.buffer,value,vlen));
|
|
|
|
f = RubyString.newString(runtime, "HTTP_");
|
|
|
|
ByteList b = new ByteList(Http11.this.hp.parser.buffer,field,flen);
|
2010-01-22 18:01:10 -05:00
|
|
|
for(int i = 0,j = b.length();i<j;i++) {
|
|
|
|
if((b.get(i) & 0xFF) == '-') {
|
|
|
|
b.set(i, (byte)'_');
|
2007-10-25 23:07:30 -04:00
|
|
|
} else {
|
2010-01-22 18:01:10 -05:00
|
|
|
b.set(i, (byte)Character.toUpperCase((char)b.get(i)));
|
2007-10-25 23:07:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
f.cat(b);
|
2010-01-22 18:01:10 -05:00
|
|
|
req.op_aset(req.getRuntime().getCurrentContext(), f,v);
|
2007-10-25 23:07:30 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private Http11Parser.ElementCB request_method = new Http11Parser.ElementCB() {
|
|
|
|
public void call(Object data, int at, int length) {
|
|
|
|
RubyHash req = (RubyHash)data;
|
|
|
|
RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
|
2010-01-22 18:01:10 -05:00
|
|
|
req.op_aset(req.getRuntime().getCurrentContext(), runtime.newString("REQUEST_METHOD"),val);
|
2007-10-25 23:07:30 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private Http11Parser.ElementCB request_uri = new Http11Parser.ElementCB() {
|
|
|
|
public void call(Object data, int at, int length) {
|
|
|
|
RubyHash req = (RubyHash)data;
|
|
|
|
validateMaxLength(length, MAX_REQUEST_URI_LENGTH, MAX_REQUEST_URI_LENGTH_ERR);
|
|
|
|
RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
|
2010-01-22 18:01:10 -05:00
|
|
|
req.op_aset(req.getRuntime().getCurrentContext(), runtime.newString("REQUEST_URI"),val);
|
2007-10-25 23:07:30 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private Http11Parser.ElementCB fragment = new Http11Parser.ElementCB() {
|
|
|
|
public void call(Object data, int at, int length) {
|
|
|
|
RubyHash req = (RubyHash)data;
|
|
|
|
validateMaxLength(length, MAX_FRAGMENT_LENGTH, MAX_FRAGMENT_LENGTH_ERR);
|
|
|
|
RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
|
2010-01-22 18:01:10 -05:00
|
|
|
req.op_aset(req.getRuntime().getCurrentContext(), runtime.newString("FRAGMENT"),val);
|
2007-10-25 23:07:30 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private Http11Parser.ElementCB request_path = new Http11Parser.ElementCB() {
|
|
|
|
public void call(Object data, int at, int length) {
|
|
|
|
RubyHash req = (RubyHash)data;
|
|
|
|
validateMaxLength(length, MAX_REQUEST_PATH_LENGTH, MAX_REQUEST_PATH_LENGTH_ERR);
|
|
|
|
RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
|
2010-01-22 18:01:10 -05:00
|
|
|
req.op_aset(req.getRuntime().getCurrentContext(), runtime.newString("REQUEST_PATH"),val);
|
2007-10-25 23:07:30 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private Http11Parser.ElementCB query_string = new Http11Parser.ElementCB() {
|
|
|
|
public void call(Object data, int at, int length) {
|
|
|
|
RubyHash req = (RubyHash)data;
|
|
|
|
validateMaxLength(length, MAX_QUERY_STRING_LENGTH, MAX_QUERY_STRING_LENGTH_ERR);
|
|
|
|
RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
|
2010-01-22 18:01:10 -05:00
|
|
|
req.op_aset(req.getRuntime().getCurrentContext(), runtime.newString("QUERY_STRING"),val);
|
2007-10-25 23:07:30 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private Http11Parser.ElementCB http_version = new Http11Parser.ElementCB() {
|
|
|
|
public void call(Object data, int at, int length) {
|
|
|
|
RubyHash req = (RubyHash)data;
|
|
|
|
RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
|
2010-01-22 18:01:10 -05:00
|
|
|
req.op_aset(req.getRuntime().getCurrentContext(), runtime.newString("HTTP_VERSION"),val);
|
2007-10-25 23:07:30 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private Http11Parser.ElementCB header_done = new Http11Parser.ElementCB() {
|
|
|
|
public void call(Object data, int at, int length) {
|
|
|
|
RubyHash req = (RubyHash)data;
|
2010-01-22 18:01:10 -05:00
|
|
|
ThreadContext context = req.getRuntime().getCurrentContext();
|
2007-10-25 23:07:30 -04:00
|
|
|
IRubyObject temp,ctype,clen;
|
2010-07-24 13:04:37 -04:00
|
|
|
|
2010-01-22 18:01:10 -05:00
|
|
|
clen = req.op_aref(context, runtime.newString("HTTP_CONTENT_LENGTH"));
|
2007-10-25 23:07:30 -04:00
|
|
|
if(!clen.isNil()) {
|
2010-01-22 18:01:10 -05:00
|
|
|
req.op_aset(context, runtime.newString("CONTENT_LENGTH"),clen);
|
2007-10-25 23:07:30 -04:00
|
|
|
}
|
|
|
|
|
2010-01-22 18:01:10 -05:00
|
|
|
ctype = req.op_aref(context, runtime.newString("HTTP_CONTENT_TYPE"));
|
2007-10-25 23:07:30 -04:00
|
|
|
if(!ctype.isNil()) {
|
2010-01-22 18:01:10 -05:00
|
|
|
req.op_aset(context, runtime.newString("CONTENT_TYPE"),ctype);
|
2007-10-25 23:07:30 -04:00
|
|
|
}
|
|
|
|
|
2010-01-22 18:01:10 -05:00
|
|
|
req.op_aset(context, runtime.newString("GATEWAY_INTERFACE"),runtime.newString("CGI/1.2"));
|
|
|
|
if(!(temp = req.op_aref(context, runtime.newString("HTTP_HOST"))).isNil()) {
|
2007-10-25 23:07:30 -04:00
|
|
|
String s = temp.toString();
|
|
|
|
int colon = s.indexOf(':');
|
|
|
|
if(colon != -1) {
|
2010-01-22 18:01:10 -05:00
|
|
|
req.op_aset(context, runtime.newString("SERVER_NAME"),runtime.newString(s.substring(0,colon)));
|
|
|
|
req.op_aset(context, runtime.newString("SERVER_PORT"),runtime.newString(s.substring(colon+1)));
|
2007-10-25 23:07:30 -04:00
|
|
|
} else {
|
2010-01-22 18:01:10 -05:00
|
|
|
req.op_aset(context, runtime.newString("SERVER_NAME"),temp);
|
|
|
|
req.op_aset(context, runtime.newString("SERVER_PORT"),runtime.newString("80"));
|
2007-10-25 23:07:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
req.setInstanceVariable("@http_body", RubyString.newString(runtime, new ByteList(hp.parser.buffer, at, length)));
|
2010-01-22 18:01:10 -05:00
|
|
|
req.op_aset(context, runtime.newString("SERVER_PROTOCOL"),runtime.newString("HTTP/1.1"));
|
2010-07-24 13:04:12 -04:00
|
|
|
req.op_aset(context, runtime.newString("SERVER_SOFTWARE"),runtime.newString("Mongrel 1.2.0.beta.1"));
|
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) {
|
|
|
|
int from = 0;
|
|
|
|
from = RubyNumeric.fix2int(start);
|
|
|
|
ByteList d = ((RubyString)data).getByteList();
|
2010-01-22 18:01:10 -05:00
|
|
|
if(from >= d.length()) {
|
2007-10-25 23:07:30 -04:00
|
|
|
throw new RaiseException(runtime, eHttpParserError, "Requested start is after data buffer end.", true);
|
|
|
|
} else {
|
|
|
|
this.hp.parser.data = req_hash;
|
|
|
|
this.hp.execute(d,from);
|
|
|
|
validateMaxLength(this.hp.parser.nread,MAX_HEADER_LENGTH, MAX_HEADER_LENGTH_ERR);
|
|
|
|
if(this.hp.has_error()) {
|
|
|
|
throw new RaiseException(runtime, eHttpParserError, "Invalid HTTP format, parsing fails.", true);
|
|
|
|
} else {
|
|
|
|
return runtime.newFixnum(this.hp.parser.nread);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}// Http11
|