-
Notifications
You must be signed in to change notification settings - Fork 284
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
Add Unix domain socket support for listenTCP/listenHTTP for libevent on Posix #2073
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -389,8 +389,29 @@ final class Libevent2Driver : EventDriver { | |
|
||
Libevent2TCPListener listenTCP(ushort port, void delegate(TCPConnection conn) @safe connection_callback, string address, TCPListenOptions options) | ||
{ | ||
auto bind_addr = resolveHost(address, AF_UNSPEC, false); | ||
bind_addr.port = port; | ||
NetworkAddress bind_addr; | ||
version(Posix) | ||
{ | ||
import core.sys.posix.sys.un; | ||
import core.stdc.string : strcpy; | ||
|
||
if (address[0] == '/') | ||
{ | ||
bind_addr.family = AF_UNIX; | ||
sockaddr_un* s = bind_addr.sockAddrUnix(); | ||
enforce(s.sun_path.length > address.length, "Unix sockets cannot have that long a name."); | ||
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. "that long a name" - it's generally good to be explicit in the error messages. The second part of 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. Also this needs to be 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. This is the same enforce and message that was used for the UDS implementation of requestHTTP. 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. Phobos doesn't seem to specify the length either, but I could change it to 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. It shouldn't have to be 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. AFAICS, the path must always be 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. We're copying from |
||
s.sun_family = AF_UNIX; | ||
() @trusted { strcpy(cast(char*)s.sun_path.ptr,address.toStringz()); } (); | ||
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. The 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. Also copied from the UDS implementation of requestHTTP. 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.
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. Would be better in many cases because it always avoids a GC allocation, while |
||
} else | ||
{ | ||
bind_addr = resolveHost(address, AF_UNSPEC, false); | ||
bind_addr.port = port; | ||
} | ||
} else | ||
{ | ||
bind_addr = resolveHost(address, AF_UNSPEC, false); | ||
bind_addr.port = port; | ||
} | ||
|
||
auto listenfd_raw = () @trusted { return socket(bind_addr.family, SOCK_STREAM, 0); } (); | ||
// on Win64 socket() returns a 64-bit value but libevent expects an int | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "unit-socket-server-example", | ||
"description": "A minimal HTTP server using Unix domain sockets", | ||
"dependencies": { | ||
"vibe-d:http": {"path": "../../"}, | ||
"vibe-d:web": {"path": "../../"}, | ||
"vibe-d:core": {"path": "../../"} | ||
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. FWIW you would only need 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. Hmm, I think I copied this from another example's dub.json. Might be worth taking a look at others to see if they have unnecessary dependencies as well then. 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.
|
||
}, | ||
"versions": ["VibeNoSSL", "VibeDefaultMain"], | ||
"subConfigurations": { "vibe-d:core": "libevent"} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import vibe.inet.url; | ||
import vibe.http.server; | ||
import vibe.http.router; | ||
import std.stdio; | ||
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. Use 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. Yeah sorry, that's a leftover, will fix that. |
||
|
||
void handleHelloRequest(scope HTTPServerRequest req, scope HTTPServerResponse res) | ||
{ | ||
res.writeBody("Hello, World!", "text/plain"); | ||
} | ||
|
||
shared static this() | ||
{ | ||
auto router = new URLRouter; | ||
router.get("/hello", &handleHelloRequest); | ||
|
||
auto settings = new HTTPServerSettings; | ||
settings.bindAddresses = ["/tmp/vibe.sock"]; | ||
|
||
listenHTTP(settings, router); | ||
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. Have a look at the other tests for examples on how you can verify the output of the server with 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. Will check that out, thanks. 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. Hmm, I'm not sure how much sense it would make adding tests for this PR considering the client side (i.e. 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. Would surely be good to add a test that tests both sides at once with a simple request. But since it depends on vibe-core fixes to make it work there, too, I'd defer that, though and treat this topic WIP in the meantime. |
||
} |
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.
This disallows relative paths, is that necessary?
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.
No, but we couldn't simply check if it's a valid path because an IP address might also be a valid path.
@s-ludwig Any objections to using
std.path.isValidPath(address) && std.socket.getAddressInfo(address, AddressInfoFlags.NUMERICHOST).empty
here? Or is there a way to do this withvibe.core
?We could also just change it to
address.canFind('/')
, but then we wouldn't support Windows paths and paths below the working directory without./
.