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

revert trunk to 1.1

git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@744 19e92222-5c0b-0410-8929-a290d50e31e9
This commit is contained in:
evanweaver 2007-10-26 03:07:30 +00:00
parent d7fa202289
commit e045f787e1
17 changed files with 976 additions and 797 deletions

View file

@ -1,2 +1,4 @@
v1.1. Pure Ruby URIClassifier. JRuby support. Move C URIClassifier into mongrel_experimental project.
v1.0.2. Signed gem; many minor bugfixes and patches.

View file

@ -6,7 +6,7 @@ require 'echoe'
e = Echoe.new("mongrel") do |p|
p.summary = "A small fast HTTP library and server that runs Rails, Camping, Nitro and Iowa apps."
p.author ="Zed A. Shaw"
p.clean_pattern = ['ext/http11/*.{bundle,so,o,obj,pdb,lib,def,exp}', 'ext/http11/Makefile', 'pkg', 'lib/*.bundle', '*.gem', 'site/output', '.config']
p.clean_pattern = ['ext/http11/*.{bundle,so,o,obj,pdb,lib,def,exp}', 'ext/http11/Makefile', 'pkg', 'lib/*.bundle', '*.gem', 'site/output', '.config', 'lib/http11.jar', 'ext/http11_java/classes']
p.rdoc_pattern = ['README', 'LICENSE', 'COPYING', 'lib/**/*.rb', 'doc/**/*.rdoc', 'ext/http11/http11.c']
p.ignore_pattern = /^(pkg|site|projects|doc|log)|CVS|\.log/
p.ruby_version = '>= 1.8.4'
@ -30,6 +30,10 @@ e = Echoe.new("mongrel") do |p|
extensions.clear
self.files += ['lib/http11.so']
self.platform = Gem::Platform::WIN32
when /java/
extensions.clear
self.files += ['lib/http11.jar']
self.platform = 'jruby'
else
add_dependency('daemons', '>= 1.0.3')
add_dependency('fastthread', '>= 1.0.1')
@ -48,6 +52,21 @@ task :ragel do
sh "ragel http11_parser.rl | rlgen-cd -G2 -o #{target}"
raise "Failed to build C source" unless File.exist? target
end
Dir.chdir "ext/http11" do
target = "../../ext/http11_java/org/jruby/mongrel/Http11Parser.java"
File.unlink target if File.exist? target
sh "ragel -J http11_parser.java.rl | rlgen-java -o #{target}"
raise "Failed to build Java source" unless File.exist? target
end
end
#### XXX Hack around JRuby test/unit interaction problems
desc "Run each test suite in isolation on JRuby"
task :test_java do
e.test_pattern.each do |f|
sh "/opt/local/jruby/bin/jruby -w -Ilib:ext:bin:test -e 'require \"#{f}\"'" rescue nil
end
end
#### XXX Hack around RubyGems and Echoe for pre-compiled extensions.
@ -56,6 +75,18 @@ def move_extensions
Dir["ext/**/*.#{Config::CONFIG['DLEXT']}"].each { |file| cp file, "lib/" }
end
def java_classpath_arg
# A myriad of ways to discover the JRuby classpath
classpath = begin
require 'java'
# Already running in a JRuby JVM
Java::java.lang.System.getProperty('java.class.path')
rescue LoadError
ENV['JRUBY_PARENT_CLASSPATH'] || ENV['JRUBY_HOME'] && FileList["#{ENV['JRUBY_HOME']}/lib/*.jar"].join(File::PATH_SEPARATOR)
end
classpath ? "-cp #{classpath}" : ""
end
case RUBY_PLATFORM
when /mswin/
filename = "lib/http11.so"
@ -67,6 +98,19 @@ when /mswin/
move_extensions
end
task :compile => [filename]
when /java/
filename = "lib/http11.jar"
file filename do
build_dir = "ext/http11_java/classes"
mkdir_p build_dir
sources = FileList['ext/http11_java/**/*.java'].join(' ')
sh "javac -target 1.4 -source 1.4 -d #{build_dir} #{java_classpath_arg} #{sources}"
sh "jar cf lib/http11.jar -C #{build_dir} ."
move_extensions
end
task :compile => [filename]
end
#### Project-wide install and uninstall tasks
@ -89,6 +133,7 @@ task :package_all => [:package] do
sub_project("mongrel_console", :package)
sub_project("mongrel_cluster", :package)
sub_project("mongrel_service", :package) if RUBY_PLATFORM =~ /mswin/
sh("rake java package") unless RUBY_PLATFORM =~ /java/
end
task :install_requirements do

View file

