-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add HTTP::Server#bind_unix #5959
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
RX14
merged 2 commits into
crystal-lang:master
from
straight-shoota:jm/feature/http-server-unix
Apr 18, 2018
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| require "spec" | ||
| require "http/server" | ||
| require "tempfile" | ||
|
|
||
| private class RaiseErrno < IO | ||
| def initialize(@value : Int32) | ||
|
|
@@ -40,6 +41,24 @@ private class ReverseResponseOutput < IO | |
| end | ||
| end | ||
|
|
||
| # TODO: replace with `HTTP::Client` once it supports connecting to Unix socket. | ||
| private def unix_request(path) | ||
| UNIXSocket.open(path) do |io| | ||
| io << "GET / HTTP/1.1\r\n" | ||
| io << "X-Unix-Socket: #{path}\r\n" | ||
| io << "\r\n" | ||
| io.flush | ||
|
|
||
| loop do | ||
| line = io.gets || break | ||
|
|
||
| if line.empty? | ||
| return io.gets | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| module HTTP | ||
| class Server | ||
| describe Response do | ||
|
|
@@ -310,6 +329,42 @@ module HTTP | |
| end | ||
| end | ||
| end | ||
|
|
||
| {% if flag?(:unix) %} | ||
| describe "#bind_unix" do | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indent |
||
| it "binds to different unix sockets" do | ||
| path1 = Tempfile.tempname | ||
| path2 = Tempfile.tempname | ||
|
|
||
| begin | ||
| server = Server.new do |context| | ||
| # TODO: Replace custom header with local_address | ||
| context.response.puts "Test Server (#{context.request.headers["X-Unix-Socket"]?})" | ||
| context.response.close | ||
| end | ||
|
|
||
| socket1 = UNIXServer.new(path1) | ||
| server.bind socket1 | ||
| socket2 = server.bind_unix path2 | ||
|
|
||
| spawn server.listen | ||
|
|
||
| Fiber.yield | ||
|
|
||
| unix_request(path1).should eq "Test Server (#{path1})" | ||
| unix_request(path2).should eq "Test Server (#{path2})" | ||
|
|
||
| server.close | ||
|
|
||
| File.exists?(path1).should be_false | ||
| File.exists?(path2).should be_false | ||
| ensure | ||
| File.delete(path1) if File.exists?(path1) | ||
| File.delete(path2) if File.exists?(path2) | ||
| end | ||
| end | ||
| end | ||
| {% end %} | ||
| end | ||
|
|
||
| describe HTTP::Server::RequestProcessor do | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it shorter to use
HTTP::Requestdirectly here?