diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2015-11-08 18:18:33 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2015-11-08 18:18:33 +0900 |
commit | 2b23faa53b254bb20b5f3604f8bd8a42dab4f8d0 (patch) | |
tree | 49296e60a8222897b85562078da9c2d5d8765bc8 | |
parent | 939a1c014add6e938b31c19dd9a74332e0691bde (diff) | |
download | plum-2b23faa53b254bb20b5f3604f8bd8a42dab4f8d0.tar.gz |
stream_utils: remove #respond
-rw-r--r-- | examples/non_tls_server.rb | 18 | ||||
-rw-r--r-- | examples/static_server.rb | 41 | ||||
-rw-r--r-- | lib/plum/stream_utils.rb | 12 | ||||
-rw-r--r-- | test/plum/client/test_client.rb | 3 |
4 files changed, 36 insertions, 38 deletions
diff --git a/examples/non_tls_server.rb b/examples/non_tls_server.rb index ae8d692..e7bad41 100644 --- a/examples/non_tls_server.rb +++ b/examples/non_tls_server.rb @@ -77,28 +77,31 @@ loop do <input type=submit> </form> EOF - stream.respond({ + stream.send_headers({ ":status": "200", "server": "plum", "content-type": "text/html", "content-length": body.bytesize - }, body) + }, end_stream: false) + stream.send_data(body, end_stream: true) when ["POST", "/post.page"] body = "Posted value is: #{CGI.unescape(data).gsub("<", "<").gsub(">", ">")}<br> <a href=/>Back to top page</a>" - stream.respond({ + stream.send_headers({ ":status": "200", "server": "plum", "content-type": "text/html", "content-length": body.bytesize - }, body) + }, end_stream: false) + stream.send_data(body, end_stream: true) else body = "Page not found! <a href=/>Back to top page</a>" - stream.respond({ + stream.send_headers({ ":status": "404", "server": "plum", "content-type": "text/html", "content-length": body.bytesize - }, body) + }, end_stream: false) + stream.send_data(body, end_stream: true) end end end @@ -109,12 +112,13 @@ loop do plum << sock.readpartial(1024) end rescue Plum::LegacyHTTPError + data = "Use modern web browser with HTTP/2 support." resp = "HTTP/1.1 505 HTTP Version Not Supported\r\n" "Content-Type: text/plain\r\n" "Content-Length: #{data.bytesize}\r\n" "Server: plum/#{Plum::VERSION}\r\n" "\r\n" - "Use modern web browser with HTTP/2 support." + "#{data}" sock.write(resp) rescue diff --git a/examples/static_server.rb b/examples/static_server.rb index e2a3645..4013f28 100644 --- a/examples/static_server.rb +++ b/examples/static_server.rb @@ -95,52 +95,57 @@ loop do <input type=submit> </form> EOF - stream.respond({ + stream.send_headers({ ":status": "200", "server": "plum", "content-type": "text/html", - "content-length": body.size - }, body) + "content-length": body.bytesize + }, end_stream: false) + stream.send_data(body, end_stream: true) when ["GET", "/abc.html"] body = "ABC! <a href=/>Back to top page</a><br><img src=/image.nyan>" + stream.send_headers({ + ":status": "200", + "server": "plum", + "content-type": "text/html", + "content-length": body.bytesize + }, end_stream: false) i_stream = stream.promise({ ":authority": "localhost:40443", ":method": "GET", ":scheme": "https", ":path": "/image.nyan" }) - stream.respond({ - ":status": "200", - "server": "plum", - "content-type": "text/html", - "content-length": body.size - }, body) + stream.send_data(body, end_stream: true) image = ("iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAgMAAADXB5lNAAAACVBMVEX///93o0jG/4mTMy20AAAA" \ "bklEQVQ4y2NgoAoIRQJkCoSimIdTgJGBBU1ABE1A1AVdBQuaACu6gCALhhZ0axlZCDgMWYAB6ilU" \ "35IoADEMxWyyBDD45AhQCFahM0kXWIVu3sAJrILzyBcgytoFeATABBcXWohhCEC14BCgGAAAX1ZQ" \ "ZtJp0zAAAAAASUVORK5CYII=").unpack("m")[0] - i_stream.respond({ + i_stream.send_headers({ ":status": "200", "server": "plum", "content-type": "image/png", - "content-length": image.size - }, image) + "content-length": image.bytesize + }, end_stream: false) + i_stream.send_data(image, end_stream: true) when ["POST", "/post.page"] body = "Posted value is: #{CGI.unescape(data).gsub("<", "<").gsub(">", ">")}<br> <a href=/>Back to top page</a>" - stream.respond({ + stream.send_headers({ ":status": "200", "server": "plum", "content-type": "text/html", - "content-length": body.size - }, body) + "content-length": body.bytesize + }, end_stream: false) + stream.send_data(body, end_stream: true) else body = "Page not found! <a href=/>Back to top page</a>" - stream.respond({ + stream.send_headers({ ":status": "404", "server": "plum", "content-type": "text/html", - "content-length": body.size - }, body) + "content-length": body.bytesize + }, end_stream: false) + stream.send_data(body, end_stream: true) end end end diff --git a/lib/plum/stream_utils.rb b/lib/plum/stream_utils.rb index a8d959f..e344528 100644 --- a/lib/plum/stream_utils.rb +++ b/lib/plum/stream_utils.rb @@ -3,18 +3,6 @@ using Plum::BinaryString module Plum module StreamUtils - # Responds to a HTTP request. - # @param headers [Enumerable<String, String>] The response headers. - # @param body [String, IO] The response body. - def respond(headers, body = nil, end_stream: true) # TODO: priority, padding - if body - send_headers(headers, end_stream: false) - send_data(body, end_stream: end_stream) - else - send_headers(headers, end_stream: end_stream) - end - end - # Reserves a stream to server push. Sends PUSH_PROMISE and create new stream. # @param headers [Enumerable<String, String>] The *request* headers. It must contain all of them: ':authority', ':method', ':scheme' and ':path'. # @return [Stream] The stream to send push response. diff --git a/test/plum/client/test_client.rb b/test/plum/client/test_client.rb index 1cc0930..c9514cf 100644 --- a/test/plum/client/test_client.rb +++ b/test/plum/client/test_client.rb @@ -112,7 +112,8 @@ class ClientTest < Minitest::Test stream.send_data("a", end_stream: false) raise ExampleError, "example error" else - stream.respond({ ":status" => 200 }, headers.to_h[":method"] + headers.to_h["header"].to_s + data.to_s) + stream.send_headers({ ":status" => 200 }, end_stream: false) + stream.send_data(headers.to_h[":method"] + headers.to_h["header"].to_s + data.to_s, end_stream: true) end } } yield plum if block_given? |