@ -8,11 +8,9 @@
#include <string.h>
#include "http11_parser.h"
#include <ctype.h>
#include "tst.h"
static VALUE mMongrel;
static VALUE cHttpParser;
static VALUE cURIClassifier;
static VALUE eHttpParserError;
#define id_handler_map rb_intern("@handler_map")
@ -363,191 +361,6 @@ VALUE HttpParser_nread(VALUE self)
return INT2FIX(http->nread);
}
void URIClassifier_free(void *data)
{
TRACE();
if(data) {
tst_cleanup((struct tst *)data);
}
}
VALUE URIClassifier_alloc(VALUE klass)
{
VALUE obj;
struct tst *tst = tst_init(TRIE_INCREASE);
TRACE();
assert(tst && "failed to initialize trie structure");
obj = Data_Wrap_Struct(klass, NULL, URIClassifier_free, tst);
return obj;
}
/**
* call-seq:
* URIClassifier.new -> URIClassifier
*
* Initializes a new URIClassifier object that you can use to associate URI sequences
* with objects. You can actually use it with any string sequence and any objects,
* but it's mostly used with URIs.
*
* It uses TST from http://www.octavian.org/cs/software.html to build an ternary search
* trie to hold all of the URIs. It uses this to do an initial search for the a URI
* prefix, and then to break the URI into SCRIPT_NAME and PATH_INFO portions. It actually
* will do two searches most of the time in order to find the right handler for the
* registered prefix portion.
*
*/
VALUE URIClassifier_init(VALUE self)
{
VALUE hash;
/* we create an internal hash to protect stuff from the GC */
hash = rb_hash_new();
rb_ivar_set(self, id_handler_map, hash);
return self;
}
/**
* call-seq:
* uc.register("/someuri", SampleHandler.new) -> nil
*
* Registers the SampleHandler (one for all requests) with the "/someuri".
* When URIClassifier::resolve is called with "/someuri" it'll return
* SampleHandler immediately. When called with "/someuri/iwant" it'll also
* return SomeHandler immediatly, with no additional searches, but it will
* return path info with "/iwant".
*
* You actually can reuse this class to register nearly anything and
* quickly resolve it. This could be used for caching, fast mapping, etc.
* The downside is it uses much more memory than a Hash, but it can be
* a lot faster. It's main advantage is that it works on prefixes, which
* is damn hard to get right with a Hash.
*/
VALUE URIClassifier_register(VALUE self, VALUE uri, VALUE handler)
{
int rc = 0;
void *ptr = NULL;
struct tst *tst = NULL;
DATA_GET(self, struct tst, tst);
rc = tst_insert((unsigned char *)StringValueCStr(uri), (void *)handler , tst, 0, &ptr);
if(rc == TST_DUPLICATE_KEY) {
rb_raise(rb_eStandardError, "Handler already registered with that name");
} else if(rc == TST_ERROR) {
rb_raise(rb_eStandardError, "Memory error registering handler");
} else if(rc == TST_NULL_KEY) {
rb_raise(rb_eStandardError, "URI was empty");
}
rb_hash_aset(rb_ivar_get(self, id_handler_map), uri, handler);
return Qnil;
}
/**
* call-seq:
* uc.unregister("/someuri")
*
* Yep, just removes this uri and it's handler from the trie.
*/
VALUE URIClassifier_unregister(VALUE self, VALUE uri)
{
void *handler = NULL;
struct tst *tst = NULL;
DATA_GET(self, struct tst, tst);
handler = tst_delete((unsigned char *)StringValueCStr(uri), tst);
if(handler) {
rb_hash_delete(rb_ivar_get(self, id_handler_map), uri);
return (VALUE)handler;
} else {
return Qnil;
}
}
/**
* call-seq:
* uc.resolve("/someuri") -> "/someuri", "", handler
* uc.resolve("/someuri/pathinfo") -> "/someuri", "/pathinfo", handler
* uc.resolve("/notfound/orhere") -> nil, nil, nil
* uc.resolve("/") -> "/", "/", handler # if uc.register("/", handler)
* uc.resolve("/path/from/root") -> "/", "/path/from/root", handler # if uc.register("/", handler)
*
* Attempts to resolve either the whole URI or at the longest prefix, returning
* the prefix (as script_info), path (as path_info), and registered handler
* (usually an HttpHandler). If it doesn't find a handler registered at the longest
* match then it returns nil,nil,nil.
*
* Because the resolver uses a trie you are able to register a handler at *any* character
* in the URI and it will be handled as long as it's the longest prefix. So, if you
* registered handler #1 at "/something/lik", and #2 at "/something/like/that", then a
* a search for "/something/like" would give you #1. A search for "/something/like/that/too"
* would give you #2.
*
* This is very powerful since it means you can also attach handlers to parts of the ;
* (semi-colon) separated path params, any part of the path, use off chars, anything really.
* It also means that it's very efficient to do this only taking as long as the URI has
* characters.
*
* A slight modification to the CGI 1.2 standard is given for handlers registered to "/".
* CGI expects all CGI scripts to be at some script path, so it doesn't really say anything
* about a script that handles the root. To make this work, the resolver will detect that
* the requested handler is at "/", and return that for script_name, and then simply return
* the full URI back as path_info.
*
* It expects strings with no embedded '\0' characters. Don't try other string-like stuff yet.
*/
VALUE URIClassifier_resolve(VALUE self, VALUE uri)
{
void *handler = NULL;
int pref_len = 0;
struct tst *tst = NULL;
VALUE result;
unsigned char *uri_str = NULL;
DATA_GET(self, struct tst, tst);
uri_str = (unsigned char *)StringValueCStr(uri);
handler = tst_search(uri_str, tst, TST_LONGEST_MATCH, &pref_len);
/* setup for multiple return values */
result = rb_ary_new();
if(handler) {
rb_ary_push(result, rb_str_substr (uri, 0, pref_len));
/* compensate for a script_name="/" where we need to add the "/" to path_info to keep it consistent */
if(pref_len == 1 && uri_str[0] == '/') {
/* matches the root URI so we have to use the whole URI as the path_info */
rb_ary_push(result, uri);
} else {
/* matches a script so process like normal */
rb_ary_push(result, rb_str_substr(uri, pref_len, RSTRING(uri)->len));
}
rb_ary_push(result, (VALUE)handler);
} else {
/* not found so push back nothing */
rb_ary_push(result, Qnil);
rb_ary_push(result, Qnil);
rb_ary_push(result, Qnil);
}
return result;
}
void Init_http11()
{
@ -586,11 +399,4 @@ void Init_http11()
rb_define_method(cHttpParser, "error?", HttpParser_has_error,0);
rb_define_method(cHttpParser, "finished?", HttpParser_is_finished,0);
rb_define_method(cHttpParser, "nread", HttpParser_nread,0);
cURIClassifier = rb_define_class_under(mMongrel, "URIClassifier", rb_cObject);
rb_define_alloc_func(cURIClassifier, URIClassifier_alloc);
rb_define_method(cURIClassifier, "initialize", URIClassifier_init, 0);
rb_define_method(cURIClassifier, "register", URIClassifier_register, 2);
rb_define_method(cURIClassifier, "unregister", URIClassifier_unregister, 1);
rb_define_method(cURIClassifier, "resolve", URIClassifier_resolve, 1);
}

View file

@ -1,40 +0,0 @@
struct node
{
unsigned char value;
struct node *left;
struct node *middle;
struct node *right;
};
struct tst
{
int node_line_width;
struct node_lines *node_lines;
struct node *free_list;
struct node *head[127];
};
struct node_lines
{
struct node *node_line;
struct node_lines *next;
};
enum tst_constants
{
TST_OK, TST_ERROR, TST_NULL_KEY, TST_DUPLICATE_KEY, TST_REPLACE, TST_LONGEST_MATCH
};
struct tst *tst_init(int node_line_width);
int tst_insert(unsigned char *key, void *data, struct tst *tst, int option, void **exist_ptr);
void *tst_search(const unsigned char *key, struct tst *tst, int option, unsigned int *match_len);
void *tst_delete(unsigned char *key, struct tst *tst);
void tst_cleanup(struct tst *tst);

View file

@ -1,23 +0,0 @@
#include "tst.h"
#include <stdio.h>
#include <stdlib.h>
void tst_cleanup(struct tst *tst)
{
struct node_lines *current_line;
struct node_lines *next_line;
next_line = tst->node_lines;
do
{
current_line = next_line;
next_line = current_line->next;
free(current_line->node_line);
free(current_line);
}
while(next_line != NULL);
free(tst);
}

View file

@ -1,146 +0,0 @@
#include "tst.h"
#include <stdio.h>
#include <stdlib.h>
void *tst_delete(unsigned char *key, struct tst *tst)
{
struct node *current_node;
struct node *current_node_parent;
struct node *last_branch;
struct node *last_branch_parent;
struct node *next_node;
struct node *last_branch_replacement;
struct node *last_branch_dangling_child;
int key_index;
if(key[0] == 0)
return NULL;
if(tst->head[(int)key[0]] == NULL)
return NULL;
last_branch = NULL;
last_branch_parent = NULL;
current_node = tst->head[(int)key[0]];
current_node_parent = NULL;
key_index = 1;
while(current_node != NULL)
{
if(key[key_index] == current_node->value)
{
if( (current_node->left != NULL) || (current_node->right != NULL) )
{
last_branch = current_node;
last_branch_parent = current_node_parent;
}
if(key[key_index] == 0)
break;
else
{
current_node_parent = current_node;
current_node = current_node->middle;
key_index++;
continue;
}
}
else if( ((current_node->value == 0) && (key[key_index] < 64)) ||
((current_node->value != 0) && (key[key_index] <
current_node->value)) )
{
last_branch_parent = current_node;
current_node_parent = current_node;
current_node = current_node->left;
last_branch = current_node;
continue;
}
else
{
last_branch_parent = current_node;
current_node_parent = current_node;
current_node = current_node->right;
last_branch = current_node;
continue;
}
}
if(current_node == NULL)
return NULL;
if(last_branch == NULL)
{
next_node = tst->head[(int)key[0]];
tst->head[(int)key[0]] = NULL;
}
else if( (last_branch->left == NULL) && (last_branch->right == NULL) )
{
if(last_branch_parent->left == last_branch)
last_branch_parent->left = NULL;
else
last_branch_parent->right = NULL;
next_node = last_branch;
}
else
{
if( (last_branch->left != NULL) && (last_branch->right != NULL) )
{
last_branch_replacement = last_branch->right;
last_branch_dangling_child = last_branch->left;
}
else if(last_branch->right != NULL)
{
last_branch_replacement = last_branch->right;
last_branch_dangling_child = NULL;
}
else
{
last_branch_replacement = last_branch->left;
last_branch_dangling_child = NULL;
}
if(last_branch_parent == NULL)
tst->head[(int)key[0]]=last_branch_replacement;
else
{
if (last_branch_parent->left == last_branch)
last_branch_parent->left = last_branch_replacement;
else if (last_branch_parent->right == last_branch)
last_branch_parent->right = last_branch_replacement;
else
last_branch_parent->middle = last_branch_replacement;
}
if(last_branch_dangling_child != NULL)
{
current_node = last_branch_replacement;
while (current_node->left != NULL)
current_node = current_node->left;
current_node->left = last_branch_dangling_child;
}
next_node = last_branch;
}
do
{
current_node = next_node;
next_node = current_node->middle;
current_node->left = NULL;
current_node->right = NULL;
current_node->middle = tst->free_list;
tst->free_list = current_node;
}
while(current_node->value != 0);
return next_node;
}

