xref: /aosp_15_r20/external/libwebsockets/READMEs/README.unix-domain-reverse-proxy.md (revision 1c60b9aca93fdbc9b5f19b2d2194c91294b22281)
1*1c60b9acSAndroid Build Coastguard Worker## Unix Domain Sockets Reverse Proxy
2*1c60b9acSAndroid Build Coastguard Worker
3*1c60b9acSAndroid Build Coastguard Worker### Introduction
4*1c60b9acSAndroid Build Coastguard Worker
5*1c60b9acSAndroid Build Coastguard Workerlws is able to use a mount to place reverse proxies into the URL space.
6*1c60b9acSAndroid Build Coastguard Worker
7*1c60b9acSAndroid Build Coastguard WorkerThese are particularly useful when using Unix Domain Sockets, basically
8*1c60b9acSAndroid Build Coastguard Workerfiles in the server filesystem, to communicate between lws and a separate
9*1c60b9acSAndroid Build Coastguard Workerserver process and integrate the result into a coherent URL namespace on
10*1c60b9acSAndroid Build Coastguard Workerthe lws side.  It's also possible to proxy using tcp sockets.
11*1c60b9acSAndroid Build Coastguard Worker
12*1c60b9acSAndroid Build Coastguard Worker![overview](../doc-assets/http-proxy-overview.svg)
13*1c60b9acSAndroid Build Coastguard Worker
14*1c60b9acSAndroid Build Coastguard WorkerThis has the advantage that the actual web server that forwards the
15*1c60b9acSAndroid Build Coastguard Workerdata from the unix socket owner is in a different process than the server
16*1c60b9acSAndroid Build Coastguard Workerthat serves on the unix socket.  If it has problems, they do not affect
17*1c60b9acSAndroid Build Coastguard Workerthe actual public-facing web server.  The unix domain socket server may
18*1c60b9acSAndroid Build Coastguard Workerbe in a completely different language than the web server.
19*1c60b9acSAndroid Build Coastguard Worker
20*1c60b9acSAndroid Build Coastguard WorkerCompared to CGI, there are no forks to make a connection to the unix
21*1c60b9acSAndroid Build Coastguard Workerdomain socket server.
22*1c60b9acSAndroid Build Coastguard Worker
23*1c60b9acSAndroid Build Coastguard Worker### Mount origin format
24*1c60b9acSAndroid Build Coastguard Worker
25*1c60b9acSAndroid Build Coastguard WorkerUnix Domain Sockets are effectively "files" in the server filesystem, and
26*1c60b9acSAndroid Build Coastguard Workerare defined by their filepath.  The "server" side that is to be proxied opens
27*1c60b9acSAndroid Build Coastguard Workerthe socket and listens on it, which creates a file in the server filesystem.
28*1c60b9acSAndroid Build Coastguard WorkerThe socket understands either http or https protocol.
29*1c60b9acSAndroid Build Coastguard Worker
30*1c60b9acSAndroid Build Coastguard WorkerLws can be told to act as a proxy for that at a mountpoint in the lws vhost
31*1c60b9acSAndroid Build Coastguard Workerurl space.
32*1c60b9acSAndroid Build Coastguard Worker
33*1c60b9acSAndroid Build Coastguard WorkerIf your mount is expressed in C code, then the mount type is LWSMPRO_HTTP or
34*1c60b9acSAndroid Build Coastguard WorkerLWSMPRO_HTTPS depending on the protocol the unix socket understands, and the
35*1c60b9acSAndroid Build Coastguard Workerorigin address has the form `+/path/to/unix/socket:/path/inside/mount`.
36*1c60b9acSAndroid Build Coastguard Worker
37*1c60b9acSAndroid Build Coastguard WorkerThe + at the start indicates it is a local unix socket we are proxying, and
38*1c60b9acSAndroid Build Coastguard Workerthe ':' acts as a delimiter for the socket path, since unlike other addresses
39*1c60b9acSAndroid Build Coastguard Workerthe unix socket path can contain '/' itself.
40*1c60b9acSAndroid Build Coastguard Worker
41*1c60b9acSAndroid Build Coastguard Worker### Connectivity rules and translations
42*1c60b9acSAndroid Build Coastguard Worker
43*1c60b9acSAndroid Build Coastguard WorkerOnward proxy connections from lws to the Unix Domain Socket happen using
44*1c60b9acSAndroid Build Coastguard Workerhttp/1.1.  That implies `transfer-encoding: chunking` in the case that the
45*1c60b9acSAndroid Build Coastguard Workerlength of the output is not known beforehand.
46*1c60b9acSAndroid Build Coastguard Worker
47*1c60b9acSAndroid Build Coastguard WorkerLws takes care of stripping any chunking (which is illegal in h2) and
48*1c60b9acSAndroid Build Coastguard Workertranslating between h1 and h2 header formats if the return connection is
49*1c60b9acSAndroid Build Coastguard Workeractually in http/2.
50*1c60b9acSAndroid Build Coastguard Worker
51*1c60b9acSAndroid Build Coastguard WorkerThe h1 onward proxy connection translates the following headers from the return
52*1c60b9acSAndroid Build Coastguard Workerconnection, which may be h1 or h2:
53*1c60b9acSAndroid Build Coastguard Worker
54*1c60b9acSAndroid Build Coastguard WorkerHeader|Function
55*1c60b9acSAndroid Build Coastguard Worker---|---
56*1c60b9acSAndroid Build Coastguard Workerhost|Which vhost
57*1c60b9acSAndroid Build Coastguard Workeretag|Information on any etag the client has cached for this URI
58*1c60b9acSAndroid Build Coastguard Workerif-modified-since|Information on the freshness of any etag the client has cached for this URI
59*1c60b9acSAndroid Build Coastguard Workeraccept-language|Which languages the return path client prefers
60*1c60b9acSAndroid Build Coastguard Workeraccept-encoding|Which compression encodings the client can accept
61*1c60b9acSAndroid Build Coastguard Workercache-control|Information from the return path client about cache acceptability
62*1c60b9acSAndroid Build Coastguard Workerx-forwarded-for|The IP address of the return path client
63*1c60b9acSAndroid Build Coastguard Worker
64*1c60b9acSAndroid Build Coastguard WorkerThis implies that the proxied connection can
65*1c60b9acSAndroid Build Coastguard Worker
66*1c60b9acSAndroid Build Coastguard Worker - return 301 etc to say the return path client's etag is still valid
67*1c60b9acSAndroid Build Coastguard Worker
68*1c60b9acSAndroid Build Coastguard Worker - choose to compress using an acceptable content-encoding
69*1c60b9acSAndroid Build Coastguard Worker
70*1c60b9acSAndroid Build Coastguard WorkerThe following headers are translated from the headers replied via the onward
71*1c60b9acSAndroid Build Coastguard Workerconnection (always h1) back to the return path (which may be h1 or h2)
72*1c60b9acSAndroid Build Coastguard Worker
73*1c60b9acSAndroid Build Coastguard WorkerHeader|Function
74*1c60b9acSAndroid Build Coastguard Worker---|---
75*1c60b9acSAndroid Build Coastguard Workercontent-length|If present, an assertion of how much payload is expected
76*1c60b9acSAndroid Build Coastguard Workercontent-type|The mimetype of the payload
77*1c60b9acSAndroid Build Coastguard Workeretag|The canonical etag for the content at this URI
78*1c60b9acSAndroid Build Coastguard Workeraccept-language|This is returned to the return path client because there is no easy way for the return path client to know what it sent originally.  It allows clientside selection of i18n.
79*1c60b9acSAndroid Build Coastguard Workercontent-encoding|Any compression format on the payload (selected from what the client sent in accept-encoding, if anything)
80*1c60b9acSAndroid Build Coastguard Workercache-control|The onward server's response about cacheability of its payload
81*1c60b9acSAndroid Build Coastguard Worker
82*1c60b9acSAndroid Build Coastguard Worker### h1 -> h2 conversion
83*1c60b9acSAndroid Build Coastguard Worker
84*1c60b9acSAndroid Build Coastguard WorkerChunked encoding that may have been used on the outgoing proxy client connection
85*1c60b9acSAndroid Build Coastguard Workeris removed for h2 return connections (chunked encoding is illegal for h2).
86*1c60b9acSAndroid Build Coastguard Worker
87*1c60b9acSAndroid Build Coastguard WorkerHeaders are converted to all lower-case and hpack format for h2 return connections.
88*1c60b9acSAndroid Build Coastguard Worker
89*1c60b9acSAndroid Build Coastguard WorkerHeader and payload proxying is staged according to when the return connection
90*1c60b9acSAndroid Build Coastguard Worker(which may be an h2 child stream) is writable.
91*1c60b9acSAndroid Build Coastguard Worker
92*1c60b9acSAndroid Build Coastguard Worker### Behaviour if unix domain socket server unavailable
93*1c60b9acSAndroid Build Coastguard Worker
94*1c60b9acSAndroid Build Coastguard WorkerIf the server that listens on the unix domain socket is down or being restarted,
95*1c60b9acSAndroid Build Coastguard Workerlws understands that it couldn't connect to it and returns a clean 503 response
96*1c60b9acSAndroid Build Coastguard Worker`HTTP_STATUS_SERVICE_UNAVAILABLE` along with a brief human-readable explanation.
97*1c60b9acSAndroid Build Coastguard Worker
98*1c60b9acSAndroid Build Coastguard WorkerThe generated status page produced will try to bring in a stylesheet
99*1c60b9acSAndroid Build Coastguard Worker`/error.css`.  This allows you to produce a styled error pages with logos,
100*1c60b9acSAndroid Build Coastguard Workergraphics etc.  See [this](https://libwebsockets.org/git/badrepo) for an example of what you can do with it.
101*1c60b9acSAndroid Build Coastguard Worker
102