Skip to content

Commit 8a3a90f

Browse files
committed
feature: handle unix socket (#38)
PR by [philippseith](chronoxor/NetCoreServer#250)
1 parent 32e7c69 commit 8a3a90f

File tree

9 files changed

+110
-4
lines changed

9 files changed

+110
-4
lines changed

documentation/docs/.vitepress/config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export default defineConfig({
7272
{ text: 'Json Web Token', link: '/guide/api-json-web-token' },
7373
{ text: 'Cross-Origin Resource Sharing', link: '/guide/api-cors' },
7474
{ text: 'SSL Certificate', link: '/guide/ssl-certificate' },
75+
{ text: 'Unix Sockets', link: '/guide/unix-sockets' },
7576
]
7677
},
7778
{

documentation/docs/guide/ssl-certificate.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The HTTPS protocol is supported and you can bring your own certificate in `PKCS#12` format.
44

5-
With a little change the [basic static example](./static-files) can serve HTTPS.
5+
With just a small change, the [basic static example](./static-files) can serve HTTPS.
66

77
::: code-group
88

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Unix Sockets
2+
3+
[Unix Domain Socket](https://en.wikipedia.org/wiki/Unix_domain_socket) (UDS) can also be used as an entrypoint for the server.
4+
They are supported on : **Linux**, **MacOS**, **Android**... and even **Windows** !
5+
6+
With just a small change, the [basic api example](./api-basic) can also be served over a Unix socket.
7+
8+
::: code-group
9+
10+
<<< @/snippets/unix-sockets.cs#snippet{13 csharp:line-numbers} [program.cs]
11+
12+
:::
13+
14+
You can use `curl` to test :
15+
16+
```
17+
$ curl --unix-socket C:\www\test.sock http://localhost/api/test
18+
> { "message" : "Hello World !" }
19+
```
20+
21+
There only one change :
22+
- L13 : use the `SimpleWServer()` constructor with `UnixDomainSocketEndPoint` argument.
23+

documentation/docs/reference/simplewserver.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public SimpleWServer(DnsEndPoint endpoint)
3131
public SimpleWServer(IPEndPoint endpoint)
3232
```
3333

34+
```csharp
35+
/// <summary>
36+
/// Initialize HTTP server with a given Unix domain socket endpoint
37+
/// </summary>
38+
/// <param name="endpoint">Unix domain socket endpoint</param>
39+
public WsServer(UnixDomainSocketEndPoint endpoint)
40+
```
41+
3442
### HTTPS
3543

3644
```csharp
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Net;
3+
using System.Net.Sockets;
4+
using SimpleW;
5+
6+
namespace Sample {
7+
class Program {
8+
9+
static void Main() {
10+
11+
// unix socket
12+
string unixSocketPath = @"C:\www\server.sock";
13+
var server = new SimpleWServer(new UnixDomainSocketEndPoint(unixSocketPath));
14+
15+
// find all Controllers classes and serve on the "/api" endpoint
16+
server.AddDynamicContent("/api");
17+
18+
// start non blocking background server
19+
server.Start();
20+
21+
Console.WriteLine(@"server available on : unix:C:\www\server.sock");
22+
23+
// block console for debug
24+
Console.ReadKey();
25+
26+
}
27+
}
28+
29+
// inherit from Controller
30+
public class SomeController : Controller {
31+
32+
// use the Route attribute to target a public method
33+
[Route("GET", "/test")]
34+
public object SomePublicMethod() {
35+
36+
// the Request property contains all data (Url, Headers...) from the client Request
37+
var url = Request.Url;
38+
39+
// the return will be serialized to json and sent as response to client
40+
return new {
41+
message = Message()
42+
};
43+
}
44+
45+
private string Message() {
46+
return "Hello World !";
47+
}
48+
49+
}
50+
51+
}

src/SimpleW/NetCoreServer/HttpServer.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
2-
using System.Net;
32
using System.IO;
3+
using System.Net;
4+
using System.Net.Sockets;
45

56
namespace NetCoreServer
67
{
@@ -32,6 +33,11 @@ public class HttpServer : TcpServer
3233
/// </summary>
3334
/// <param name="endpoint">IP endpoint</param>
3435
public HttpServer(IPEndPoint endpoint) : base(endpoint) { Cache = new FileCache(); }
36+
/// <summary>
37+
/// Initialize HTTP server with a given Unix domain socket endpoint
38+
/// </summary>
39+
/// <param name="endpoint">Unix domain socket endpoint</param>
40+
public HttpServer(UnixDomainSocketEndPoint endpoint) : base(endpoint) { Cache = new FileCache(); }
3541

3642
/// <summary>
3743
/// Get the static content cache

src/SimpleW/NetCoreServer/TcpServer.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ private TcpServer(EndPoint endpoint, string address, int port)
4949
Port = port;
5050
Endpoint = endpoint;
5151
}
52+
/// <summary>
53+
/// Initialize TCP server with a given Unix domain socket endpoint
54+
/// </summary>
55+
/// <param name="endpoint">Unix domain socket endpoint</param>
56+
public TcpServer(UnixDomainSocketEndPoint endpoint) : this(endpoint, endpoint.ToString(), 0) {}
5257

5358
/// <summary>
5459
/// Server Id
@@ -187,7 +192,8 @@ private TcpServer(EndPoint endpoint, string address, int port)
187192
/// <returns>Socket object</returns>
188193
protected virtual Socket CreateSocket()
189194
{
190-
return new Socket(Endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
195+
ProtocolType protocolType = Endpoint is UnixDomainSocketEndPoint ? ProtocolType.IP : ProtocolType.Tcp;
196+
return new Socket(Endpoint.AddressFamily, SocketType.Stream, protocolType);
191197
}
192198

193199
/// <summary>

src/SimpleW/NetCoreServer/WsServer.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Net;
3+
using System.Net.Sockets;
34
using System.Text;
45

56
namespace NetCoreServer
@@ -34,7 +35,11 @@ public class WsServer : HttpServer, IWebSocket
3435
/// </summary>
3536
/// <param name="endpoint">IP endpoint</param>
3637
public WsServer(IPEndPoint endpoint) : base(endpoint) { WebSocket = new WebSocket(this); }
37-
38+
/// <summary>
39+
/// Initialize HTTP server with a given Unix domain socket endpoint
40+
/// </summary>
41+
/// <param name="endpoint">Unix domain socket endpoint</param>
42+
public WsServer(UnixDomainSocketEndPoint endpoint) : base(endpoint) { WebSocket = new WebSocket(this); }
3843
#region Session management
3944

4045
public virtual bool CloseAll() => CloseAll(0, Span<byte>.Empty);

src/SimpleW/SimpleW/SimpleWServer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ public SimpleWServer(DnsEndPoint endpoint) : base(endpoint) { }
5151
/// <param name="endpoint">IP endpoint</param>
5252
public SimpleWServer(IPEndPoint endpoint) : base(endpoint) { }
5353

54+
/// <summary>
55+
/// Initialize server with a given Unix domain socket endpoint
56+
/// </summary>
57+
/// <param name="endpoint">Unix domain socket endpoint</param>
58+
public SimpleWServer(UnixDomainSocketEndPoint endpoint) : base(endpoint) { }
59+
5460
/// <summary>
5561
/// Herited mandatory factory http/ws session builder
5662
/// </summary>

0 commit comments

Comments
 (0)