View file

@ -1,38 +0,0 @@
#include "tst.h"
#include <stdio.h>
#include <stdlib.h>
int tst_grow_node_free_list(struct tst *tst)
{
struct node *current_node;
struct node_lines *new_line;
int i;
if((new_line = (struct node_lines *) malloc(sizeof(struct node_lines))) == NULL)
return TST_ERROR;
if((new_line->node_line = (struct node *)
calloc(tst->node_line_width, sizeof(struct node))) == NULL)
{
free(new_line);
return TST_ERROR;
}
else
{
new_line->next = tst->node_lines;
tst->node_lines = new_line;
}
current_node = tst->node_lines->node_line;
tst->free_list = current_node;
for (i = 1; i < tst->node_line_width; i++)
{
current_node->middle = &(tst->node_lines->node_line[i]);
current_node = current_node->middle;
}
current_node->middle = NULL;
return 1;
}

View file

@ -1,41 +0,0 @@
#include "tst.h"
#include <stdio.h>
#include <stdlib.h>
struct tst *tst_init(int width)
{
struct tst *tst;
struct node *current_node;
int i;
if((tst = (struct tst *) calloc(1, sizeof(struct tst))) == NULL)
return NULL;
if ((tst->node_lines = (struct node_lines *) calloc(1, sizeof(struct node_lines))) == NULL)
{
free(tst);
return NULL;
}
tst->node_line_width = width;
tst->node_lines->next = NULL;
if ((tst->node_lines->node_line = (struct node *) calloc(width, sizeof(struct node))) == NULL)
{
free(tst->node_lines);
free(tst);
return NULL;
}
current_node = tst->node_lines->node_line;
tst->free_list = current_node;
for (i = 1; i < width; i++)
{
current_node->middle = &(tst->node_lines->node_line[i]);
current_node = current_node->middle;
}
current_node->middle = NULL;
return tst;
}

View file

@ -1,218 +0,0 @@
#include "tst.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int tst_grow_node_free_list(struct tst *tst);
int tst_insert(unsigned char *key, void *data, struct tst *tst, int option, void **exist_ptr)
{
struct node *current_node;
struct node *new_node_tree_begin = NULL;
struct node *new_node;
int key_index;
int perform_loop = 1;
if (key == NULL)
return TST_NULL_KEY;
if(key[0] == 0)
return TST_NULL_KEY;
if(tst->head[(int)key[0]] == NULL)
{
if(tst->free_list == NULL)
{
if(tst_grow_node_free_list(tst) != 1)
return TST_ERROR;
}
tst->head[(int)key[0]] = tst->free_list;
tst->free_list = tst->free_list->middle;
current_node = tst->head[(int)key[0]];
current_node->value = key[1];
if(key[1] == 0)
{
current_node->middle = data;
return TST_OK;
}
else
perform_loop = 0;
}
current_node = tst->head[(int)key[0]];
key_index = 1;
while(perform_loop == 1)
{
if(key[key_index] == current_node->value)
{
if(key[key_index] == 0)
{
if (option == TST_REPLACE)
{
if (exist_ptr != NULL)
*exist_ptr = current_node->middle;
current_node->middle = data;
return TST_OK;
}
else
{
if (exist_ptr != NULL)
*exist_ptr = current_node->middle;
return TST_DUPLICATE_KEY;
}
}
else
{
if(current_node->middle == NULL)
{
if(tst->free_list == NULL)
{
if(tst_grow_node_free_list(tst) != 1)
return TST_ERROR;
}
current_node->middle = tst->free_list;
tst->free_list = tst->free_list->middle;
new_node_tree_begin = current_node;
current_node = current_node->middle;
current_node->value = key[key_index];
break;
}
else
{
current_node = current_node->middle;
key_index++;
continue;
}
}
}
if(key[key_index] == 0)
{
if(tst->free_list == NULL)
{
if(tst_grow_node_free_list(tst) != 1)
return TST_ERROR;
}
new_node = tst->free_list;
tst->free_list = tst->free_list->middle;
memcpy((void*)new_node, (void*)current_node, sizeof(struct node));
current_node->value = 0;
if(new_node->value < 64)
{
current_node->left = new_node;
current_node->right = '\0';
}
else
{
current_node->left = '\0';
current_node->right = new_node;
}
current_node->middle = data;
return TST_OK;
}
if( ((current_node->value == 0) && (key[key_index] < 64)) ||
((current_node->value != 0) && (key[key_index] <
current_node->value)) )
{
if (current_node->left == NULL)
{
if(tst->free_list == NULL)
{
if(tst_grow_node_free_list(tst) != 1)
return TST_ERROR;
}
current_node->left = tst->free_list;
tst->free_list = tst->free_list->middle;
new_node_tree_begin = current_node;
current_node = current_node->left;
current_node->value = key[key_index];
if(key[key_index] == 0)
{
current_node->middle = data;
return TST_OK;
}
else
break;
}
else
{
current_node = current_node->left;
continue;
}
}
else
{
if (current_node->right == NULL)
{
if(tst->free_list == NULL)
{
if(tst_grow_node_free_list(tst) != 1)
return TST_ERROR;
}
current_node->right = tst->free_list;
tst->free_list = tst->free_list->middle;
new_node_tree_begin = current_node;
current_node = current_node->right;
current_node->value = key[key_index];
break;
}
else
{
current_node = current_node->right;
continue;
}
}
}
do
{
key_index++;
if(tst->free_list == NULL)
{
if(tst_grow_node_free_list(tst) != 1)
{
current_node = new_node_tree_begin->middle;
while (current_node->middle != NULL)
current_node = current_node->middle;
current_node->middle = tst->free_list;
tst->free_list = new_node_tree_begin->middle;
new_node_tree_begin->middle = NULL;
return TST_ERROR;
}
}
if(tst->free_list == NULL)
{
if(tst_grow_node_free_list(tst) != 1)
return TST_ERROR;
}
current_node->middle = tst->free_list;
tst->free_list = tst->free_list->middle;
current_node = current_node->middle;
current_node->value = key[key_index];
} while(key[key_index] !=0);
current_node->middle = data;
return TST_OK;
}

View file

