diff options
Diffstat (limited to 'web/server/h2o/libh2o/deps/mruby-input-stream')
10 files changed, 528 insertions, 0 deletions
diff --git a/web/server/h2o/libh2o/deps/mruby-input-stream/.travis.yml b/web/server/h2o/libh2o/deps/mruby-input-stream/.travis.yml new file mode 100644 index 000000000..fc81cadad --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-input-stream/.travis.yml @@ -0,0 +1,6 @@ +sudo: false +script: + - export MRUBY_CONFIG="$TRAVIS_BUILD_DIR/build_config_sample.rb" + - git clone --depth 1 "https://github.com/mruby/mruby.git" + - cd mruby + - ./minirake test diff --git a/web/server/h2o/libh2o/deps/mruby-input-stream/LICENSE b/web/server/h2o/libh2o/deps/mruby-input-stream/LICENSE new file mode 100644 index 000000000..6e817bb59 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-input-stream/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2015 Masayoshi Takahashi + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/web/server/h2o/libh2o/deps/mruby-input-stream/README.md b/web/server/h2o/libh2o/deps/mruby-input-stream/README.md new file mode 100644 index 000000000..a9efe87fa --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-input-stream/README.md @@ -0,0 +1,28 @@ +# mruby-input-stream + +[![Build Status](https://travis-ci.org/takahashim/mruby-input-stream.svg?branch=master)](https://travis-ci.org/takahashim/mruby-input-stream) + +Input Stream class for Rack. + + +## Install + +add conf.gem to `build_config.rb`: + + MRuby::Build.new do |conf| + + # ... (snip) ... + + conf.gem :github => 'takahashim/mruby-input-stream' + end + +## License + +MIT + +## Author + +Masayoshi Takahashi + + + diff --git a/web/server/h2o/libh2o/deps/mruby-input-stream/Rakefile b/web/server/h2o/libh2o/deps/mruby-input-stream/Rakefile new file mode 100644 index 000000000..0c0c78ea1 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-input-stream/Rakefile @@ -0,0 +1,28 @@ +MRUBY_CONFIG=File.expand_path(ENV["MRUBY_CONFIG"] || "./build_config_sample.rb") +RAKE="ruby ./minirake" + +file :mruby do + sh "git clone --depth 1 git://github.com/mruby/mruby.git" +end + +task :default => :test + +desc "test this mrbgem" +task :test => :mruby do + sh "cd mruby && MRUBY_CONFIG=#{MRUBY_CONFIG} #{RAKE} test" +end + +desc "build mruby with this mrbgem" +task :build => :mruby do + sh "cd mruby && MRUBY_CONFIG=#{MRUBY_CONFIG} #{RAKE} all" +end + +desc "cleanup" +task :clean do + sh "cd mruby && rake deep_clean" if File.exist?("mruby") +end + +desc "cleanup including temporary files" +task :realclean do + sh "rm -rf mruby" +end diff --git a/web/server/h2o/libh2o/deps/mruby-input-stream/build_config_sample.rb b/web/server/h2o/libh2o/deps/mruby-input-stream/build_config_sample.rb new file mode 100644 index 000000000..9d2321d9c --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-input-stream/build_config_sample.rb @@ -0,0 +1,17 @@ +MRuby::Build.new do |conf| + toolchain :gcc + + conf.gembox 'default' + conf.gem File.expand_path(File.dirname(__FILE__)) +end + +MRuby::Build.new('test') do |conf| + toolchain :gcc + + enable_debug + conf.enable_bintest + conf.enable_test + + conf.gembox 'default' + conf.gem File.expand_path(File.dirname(__FILE__)) +end diff --git a/web/server/h2o/libh2o/deps/mruby-input-stream/mrbgem.rake b/web/server/h2o/libh2o/deps/mruby-input-stream/mrbgem.rake new file mode 100644 index 000000000..082a128aa --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-input-stream/mrbgem.rake @@ -0,0 +1,6 @@ +MRuby::Gem::Specification.new('mruby-input-stream') do |spec| + spec.license = 'MIT' + spec.authors = 'Masayoshi Takahashi' + spec.summary = 'InputStream class for Rack' + spec.version = '1.0.0' +end diff --git a/web/server/h2o/libh2o/deps/mruby-input-stream/mrblib/input_stream.rb b/web/server/h2o/libh2o/deps/mruby-input-stream/mrblib/input_stream.rb new file mode 100644 index 000000000..d452f7ff1 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-input-stream/mrblib/input_stream.rb @@ -0,0 +1,16 @@ +class InputStream + include Enumerable + + # + # from String#each + def each(&block) + self.rewind + while pos = self.byteindex(0x0a) + block.call(self.read(pos+1)) + end + rest = self.read() + if rest + block.call(rest) + end + end +end diff --git a/web/server/h2o/libh2o/deps/mruby-input-stream/src/mruby_input_stream.c b/web/server/h2o/libh2o/deps/mruby-input-stream/src/mruby_input_stream.c new file mode 100644 index 000000000..a5f90b981 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-input-stream/src/mruby_input_stream.c @@ -0,0 +1,278 @@ +#include <string.h> +#include "mruby.h" +#include "mruby/value.h" +#include "mruby/data.h" +#include "mruby/string.h" +#include "mruby_input_stream.h" + +typedef struct mrb_input_stream_t { + const char *base; + mrb_int len; + mrb_int pos; + mrb_input_stream_free_callback free_cb; + void *free_cb_data; +} mrb_input_stream_t; + +static mrb_input_stream_t* +mrb_input_stream_create(mrb_state *mrb, const char *base, mrb_int len, mrb_input_stream_free_callback cb, void *cb_data); + +static void +mrb_mruby_input_stream_free(mrb_state *mrb, void *ptr); + +static mrb_int +seek_char(mrb_input_stream_t *stream, const char chr); + +const static struct mrb_data_type mrb_input_stream_type = { + "InputStream", + mrb_mruby_input_stream_free, +}; + +static void +assert_is_open(mrb_state *mrb, mrb_input_stream_t *stream) +{ + if (stream->len < 0) { + struct RClass *klass = mrb_class_get(mrb, "IOError"); + if (klass == NULL) + klass = E_RUNTIME_ERROR; + mrb_raise(mrb, klass, "stream closed"); + } +} + +static mrb_value +mrb_input_stream_init(mrb_state *mrb, mrb_value self) +{ + mrb_value str; + mrb_int len; + char *ptr; + mrb_input_stream_t *stream; + mrb_int n = mrb_get_args(mrb, "|S", &str); + if (n > 1) { + mrb_raisef(mrb, E_ARGUMENT_ERROR, "wrong number of arguments (%S for 1)", mrb_fixnum_value(n)); + } + + if (n == 1) { + len = RSTRING_LEN(str); + ptr = RSTRING_PTR(str); + stream = mrb_input_stream_create(mrb, ptr, len, NULL, NULL); + } else { + stream = mrb_input_stream_create(mrb, NULL, 0, NULL, NULL); + } + + DATA_TYPE(self) = &mrb_input_stream_type; + DATA_PTR(self) = stream; + return self; +} + +static void +default_free_cb(mrb_state *mrb, const char *base, mrb_int len, void *cb_data) +{ + if (base != NULL) + mrb_free(mrb, (void *)base); +} + +static void +mrb_mruby_input_stream_free(mrb_state *mrb, void *ptr) +{ + mrb_input_stream_t *stream = (mrb_input_stream_t *)ptr; + if (stream->free_cb != NULL) + stream->free_cb(mrb, stream->base, stream->len, stream->free_cb_data); + mrb_free(mrb, stream); +} + +static void setup_stream(mrb_state *mrb, mrb_input_stream_t *stream, const char *base, mrb_int len, mrb_int pos, mrb_input_stream_free_callback free_cb, void *free_cb_data) +{ + if (free_cb == NULL) { + if (len > 0) { + char *dst_base = (char *)mrb_malloc(mrb, sizeof(char)*len); + memcpy(dst_base, base, len); + stream->base = dst_base; + stream->len = len; + } else { + stream->base = NULL; + stream->len = len; + } + stream->free_cb = default_free_cb; + stream->free_cb_data = NULL; + } else { + stream->base = base; + stream->len = len; + stream->free_cb = free_cb; + stream->free_cb_data = free_cb_data; + } + + stream->pos = pos; +} + +mrb_input_stream_t* +mrb_input_stream_create(mrb_state *mrb, const char *base, mrb_int len, mrb_input_stream_free_callback free_cb, void *free_cb_data) +{ + mrb_input_stream_t *stream = (mrb_input_stream_t *)mrb_malloc(mrb, sizeof(mrb_input_stream_t)); + + setup_stream(mrb, stream, base, len, 0, free_cb, free_cb_data); + return stream; +} + +mrb_value +mrb_input_stream_value(mrb_state *mrb, const char *base, mrb_int len) +{ + mrb_input_stream_t *stream = mrb_input_stream_create(mrb, base, len, NULL, NULL); + struct RClass *c = mrb_class_get(mrb, "InputStream"); + struct RData *d = mrb_data_object_alloc(mrb, c, stream, &mrb_input_stream_type); + + return mrb_obj_value(d); +} + +void +mrb_input_stream_get_data(mrb_state *mrb, mrb_value self, const char **base, mrb_int *len, mrb_int *pos, mrb_input_stream_free_callback *free_cb, void **free_cb_data) +{ + mrb_input_stream_t *stream = DATA_PTR(self); + + if (base != NULL) + *base = stream->base; + if (len != NULL) + *len = stream->len; + if (pos != NULL) + *pos = stream->pos; + if (free_cb != NULL) + *free_cb = stream->free_cb; + if (free_cb_data != NULL) + *free_cb_data = stream->free_cb_data; +} + +void +mrb_input_stream_set_data(mrb_state *mrb, mrb_value self, const char *base, mrb_int len, mrb_int pos, mrb_input_stream_free_callback free_cb, void *free_cb_data) +{ + mrb_input_stream_t *stream = DATA_PTR(self); + + if (stream->free_cb != NULL) + stream->free_cb(mrb, stream->base, stream->len, stream->free_cb_data); + setup_stream(mrb, stream, base, len, pos, free_cb, free_cb_data); +} + +static mrb_value +mrb_input_stream_gets(mrb_state *mrb, mrb_value self) +{ + mrb_input_stream_t *stream = DATA_PTR(self); + mrb_int pos, len; + + assert_is_open(mrb, stream); + + pos = stream->pos; + len = seek_char(stream, '\n'); + if (len < 0) { + return mrb_nil_value(); + } + if (stream->pos + len < stream->len) { + len++; + } + stream->pos += len; + return mrb_str_new(mrb, (stream->base + pos), len); +} + +static mrb_int +seek_char(mrb_input_stream_t *stream, char chr){ + const char *base = stream->base; + size_t len = stream->len; + mrb_int pos = stream->pos; + const char *end = base + len; + const char *start = base + pos; + const char *s = start; + + if (pos >= len) { + return -1; + } + + while (s < end) { + if (*s == chr) { + break; + } + s++; + } + return (s - start); +} + +static mrb_value +mrb_input_stream_read(mrb_state *mrb, mrb_value self) +{ + mrb_int len; + mrb_value buf; + mrb_int n = mrb_get_args(mrb, "|iS", &len, &buf), pos; + mrb_input_stream_t *stream = DATA_PTR(self); + const char *start; + + assert_is_open(mrb, stream); + + pos = stream->pos; + start = stream->base + pos; + + if (pos >= stream->len) { + return mrb_nil_value(); + } + if (n == 0) { + stream->pos = stream->len; + return mrb_str_new(mrb, start, stream->len - pos); + } else { + mrb_int newpos = pos + len; + if (newpos > stream->len) { + newpos = stream->len; + } + stream->pos = newpos; + if (n == 1) { + return mrb_str_new(mrb, start, newpos - pos); + } else { + return mrb_str_cat(mrb, buf, start, newpos - pos); + } + } +} + +static mrb_value +mrb_input_stream_rewind(mrb_state *mrb, mrb_value self) +{ + mrb_input_stream_t *stream = DATA_PTR(self); + assert_is_open(mrb, stream); + stream->pos = 0; + return self; +} + + +static mrb_value +mrb_input_stream_byteindex(mrb_state *mrb, mrb_value self) +{ + mrb_input_stream_t *stream = DATA_PTR(self); + mrb_int chr, n, len; + + assert_is_open(mrb, stream); + + n = mrb_get_args(mrb, "i", &chr); + if (n != 1) { + mrb_raisef(mrb, E_ARGUMENT_ERROR, "wrong number of arguments (%S for 1)", mrb_fixnum_value(n)); + } + if (chr < 0 || chr > 255) { + mrb_raise(mrb, E_ARGUMENT_ERROR, "index should be a byte (0 - 255)"); + } + + len = seek_char(stream, chr); + if (len < 0) { + return mrb_nil_value(); + } + + return mrb_fixnum_value(len); +} + + +void +mrb_mruby_input_stream_gem_init(mrb_state* mrb) +{ + struct RClass * c = mrb_define_class(mrb, "InputStream", mrb->object_class); + + mrb_define_method(mrb, c, "gets", mrb_input_stream_gets, MRB_ARGS_NONE()); + mrb_define_method(mrb, c, "read", mrb_input_stream_read, MRB_ARGS_ANY()); + mrb_define_method(mrb, c, "initialize", mrb_input_stream_init, MRB_ARGS_BLOCK()); + mrb_define_method(mrb, c, "rewind", mrb_input_stream_rewind, MRB_ARGS_NONE()); + mrb_define_method(mrb, c, "byteindex", mrb_input_stream_byteindex, MRB_ARGS_ANY()); +} + +void +mrb_mruby_input_stream_gem_final(mrb_state* mrb) +{ +} diff --git a/web/server/h2o/libh2o/deps/mruby-input-stream/src/mruby_input_stream.h b/web/server/h2o/libh2o/deps/mruby-input-stream/src/mruby_input_stream.h new file mode 100644 index 000000000..39150ad6d --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-input-stream/src/mruby_input_stream.h @@ -0,0 +1,19 @@ +/* + * input_stream.h + * + */ +#ifndef mruby_input_stream_h +#define mruby_input_stream_h + +typedef void (*mrb_input_stream_free_callback)(mrb_state *mrb, const char *base, mrb_int len, void *cb_data); + +mrb_value +mrb_input_stream_value(mrb_state *mrb, const char *base, mrb_int len); + +void +mrb_input_stream_get_data(mrb_state *mrb, mrb_value self, const char **base, mrb_int *len, mrb_int *pos, mrb_input_stream_free_callback *free_cb, void **free_cb_data); + +void +mrb_input_stream_set_data(mrb_state *mrb, mrb_value self, const char *base, mrb_int len, mrb_int pos, mrb_input_stream_free_callback free_cb, void *free_cb_data); + +#endif /* input_stream_h */ diff --git a/web/server/h2o/libh2o/deps/mruby-input-stream/test/input_stream.rb b/web/server/h2o/libh2o/deps/mruby-input-stream/test/input_stream.rb new file mode 100644 index 000000000..8201786b1 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-input-stream/test/input_stream.rb @@ -0,0 +1,111 @@ +assert('InputStream.new()') do + vec = InputStream.new + assert_equal InputStream, vec.class +end + +assert('InputStream.new("foo")') do + vec = InputStream.new("foo") + assert_equal InputStream, vec.class +end + +assert('InputStream#read') do + vec = InputStream.new("foo") + assert_equal "foo", vec.read +end + +assert('InputStream#read(2)') do + vec = InputStream.new("abcdef") + assert_equal "ab", vec.read(2) + assert_equal "cde", vec.read(3) + assert_equal "f", vec.read(4) + assert_equal nil, vec.read(5) +end + +assert('InputStream#read(2, "")') do + vec = InputStream.new("abcdef") + s1 = "" + assert_equal "ab", vec.read(2, s1) + assert_equal "ab", s1 + assert_equal "abcde", vec.read(3, s1) + assert_equal "abcde", s1 + assert_equal "abcdef", vec.read(4, s1) + assert_equal "abcdef", s1 + assert_equal nil, vec.read(5, s1) + assert_equal "abcdef", s1 +end + +assert('InputStream#read(10)') do + vec = InputStream.new("abc") + assert_equal "abc", vec.read(10) + assert_equal nil, vec.read(10) +end + +assert('InputStream#read ""') do + vec = InputStream.new("") + assert_equal nil, vec.read(2) + assert_equal nil, vec.read(0) + assert_equal nil, vec.read(2) +end + +assert('InputStream#read(0)') do + vec = InputStream.new("foo") + assert_equal "", vec.read(0) + assert_equal "", vec.read(0) + assert_equal "f", vec.read(1) +end + + +assert('InputStream#gets') do + vec = InputStream.new("foo") + assert_equal "foo", vec.gets + assert_equal nil, vec.gets +end + +assert('InputStream#gets ""') do + vec = InputStream.new("") + assert_equal nil, vec.gets +end + +assert('InputStream#gets long') do + vec = InputStream.new("foo\nbar\nbuz\n") + assert_equal "foo\n", vec.gets + assert_equal "bar\n", vec.gets + assert_equal "buz\n", vec.gets + assert_equal nil, vec.gets +end + +assert('InputStream#gets NN') do + vec = InputStream.new("\n\nbuz") + assert_equal "\n", vec.gets + assert_equal "\n", vec.gets + assert_equal "buz", vec.gets + assert_equal nil, vec.gets +end + +assert('InputStream#each') do + vec = InputStream.new("foo\nbar\nbuz\nzzz") + buf = [] + vec.each do |line| + buf << line + end + assert_equal ["foo\n", "bar\n", "buz\n", "zzz"], buf +end + +assert('InputStream#each ""') do + vec = InputStream.new("") + buf = [] + vec.each do |line| + buf << line + end + assert_equal [], buf +end + +assert('InputStream#rewind') do + vec = InputStream.new("abcdef") + assert_equal "ab", vec.read(2) + assert_equal "cde", vec.read(3) + vec.rewind + assert_equal "abcd", vec.read(4) + assert_equal "ef", vec.read(5) +end + |