Skip to content

Commit eabadd9

Browse files
committed
docs: documented the new ngx.req.socket(true) API, upon which fancy protocols like WebSocket can be implemented.
1 parent 04e53ee commit eabadd9

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

README

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2946,6 +2946,8 @@ Nginx API for Lua
29462946
ngx.req.socket
29472947
syntax: *tcpsock, err = ngx.req.socket()*
29482948

2949+
syntax: *tcpsock, err = ngx.req.socket(raw)*
2950+
29492951
context: *rewrite_by_lua*, access_by_lua*, content_by_lua**
29502952

29512953
Returns a read-only cosocket object that wraps the downstream
@@ -2962,9 +2964,29 @@ Nginx API for Lua
29622964

29632965
If any request body data has been pre-read into the Nginx core request
29642966
header buffer, the resulting cosocket object will take care of this to
2965-
avoid potential data loss resulting from such pre-reading.
2966-
2967-
Chunked request bodies are not yet supported in this API.
2967+
avoid potential data loss resulting from such pre-reading. Chunked
2968+
request bodies are not yet supported in this API.
2969+
2970+
Since the "v0.9.0" release, this function accepts an optional boolean
2971+
"raw" argument. When this argument is "true", this function returns a
2972+
full duplex cosocket object wrapping around the raw downstream
2973+
connection socket, upon which you can call the receive, receiveuntil,
2974+
and send methods.
2975+
2976+
When the "raw" argument is "true", it is required that no pending data
2977+
from any previous ngx.say, ngx.print, or ngx.send_headers calls exists.
2978+
So if you have these downstream output calls previously, you should call
2979+
ngx.flush(true) before calling "ngx.req.socket(true)" to ensure that
2980+
there is no pending output data. Another requirement for this case is
2981+
that the request body must have already been read completely.
2982+
2983+
You can use the "raw request socket" returned by "ngx.req.socket(true)"
2984+
to implement fancy protocols like WebSocket
2985+
(<http://en.wikipedia.org/wiki/WebSocket>), or just emit your own raw
2986+
HTTP response header or body data. You can refer to the
2987+
lua-resty-websocket library
2988+
(<https://github.com/agentzh/lua-resty-websocket>) for a real world
2989+
example.
29682990

29692991
This function was first introduced in the "v0.5.0rc1" release.
29702992

README.markdown

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2710,6 +2710,8 @@ ngx.req.socket
27102710
--------------
27112711
**syntax:** *tcpsock, err = ngx.req.socket()*
27122712

2713+
**syntax:** *tcpsock, err = ngx.req.socket(raw)*
2714+
27132715
**context:** *rewrite_by_lua*, access_by_lua*, content_by_lua**
27142716

27152717
Returns a read-only cosocket object that wraps the downstream connection. Only [receive](http://wiki.nginx.org/HttpLuaModule#tcpsock:receive) and [receiveuntil](http://wiki.nginx.org/HttpLuaModule#tcpsock:receiveuntil) methods are supported on this object.
@@ -2719,9 +2721,14 @@ In case of error, `nil` will be returned as well as a string describing the erro
27192721
The socket object returned by this method is usually used to read the current request's body in a streaming fashion. Do not turn on the [lua_need_request_body](http://wiki.nginx.org/HttpLuaModule#lua_need_request_body) directive, and do not mix this call with [ngx.req.read_body](http://wiki.nginx.org/HttpLuaModule#ngx.req.read_body) and [ngx.req.discard_body](http://wiki.nginx.org/HttpLuaModule#ngx.req.discard_body).
27202722

27212723
If any request body data has been pre-read into the Nginx core request header buffer, the resulting cosocket object will take care of this to avoid potential data loss resulting from such pre-reading.
2722-
27232724
Chunked request bodies are not yet supported in this API.
27242725

2726+
Since the `v0.9.0` release, this function accepts an optional boolean `raw` argument. When this argument is `true`, this function returns a full duplex cosocket object wrapping around the raw downstream connection socket, upon which you can call the [receive](http://wiki.nginx.org/HttpLuaModule#tcpsock:receive), [receiveuntil](http://wiki.nginx.org/HttpLuaModule#tcpsock:receiveuntil), and [send](http://wiki.nginx.org/HttpLuaModule#tcpsock:send) methods.
2727+
2728+
When the `raw` argument is `true`, it is required that no pending data from any previous [ngx.say](http://wiki.nginx.org/HttpLuaModule#ngx.say), [ngx.print](http://wiki.nginx.org/HttpLuaModule#ngx.print), or [ngx.send_headers](http://wiki.nginx.org/HttpLuaModule#ngx.send_headers) calls exists. So if you have these downstream output calls previously, you should call [ngx.flush(true)](http://wiki.nginx.org/HttpLuaModule#ngx.flush) before calling `ngx.req.socket(true)` to ensure that there is no pending output data. Another requirement for this case is that the request body must have already been read completely.
2729+
2730+
You can use the "raw request socket" returned by `ngx.req.socket(true)` to implement fancy protocols like [WebSocket](http://en.wikipedia.org/wiki/WebSocket), or just emit your own raw HTTP response header or body data. You can refer to the [lua-resty-websocket library](https://github.com/agentzh/lua-resty-websocket) for a real world example.
2731+
27252732
This function was first introduced in the `v0.5.0rc1` release.
27262733

27272734
ngx.exec

doc/HttpLuaModule.wiki

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2626,6 +2626,8 @@ See also [[#ngx.req.init_body|ngx.req.init_body]].
26262626
== ngx.req.socket ==
26272627
'''syntax:''' ''tcpsock, err = ngx.req.socket()''
26282628
2629+
'''syntax:''' ''tcpsock, err = ngx.req.socket(raw)''
2630+
26292631
'''context:''' ''rewrite_by_lua*, access_by_lua*, content_by_lua*''
26302632
26312633
Returns a read-only cosocket object that wraps the downstream connection. Only [[#tcpsock:receive|receive]] and [[#tcpsock:receiveuntil|receiveuntil]] methods are supported on this object.
@@ -2635,9 +2637,14 @@ In case of error, <code>nil</code> will be returned as well as a string describi
26352637
The socket object returned by this method is usually used to read the current request's body in a streaming fashion. Do not turn on the [[#lua_need_request_body|lua_need_request_body]] directive, and do not mix this call with [[#ngx.req.read_body|ngx.req.read_body]] and [[#ngx.req.discard_body|ngx.req.discard_body]].
26362638
26372639
If any request body data has been pre-read into the Nginx core request header buffer, the resulting cosocket object will take care of this to avoid potential data loss resulting from such pre-reading.
2638-
26392640
Chunked request bodies are not yet supported in this API.
26402641
2642+
Since the <code>v0.9.0</code> release, this function accepts an optional boolean <code>raw</code> argument. When this argument is <code>true</code>, this function returns a full duplex cosocket object wrapping around the raw downstream connection socket, upon which you can call the [[#tcpsock:receive|receive]], [[#tcpsock:receiveuntil|receiveuntil]], and [[#tcpsock:send|send]] methods.
2643+
2644+
When the <code>raw</code> argument is <code>true</code>, it is required that no pending data from any previous [[#ngx.say|ngx.say]], [[#ngx.print|ngx.print]], or [[#ngx.send_headers|ngx.send_headers]] calls exists. So if you have these downstream output calls previously, you should call [[#ngx.flush|ngx.flush(true)]] before calling <code>ngx.req.socket(true)</code> to ensure that there is no pending output data. Another requirement for this case is that the request body must have already been read completely.
2645+
2646+
You can use the "raw request socket" returned by <code>ngx.req.socket(true)</code> to implement fancy protocols like [http://en.wikipedia.org/wiki/WebSocket WebSocket], or just emit your own raw HTTP response header or body data. You can refer to the [https://github.com/agentzh/lua-resty-websocket lua-resty-websocket library] for a real world example.
2647+
26412648
This function was first introduced in the <code>v0.5.0rc1</code> release.
26422649
26432650
== ngx.exec ==

0 commit comments

Comments
 (0)