1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00

Upgrade JRuby parser code to fix deprecation

This commit is contained in:
Nick Sieger 2010-01-22 17:01:10 -06:00
parent 1d1740647b
commit d9479cb6b8
4 changed files with 49 additions and 72 deletions

View file

@ -116,12 +116,12 @@ public class Http11Parser {
public int execute(ByteList buffer, int off) { public int execute(ByteList buffer, int off) {
int p, pe; int p, pe;
int cs = parser.cs; int cs = parser.cs;
int len = buffer.realSize; int len = buffer.length();
assert off<=len : "offset past end of buffer"; assert off<=len : "offset past end of buffer";
p = off; p = off;
pe = len; pe = len;
byte[] data = buffer.bytes; byte[] data = buffer.unsafeBytes();
parser.buffer = buffer; parser.buffer = buffer;
%% write exec; %% write exec;

View file

@ -1,30 +1,3 @@
/***** BEGIN LICENSE BLOCK *****
* Version: CPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Common Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/cpl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2007 Ola Bini <ola@ologix.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the CPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the CPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby.mongrel; package org.jruby.mongrel;
import org.jruby.Ruby; import org.jruby.Ruby;
@ -35,8 +8,10 @@ import org.jruby.RubyNumeric;
import org.jruby.RubyObject; import org.jruby.RubyObject;
import org.jruby.RubyString; import org.jruby.RubyString;
import org.jruby.runtime.CallbackFactory; import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.ObjectAllocator; import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject; import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.exceptions.RaiseException; import org.jruby.exceptions.RaiseException;
@ -73,16 +48,8 @@ public class Http11 extends RubyObject {
RubyModule mMongrel = runtime.defineModule("Mongrel"); RubyModule mMongrel = runtime.defineModule("Mongrel");
mMongrel.defineClassUnder("HttpParserError",runtime.getClass("IOError"),runtime.getClass("IOError").getAllocator()); mMongrel.defineClassUnder("HttpParserError",runtime.getClass("IOError"),runtime.getClass("IOError").getAllocator());
CallbackFactory cf = runtime.callbackFactory(Http11.class);
RubyClass cHttpParser = mMongrel.defineClassUnder("HttpParser",runtime.getObject(),ALLOCATOR); RubyClass cHttpParser = mMongrel.defineClassUnder("HttpParser",runtime.getObject(),ALLOCATOR);
cHttpParser.defineFastMethod("initialize",cf.getFastMethod("initialize")); cHttpParser.defineAnnotatedMethods(Http11.class);
cHttpParser.defineFastMethod("reset",cf.getFastMethod("reset"));
cHttpParser.defineFastMethod("finish",cf.getFastMethod("finish"));
cHttpParser.defineFastMethod("execute",cf.getFastMethod("execute", IRubyObject.class, IRubyObject.class, IRubyObject.class));
cHttpParser.defineFastMethod("error?",cf.getFastMethod("has_error"));
cHttpParser.defineFastMethod("finished?",cf.getFastMethod("is_finished"));
cHttpParser.defineFastMethod("nread",cf.getFastMethod("nread"));
} }
private Ruby runtime; private Ruby runtime;
@ -121,15 +88,15 @@ public class Http11 extends RubyObject {
v = RubyString.newString(runtime, new ByteList(Http11.this.hp.parser.buffer,value,vlen)); v = RubyString.newString(runtime, new ByteList(Http11.this.hp.parser.buffer,value,vlen));
f = RubyString.newString(runtime, "HTTP_"); f = RubyString.newString(runtime, "HTTP_");
ByteList b = new ByteList(Http11.this.hp.parser.buffer,field,flen); ByteList b = new ByteList(Http11.this.hp.parser.buffer,field,flen);
for(int i=0,j=b.realSize;i<j;i++) { for(int i = 0,j = b.length();i<j;i++) {
if((b.bytes[i]&0xFF) == '-') { if((b.get(i) & 0xFF) == '-') {
b.bytes[i] = (byte)'_'; b.set(i, (byte)'_');
} else { } else {
b.bytes[i] = (byte)Character.toUpperCase((char)b.bytes[i]); b.set(i, (byte)Character.toUpperCase((char)b.get(i)));
} }
} }
f.cat(b); f.cat(b);
req.aset(f,v); req.op_aset(req.getRuntime().getCurrentContext(), f,v);
} }
}; };
@ -137,7 +104,7 @@ public class Http11 extends RubyObject {
public void call(Object data, int at, int length) { public void call(Object data, int at, int length) {
RubyHash req = (RubyHash)data; RubyHash req = (RubyHash)data;
RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length)); RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
req.aset(runtime.newString("REQUEST_METHOD"),val); req.op_aset(req.getRuntime().getCurrentContext(), runtime.newString("REQUEST_METHOD"),val);
} }
}; };
@ -146,7 +113,7 @@ public class Http11 extends RubyObject {
RubyHash req = (RubyHash)data; RubyHash req = (RubyHash)data;
validateMaxLength(length, MAX_REQUEST_URI_LENGTH, MAX_REQUEST_URI_LENGTH_ERR); validateMaxLength(length, MAX_REQUEST_URI_LENGTH, MAX_REQUEST_URI_LENGTH_ERR);
RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length)); RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
req.aset(runtime.newString("REQUEST_URI"),val); req.op_aset(req.getRuntime().getCurrentContext(), runtime.newString("REQUEST_URI"),val);
} }
}; };
@ -155,7 +122,7 @@ public class Http11 extends RubyObject {
RubyHash req = (RubyHash)data; RubyHash req = (RubyHash)data;
validateMaxLength(length, MAX_FRAGMENT_LENGTH, MAX_FRAGMENT_LENGTH_ERR); validateMaxLength(length, MAX_FRAGMENT_LENGTH, MAX_FRAGMENT_LENGTH_ERR);
RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length)); RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
req.aset(runtime.newString("FRAGMENT"),val); req.op_aset(req.getRuntime().getCurrentContext(), runtime.newString("FRAGMENT"),val);
} }
}; };
@ -164,7 +131,7 @@ public class Http11 extends RubyObject {
RubyHash req = (RubyHash)data; RubyHash req = (RubyHash)data;
validateMaxLength(length, MAX_REQUEST_PATH_LENGTH, MAX_REQUEST_PATH_LENGTH_ERR); validateMaxLength(length, MAX_REQUEST_PATH_LENGTH, MAX_REQUEST_PATH_LENGTH_ERR);
RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length)); RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
req.aset(runtime.newString("REQUEST_PATH"),val); req.op_aset(req.getRuntime().getCurrentContext(), runtime.newString("REQUEST_PATH"),val);
} }
}; };
@ -173,7 +140,7 @@ public class Http11 extends RubyObject {
RubyHash req = (RubyHash)data; RubyHash req = (RubyHash)data;
validateMaxLength(length, MAX_QUERY_STRING_LENGTH, MAX_QUERY_STRING_LENGTH_ERR); validateMaxLength(length, MAX_QUERY_STRING_LENGTH, MAX_QUERY_STRING_LENGTH_ERR);
RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length)); RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
req.aset(runtime.newString("QUERY_STRING"),val); req.op_aset(req.getRuntime().getCurrentContext(), runtime.newString("QUERY_STRING"),val);
} }
}; };
@ -181,64 +148,69 @@ public class Http11 extends RubyObject {
public void call(Object data, int at, int length) { public void call(Object data, int at, int length) {
RubyHash req = (RubyHash)data; RubyHash req = (RubyHash)data;
RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length)); RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
req.aset(runtime.newString("HTTP_VERSION"),val); req.op_aset(req.getRuntime().getCurrentContext(), runtime.newString("HTTP_VERSION"),val);
} }
}; };
private Http11Parser.ElementCB header_done = new Http11Parser.ElementCB() { private Http11Parser.ElementCB header_done = new Http11Parser.ElementCB() {
public void call(Object data, int at, int length) { public void call(Object data, int at, int length) {
RubyHash req = (RubyHash)data; RubyHash req = (RubyHash)data;
ThreadContext context = req.getRuntime().getCurrentContext();
IRubyObject temp,ctype,clen; IRubyObject temp,ctype,clen;
clen = req.aref(runtime.newString("HTTP_CONTENT_LENGTH")); clen = req.op_aref(context, runtime.newString("HTTP_CONTENT_LENGTH"));
if(!clen.isNil()) { if(!clen.isNil()) {
req.aset(runtime.newString("CONTENT_LENGTH"),clen); req.op_aset(context, runtime.newString("CONTENT_LENGTH"),clen);
} }
ctype = req.aref(runtime.newString("HTTP_CONTENT_TYPE")); ctype = req.op_aref(context, runtime.newString("HTTP_CONTENT_TYPE"));
if(!ctype.isNil()) { if(!ctype.isNil()) {
req.aset(runtime.newString("CONTENT_TYPE"),ctype); req.op_aset(context, runtime.newString("CONTENT_TYPE"),ctype);
} }
req.aset(runtime.newString("GATEWAY_INTERFACE"),runtime.newString("CGI/1.2")); req.op_aset(context, runtime.newString("GATEWAY_INTERFACE"),runtime.newString("CGI/1.2"));
if(!(temp = req.aref(runtime.newString("HTTP_HOST"))).isNil()) { if(!(temp = req.op_aref(context, runtime.newString("HTTP_HOST"))).isNil()) {
String s = temp.toString(); String s = temp.toString();
int colon = s.indexOf(':'); int colon = s.indexOf(':');
if(colon != -1) { if(colon != -1) {
req.aset(runtime.newString("SERVER_NAME"),runtime.newString(s.substring(0,colon))); req.op_aset(context, runtime.newString("SERVER_NAME"),runtime.newString(s.substring(0,colon)));
req.aset(runtime.newString("SERVER_PORT"),runtime.newString(s.substring(colon+1))); req.op_aset(context, runtime.newString("SERVER_PORT"),runtime.newString(s.substring(colon+1)));
} else { } else {
req.aset(runtime.newString("SERVER_NAME"),temp); req.op_aset(context, runtime.newString("SERVER_NAME"),temp);
req.aset(runtime.newString("SERVER_PORT"),runtime.newString("80")); req.op_aset(context, runtime.newString("SERVER_PORT"),runtime.newString("80"));
} }
} }
req.setInstanceVariable("@http_body", RubyString.newString(runtime, new ByteList(hp.parser.buffer, at, length))); req.setInstanceVariable("@http_body", RubyString.newString(runtime, new ByteList(hp.parser.buffer, at, length)));
req.aset(runtime.newString("SERVER_PROTOCOL"),runtime.newString("HTTP/1.1")); req.op_aset(context, runtime.newString("SERVER_PROTOCOL"),runtime.newString("HTTP/1.1"));
req.aset(runtime.newString("SERVER_SOFTWARE"),runtime.newString("Mongrel 1.1.6")); req.op_aset(context, runtime.newString("SERVER_SOFTWARE"),runtime.newString("Mongrel 1.1.6"));
} }
}; };
@JRubyMethod
public IRubyObject initialize() { public IRubyObject initialize() {
this.hp.parser.init(); this.hp.parser.init();
return this; return this;
} }
@JRubyMethod
public IRubyObject reset() { public IRubyObject reset() {
this.hp.parser.init(); this.hp.parser.init();
return runtime.getNil(); return runtime.getNil();
} }
@JRubyMethod
public IRubyObject finish() { public IRubyObject finish() {
this.hp.finish(); this.hp.finish();
return this.hp.is_finished() ? runtime.getTrue() : runtime.getFalse(); return this.hp.is_finished() ? runtime.getTrue() : runtime.getFalse();
} }
@JRubyMethod
public IRubyObject execute(IRubyObject req_hash, IRubyObject data, IRubyObject start) { public IRubyObject execute(IRubyObject req_hash, IRubyObject data, IRubyObject start) {
int from = 0; int from = 0;
from = RubyNumeric.fix2int(start); from = RubyNumeric.fix2int(start);
ByteList d = ((RubyString)data).getByteList(); ByteList d = ((RubyString)data).getByteList();
if(from >= d.realSize) { if(from >= d.length()) {
throw new RaiseException(runtime, eHttpParserError, "Requested start is after data buffer end.", true); throw new RaiseException(runtime, eHttpParserError, "Requested start is after data buffer end.", true);
} else { } else {
this.hp.parser.data = req_hash; this.hp.parser.data = req_hash;
@ -252,14 +224,17 @@ public class Http11 extends RubyObject {
} }
} }
@JRubyMethod(name = "error?")
public IRubyObject has_error() { public IRubyObject has_error() {
return this.hp.has_error() ? runtime.getTrue() : runtime.getFalse(); return this.hp.has_error() ? runtime.getTrue() : runtime.getFalse();
} }
@JRubyMethod(name = "finished?")
public IRubyObject is_finished() { public IRubyObject is_finished() {
return this.hp.is_finished() ? runtime.getTrue() : runtime.getFalse(); return this.hp.is_finished() ? runtime.getTrue() : runtime.getFalse();
} }
@JRubyMethod
public IRubyObject nread() { public IRubyObject nread() {
return runtime.newFixnum(this.hp.parser.nread); return runtime.newFixnum(this.hp.parser.nread);
} }

View file

@ -14,7 +14,7 @@ public class Http11Parser {
/** Data **/ /** Data **/
// line 18 "ext/http11_java/org/jruby/mongrel/Http11Parser.java" // line 18 "ext/http11/org/jruby/mongrel/Http11Parser.java"
private static byte[] init__http_parser_actions_0() private static byte[] init__http_parser_actions_0()
{ {
return new byte [] { return new byte [] {
@ -236,7 +236,7 @@ static final int http_parser_en_main = 1;
cs = 0; cs = 0;
// line 240 "ext/http11_java/org/jruby/mongrel/Http11Parser.java" // line 240 "ext/http11/org/jruby/mongrel/Http11Parser.java"
{ {
cs = http_parser_start; cs = http_parser_start;
} }
@ -257,16 +257,16 @@ static final int http_parser_en_main = 1;
public int execute(ByteList buffer, int off) { public int execute(ByteList buffer, int off) {
int p, pe; int p, pe;
int cs = parser.cs; int cs = parser.cs;
int len = buffer.realSize; int len = buffer.length();
assert off<=len : "offset past end of buffer"; assert off<=len : "offset past end of buffer";
p = off; p = off;
pe = len; pe = len;
byte[] data = buffer.bytes; byte[] data = buffer.unsafeBytes();
parser.buffer = buffer; parser.buffer = buffer;
// line 270 "ext/http11_java/org/jruby/mongrel/Http11Parser.java" // line 270 "ext/http11/org/jruby/mongrel/Http11Parser.java"
{ {
int _klen; int _klen;
int _trans = 0; int _trans = 0;
@ -431,7 +431,7 @@ case 1:
{ p += 1; _goto_targ = 5; if (true) continue _goto;} { p += 1; _goto_targ = 5; if (true) continue _goto;}
} }
break; break;
// line 435 "ext/http11_java/org/jruby/mongrel/Http11Parser.java" // line 435 "ext/http11/org/jruby/mongrel/Http11Parser.java"
} }
} }
} }

View file

@ -9,10 +9,12 @@ file 'ext/http11/http11_parser.c' => ['ext/http11/http11_parser.rl'] do |t|
end end
end end
file 'ext/http11_java/org/jruby/mongrel/Http11Parser.java' => ['ext/http11/http11_parser.rl'] do |t| file 'ext/http11/org/jruby/mongrel/Http11Parser.java' => ['ext/http11/http11_parser.java.rl'] do |t|
begin begin
sh "ragel #{t.prerequisites.last} -J -o #{t.name}" sh "ragel #{t.prerequisites.last} -J -G2 -o #{t.name}"
rescue rescue
fail "Could not build wrapper using Ragel (it failed or not installed?)" fail "Could not build wrapper using Ragel (it failed or not installed?)"
end end
end end
task :ragel => (defined?(JRUBY_VERSION) ? 'ext/http11/org/jruby/mongrel/Http11Parser.java' : 'ext/http11/http11_parser.c')