Skip to content

Commit

Permalink
Merge 24e226c into fc371f9
Browse files Browse the repository at this point in the history
  • Loading branch information
syucream authored Nov 20, 2016
2 parents fc371f9 + 24e226c commit 5186fba
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 7 deletions.
15 changes: 15 additions & 0 deletions spec/ats_config/ats_headers_out_get.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require '../support/factory_girl_helper'
include FactoryGirlHelper

# generate template
puts <<EOS
class Handler
def on_read_response_hdr
hout = ATS::Headers_out.new
f = ATS::Filter.new
f.body = "#{get(:headers_out_get).key}: " + hout["#{get(:headers_out_get).key}"]
end
end
es = ATS::EventSystem.new
es.register Handler
EOS
3 changes: 3 additions & 0 deletions spec/ats_config/remap.config
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ map #{get(:redirect).path} http://127.0.0.1:80/ @plugin=ts_mruby.so @pparam=#{et
# ATS::Request
map #{get(:request).path} http://127.0.0.1:80/ @plugin=ts_mruby.so @pparam=#{etc_dir}ats_request.rb

# ATS::Headers_out
map #{get(:headers_out_get).path} http://127.0.0.1:8080/origin @plugin=ts_mruby.so @pparam=#{etc_dir}ats_headers_out_get.rb

# ATS::Filter
map #{get(:body_eq).path} http://127.0.0.1:8080#{get(:origin).path} @plugin=ts_mruby.so @pparam=#{etc_dir}ats_filter_body_eq.rb
map #{get(:body_eq).alias_path} http://127.0.0.1:8080#{get(:origin).path} @plugin=ts_mruby.so @pparam=#{etc_dir}ats_filter_output_eq.rb
Expand Down
10 changes: 10 additions & 0 deletions spec/factories/ats_headers_out.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class HeadersOutGet
attr_accessor :path, :key
end

FactoryGirl.define do
factory :headers_out_get do
path '/ats_headers_out_get'
key 'Server'
end
end
13 changes: 13 additions & 0 deletions spec/features/ats_headers_out_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require './spec_helper'

describe 'ATS class', :js => false do
let (:getter) { get(:headers_out_get) }

describe 'get' do
it 'gets specified response header field value' do
visit getter.path
expect(page.body).to match(/^#{getter.key}: .+$/)
end
end

end
15 changes: 10 additions & 5 deletions src/ts_mruby_internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,22 +201,27 @@ void FilterPlugin::appendBody(const string &data) {
transformedBuffer_.append(data);
}

void FilterPlugin::appendBlock(const mrb_value block) { block_ = block; }
void FilterPlugin::appendBlock(const mrb_value block) {
auto* tlmrb = ts_mruby::getThreadLocalMrubyStates();
block_ = tlmrb->getManager().lend_mrb_value(block);
}

void FilterPlugin::consume(const std::string &data) {
origBuffer_.append(data);
}

void FilterPlugin::handleInputComplete() {
if (!mrb_nil_p(block_)) {
mrb_state *state = mrb_open();
if (block_.get() != nullptr && !mrb_nil_p(block_->getValue())) {
auto* tlmrb = ts_mruby::getThreadLocalMrubyStates();
mrb_state* mrb = tlmrb->getMrb();

mrb_value rv =
mrb_yield(state, block_, mrb_str_new(state, origBuffer_.c_str(),
mrb_yield(mrb, block_->getValue(), mrb_str_new(mrb, origBuffer_.c_str(),
origBuffer_.length()));

// Convert to_s if the value isn't Ruby string
if (mrb_type(rv) != MRB_TT_STRING) {
rv = mrb_funcall(state, rv, "to_s", 0, NULL);
rv = mrb_funcall(mrb, rv, "to_s", 0, NULL);
}

transformedBuffer_ = string(RSTRING_PTR(rv), RSTRING_LEN(rv));
Expand Down
3 changes: 1 addition & 2 deletions src/ts_mruby_internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class FilterPlugin : public atscppapi::TransformationPlugin {
private:
std::string origBuffer_;
std::string transformedBuffer_;
mrb_value block_;
std::shared_ptr<LentMrbValue> block_;

public:
FilterPlugin(atscppapi::Transaction &transaction)
Expand All @@ -240,7 +240,6 @@ class FilterPlugin : public atscppapi::TransformationPlugin {

origBuffer_.reserve(FILTER_RESERVED_BUFFER_SIZE);
transformedBuffer_.reserve(FILTER_RESERVED_BUFFER_SIZE);
block_ = mrb_nil_value();
}

void appendBody(const std::string &data);
Expand Down

0 comments on commit 5186fba

Please sign in to comment.