Skip to content

Commit

Permalink
Merge pull request #77 from syucream/feature/hostname
Browse files Browse the repository at this point in the history
Fix #76 Support Request#hostname
  • Loading branch information
syucream committed Sep 20, 2016
2 parents 1078d3d + 3ee9360 commit 9346ef4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
9 changes: 9 additions & 0 deletions spec/ats_config/ats_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require '../support/factory_girl_helper'
include FactoryGirlHelper

# generate template
puts <<EOS
template = "#{get(:request).template}"
req = ATS::Request.new
ATS::echo template % {hostname: req.hostname}
EOS
3 changes: 3 additions & 0 deletions spec/ats_config/remap.config
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ map #{get(:rputs).path} http://127.0.0.1:80/ @plugin=ts_mruby.so @pparam=#{etc_d
map #{get(:echo).path} http://127.0.0.1:80/ @plugin=ts_mruby.so @pparam=#{etc_dir}ats_echo.rb
map #{get(:return).path} http://127.0.0.1:80/ @plugin=ts_mruby.so @pparam=#{etc_dir}ats_return.rb

# ATS::Request
map #{get(:request).path} http://127.0.0.1:80/ @plugin=ts_mruby.so @pparam=#{etc_dir}ats_request.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
17 changes: 17 additions & 0 deletions spec/factories/ats_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Request
attr_accessor :path, :keys, :template
end

FactoryGirl.define do
factory :request do
keyhash = {
'ATS::Request#hostname' => 'hostname'
}

path '/ats_request'
keys keyhash
template "{\n" +
(keyhash.map do |k, v| " '#{k}': %{#{v}}" end).join("\n") +
"\n}"
end
end
16 changes: 16 additions & 0 deletions spec/features/ats_request_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require './spec_helper'

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

describe 'getter' do
it 'gets request info' do
visit request.path

keys = request.keys
keys.each do |k, v|
expect(page.body).to match(/'#{k}': .+\n/)
end
end
end
end
11 changes: 11 additions & 0 deletions src/ts_mruby_request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ static mrb_value ts_mrb_set_content_type(mrb_state *mrb, mrb_value self) {
return self;
}

static mrb_value ts_mrb_get_hostname(mrb_state *mrb, mrb_value self) {
auto *context = reinterpret_cast<TSMrubyContext *>(mrb->ud);
Transaction *transaction = context->getTransaction();
const Url &url = transaction->getClientRequest().getUrl();

const string &hostname = url.getHost();
return mrb_str_new(mrb, hostname.c_str(), hostname.length());
}

static mrb_value ts_mrb_get_request_uri(mrb_state *mrb, mrb_value self) {
auto *context = reinterpret_cast<TSMrubyContext *>(mrb->ud);
Transaction *transaction = context->getTransaction();
Expand Down Expand Up @@ -398,6 +407,8 @@ void ts_mrb_request_class_init(mrb_state *mrb, struct RClass *rclass) {
MRB_ARGS_NONE());
mrb_define_method(mrb, class_request, "content_type=",
ts_mrb_set_content_type, MRB_ARGS_REQ(1));
mrb_define_method(mrb, class_request, "hostname",
ts_mrb_get_hostname, MRB_ARGS_NONE());

// XXX Unsupported: ATS doesn't support API's that overwrite request line
// mrb_define_method(mrb, class_request, "request_line",
Expand Down

0 comments on commit 9346ef4

Please sign in to comment.