@ -1,73 +0,0 @@
#include "tst.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
void *tst_search(const unsigned char *key, struct tst *tst, int option,
unsigned int *match_len)
{
struct node *current_node;
struct node *longest_match = NULL;
unsigned int longest_match_len = 0;
int key_index;
assert(key != NULL && "key can't be NULL");
assert(tst != NULL && "tst can't be NULL");
if (key[0] == 0)
return NULL;
if (tst->head[(int) key[0]] == NULL)
return NULL;
if (match_len)
*match_len = 0;
current_node = tst->head[(int) key[0]];
key_index = 1;
while (current_node != NULL) {
if (key[key_index] == current_node->value) {
if (current_node->value == 0) {
if (match_len)
*match_len = key_index;
return current_node->middle;
} else {
current_node = current_node->middle;
key_index++;
continue;
}
} else {
if (current_node->value == 0) {
if (option & TST_LONGEST_MATCH) {
longest_match = current_node->middle;
longest_match_len = key_index;
}
if (key[key_index] < 64) {
current_node = current_node->left;
continue;
} else {
current_node = current_node->right;
continue;
}
} else {
if (key[key_index] < current_node->value) {
current_node = current_node->left;
continue;
} else {
current_node = current_node->right;
continue;
}
}
}
}
if (match_len)
*match_len = longest_match_len;
return longest_match;
}

View file

@ -0,0 +1,13 @@
import java.io.IOException;
import org.jruby.Ruby;
import org.jruby.runtime.load.BasicLibraryService;
import org.jruby.mongrel.Http11;
public class Http11Service implements BasicLibraryService {
public boolean basicLoad(final Ruby runtime) throws IOException {
Http11.createHttp11(runtime);
return true;
}
}

View file

@ -0,0 +1,266 @@
/***** 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;
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;
import org.jruby.runtime.CallbackFactory;
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>
*/
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());
CallbackFactory cf = runtime.callbackFactory(Http11.class);
RubyClass cHttpParser = mMongrel.defineClassUnder("HttpParser",runtime.getObject(),ALLOCATOR);
cHttpParser.defineFastMethod("initialize",cf.getFastMethod("initialize"));
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 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);
for(int i=0,j=b.realSize;i<j;i++) {
if((b.bytes[i]&0xFF) == '-') {
b.bytes[i] = (byte)'_';
} else {
b.bytes[i] = (byte)Character.toUpperCase((char)b.bytes[i]);
}
}
f.cat(b);
req.aset(f,v);
}
};
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));
req.aset(runtime.newString("REQUEST_METHOD"),val);
}
};
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));
req.aset(runtime.newString("REQUEST_URI"),val);
}
};
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));
req.aset(runtime.newString("FRAGMENT"),val);
}
};
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));
req.aset(runtime.newString("REQUEST_PATH"),val);
}
};
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));
req.aset(runtime.newString("QUERY_STRING"),val);
}
};
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));
req.aset(runtime.newString("HTTP_VERSION"),val);
}
};
private Http11Parser.ElementCB header_done = new Http11Parser.ElementCB() {
public void call(Object data, int at, int length) {
RubyHash req = (RubyHash)data;
IRubyObject temp,ctype,clen;
clen = req.aref(runtime.newString("HTTP_CONTENT_LENGTH"));
if(!clen.isNil()) {
req.aset(runtime.newString("CONTENT_LENGTH"),clen);
}
ctype = req.aref(runtime.newString("HTTP_CONTENT_TYPE"));
if(!ctype.isNil()) {
req.aset(runtime.newString("CONTENT_TYPE"),ctype);
}
req.aset(runtime.newString("GATEWAY_INTERFACE"),runtime.newString("CGI/1.2"));
if(!(temp = req.aref(runtime.newString("HTTP_HOST"))).isNil()) {
String s = temp.toString();
int colon = s.indexOf(':');
if(colon != -1) {
req.aset(runtime.newString("SERVER_NAME"),runtime.newString(s.substring(0,colon)));
req.aset(runtime.newString("SERVER_PORT"),runtime.newString(s.substring(colon+1)));
} else {
req.aset(runtime.newString("SERVER_NAME"),temp);
req.aset(runtime.newString("SERVER_PORT"),runtime.newString("80"));
}
}
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.aset(runtime.newString("SERVER_SOFTWARE"),runtime.newString("Mongrel 1.0.1"));
}
};
public IRubyObject initialize() {
this.hp.parser.init();
return this;
}
public IRubyObject reset() {
this.hp.parser.init();
return runtime.getNil();
}
public IRubyObject finish() {
this.hp.finish();
return this.hp.is_finished() ? runtime.getTrue() : runtime.getFalse();
}
public IRubyObject execute(IRubyObject req_hash, IRubyObject data, IRubyObject start) {
int from = 0;
from = RubyNumeric.fix2int(start);
ByteList d = ((RubyString)data).getByteList();
if(from >= d.realSize) {
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);
}
}
}
public IRubyObject has_error() {
return this.hp.has_error() ? runtime.getTrue() : runtime.getFalse();
}
public IRubyObject is_finished() {
return this.hp.is_finished() ? runtime.getTrue() : runtime.getFalse();
}
public IRubyObject nread() {
return runtime.newFixnum(this.hp.parser.nread);
}
}// Http11

View file

