1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00
puma--puma/ext/http11_java/http11_parser.rl
evanweaver 6d91530c8d missed one
git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@673 19e92222-5c0b-0410-8929-a290d50e31e9
2007-10-17 07:03:44 +00:00

112 lines
2.4 KiB
Ragel

package org.jruby.mongrel;
import org.jruby.util.ByteList;
public class Http11Parser {
/** Machine **/
/** Data **/
%% write data;
public static interface ElementCB {
public void call(Object data, int at, int length);
}
public static interface FieldCB {
public void call(Object data, int field, int flen, int value, int vlen);
}
public static class HttpParser {
int cs;
int body_start;
int content_len;
int nread;
int mark;
int field_start;
int field_len;
int query_start;
Object data;
ByteList buffer;
public FieldCB http_field;
public ElementCB request_method;
public ElementCB request_uri;
public ElementCB request_path;
public ElementCB query_string;
public ElementCB http_version;
public ElementCB header_done;
public void init() {
cs = 0;
%% write init;
body_start = 0;
content_len = 0;
mark = 0;
nread = 0;
field_len = 0;
field_start = 0;
}
}
public final HttpParser parser = new HttpParser();
public int execute(ByteList buffer, int off) {
int p, pe;
int cs = parser.cs;
int len = buffer.realSize;
assert off<=len : "offset past end of buffer";
p = off;
pe = len;
byte[] data = buffer.bytes;
parser.buffer = buffer;
%% write exec;
parser.cs = cs;
parser.nread += (p - off);
assert p <= pe : "buffer overflow after parsing execute";
assert parser.nread <= len : "nread longer than length";
assert parser.body_start <= len : "body starts after buffer end";
assert parser.mark < len : "mark is after buffer end";
assert parser.field_len <= len : "field has length longer than whole buffer";
assert parser.field_start < len : "field starts after buffer end";
if(parser.body_start>0) {
/* final \r\n combo encountered so stop right here */
%%write eof;
parser.nread++;
}
return parser.nread;
}
public int finish() {
int cs = parser.cs;
%%write eof;
parser.cs = cs;
if(has_error()) {
return -1;
} else if(is_finished()) {
return 1;
} else {
return 0;
}
}
public boolean has_error() {
return parser.cs == http_parser_error;
}
public boolean is_finished() {
return parser.cs == http_parser_first_final;
}
}