[Groonga-commit] ranguba/groonga-client at 93987e7 [master] http: support sub path

Back to archive index

Kouhei Sutou null+****@clear*****
Mon Mar 21 22:19:24 JST 2016


Kouhei Sutou	2016-03-21 22:19:24 +0900 (Mon, 21 Mar 2016)

  New Revision: 93987e7bf2c089ed81fbd6749b6a4d31fdbf2734
  https://github.com/ranguba/groonga-client/commit/93987e7bf2c089ed81fbd6749b6a4d31fdbf2734

  Message:
    http: support sub path

  Added files:
    lib/groonga/client/protocol/http/path-resolvable.rb
  Modified files:
    lib/groonga/client.rb
    lib/groonga/client/protocol/http/coolio.rb
    lib/groonga/client/protocol/http/synchronous.rb
    test/test-client.rb

  Modified: lib/groonga/client.rb (+2 -1)
===================================================================
--- lib/groonga/client.rb    2016-03-21 16:44:45 +0900 (b47f98f)
+++ lib/groonga/client.rb    2016-03-21 22:19:24 +0900 (32af8bd)
@@ -197,6 +197,7 @@ module Groonga
       scheme = (options.delete(:protocol) || "gqtp").to_s
       host = options.delete(:host) || options.delete(:address) || "127.0.0.1"
       port = options.delete(:port) || default_port(scheme)
+      path = options.delete(:path)
       user = options.delete(:user)
       password = options.delete(:password)
       if user and password
@@ -211,7 +212,7 @@ module Groonga
         host,
         port,
         nil,
-        nil,
+        path,
         nil,
         nil,
         nil,

  Modified: lib/groonga/client/protocol/http/coolio.rb (+8 -3)
===================================================================
--- lib/groonga/client/protocol/http/coolio.rb    2016-03-21 16:44:45 +0900 (21bda0b)
+++ lib/groonga/client/protocol/http/coolio.rb    2016-03-21 22:19:24 +0900 (2b963c2)
@@ -1,4 +1,4 @@
-# Copyright (C) 2014  Kouhei Sutou <kou �� clear-code.com>
+# Copyright (C) 2014-2016  Kouhei Sutou <kou �� clear-code.com>
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -18,6 +18,7 @@ require "coolio"
 
 require "groonga/client/empty-request"
 require "groonga/client/protocol/error"
+require "groonga/client/protocol/http/path-resolvable"
 
 module Groonga
   class Client
@@ -38,6 +39,8 @@ module Groonga
           end
 
           class GroongaHTTPClient < ::Coolio::HttpClient
+            include PathResolvable
+
             def initialize(socket, callback)
               super(socket)
               @body = ""
@@ -73,10 +76,11 @@ module Groonga
           def send(command, &block)
             client = GroongaHTTPClient.connect(@host, @port, block)
             client.attach(@loop)
+            url = URI("http://#{@host}:#{@port}")
             if command.is_a?(Groonga::Command::Load)
               raw_values = command[:values]
               command[:values] = nil
-              path = command.to_uri_format
+              path = resolve_path(url, command.to_uri_format)
               command[:values] = raw_values
               options = {
                 :head => {
@@ -87,7 +91,8 @@ module Groonga
               }
               client.request("POST", path, options)
             else
-              client.request("GET", command.to_uri_format)
+              path = resolve_path(url, command.to_uri_format)
+              client.request("GET", path)
             end
             Request.new(client, @loop)
           end

  Added: lib/groonga/client/protocol/http/path-resolvable.rb (+30 -0) 100644
===================================================================
--- /dev/null
+++ lib/groonga/client/protocol/http/path-resolvable.rb    2016-03-21 22:19:24 +0900 (fa0f3fa)
@@ -0,0 +1,30 @@
+# Copyright (C) 2016  Kouhei Sutou <kou �� clear-code.com>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+
+module Groonga
+  class Client
+    module Protocol
+      class HTTP
+        module PathResolvable
+          private
+          def resolve_path(uri, path)
+            uri.path.chomp("/") + path
+          end
+        end
+      end
+    end
+  end
+end

  Modified: lib/groonga/client/protocol/http/synchronous.rb (+6 -2)
===================================================================
--- lib/groonga/client/protocol/http/synchronous.rb    2016-03-21 16:44:45 +0900 (4daee69)
+++ lib/groonga/client/protocol/http/synchronous.rb    2016-03-21 22:19:24 +0900 (8103de9)
@@ -20,12 +20,15 @@ require "net/http"
 require "groonga/client/version"
 require "groonga/client/empty-request"
 require "groonga/client/protocol/error"
+require "groonga/client/protocol/http/path-resolvable"
 
 module Groonga
   class Client
     module Protocol
       class HTTP
         class Synchronous
+          include PathResolvable
+
           def initialize(url, options={})
             @url = url
             @options = options
@@ -119,14 +122,15 @@ module Groonga
             if command.is_a?(Groonga::Command::Load)
               raw_values = command[:values]
               command[:values] = nil
-              path = command.to_uri_format
+              path = resolve_path(@url, command.to_uri_format)
               command[:values] = raw_values
               request = Net::HTTP::Post.new(path, headers)
               request.content_type = "application/json"
               request.content_length = raw_values.bytesize
               request.body_stream = StringIO.new(raw_values)
             else
-              request = Net::HTTP::Get.new(command.to_uri_format, headers)
+              path = resolve_path(@url, command.to_uri_format)
+              request = Net::HTTP::Get.new(path, headers)
             end
             setup_authentication(request)
             http.request(request)

  Modified: test/test-client.rb (+28 -0)
===================================================================
--- test/test-client.rb    2016-03-21 16:44:45 +0900 (68f0781)
+++ test/test-client.rb    2016-03-21 22:19:24 +0900 (6377eb7)
@@ -469,5 +469,33 @@ EOH
                      @request_headers["authorization"])
       end
     end
+
+    class TestPath < self
+      def component_based_open_options
+        {
+          :protocol => @protocol,
+          :host => @address,
+          :port => @port,
+        }
+      end
+
+      def test_with_trailing_slash
+        stub_response("[]")
+        options = component_based_open_options.merge(:path => "/sub_path/")
+        Groonga::Client.open(options) do |client|
+          client.status
+        end
+        assert_equal("/sub_path/d/status", @request_path)
+      end
+
+      def test_without_trailing_slash
+        stub_response("[]")
+        options = component_based_open_options.merge(:path => "/sub_path")
+        Groonga::Client.open(options) do |client|
+          client.status
+        end
+        assert_equal("/sub_path/d/status", @request_path)
+      end
+    end
   end
 end
-------------- next part --------------
HTML����������������������������...
Descargar 



More information about the Groonga-commit mailing list
Back to archive index