@ -0,0 +1,572 @@
// line 1 "http11_parser.java.rl"
package org.jruby.mongrel;
import org.jruby.util.ByteList;
public class Http11Parser {
/** Machine **/
// line 64 "http11_parser.java.rl"
/** Data **/
// line 16 "../../ext/http11_java/org/jruby/mongrel/Http11Parser.java"
private static void init__http_parser_actions_0( byte[] r )
{
r[0]=0; r[1]=1; r[2]=0; r[3]=1; r[4]=1; r[5]=1; r[6]=2; r[7]=1;
r[8]=3; r[9]=1; r[10]=4; r[11]=1; r[12]=5; r[13]=1; r[14]=6; r[15]=1;
r[16]=7; r[17]=1; r[18]=8; r[19]=1; r[20]=10; r[21]=1; r[22]=11; r[23]=1;
r[24]=12; r[25]=2; r[26]=9; r[27]=6; r[28]=2; r[29]=11; r[30]=6; r[31]=3;
r[32]=8; r[33]=9; r[34]=6;
}
private static byte[] create__http_parser_actions( )
{
byte[] r = new byte[35];
init__http_parser_actions_0( r );
return r;
}
private static final byte _http_parser_actions[] = create__http_parser_actions();
private static void init__http_parser_key_offsets_0( short[] r )
{
r[0]=0; r[1]=0; r[2]=8; r[3]=17; r[4]=27; r[5]=29; r[6]=30; r[7]=31;
r[8]=32; r[9]=33; r[10]=34; r[11]=36; r[12]=39; r[13]=41; r[14]=44; r[15]=45;
r[16]=61; r[17]=62; r[18]=78; r[19]=80; r[20]=81; r[21]=90; r[22]=99; r[23]=105;
r[24]=111; r[25]=121; r[26]=130; r[27]=136; r[28]=142; r[29]=153; r[30]=159; r[31]=165;
r[32]=175; r[33]=181; r[34]=187; r[35]=196; r[36]=205; r[37]=211; r[38]=217; r[39]=226;
r[40]=235; r[41]=244; r[42]=253; r[43]=262; r[44]=271; r[45]=280; r[46]=289; r[47]=298;
r[48]=307; r[49]=316; r[50]=325; r[51]=334; r[52]=343; r[53]=352; r[54]=361; r[55]=370;
r[56]=379; r[57]=380;
}
private static short[] create__http_parser_key_offsets( )
{
short[] r = new short[58];
init__http_parser_key_offsets_0( r );
return r;
}
private static final short _http_parser_key_offsets[] = create__http_parser_key_offsets();
private static void init__http_parser_trans_keys_0( char[] r )
{
r[0]=36; r[1]=95; r[2]=45; r[3]=46; r[4]=48; r[5]=57; r[6]=65; r[7]=90;
r[8]=32; r[9]=36; r[10]=95; r[11]=45; r[12]=46; r[13]=48; r[14]=57; r[15]=65;
r[16]=90; r[17]=42; r[18]=43; r[19]=47; r[20]=58; r[21]=45; r[22]=57; r[23]=65;
r[24]=90; r[25]=97; r[26]=122; r[27]=32; r[28]=35; r[29]=72; r[30]=84; r[31]=84;
r[32]=80; r[33]=47; r[34]=48; r[35]=57; r[36]=46; r[37]=48; r[38]=57; r[39]=48;
r[40]=57; r[41]=13; r[42]=48; r[43]=57; r[44]=10; r[45]=13; r[46]=33; r[47]=124;
r[48]=126; r[49]=35; r[50]=39; r[51]=42; r[52]=43; r[53]=45; r[54]=46; r[55]=48;
r[56]=57; r[57]=65; r[58]=90; r[59]=94; r[60]=122; r[61]=10; r[62]=33; r[63]=58;
r[64]=124; r[65]=126; r[66]=35; r[67]=39; r[68]=42; r[69]=43; r[70]=45; r[71]=46;
r[72]=48; r[73]=57; r[74]=65; r[75]=90; r[76]=94; r[77]=122; r[78]=13; r[79]=32;
r[80]=13; r[81]=32; r[82]=37; r[83]=60; r[84]=62; r[85]=127; r[86]=0; r[87]=31;
r[88]=34; r[89]=35; r[90]=32; r[91]=37; r[92]=60; r[93]=62; r[94]=127; r[95]=0;
r[96]=31; r[97]=34; r[98]=35; r[99]=48; r[100]=57; r[101]=65; r[102]=70; r[103]=97;
r[104]=102; r[105]=48; r[106]=57; r[107]=65; r[108]=70; r[109]=97; r[110]=102; r[111]=43;
r[112]=58; r[113]=45; r[114]=46; r[115]=48; r[116]=57; r[117]=65; r[118]=90; r[119]=97;
r[120]=122; r[121]=32; r[122]=34; r[123]=35; r[124]=37; r[125]=60; r[126]=62; r[127]=127;
r[128]=0; r[129]=31; r[130]=48; r[131]=57; r[132]=65; r[133]=70; r[134]=97; r[135]=102;
r[136]=48; r[137]=57; r[138]=65; r[139]=70; r[140]=97; r[141]=102; r[142]=32; r[143]=34;
r[144]=35; r[145]=37; r[146]=59; r[147]=60; r[148]=62; r[149]=63; r[150]=127; r[151]=0;
r[152]=31; r[153]=48; r[154]=57; r[155]=65; r[156]=70; r[157]=97; r[158]=102; r[159]=48;
r[160]=57; r[161]=65; r[162]=70; r[163]=97; r[164]=102; r[165]=32; r[166]=34; r[167]=35;
r[168]=37; r[169]=60; r[170]=62; r[171]=63; r[172]=127; r[173]=0; r[174]=31; r[175]=48;
r[176]=57; r[177]=65; r[178]=70; r[179]=97; r[180]=102; r[181]=48; r[182]=57; r[183]=65;
r[184]=70; r[185]=97; r[186]=102; r[187]=32; r[188]=34; r[189]=35; r[190]=37; r[191]=60;
r[192]=62; r[193]=127; r[194]=0; r[195]=31; r[196]=32; r[197]=34; r[198]=35; r[199]=37;
r[200]=60; r[201]=62; r[202]=127; r[203]=0; r[204]=31; r[205]=48; r[206]=57; r[207]=65;
r[208]=70; r[209]=97; r[210]=102; r[211]=48; r[212]=57; r[213]=65; r[214]=70; r[215]=97;
r[216]=102; r[217]=32; r[218]=36; r[219]=95; r[220]=45; r[221]=46; r[222]=48; r[223]=57;
r[224]=65; r[225]=90; r[226]=32; r[227]=36; r[228]=95; r[229]=45; r[230]=46; r[231]=48;
r[232]=57; r[233]=65; r[234]=90; r[235]=32; r[236]=36; r[237]=95; r[238]=45; r[239]=46;
r[240]=48; r[241]=57; r[242]=65; r[243]=90; r[244]=32; r[245]=36; r[246]=95; r[247]=45;
r[248]=46; r[249]=48; r[250]=57; r[251]=65; r[252]=90; r[253]=32; r[254]=36; r[255]=95;
r[256]=45; r[257]=46; r[258]=48; r[259]=57; r[260]=65; r[261]=90; r[262]=32; r[263]=36;
r[264]=95; r[265]=45; r[266]=46; r[267]=48; r[268]=57; r[269]=65; r[270]=90; r[271]=32;
r[272]=36; r[273]=95; r[274]=45; r[275]=46; r[276]=48; r[277]=57; r[278]=65; r[279]=90;
r[280]=32; r[281]=36; r[282]=95; r[283]=45; r[284]=46; r[285]=48; r[286]=57; r[287]=65;
r[288]=90; r[289]=32; r[290]=36; r[291]=95; r[292]=45; r[293]=46; r[294]=48; r[295]=57;
r[296]=65; r[297]=90; r[298]=32; r[299]=36; r[300]=95; r[301]=45; r[302]=46; r[303]=48;
r[304]=57; r[305]=65; r[306]=90; r[307]=32; r[308]=36; r[309]=95; r[310]=45; r[311]=46;
r[312]=48; r[313]=57; r[314]=65; r[315]=90; r[316]=32; r[317]=36; r[318]=95; r[319]=45;
r[320]=46; r[321]=48; r[322]=57; r[323]=65; r[324]=90; r[325]=32; r[326]=36; r[327]=95;
r[328]=45; r[329]=46; r[330]=48; r[331]=57; r[332]=65; r[333]=90; r[334]=32; r[335]=36;
r[336]=95; r[337]=45; r[338]=46; r[339]=48; r[340]=57; r[341]=65; r[342]=90; r[343]=32;
r[344]=36; r[345]=95; r[346]=45; r[347]=46; r[348]=48; r[349]=57; r[350]=65; r[351]=90;
r[352]=32; r[353]=36; r[354]=95; r[355]=45; r[356]=46; r[357]=48; r[358]=57; r[359]=65;
r[360]=90; r[361]=32; r[362]=36; r[363]=95; r[364]=45; r[365]=46; r[366]=48; r[367]=57;
r[368]=65; r[369]=90; r[370]=32; r[371]=36; r[372]=95; r[373]=45; r[374]=46; r[375]=48;
r[376]=57; r[377]=65; r[378]=90; r[379]=32; r[380]=0;
}
private static char[] create__http_parser_trans_keys( )
{
char[] r = new char[381];
init__http_parser_trans_keys_0( r );
return r;
}
private static final char _http_parser_trans_keys[] = create__http_parser_trans_keys();
private static void init__http_parser_single_lengths_0( byte[] r )
{
r[0]=0; r[1]=2; r[2]=3; r[3]=4; r[4]=2; r[5]=1; r[6]=1; r[7]=1;
r[8]=1; r[9]=1; r[10]=0; r[11]=1; r[12]=0; r[13]=1; r[14]=1; r[15]=4;
r[16]=1; r[17]=4; r[18]=2; r[19]=1; r[20]=5; r[21]=5; r[22]=0; r[23]=0;
r[24]=2; r[25]=7; r[26]=0; r[27]=0; r[28]=9; r[29]=0; r[30]=0; r[31]=8;
r[32]=0; r[33]=0; r[34]=7; r[35]=7; r[36]=0; r[37]=0; r[38]=3; r[39]=3;
r[40]=3; r[41]=3; r[42]=3; r[43]=3; r[44]=3; r[45]=3; r[46]=3; r[47]=3;
r[48]=3; r[49]=3; r[50]=3; r[51]=3; r[52]=3; r[53]=3; r[54]=3; r[55]=3;
r[56]=1; r[57]=0;
}
private static byte[] create__http_parser_single_lengths( )
{
byte[] r = new byte[58];
init__http_parser_single_lengths_0( r );
return r;
}
private static final byte _http_parser_single_lengths[] = create__http_parser_single_lengths();
private static void init__http_parser_range_lengths_0( byte[] r )
{
r[0]=0; r[1]=3; r[2]=3; r[3]=3; r[4]=0; r[5]=0; r[6]=0; r[7]=0;
r[8]=0; r[9]=0; r[10]=1; r[11]=1; r[12]=1; r[13]=1; r[14]=0; r[15]=6;
r[16]=0; r[17]=6; r[18]=0; r[19]=0; r[20]=2; r[21]=2; r[22]=3; r[23]=3;
r[24]=4; r[25]=1; r[26]=3; r[27]=3; r[28]=1; r[29]=3; r[30]=3; r[31]=1;
r[32]=3; r[33]=3; r[34]=1; r[35]=1; r[36]=3; r[37]=3; r[38]=3; r[39]=3;
r[40]=3; r[41]=3; r[42]=3; r[43]=3; r[44]=3; r[45]=3; r[46]=3; r[47]=3;
r[48]=3; r[49]=3; r[50]=3; r[51]=3; r[52]=3; r[53]=3; r[54]=3; r[55]=3;
r[56]=0; r[57]=0;
}
private static byte[] create__http_parser_range_lengths( )
{
byte[] r = new byte[58];
init__http_parser_range_lengths_0( r );
return r;
}
private static final byte _http_parser_range_lengths[] = create__http_parser_range_lengths();
private static void init__http_parser_index_offsets_0( short[] r )
{
r[0]=0; r[1]=0; r[2]=6; r[3]=13; r[4]=21; r[5]=24; r[6]=26; r[7]=28;
r[8]=30; r[9]=32; r[10]=34; r[11]=36; r[12]=39; r[13]=41; r[14]=44; r[15]=46;
r[16]=57; r[17]=59; r[18]=70; r[19]=73; r[20]=75; r[21]=83; r[22]=91; r[23]=95;
r[24]=99; r[25]=106; r[26]=115; r[27]=119; r[28]=123; r[29]=134; r[30]=138; r[31]=142;
r[32]=152; r[33]=156; r[34]=160; r[35]=169; r[36]=178; r[37]=182; r[38]=186; r[39]=193;
r[40]=200; r[41]=207; r[42]=214; r[43]=221; r[44]=228; r[45]=235; r[46]=242; r[47]=249;
r[48]=256; r[49]=263; r[50]=270; r[51]=277; r[52]=284; r[53]=291; r[54]=298; r[55]=305;
r[56]=312; r[57]=314;
}
private static short[] create__http_parser_index_offsets( )
{
short[] r = new short[58];
init__http_parser_index_offsets_0( r );
return r;
}
private static final short _http_parser_index_offsets[] = create__http_parser_index_offsets();
private static void init__http_parser_indicies_0( byte[] r )
{
r[0]=0; r[1]=0; r[2]=0; r[3]=0; r[4]=0; r[5]=1; r[6]=2; r[7]=3;
r[8]=3; r[9]=3; r[10]=3; r[11]=3; r[12]=1; r[13]=4; r[14]=5; r[15]=6;
r[16]=7; r[17]=5; r[18]=5; r[19]=5; r[20]=1; r[21]=8; r[22]=9; r[23]=1;
r[24]=10; r[25]=1; r[26]=11; r[27]=1; r[28]=12; r[29]=1; r[30]=13; r[31]=1;
r[32]=14; r[33]=1; r[34]=15; r[35]=1; r[36]=16; r[37]=15; r[38]=1; r[39]=17;
r[40]=1; r[41]=18; r[42]=17; r[43]=1; r[44]=19; r[45]=1; r[46]=20; r[47]=21;
r[48]=21; r[49]=21; r[50]=21; r[51]=21; r[52]=21; r[53]=21; r[54]=21; r[55]=21;
r[56]=1; r[57]=22; r[58]=1; r[59]=23; r[60]=24; r[61]=23; r[62]=23; r[63]=23;
r[64]=23; r[65]=23; r[66]=23; r[67]=23; r[68]=23; r[69]=1; r[70]=26; r[71]=27;
r[72]=25; r[73]=26; r[74]=28; r[75]=29; r[76]=31; r[77]=1; r[78]=1; r[79]=1;
r[80]=1; r[81]=1; r[82]=30; r[83]=29; r[84]=33; r[85]=1; r[86]=1; r[87]=1;
r[88]=1; r[89]=1; r[90]=32; r[91]=34; r[92]=34; r[93]=34; r[94]=1; r[95]=32;
r[96]=32; r[97]=32; r[98]=1; r[99]=35; r[100]=36; r[101]=35; r[102]=35; r[103]=35;
r[104]=35; r[105]=1; r[106]=8; r[107]=1; r[108]=9; r[109]=37; r[110]=1; r[111]=1;
r[112]=1; r[113]=1; r[114]=36; r[115]=38; r[116]=38; r[117]=38; r[118]=1; r[119]=36;
r[120]=36; r[121]=36; r[122]=1; r[123]=39; r[124]=1; r[125]=41; r[126]=42; r[127]=43;
r[128]=1; r[129]=1; r[130]=44; r[131]=1; r[132]=1; r[133]=40; r[134]=45; r[135]=45;
r[136]=45; r[137]=1; r[138]=40; r[139]=40; r[140]=40; r[141]=1; r[142]=8; r[143]=1;
r[144]=9; r[145]=47; r[146]=1; r[147]=1; r[148]=48; r[149]=1; r[150]=1; r[151]=46;
r[152]=49; r[153]=49; r[154]=49; r[155]=1; r[156]=46; r[157]=46; r[158]=46; r[159]=1;
r[160]=50; r[161]=1; r[162]=52; r[163]=53; r[164]=1; r[165]=1; r[166]=1; r[167]=1;
r[168]=51; r[169]=54; r[170]=1; r[171]=56; r[172]=57; r[173]=1; r[174]=1; r[175]=1;
r[176]=1; r[177]=55; r[178]=58; r[179]=58; r[180]=58; r[181]=1; r[182]=55; r[183]=55;
r[184]=55; r[185]=1; r[186]=2; r[187]=59; r[188]=59; r[189]=59; r[190]=59; r[191]=59;
r[192]=1; r[193]=2; r[194]=60; r[195]=60; r[196]=60; r[197]=60; r[198]=60; r[199]=1;
r[200]=2; r[201]=61; r[202]=61; r[203]=61; r[204]=61; r[205]=61; r[206]=1; r[207]=2;
r[208]=62; r[209]=62; r[210]=62; r[211]=62; r[212]=62; r[213]=1; r[214]=2; r[215]=63;
r[216]=63; r[217]=63; r[218]=63; r[219]=63; r[220]=1; r[221]=2; r[222]=64; r[223]=64;
r[224]=64; r[225]=64; r[226]=64; r[227]=1; r[228]=2; r[229]=65; r[230]=65; r[231]=65;
r[232]=65; r[233]=65; r[234]=1; r[235]=2; r[236]=66; r[237]=66; r[238]=66; r[239]=66;
r[240]=66; r[241]=1; r[242]=2; r[243]=67; r[244]=67; r[245]=67; r[246]=67; r[247]=67;
r[248]=1; r[249]=2; r[250]=68; r[251]=68; r[252]=68; r[253]=68; r[254]=68; r[255]=1;
r[256]=2; r[257]=69; r[258]=69; r[259]=69; r[260]=69; r[261]=69; r[262]=1; r[263]=2;
r[264]=70; r[265]=70; r[266]=70; r[267]=70; r[268]=70; r[269]=1; r[270]=2; r[271]=71;
r[272]=71; r[273]=71; r[274]=71; r[275]=71; r[276]=1; r[277]=2; r[278]=72; r[279]=72;
r[280]=72; r[281]=72; r[282]=72; r[283]=1; r[284]=2; r[285]=73; r[286]=73; r[287]=73;
r[288]=73; r[289]=73; r[290]=1; r[291]=2; r[292]=74; r[293]=74; r[294]=74; r[295]=74;
r[296]=74; r[297]=1; r[298]=2; r[299]=75; r[300]=75; r[301]=75; r[302]=75; r[303]=75;
r[304]=1; r[305]=2; r[306]=76; r[307]=76; r[308]=76; r[309]=76; r[310]=76; r[311]=1;
r[312]=2; r[313]=1; r[314]=1; r[315]=0;
}
private static byte[] create__http_parser_indicies( )
{
byte[] r = new byte[316];
init__http_parser_indicies_0( r );
return r;
}
private static final byte _http_parser_indicies[] = create__http_parser_indicies();
private static void init__http_parser_trans_targs_wi_0( byte[] r )
{
r[0]=2; r[1]=0; r[2]=3; r[3]=38; r[4]=4; r[5]=24; r[6]=28; r[7]=25;
r[8]=5; r[9]=20; r[10]=6; r[11]=7; r[12]=8; r[13]=9; r[14]=10; r[15]=11;
r[16]=12; r[17]=13; r[18]=14; r[19]=15; r[20]=16; r[21]=17; r[22]=57; r[23]=17;
r[24]=18; r[25]=19; r[26]=14; r[27]=18; r[28]=19; r[29]=5; r[30]=21; r[31]=22;
r[32]=21; r[33]=22; r[34]=23; r[35]=24; r[36]=25; r[37]=26; r[38]=27; r[39]=5;
r[40]=28; r[41]=20; r[42]=29; r[43]=31; r[44]=34; r[45]=30; r[46]=31; r[47]=32;
r[48]=34; r[49]=33; r[50]=5; r[51]=35; r[52]=20; r[53]=36; r[54]=5; r[55]=35;
r[56]=20; r[57]=36; r[58]=37; r[59]=39; r[60]=40; r[61]=41; r[62]=42; r[63]=43;
r[64]=44; r[65]=45; r[66]=46; r[67]=47; r[68]=48; r[69]=49; r[70]=50; r[71]=51;
r[72]=52; r[73]=53; r[74]=54; r[75]=55; r[76]=56;
}
private static byte[] create__http_parser_trans_targs_wi( )
{
byte[] r = new byte[77];
init__http_parser_trans_targs_wi_0( r );
return r;
}
private static final byte _http_parser_trans_targs_wi[] = create__http_parser_trans_targs_wi();
private static void init__http_parser_trans_actions_wi_0( byte[] r )
{
r[0]=1; r[1]=0; r[2]=11; r[3]=0; r[4]=1; r[5]=1; r[6]=1; r[7]=1;
r[8]=13; r[9]=13; r[10]=1; r[11]=0; r[12]=0; r[13]=0; r[14]=0; r[15]=0;
r[16]=0; r[17]=0; r[18]=19; r[19]=0; r[20]=0; r[21]=3; r[22]=23; r[23]=0;
r[24]=5; r[25]=7; r[26]=9; r[27]=7; r[28]=0; r[29]=15; r[30]=1; r[31]=1;
r[32]=0; r[33]=0; r[34]=0; r[35]=0; r[36]=0; r[37]=0; r[38]=0; r[39]=28;
r[40]=0; r[41]=28; r[42]=0; r[43]=21; r[44]=21; r[45]=0; r[46]=0; r[47]=0;
r[48]=0; r[49]=0; r[50]=31; r[51]=17; r[52]=31; r[53]=17; r[54]=25; r[55]=0;
r[56]=25; r[57]=0; r[58]=0; r[59]=0; r[60]=0; r[61]=0; r[62]=0; r[63]=0;
r[64]=0; r[65]=0; r[66]=0; r[67]=0; r[68]=0; r[69]=0; r[70]=0; r[71]=0;
r[72]=0; r[73]=0; r[74]=0; r[75]=0; r[76]=0;
}
private static byte[] create__http_parser_trans_actions_wi( )
{
byte[] r = new byte[77];
init__http_parser_trans_actions_wi_0( r );
return r;
}
private static final byte _http_parser_trans_actions_wi[] = create__http_parser_trans_actions_wi();
static final int http_parser_start = 1;
static final int http_parser_first_final = 57;
static final int http_parser_error = 0;
static final int http_parser_en_main = 1;
// line 68 "http11_parser.java.rl"
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 fragment;
public ElementCB request_path;
public ElementCB query_string;
public ElementCB http_version;
public ElementCB header_done;
public void init() {
cs = 0;
// line 330 "../../ext/http11_java/org/jruby/mongrel/Http11Parser.java"
{
cs = http_parser_start;
}
// line 103 "http11_parser.java.rl"
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;
// line 359 "../../ext/http11_java/org/jruby/mongrel/Http11Parser.java"
{
int _klen;
int _trans;
int _acts;
int _nacts;
int _keys;
if ( p != pe ) {
if ( cs != 0 ) {
_resume: while ( true ) {
_again: do {
_match: do {
_keys = _http_parser_key_offsets[cs];
_trans = _http_parser_index_offsets[cs];
_klen = _http_parser_single_lengths[cs];
if ( _klen > 0 ) {
int _lower = _keys;
int _mid;
int _upper = _keys + _klen - 1;
while (true) {
if ( _upper < _lower )
break;
_mid = _lower + ((_upper-_lower) >> 1);
if ( data[p] < _http_parser_trans_keys[_mid] )
_upper = _mid - 1;
else if ( data[p] > _http_parser_trans_keys[_mid] )
_lower = _mid + 1;
else {
_trans += (_mid - _keys);
break _match;
}
}
_keys += _klen;
_trans += _klen;
}
_klen = _http_parser_range_lengths[cs];
if ( _klen > 0 ) {
int _lower = _keys;
int _mid;
int _upper = _keys + (_klen<<1) - 2;
while (true) {
if ( _upper < _lower )
break;
_mid = _lower + (((_upper-_lower) >> 1) & ~1);
if ( data[p] < _http_parser_trans_keys[_mid] )
_upper = _mid - 2;
else if ( data[p] > _http_parser_trans_keys[_mid+1] )
_lower = _mid + 2;
else {
_trans += ((_mid - _keys)>>1);
break _match;
}
}
_trans += _klen;
}
} while (false);
_trans = _http_parser_indicies[_trans];
cs = _http_parser_trans_targs_wi[_trans];
if ( _http_parser_trans_actions_wi[_trans] == 0 )
break _again;
_acts = _http_parser_trans_actions_wi[_trans];
_nacts = (int) _http_parser_actions[_acts++];
while ( _nacts-- > 0 )
{
switch ( _http_parser_actions[_acts++] )
{
case 0:
// line 13 "http11_parser.java.rl"
{parser.mark = p; }
break;
case 1:
// line 15 "http11_parser.java.rl"
{ parser.field_start = p; }
break;
case 2:
// line 16 "http11_parser.java.rl"
{
parser.field_len = p-parser.field_start;
}
break;
case 3:
// line 20 "http11_parser.java.rl"
{ parser.mark = p; }
break;
case 4:
// line 21 "http11_parser.java.rl"
{
if(parser.http_field != null) {
parser.http_field.call(parser.data, parser.field_start, parser.field_len, parser.mark, p-parser.mark);
}
}
break;
case 5:
// line 26 "http11_parser.java.rl"
{
if(parser.request_method != null)
parser.request_method.call(parser.data, parser.mark, p-parser.mark);
}
break;
case 6:
// line 30 "http11_parser.java.rl"
{
if(parser.request_uri != null)
parser.request_uri.call(parser.data, parser.mark, p-parser.mark);
}
break;
case 7:
// line 34 "http11_parser.java.rl"
{
if(parser.fragment != null)
parser.fragment.call(parser.data, parser.mark, p-parser.mark);
}
break;
case 8:
// line 39 "http11_parser.java.rl"
{parser.query_start = p; }
break;
case 9:
// line 40 "http11_parser.java.rl"
{
if(parser.query_string != null)
parser.query_string.call(parser.data, parser.query_start, p-parser.query_start);
}
break;
case 10:
// line 45 "http11_parser.java.rl"
{
if(parser.http_version != null)
parser.http_version.call(parser.data, parser.mark, p-parser.mark);
}
break;
case 11:
// line 50 "http11_parser.java.rl"
{
if(parser.request_path != null)
parser.request_path.call(parser.data, parser.mark, p-parser.mark);
}
break;
case 12:
// line 55 "http11_parser.java.rl"
{
parser.body_start = p + 1;
if(parser.header_done != null)
parser.header_done.call(parser.data, p + 1, pe - p - 1);
if (true) break _resume;
}
break;
// line 513 "../../ext/http11_java/org/jruby/mongrel/Http11Parser.java"
}
}
} while (false);
if ( cs == 0 )
break _resume;
if ( ++p == pe )
break _resume;
}
} }
}
// line 127 "http11_parser.java.rl"
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 */
// line 540 "../../ext/http11_java/org/jruby/mongrel/Http11Parser.java"
// line 141 "http11_parser.java.rl"
parser.nread++;
}
return parser.nread;
}
public int finish() {
int cs = parser.cs;
// line 552 "../../ext/http11_java/org/jruby/mongrel/Http11Parser.java"
// line 151 "http11_parser.java.rl"
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;
}
}

View file

@ -37,27 +37,88 @@ require 'uri'
module Mongrel
class URIClassifier
class RegistrationError < RuntimeError
end
class UsageError < RuntimeError
end
attr_reader :handler_map
# Returns the URIs that have been registered with this classifier so far.
# The URIs returned should not be modified as this will cause a memory leak.
# You can use this to inspect the contents of the URIClassifier.
def uris
@handler_map.keys
end
# Simply does an inspect that looks like a Hash inspect.
def inspect
@handler_map.inspect
end
def initialize
@handler_map = {}
@matcher = //
@root_handler = nil
end
# Register a handler object at a particular URI. The handler can be whatever
# you want, including an array. It's up to you what to do with it.
#
# Registering a handler is not necessarily threadsafe, so be careful if you go
# mucking around once the server is running.
def register(uri, handler)
raise RegistrationError, "#{uri.inspect} is already registered" if @handler_map[uri]
raise RegistrationError, "URI is empty" if !uri or uri.empty?
raise RegistrationError, "URI must begin with a \"#{Const::SLASH}\"" unless uri[0..0] == Const::SLASH
@handler_map[uri.dup] = handler
rebuild
end
# Unregister a particular URI and its handler.
def unregister(uri)
handler = @handler_map.delete(uri)
raise RegistrationError, "#{uri.inspect} was not registered" unless handler
rebuild
handler
end
# Resolve a request URI by finding the best partial match in the registered
# handler URIs.
def resolve(request_uri)
if @root_handler
# Optimization for the pathological case of only one handler on "/"; e.g. Rails
[Const::SLASH, request_uri, @root_handler]
elsif match = @matcher.match(request_uri)
uri = match.to_s
# A root mounted ("/") handler must resolve such that path info matches the original URI.
[uri, (uri == Const::SLASH ? request_uri : match.post_match), @handler_map[uri]]
else
[nil, nil, nil]
end
end
private
def rebuild
if @handler_map.size == 1 and @handler_map[Const::SLASH]
@root_handler = @handler_map.values.first
else
@root_handler = nil
routes = @handler_map.keys.sort.sort_by do |uri|
-uri.length
end
@matcher = Regexp.new(routes.map do |uri|
Regexp.new('^' + Regexp.escape(uri))
end.join('|'))
end
end
end
# Used to stop the HttpServer via Thread.raise.
class StopServer < Exception; end
# Thrown at a thread when it is timed out.
class TimeoutError < Exception; end
# Every standard HTTP code mapped to the appropriate message. These are
# used so frequently that they are placed directly in Mongrel for easy
# access rather than Mongrel::Const.
@ -778,25 +839,16 @@ module Mongrel
# found in the prefix of a request then your handler's HttpHandler::process method
# is called. See Mongrel::URIClassifier#register for more information.
#
# If you set in_front=true then the passed in handler will be put in front in the list.
# Otherwise it's placed at the end of the list.
# If you set in_front=true then the passed in handler will be put in the front of the list
# for that particular URI. Otherwise it's placed at the end of the list.
def register(uri, handler, in_front=false)
script_name, path_info, handlers = @classifier.resolve(uri)
if not handlers
begin
@classifier.register(uri, [handler])
else
if path_info.length == 0 or (script_name == Const::SLASH and path_info == Const::SLASH)
if in_front
handlers.unshift(handler)
else
handlers << handler
end
else
@classifier.register(uri, [handler])
end
rescue URIClassifier::RegistrationError
handlers = @classifier.resolve(uri)[2]
method_name = in_front ? 'unshift' : 'push'
handlers.send(method_name, handler)
end
handler.listener = self
end

View file

@ -56,6 +56,7 @@ class ConfiguratorTest < Test::Unit::TestCase
end
end
# pp @config.listeners.values.first.classifier.routes
@config.listeners.each do |host,listener|
assert listener.classifier.uris.length == 3, "Wrong number of registered URIs"

View file

@ -124,6 +124,7 @@ class URIClassifierTest < Test::Unit::TestCase
current << c.chr
uri_classifier.register(current, c)
end
# Try to resolve everything with no asserts as a fuzzing
tests.each do |prefix|
@ -186,7 +187,6 @@ class URIClassifierTest < Test::Unit::TestCase
tests.each do |uri|
script_name, path_info, handler = uri_classifier.resolve(uri)
# p uri_classifier.resolve(uri)
assert_equal root, script_name, "#{uri} did not resolve to #{root}"
assert_equal uri, path_info
assert_equal 2, handler

View file

@ -20,6 +20,7 @@ require 'benchmark'
require 'digest/sha1'
require 'uri'
require 'stringio'
require 'pp'
require 'mongrel'
require 'mongrel/stats'