Merge ~paelzer/ubuntu/+source/nginx:lp-1874831-nchan-bump-FOCAL into ubuntu/+source/nginx:ubuntu/focal-devel

Proposed by Christian Ehrhardt 
Status: Rejected
Rejected by: Christian Ehrhardt 
Proposed branch: ~paelzer/ubuntu/+source/nginx:lp-1874831-nchan-bump-FOCAL
Merge into: ubuntu/+source/nginx:ubuntu/focal-devel
Diff against target: 42023 lines (+25985/-6146)
134 files modified
debian/changelog (+7/-0)
debian/modules/nchan/README.md (+897/-134)
debian/modules/nchan/changelog.txt (+205/-0)
debian/modules/nchan/cloc-exclude.txt (+10/-11)
debian/modules/nchan/config (+69/-18)
debian/modules/nchan/src/nchan_commands.rb (+351/-32)
debian/modules/nchan/src/nchan_config_commands.c (+275/-9)
debian/modules/nchan/src/nchan_defs.c (+8/-1)
debian/modules/nchan/src/nchan_defs.h (+9/-2)
debian/modules/nchan/src/nchan_module.c (+277/-143)
debian/modules/nchan/src/nchan_module.h (+13/-21)
debian/modules/nchan/src/nchan_setup.c (+672/-33)
debian/modules/nchan/src/nchan_types.h (+157/-40)
debian/modules/nchan/src/nchan_variables.c (+6/-0)
debian/modules/nchan/src/nchan_version.h (+1/-0)
debian/modules/nchan/src/store/memory/groups.c (+570/-0)
debian/modules/nchan/src/store/memory/groups.h (+55/-0)
debian/modules/nchan/src/store/memory/ipc-handlers.c (+526/-171)
debian/modules/nchan/src/store/memory/ipc-handlers.h (+13/-3)
debian/modules/nchan/src/store/memory/ipc.c (+27/-3)
debian/modules/nchan/src/store/memory/ipc.h (+10/-1)
debian/modules/nchan/src/store/memory/memstore.c (+1013/-460)
debian/modules/nchan/src/store/memory/store-private.h (+34/-7)
debian/modules/nchan/src/store/memory/store.h (+3/-3)
debian/modules/nchan/src/store/redis/cmp.c (+927/-410)
debian/modules/nchan/src/store/redis/cmp.h (+59/-7)
debian/modules/nchan/src/store/redis/hiredis/.gitignore (+7/-0)
debian/modules/nchan/src/store/redis/hiredis/.travis.yml (+24/-0)
debian/modules/nchan/src/store/redis/hiredis/CHANGELOG.md (+110/-0)
debian/modules/nchan/src/store/redis/hiredis/COPYING (+29/-0)
debian/modules/nchan/src/store/redis/hiredis/Makefile (+217/-0)
debian/modules/nchan/src/store/redis/hiredis/README.md (+392/-0)
debian/modules/nchan/src/store/redis/hiredis/adapters/ae.h (+127/-0)
debian/modules/nchan/src/store/redis/hiredis/adapters/libev.h (+147/-0)
debian/modules/nchan/src/store/redis/hiredis/adapters/libevent.h (+108/-0)
debian/modules/nchan/src/store/redis/hiredis/adapters/libuv.h (+121/-0)
debian/modules/nchan/src/store/redis/hiredis/async.c (+687/-0)
debian/modules/nchan/src/store/redis/hiredis/async.h (+129/-0)
debian/modules/nchan/src/store/redis/hiredis/dict.c (+338/-0)
debian/modules/nchan/src/store/redis/hiredis/dict.h (+126/-0)
debian/modules/nchan/src/store/redis/hiredis/examples/example-ae.c (+62/-0)
debian/modules/nchan/src/store/redis/hiredis/examples/example-libev.c (+52/-0)
debian/modules/nchan/src/store/redis/hiredis/examples/example-libevent.c (+53/-0)
debian/modules/nchan/src/store/redis/hiredis/examples/example-libuv.c (+53/-0)
debian/modules/nchan/src/store/redis/hiredis/examples/example.c (+78/-0)
debian/modules/nchan/src/store/redis/hiredis/fmacros.h (+21/-0)
debian/modules/nchan/src/store/redis/hiredis/hiredis.c (+1021/-0)
debian/modules/nchan/src/store/redis/hiredis/hiredis.h (+226/-0)
debian/modules/nchan/src/store/redis/hiredis/net.c (+461/-0)
debian/modules/nchan/src/store/redis/hiredis/net.h (+53/-0)
debian/modules/nchan/src/store/redis/hiredis/read.c (+525/-0)
debian/modules/nchan/src/store/redis/hiredis/read.h (+116/-0)
debian/modules/nchan/src/store/redis/hiredis/sds.c (+1095/-0)
debian/modules/nchan/src/store/redis/hiredis/sds.h (+105/-0)
debian/modules/nchan/src/store/redis/hiredis/test.c (+807/-0)
debian/modules/nchan/src/store/redis/hiredis/win32.h (+42/-0)
debian/modules/nchan/src/store/redis/rdsstore.c (+923/-1208)
debian/modules/nchan/src/store/redis/redis-lua-scripts/.gitignore (+1/-0)
debian/modules/nchan/src/store/redis/redis-lua-scripts/add_fakesub.lua (+9/-4)
debian/modules/nchan/src/store/redis/redis-lua-scripts/channel_keepalive.lua (+5/-4)
debian/modules/nchan/src/store/redis/redis-lua-scripts/delete.lua (+33/-23)
debian/modules/nchan/src/store/redis/redis-lua-scripts/find_channel.lua (+33/-19)
debian/modules/nchan/src/store/redis/redis-lua-scripts/get_message.lua (+16/-17)
debian/modules/nchan/src/store/redis/redis-lua-scripts/get_message_from_key.lua (+3/-4)
debian/modules/nchan/src/store/redis/redis-lua-scripts/publish.lua (+64/-55)
debian/modules/nchan/src/store/redis/redis-lua-scripts/publish_status.lua (+4/-4)
debian/modules/nchan/src/store/redis/redis-lua-scripts/rsck.lua (+27/-10)
debian/modules/nchan/src/store/redis/redis-lua-scripts/subscriber_register.lua (+18/-6)
debian/modules/nchan/src/store/redis/redis-lua-scripts/subscriber_unregister.lua (+3/-3)
debian/modules/nchan/src/store/redis/redis-lua-scripts/testscripts.rb (+4/-3)
debian/modules/nchan/src/store/redis/redis_lua_commands.c (+1036/-0)
debian/modules/nchan/src/store/redis/redis_lua_commands.h (+17/-984)
debian/modules/nchan/src/store/redis/redis_nginx_adapter.c (+28/-36)
debian/modules/nchan/src/store/redis/redis_nginx_adapter.h (+6/-1)
debian/modules/nchan/src/store/redis/redis_nodeset.c (+2179/-0)
debian/modules/nchan/src/store/redis/redis_nodeset.h (+276/-0)
debian/modules/nchan/src/store/redis/redis_nodeset_parser.c (+241/-0)
debian/modules/nchan/src/store/redis/redis_nodeset_parser.h (+40/-0)
debian/modules/nchan/src/store/redis/store-private.h (+53/-139)
debian/modules/nchan/src/store/redis/store.h (+8/-3)
debian/modules/nchan/src/store/spool.c (+168/-122)
debian/modules/nchan/src/store/spool.h (+6/-8)
debian/modules/nchan/src/subscribers/benchmark.c (+72/-0)
debian/modules/nchan/src/subscribers/benchmark.h (+3/-0)
debian/modules/nchan/src/subscribers/common.c (+162/-411)
debian/modules/nchan/src/subscribers/common.h (+7/-10)
debian/modules/nchan/src/subscribers/eventsource.c (+87/-7)
debian/modules/nchan/src/subscribers/getmsg_proxy.c (+106/-0)
debian/modules/nchan/src/subscribers/getmsg_proxy.h (+1/-0)
debian/modules/nchan/src/subscribers/http-chunked.c (+6/-4)
debian/modules/nchan/src/subscribers/http-multipart-mixed.c (+15/-12)
debian/modules/nchan/src/subscribers/http-raw-stream.c (+9/-7)
debian/modules/nchan/src/subscribers/internal.c (+13/-6)
debian/modules/nchan/src/subscribers/longpoll-private.h (+3/-1)
debian/modules/nchan/src/subscribers/longpoll.c (+97/-143)
debian/modules/nchan/src/subscribers/memstore_ipc.c (+43/-20)
debian/modules/nchan/src/subscribers/memstore_ipc.h (+3/-0)
debian/modules/nchan/src/subscribers/memstore_multi.c (+23/-30)
debian/modules/nchan/src/subscribers/memstore_redis.c (+67/-63)
debian/modules/nchan/src/subscribers/memstore_redis.h (+0/-1)
debian/modules/nchan/src/subscribers/websocket.c (+912/-486)
debian/modules/nchan/src/subscribers/websocket.h (+2/-1)
debian/modules/nchan/src/uthash.h (+747/-489)
debian/modules/nchan/src/util/hdr_histogram.c (+1015/-0)
debian/modules/nchan/src/util/hdr_histogram.h (+427/-0)
debian/modules/nchan/src/util/nchan_benchmark.c (+804/-0)
debian/modules/nchan/src/util/nchan_benchmark.h (+91/-0)
debian/modules/nchan/src/util/nchan_bufchainpool.c (+96/-0)
debian/modules/nchan/src/util/nchan_bufchainpool.h (+17/-0)
debian/modules/nchan/src/util/nchan_channel_id.c (+74/-8)
debian/modules/nchan/src/util/nchan_channel_id.h (+4/-1)
debian/modules/nchan/src/util/nchan_debug.c (+81/-0)
debian/modules/nchan/src/util/nchan_debug.h (+13/-0)
debian/modules/nchan/src/util/nchan_fake_request.c (+267/-24)
debian/modules/nchan/src/util/nchan_fake_request.h (+55/-3)
debian/modules/nchan/src/util/nchan_list.c (+26/-0)
debian/modules/nchan/src/util/nchan_list.h (+5/-0)
debian/modules/nchan/src/util/nchan_msg.c (+291/-33)
debian/modules/nchan/src/util/nchan_msg.h (+19/-0)
debian/modules/nchan/src/util/nchan_output.c (+73/-22)
debian/modules/nchan/src/util/nchan_output.h (+4/-2)
debian/modules/nchan/src/util/nchan_output_info.c (+271/-0)
debian/modules/nchan/src/util/nchan_output_info.h (+3/-4)
debian/modules/nchan/src/util/nchan_rbtree.h (+3/-3)
debian/modules/nchan/src/util/nchan_slist.c (+164/-0)
debian/modules/nchan/src/util/nchan_slist.h (+41/-0)
debian/modules/nchan/src/util/nchan_subrequest.c (+66/-9)
debian/modules/nchan/src/util/nchan_subrequest.h (+4/-1)
debian/modules/nchan/src/util/nchan_util.c (+866/-47)
debian/modules/nchan/src/util/nchan_util.h (+47/-3)
debian/modules/nchan/src/util/ngx_nchan_hacked_slab.c (+4/-0)
debian/modules/nchan/src/util/shmem.c (+44/-5)
debian/modules/nchan/src/util/shmem.h (+5/-0)
dev/null (+0/-128)
Reviewer Review Type Date Requested Status
Canonical Server Pending
Canonical Server packageset reviewers Pending
git-ubuntu developers Pending
Review via email: mp+409828@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Christian Ehrhardt  (paelzer) wrote :
Revision history for this message
Christian Ehrhardt  (paelzer) wrote :

Fix is a cherry pick from Salsa of what went into Groovy.

SRU template once the rrporter confirms this is helpful.

Revision history for this message
Christian Ehrhardt  (paelzer) wrote :

I'll rework this to also fix http-lua

Unmerged commits

e1587cc... by Christian Ehrhardt 

changelog: nchan: Upgrade module to 1.2.7 (LP: #1874831)

Signed-off-by: Christian Ehrhardt <email address hidden>

c8c1511... by Ondřej Nový

nchan: Upgrade to 1.2.7

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/debian/changelog b/debian/changelog
2index e91b482..79d22ef 100644
3--- a/debian/changelog
4+++ b/debian/changelog
5@@ -1,3 +1,10 @@
6+nginx (1.18.0-0ubuntu1.3) focal; urgency=medium
7+
8+ * nchan: Upgrade module to 1.2.7 as former versions are incompatible
9+ with 1.18 (LP: #1874831)
10+
11+ -- Christian Ehrhardt <christian.ehrhardt@canonical.com> Thu, 07 Oct 2021 13:59:10 +0200
12+
13 nginx (1.18.0-0ubuntu1.2) focal-security; urgency=medium
14
15 * SECURITY UPDATE: DNS Resolver issues
16diff --git a/debian/modules/nchan/README.md b/debian/modules/nchan/README.md
17index ee43ac9..152396d 100644
18--- a/debian/modules/nchan/README.md
19+++ b/debian/modules/nchan/README.md
20@@ -1,57 +1,56 @@
21-<img class="logo" alt="NCHAN" src="https://nchan.slact.net/github-logo.png" />
22+<img class="logo" alt="NCHAN" src="https://nchan.io/github-logo.png" />
23
24-https://nchan.slact.net
25+https://nchan.io
26
27 Nchan is a scalable, flexible pub/sub server for the modern web, built as a module for the [Nginx](http://nginx.org) web server. It can be configured as a standalone server, or as a shim between your application and hundreds, thousands, or millions of live subscribers. It can buffer messages in memory, on-disk, or via [Redis](http://redis.io). All connections are handled asynchronously and distributed among any number of worker processes. It can also scale to many Nginx servers with [Redis](http://redis.io).
28
29 Messages are [published](#publisher-endpoints) to channels with HTTP `POST` requests or Websocket, and [subscribed](#subscriber-endpoint) also through [Websocket](#websocket), [long-polling](#long-polling), [EventSource](#eventsource) (SSE), old-fashioned [interval polling](#interval-polling), [and](#http-chunked-transfer) [more](#http-multipart-mixed).
30
31-In a web browser, you can use Websocket or EventSource directly, or the [NchanSubscriber.js](https://github.com/slact/nchan/blob/master/NchanSubscriber.js) wrapper library. It supports Long-Polling, EventSource, and resumable Websockets, and has a few other added convenience options.
32+In a web browser, you can use Websocket or EventSource natively, or the [NchanSubscriber.js](https://github.com/slact/nchan.js) wrapper library. It supports Long-Polling, EventSource, and resumable Websockets, and has a few other added convenience options. It's also available on [NPM](https://www.npmjs.com/package/nchan).
33
34 ## Features
35- - RESTful, HTTP-native API.
36- - Supports [Websocket](https://nchan.slact.net/#websocket), [EventSource (Server-Sent Events)](https://nchan.slact.net/#eventsource), [Long-Polling](https://nchan.slact.net/#long-polling) and other HTTP-based subscribers.
37- - No-repeat, no-loss message delivery guarantees with per-channel configurable message buffers.
38+ - RESTful, HTTP-native [API](#publishing-messages).
39+ - Supports [Websocket](#websocket), [EventSource (Server-Sent Events)](#eventsource), [Long-Polling](#long-polling) and other HTTP-based subscribers.
40+ - Per-channel configurable message buffers with no-repeat, no-loss message delivery guarantees.
41 - Subscribe to [hundreds of channels](#channel-multiplexing) over a single subscriber connection.
42- - HTTP request [callbacks and hooks](https://nchan.slact.net/details#application-callbacks) for easy integration.
43- - Introspection with [channel events](https://nchan.slact.net/details#channel-events) and [url for monitoring performance statistics](https://nchan.slact.net/details#nchan_stub_status).
44- - Fast ephemeral local message storage and optional, slower, persistent storage with [Redis](https://nchan.slact.net/details#connecting-to-a-redis-server).
45- - Horizontally scalable (using [Redis](https://nchan.slact.net/details#connecting-to-a-redis-server)).
46- - Highly Available with no single point of failure (using [Redis Cluster](https://nchan.slact.net/details#redis-cluster)).
47-
48-<!-- toc -->
49+ - HTTP request [callbacks and hooks](#hooks-and-callbacks) for easy integration.
50+ - Introspection with [channel events](#channel-events) and [url for monitoring performance statistics](#nchan_stub_status-stats).
51+ - Channel [group](#channel-groups) usage [accounting and limits](#limits-and-accounting).
52+ - Fast, nonblocking [shared-memory local message storage](#memory-storage) and optional, slower, persistent storage with [Redis](#redis).
53+ - Horizontally scalable (using [Redis](#redis)).
54+ - Auto-failover and [high availability](#high-availability) with no single point of failure using [Redis Cluster](#redis-cluster).
55
56 ## Status and History
57
58-The latest Nchan release is v1.0.8 (November 28, 2016) ([changelog](https://nchan.slact.net/changelog)).
59+The latest Nchan release is 1.2.7 (March 17, 2020) ([changelog](https://nchan.io/changelog)).
60
61-The first iteration of Nchan was written in 2009-2010 as the [Nginx HTTP Push Module](https://pushmodule.slact.net), and was vastly refactored into its present state in 2014-2016. The present release is in the **testing** phase. The core features and old functionality are thoroughly tested and stable. Some of the new functionality, especially Redis Cluster may be a bit buggy.
62+The first iteration of Nchan was written in 2009-2010 as the [Nginx HTTP Push Module](https://pushmodule.slact.net), and was vastly refactored into its present state in 2014-2016.
63
64 #### Upgrade from Nginx HTTP Push Module
65
66-Although Nchan is backwards-compatible with all Push Module configuration directives, some of the more unusual and rarely used settings have been disabled and will be ignored (with a warning). See the [upgrade page](https://nchan.slact.net/upgrade) for a detailed list of changes and improvements, as well as a full list of incompatibilities.
67+Although Nchan is backwards-compatible with all Push Module configuration directives, some of the more unusual and rarely used settings have been disabled and will be ignored (with a warning). See the [upgrade page](https://nchan.io/upgrade) for a detailed list of changes and improvements, as well as a full list of incompatibilities.
68
69
70 ## Does it scale?
71
72-<img class="benchmark_graph" alt="benchmarking internal subscriber response times" src="https://nchan.slact.net/img/benchmark_internal_total.png" />
73+<img class="benchmark_graph" alt="benchmarking internal subscriber response times" src="https://nchan.io/img/benchmark_internal_total.png" />
74
75 Yes it does. Like Nginx, Nchan can easily handle as much traffic as you can throw at it. I've tried to benchmark it, but my benchmarking tools are much slower than Nchan. The data I've gathered is on how long Nchan itself takes to respond to every subscriber after publishing a message -- this excludes TCP handshake times and internal HTTP request parsing. Basically, it measures how Nchan scales assuming all other components are already tuned for scalability. The graphed data are averages of 5 runs with 50-byte messages.
76
77-With a well-tuned OS and network stack on commodity server hardware, expect to handle upwards of 300K concurrent subscribers per second at minimal CPU load. Nchan can also be scaled out to multiple Nginx instances using the [Redis storage engine](#nchan_use_redis), and that too can be scaled up beyond a single-point-of-failure by using [Redis Cluster](https://nchan.slact.net/details#using-redis).
78+With a well-tuned OS and network stack on commodity server hardware, expect to handle upwards of 300K concurrent subscribers per second at minimal CPU load. Nchan can also be scaled out to multiple Nginx instances using the [Redis storage engine](#nchan_use_redis), and that too can be scaled up beyond a single-point-of-failure by using [Redis Cluster](#redis-cluster).
79
80-Currently, Nchan's main bottleneck is not CPU load but memory bandwidth. This can be improved significantly in future versions with fewer allocations and better use of contiguous memory pools. Please consider supporting Nchan to speed up the work of memory cache optimization.
81
82 ## Install
83
84 #### Download Packages
85- - [Arch Linux](https://archlinux.org): [nginx-nchan](https://aur.archlinux.org/packages/nginx-nchan/) and [nginx-nchan-git](https://aur.archlinux.org/packages/nginx-nchan-git/) are available in the Arch User Repository.
86- - Mac OS X: a [homebrew](http://brew.sh) package is available. `brew tap homebrew/nginx; brew install nginx-full --with-nchan-module`
87- - [Debian](https://www.debian.org/): A dynamic module build for is available in the Debian package repository: [libnginx-mod-nchan](https://packages.debian.org/sid/libnginx-mod-nchan).
88- Additionally, you can use the pre-built static module packages [nginx-common.deb](https://nchan.slact.net/download/nginx-common.deb) and [nginx-extras.deb](https://nchan.slact.net/download/nginx-extras.deb). Download both and install them with `dpkg -i`, followed by `sudo apt-get -f install`.
89- - [Ubuntu](http://www.ubuntu.com/): [nginx-common.ubuntu.deb](https://nchan.slact.net/download/nginx-common.ubuntu.deb) and [nginx-extras.ubuntu.deb](https://nchan.slact.net/download/nginx-extras.ubuntu.deb). Download both and install them with `dpkg -i`, followed by `sudo apt-get -f install`. Who knows when Ubuntu will add them to their repository?...
90- - [Fedora](https://fedoraproject.org): Dynamic module builds for Nginx > 1.10.0 are available: [nginx-mod-nchan.x86_64.rpm](https://nchan.slact.net/download/nginx-mod-nchan.x86-64.rpm), [nginx-mod-nchan.src.rpm](https://nchan.slact.net/download/nginx-mod-nchan.src.rpm).
91- - A statically compiled binary and associated linux nginx installation files are also [available as a tarball](https://nchan.slact.net/download/nginx-nchan-latest.tar.gz).
92+ - [Arch Linux](https://archlinux.org): [nginx-mod-nchan](https://aur.archlinux.org/packages/nginx-mod-nchan/) and [nginx-mainline-mod-nchan](https://aur.archlinux.org/packages/nginx-mainline-mod-nchan/) are available in the Arch User Repository.
93+ - Mac OS X: a [homebrew](http://brew.sh) package is available. `brew tap denji/nginx; brew install nginx-full --with-nchan-module`
94+ - [Debian](https://www.debian.org/): A dynamic module build is available in the Debian package repository: [libnginx-mod-nchan](https://packages.debian.org/sid/libnginx-mod-nchan).
95+ Additionally, you can use the pre-built static module packages [nginx-common.deb](https://nchan.io/download/nginx-common.deb) and [nginx-extras.deb](https://nchan.io/download/nginx-extras.deb). Download both and install them with `dpkg -i`, followed by `sudo apt-get -f install`.
96+ - [Ubuntu](http://www.ubuntu.com/): [nginx-common.ubuntu.deb](https://nchan.io/download/nginx-common.ubuntu.deb) and [nginx-extras.ubuntu.deb](https://nchan.io/download/nginx-extras.ubuntu.deb). Download both and install them with `dpkg -i`, followed by `sudo apt-get -f install`. Who knows when Ubuntu will add Nchan to their repository?...
97+ - [Fedora](https://fedoraproject.org): Dynamic module builds for Nginx > 1.10.0 are available: [nginx-mod-nchan.x86_64.rpm](https://nchan.io/download/nginx-mod-nchan.x86-64.rpm), [nginx-mod-nchan.src.rpm](https://nchan.io/download/nginx-mod-nchan.src.rpm).
98+ - [Heroku](https://heroku.com): A buildpack for compiling Nchan into Nginx is available: [nchan-buildpack](https://github.com/andjosh/nchan-buildpack). A one-click, readily-deployable app is also available: [nchan-heroku](https://github.com/andjosh/nchan-heroku).
99+ - A statically compiled binary and associated linux nginx installation files are also [available as a tarball](https://nchan.io/download/nginx-nchan-latest.tar.gz).
100
101
102 #### Build From Source
103@@ -62,7 +61,7 @@ Grab the latest copy of Nginx from [nginx.org](http://nginx.org). Grab the lates
104
105 If you're using Nginx > 1.9.11, you can build Nchan as a [dynamic module](https://www.nginx.com/blog/dynamic-modules-nginx-1-9-11/) with `--add-dynamic-module=path/to/nchan`
106
107-Run `make`, `make install`, and enjoy. (Caution, contents may be hot.)
108+Run `make`, then `make install`.
109
110 ## Getting Started
111
112@@ -87,11 +86,11 @@ http {
113 }
114 ```
115
116-You can now publish messages to channels by `POST`ing data to `/sub?id=channel_id` , and subscribe by pointing Websocket, EventSource, or [NchanSubscriber.js](https://github.com/slact/nchan/blob/master/NchanSubscriber.js) to `sub/?id=channel_id`. It's that simple.
117+You can now publish messages to channels by `POST`ing data to `/pub?id=channel_id` , and subscribe by pointing Websocket, EventSource, or [NchanSubscriber.js](https://github.com/slact/nchan.js) to `sub/?id=channel_id`. It's that simple.
118
119 But Nchan is very flexible and highly configurable. So, of course, it can get a lot more complicated...
120
121-## Conceptual Overview
122+### Conceptual Overview
123
124 The basic unit of most pub/sub solutions is the messaging *channel*. Nchan is no different. Publishers send messages to channels with a certain *channel id*, and subscribers subscribed to those channels receive them. Some number of messages may be buffered for a time in a channel's message buffer before they are deleted. Pretty simple, right?
125
126@@ -121,15 +120,26 @@ http {
127 The above maps requests to the URI `/sub` onto the channel `foobar`'s *subscriber endpoint* , and similarly `/pub` onto channel `foobar`'s *publisher endpoint*.
128
129
130-#### Publisher Endpoints
131+## Publisher Endpoints
132
133 Publisher endpoints are Nginx config *locations* with the [*`nchan_publisher`*](#nchan_publisher) directive.
134
135 Messages can be published to a channel by sending HTTP **POST** requests with the message contents to the *publisher endpoint* locations. You can also publish messages through a **Websocket** connection to the same location.
136
137+```nginx
138+ location /pub {
139+ #example publisher location
140+ nchan_publisher;
141+ nchan_channel_id foo;
142+ nchan_channel_group test;
143+ nchan_message_buffer_length 50;
144+ nchan_message_timeout 5m;
145+ }
146+```
147+
148 <!-- tag:publisher -->
149
150-##### Publishing Messages
151+### Publishing Messages
152
153 Requests and websocket messages are responded to with information about the channel at time of message publication. Here's an example from publishing with `curl`:
154
155@@ -147,7 +157,7 @@ The response can be in plaintext (as above), JSON, or XML, based on the request'
156 ```console
157 > curl --request POST --data "test message" -H "Accept: text/json" http://127.0.0.2:80/pub
158
159- {"messages": 6, "requested": 55, "subscribers": 0, "last_message_id": "1450755317:0" }
160+ {"messages": 5, "requested": 18, "subscribers": 0, "last_message_id": "1450755280:0" }
161 ```
162
163 Websocket publishers also receive the same responses when publishing, with the encoding determined by the *`Accept`* header present during the handshake.
164@@ -156,7 +166,7 @@ The response code for an HTTP request is *`202` Accepted* if no subscribers are
165
166 Metadata can be added to a message when using an HTTP POST request for publishing. A `Content-Type` header will be associated as the message's content type (and output to Long-Poll, Interval-Poll, and multipart/mixed subscribers). A `X-EventSource-Event` header can also be used to associate an EventSource `event:` line value with a message.
167
168-##### Other Publisher Endpoint Actions
169+### Other Publisher Endpoint Actions
170
171 **HTTP `GET`** requests return channel information without publishing a message. The response code is `200` if the channel exists, and `404` otherwise:
172 ```console
173@@ -171,15 +181,32 @@ Metadata can be added to a message when using an HTTP POST request for publishin
174
175 **HTTP `DELETE`** requests delete a channel and end all subscriber connections. Like the `GET` requests, this returns a `200` status response with channel info if the channel existed, and a `404` otherwise.
176
177-For an in-depth explanation of how settings are applied to channels from publisher locations, see the [details page](https://nchan.slact.net/details#publisher-endpoint-configs).
178+### How Channel Settings Work
179+
180+*A channel's configuration is set to the that of its last-used publishing location.*
181+So, if you want a channel to behave consistently, and want to publish to it from multiple locations, *make sure those locations have the same configuration*.
182
183-#### Subscriber Endpoints
184+You can also can use differently-configured publisher locations to dynamically update a channel's message buffer settings. This can be used to erase messages or to scale an existing channel's message buffer as desired.
185+
186+## Subscriber Endpoints
187
188 Subscriber endpoints are Nginx config *locations* with the [*`nchan_subscriber`*](#nchan_subscriber) directive.
189
190 Nchan supports several different kinds of subscribers for receiving messages: [*Websocket*](#websocket), [*EventSource*](#eventsource) (Server Sent Events), [*Long-Poll*](#long-polling), [*Interval-Poll*](#interval-polling). [*HTTP chunked transfer*](#http-chunked-transfer), and [*HTTP multipart/mixed*](#http-multipart-mixed).
191
192-- ##### Long-Polling
193+```nginx
194+ location /sub {
195+ #example subscriber location
196+ nchan_subscriber;
197+ nchan_channel_id foo;
198+ nchan_channel_group test;
199+ nchan_subscriber_first_message oldest;
200+ }
201+```
202+
203+<!-- tag:subscriber -->
204+
205+- ### Long-Polling
206 The tried-and-true server-push method supported by every browser out there.
207 Initiated by sending an HTTP `GET` request to a channel subscriber endpoint.
208 The long-polling subscriber walks through a channel's message queue via the built-in cache mechanism of HTTP clients, namely with the "`Last-Modified`" and "`Etag`" headers. Explicitly, to receive the next message for given a long-poll subscriber response, send a request with the "`If-Modified-Since`" header set to the previous response's "`Last-Modified`" header, and "`If-None-Match`" likewise set to the previous response's "`Etag`" header.
209@@ -187,15 +214,16 @@ Nchan supports several different kinds of subscribers for receiving messages: [*
210 A message's associated content type, if present, will be sent to this subscriber with the `Content-Type` header.
211 <!-- tag:subscriber-longpoll -->
212
213-- ##### Interval-Polling
214+- ### Interval-Polling
215 Works just like long-polling, except if the requested message is not yet available, immediately responds with a `304 Not Modified`.
216- There is no way to differentiate between long-poll and interval-poll subscriber requests, so long-polling must be disabled for a subscriber location if you wish to use interval-polling.
217+ Nchan cannot automatically distinguish between long-poll and interval-poll subscriber requests, so long-polling must be disabled for a subscriber location if you wish to use interval-polling.
218
219-- ##### Websocket
220+- ### Websocket
221 Bidirectional communication for web browsers. Part of the [HTML5 spec](http://www.w3.org/TR/2014/REC-html5-20141028/single-page.html). Nchan supports the latest protocol version 13 ([RFC 6455](https://tools.ietf.org/html/rfc6455)).
222 Initiated by sending a websocket handshake to the desired subscriber endpoint location.
223- If the websocket connection is closed by the server, the `close` frame will contain the HTTP response code and status line describing the reason for closing the connection. Server-initiated keep-alive pings can be configured with the [`nchan_websocket_ping_interval`](#nchan_websocket_ping_interval) config directive. Websocket extensions are not yet supported.
224- Messages published through a websocket connection can be forwarded to an upstream application with the [`nchan_publisher_upstream_request`](#nchan_publisher_upstream_request) config directive.
225+ If the websocket connection is closed by the server, the `close` frame will contain the HTTP response code and status line describing the reason for closing the connection. Server-initiated keep-alive pings can be configured with the [`nchan_websocket_ping_interval`](#nchan_websocket_ping_interval) config directive.
226+ Messages are delivered to subscribers in `text` websocket frames, except if a message's `content-type` is "`application/octet-stream`" -- then it is delivered in a `binary` frame.
227+ <br />
228 Websocket subscribers can use the custom `ws+meta.nchan` subprotocol to receive message metadata with messages, making websocket connections resumable. Messages received with this subprotocol are of the form
229 <pre>
230 id: message_id
231@@ -204,9 +232,24 @@ Nchan supports several different kinds of subscribers for receiving messages: [*
232 message_data
233 </pre>
234 The `content-type:` line may be omitted.
235+ <br />
236+ #### Websocket Publisher
237+ Messages published through a websocket connection can be forwarded to an upstream application with the [`nchan_publisher_upstream_request`](#nchan_publisher_upstream_request) config directive.
238+ Messages published in a binary frame are automatically given the `content-type` "`application/octet-stream`".
239+ #### Permessage-deflate
240+ Nchan version 1.1.8 and above supports the [permessage-deflate protocol extension](https://tools.ietf.org/html/rfc7692). Messages are deflated once when they are published, and then can be broadcast to any number of compatible websocket subscribers. Message deflation is enabled by setting the [`nchan_deflate_message_for_websocket on;`](#nchan_deflate_message_for_websocket) directive in a publisher location.
241+ <br />
242+ The deflated data is stored alongside the original message in memory, or, if large enough, on disk. This means more [shared memory](#nchan_shared_memory_size) is necessary when using `nchan_deflate_message_for_websocket`.
243+ <br />
244+ Deflation parameters (speed, memory use, strategy, etc.), can be tweaked using the [`nchan_permessage_deflate_compression_window`](#nchan_permessage_deflate_compression_window), [`nchan_permessage_deflate_compression_level`](#nchan_permessage_deflate_compression_level),
245+ [`nchan_permessage_deflate_compression_strategy`](#nchan_permessage_deflate_compression_strategy), and
246+ [`nchan_permessage_deflate_compression_window`](#nchan_permessage_deflate_compression_window) settings.
247+ <br />
248+ Nchan also supports the (deprecated) [perframe-deflate extension](https://tools.ietf.org/html/draft-tyoshino-hybi-websocket-perframe-deflate-06) still in use by Safari as `x-webkit-perframe-deflate`.
249+ <br />
250 <!-- tag:subscriber-websocket -->
251
252-- ##### EventSource
253+- ### EventSource
254 Also known as [Server-Sent Events](https://en.wikipedia.org/wiki/Server-sent_events) or SSE, it predates Websockets in the [HTML5 spec](http://www.w3.org/TR/2014/REC-html5-20141028/single-page.html), and is a [very simple protocol](http://www.w3.org/TR/eventsource/#event-stream-interpretation).
255 Initiated by sending an HTTP `GET` request to a channel subscriber endpoint with the "`Accept: text/event-stream`" header.
256 Each message `data: ` segment will be prefaced by the message `id: `.
257@@ -217,7 +260,7 @@ Nchan supports several different kinds of subscribers for receiving messages: [*
258 A message's associated `event` type, if present, will be sent to this subscriber with the `event:` line.
259 <!-- tag:subscriber-eventsource -->
260
261-- ##### HTTP [multipart/mixed](http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html#z0)
262+- ### HTTP [multipart/mixed](http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html#z0)
263 The `multipart/mixed` MIMEtype was conceived for emails, but hey, why not use it for HTTP? It's easy to parse and includes metadata with each message.
264 Initiated by including an `Accept: multipart/mixed` header.
265 The response headers and the unused "preamble" portion of the response body are sent right away, with the boundary string generated randomly for each subscriber. Each subsequent message will be sent as one part of the multipart message, and will include the message time and tag (`Last-Modified` and `Etag`) as well as the optional `Content-Type` headers.
266@@ -225,11 +268,11 @@ Nchan supports several different kinds of subscribers for receiving messages: [*
267 A message's associated content type, if present, will be sent to this subscriber with the `Content-Type` header.
268 <!-- tag:subscriber-multipart -->
269
270-- ##### HTTP Raw Stream
271+- ### HTTP Raw Stream
272 A simple subscription method similar to the [streaming subscriber](https://github.com/wandenberg/nginx-push-stream-module/blob/master/docs/directives/subscribers.textile#push_stream_subscriber) of the [Nginx HTTP Push Stream Module](https://github.com/wandenberg/nginx-push-stream-module). Messages are appended to the response body, separated by a newline or configurable by `nchan_subscriber_http_raw_stream_separator`.
273 <!-- tag:subscriber-rawstream -->
274
275-- ##### HTTP [Chunked Transfer](http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6.1)
276+- ### HTTP [Chunked Transfer](http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6.1)
277 This subscription method uses the `chunked` `Transfer-Encoding` to receive messages.
278 Initiated by explicitly including `chunked` in the [`TE` header](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.39):
279 `TE: chunked` (or `TE: chunked;q=??` where the qval > 0)
280@@ -237,9 +280,7 @@ Nchan supports several different kinds of subscribers for receiving messages: [*
281 Unlike the other subscriber types, the `chunked` subscriber cannot be used with http/2 because it dissallows chunked encoding.
282 <!-- tag:subscriber-chunked -->
283
284-<!-- tag:subscriber -->
285-
286-#### PubSub Endpoint
287+## PubSub Endpoint
288
289 PubSub endpoints are Nginx config *locations* with the [*`nchan_pubsub`*](#nchan_pubsub) directive.
290
291@@ -249,17 +290,19 @@ requests as subscribers, and all HTTP `POST` as publishers. One simple use case
292 ```nginx
293 location = /pubsub {
294 nchan_pubsub;
295- nchan_channel_id foobar;
296+ nchan_channel_id foo;
297+ nchan_channel_group test;
298 }
299 ```
300
301-A more applicable setup may set different publisher and subscriber channel ids:
302+A more interesting setup may set different publisher and subscriber channel ids:
303
304 ```nginx
305 location = /pubsub {
306 nchan_pubsub;
307 nchan_publisher_channel_id foo;
308 nchan_subscriber_channel_id bar;
309+ nchan_channel_group test;
310 }
311 ```
312
313@@ -267,9 +310,9 @@ Here, subscribers will listen for messages on channel `foo`, and publishers will
314
315 <!-- tag:pubsub -->
316
317-### The Channel ID
318+## The Channel ID
319
320-So far the examples have used static channel ids, which is not very useful in practice. It can be set to any nginx *variable*, such as a querystring argument, a header value, or a part of the location url:
321+So far the examples have used static channel ids, which is not very useful. In practice, the channel id can be set to any nginx *variable*, such as a querystring argument, a header value, or a part of the location url:
322
323 ```nginx
324 location = /sub_by_ip {
325@@ -294,9 +337,11 @@ So far the examples have used static channel ids, which is not very useful in pr
326 }
327 ```
328
329+I recommend using the last option, a channel id derived from the request URL via a regular expression. It makes things nice and RESTful.
330+
331 <!-- tag:channel-id -->
332
333-#### Channel Multiplexing
334+### Channel Multiplexing
335
336 With channel multiplexing, subscribers can subscribe to up to 255 channels per connection. Messages published to all the specified channels will be delivered in-order to the subscriber. There are two ways to enable multiplexing:
337
338@@ -325,68 +370,626 @@ For more than 7 channels, `nchan_channel_id_split_delimiter` can be used to spli
339 }
340 ```
341
342-Publishing to multiple channels with a single request is also possible, with similar configuration:
343+It is also possible to publish to multiple channels with a single request as well as delete multiple channels with a single request, with similar configuration:
344
345 ```nginx
346 location ~ /multipub/(\w+)/(\w+)$ {
347 nchan_publisher;
348 nchan_channel_id "$1" "$2" "another_channel";
349+ #POST /multipub/foo/bar will publish to:
350+ # channels 'foo', 'bar', 'another_channel'
351+ #DELETE /multipub/foo/bar will delete:
352+ # channels 'foo', 'bar', 'another_channel'
353 }
354 ```
355
356-`DELETE` requests to a multiplexed channel broadcast the deletion to each of the channels it multiplexes, deletes all their messages and kicks out all clients subscribed to any of the channel ids.
357+When a channel is deleted, all of its messages are deleted, and all of its subscribers' connection are closed -- including ones subscribing through a multiplexed location. For example, suppose a subscriber is subscribed to channels "foo" and "bar" via a single multiplexed connection. If "foo" is deleted, the connection is closed, and the subscriber therefore loses the "bar" subscription as well.
358
359-See the [details page](https://nchan.slact.net/details#securing-channels) for more information about using good IDs and keeping channels secure.
360+See the [Channel Security](#securing-channels) section about using good IDs and keeping private channels secure.
361
362 <!-- tag:channel-multiplexing -->
363
364+### Channel Groups
365+
366+Channels can be associated with groups to avoid channel ID conflicts:
367+
368+```nginx
369+ location /test_pubsub {
370+ nchan_pubsub;
371+ nchan_channel_group "test";
372+ nchan_channel_id "foo";
373+ }
374+
375+ location /pubsub {
376+ nchan_pubsub;
377+ nchan_channel_group "production";
378+ nchan_channel_id "foo";
379+ #same channel id, different channel group. Thus, different channel.
380+ }
381+
382+ location /flexgroup_pubsub {
383+ nchan_pubsub;
384+ nchan_channel_group $arg_group;
385+ nchan_channel_id "foo";
386+ #group can be set with request variables too
387+ }
388+```
389+
390+#### Limits and Accounting
391+
392+Groups can be used to track aggregate channel usage, as well as set limits on the number of channels, subscribers, stored messages, memory use, etc:
393+
394+```nginx
395+ #enable group accounting
396+ nchan_channel_group_accounting on;
397+
398+ location ~ /pubsub/(\w+)$ {
399+ nchan_pubsub;
400+ nchan_channel_group "limited";
401+ nchan_channel_id $1;
402+ }
403+
404+ location ~ /prelimited_pubsub/(\w+)$ {
405+ nchan_pubsub;
406+ nchan_channel_group "limited";
407+ nchan_channel_id $1;
408+ nchan_group_max_subscribers 100;
409+ nchan_group_max_messages_memory 50M;
410+ }
411+
412+ location /group {
413+ nchan_channel_group limited;
414+ nchan_group_location;
415+ nchan_group_max_channels $arg_max_channels;
416+ nchan_group_max_messages $arg_max_messages;
417+ nchan_group_max_messages_memory $arg_max_messages_mem;
418+ nchan_group_max_messages_disk $arg_max_messages_disk;
419+ nchan_group_max_subscribers $arg_max_subs;
420+ }
421+```
422+
423+Here, `/group` is an `nchan_group_location`, which is used for accessing and modifying group data. To get group data, send a `GET` request to a `nchan_group_location`:
424+
425+```sh
426+> curl http://localhost/group
427+
428+channels: 10
429+subscribers: 0
430+messages: 219
431+shared memory used by messages: 42362 bytes
432+disk space used by messages: 0 bytes
433+limits:
434+ max channels: 0
435+ max subscribers: 0
436+ max messages: 0
437+ max messages shared memory: 0
438+ max messages disk space: 0
439+```
440+
441+By default, the data is returned in human-readable plaintext, but can also be formatted as JSON, XML, or YAML:
442+
443+```sh
444+> curl -H "Accept: text/json" http://localhost/group
445+
446+{
447+ "channels": 21,
448+ "subscribers": 40,
449+ "messages": 53,
450+ "messages_memory": 19941,
451+ "messages_disk": 0,
452+ "limits": {
453+ "channels": 0,
454+ "subscribers": 0,
455+ "messages": 0,
456+ "messages_memory": 0,
457+ "messages_disk": 0
458+ }
459+}
460+```
461+
462+The data in the response are for the single Nchan instance only, regardless of whether Redis is used. A limit of 0 means 'unlimited'.
463+
464+Limits can be set per-location, as with the above `/prelimited_pubsub/...` location, or with a POST request to the `nchan_group_location`:
465+```sh
466+> curl -X POST "http://localhost/group?max_channels=15&max_subs=1000&max_messages_disk=0.5G"
467+
468+channels: 0
469+subscribers: 0
470+messages: 0
471+shared memory used by messages: 0 bytes
472+disk space used by messages: 0 bytes
473+limits:
474+ max channels: 15
475+ max subscribers: 1000
476+ max messages: 0
477+ max messages shared memory: 0
478+ max messages disk space: 536870912
479+
480+```
481+
482+Limits are only applied locally, regardless of whether Redis is enabled.
483+If a publisher or subscriber request exceeds a group limit, Nchan will respond to it with a `403 Forbidden` response.
484+
485+<!-- tag:group -->
486+
487+## Hooks and Callbacks
488+
489+<!-- tag:hook -->
490+
491+### Request Authorization
492+
493+This feature, configured with [`nchan_authorize_request`](#nchan_authorize_request), behaves just like the Nginx [http_auth_request module](http://nginx.org/en/docs/http/ngx_http_auth_request_module.html#auth_request_set).
494+
495+Consider the configuration:
496+```nginx
497+ upstream my_app {
498+ server 127.0.0.1:8080;
499+ }
500+ location = /auth {
501+ proxy_pass http://my_app/pubsub_authorize;
502+ proxy_pass_request_body off;
503+ proxy_set_header Content-Length "";
504+ proxy_set_header X-Subscriber-Type $nchan_subscriber_type;
505+ proxy_set_header X-Publisher-Type $nchan_publisher_type;
506+ proxy_set_header X-Prev-Message-Id $nchan_prev_message_id;
507+ proxy_set_header X-Channel-Id $nchan_channel_id;
508+ proxy_set_header X-Original-URI $request_uri;
509+ proxy_set_header X-Forwarded-For $remote_addr;
510+ }
511+
512+ location ~ /pubsub/auth/(\w+)$ {
513+ nchan_channel_id $1;
514+ nchan_authorize_request /auth;
515+ nchan_pubsub;
516+ nchan_channel_group test;
517+ }
518+```
519+
520+Here, any request to the location `/pubsub/auth/<...>` will need to be authorized by your application (`my_app`). Nginx will generate a `GET /pubsub_authorize` request to the application, with additional headers set by the `proxy_set_header` directives. Note that Nchan-specific variables are available for this authorization request. Once your application receives this request, it should decide whether or not to authorize the subscriber. This can be done based on a forwarded session cookie, IP address, or any set of parameters of your choosing. If authorized, it should respond with an empty `200 OK` response.
521+All non-`2xx` response codes (such as `403 Forbidden`) are intepreted as authorization failures. In this case, the failing response is proxied to the client.
522+
523+Note that Websocket and EventSource clients will only try to authorize during the initial handshake request, whereas Long-Poll and Interval-Poll subscribers will need to be authorized each time they request the next message, which may flood your application with too many authorization requests.
524+
525+<!-- commands: nchan_authorize_request -->
526+
527+### Subscriber Presence
528+
529+Subscribers can notify an application when they have subscribed and unsubscribed to a channel using the [`nchan_subscribe_request`](#nchan_subscribe_request)
530+and [`nchan_unsubscribe_request`](#nchan_unsubscribe_request) settings.
531+These should point to Nginx locations configured to forward requests to an upstream proxy (your application):
532+
533+```nginx
534+ location ~ /sub/(\w+)$ {
535+ nchan_channel_id $1;
536+ nchan_subscribe_request /upstream/sub;
537+ nchan_unsubscribe_request /upstream/unsub;
538+ nchan_subscriber;
539+ nchan_channel_group test;
540+ }
541+
542+ location = /upstream/unsub {
543+ proxy_pass http://127.0.0.1:9292/unsub;
544+ proxy_ignore_client_abort on; #!!!important!!!!
545+ proxy_set_header X-Subscriber-Type $nchan_subscriber_type;
546+ proxy_set_header X-Channel-Id $nchan_channel_id;
547+ proxy_set_header X-Original-URI $request_uri;
548+ }
549+ location = /upstream/sub {
550+ proxy_pass http://127.0.0.1:9292/sub;
551+ proxy_set_header X-Subscriber-Type $nchan_subscriber_type;
552+ proxy_set_header X-Message-Id $nchan_message_id;
553+ proxy_set_header X-Channel-Id $nchan_channel_id;
554+ proxy_set_header X-Original-URI $request_uri;
555+ }
556+```
557+
558+In order for `nchan_unsubscribe_request` to work correctly, the location it points to must have `proxy_ignore_client_abort on;`. Otherwise, suddenly aborted subscribers may not trigger an unsubscribe request.
559+
560+Note that the subscribe/unsubscribe hooks are **disabled for long-poll and interval-poll clients**, because they would trigger these hooks each time they receive a message.
561+
562+<!-- commands: nchan_subscribe_request nchan_unsubscribe_request -->
563+
564+### Message Forwarding
565+
566+Messages can be forwarded to an upstream application before being published using the `nchan_publisher_upstream_request` setting:
567+
568+```nginx
569+ location ~ /pub/(\w+)$ {
570+ #publisher endpoint
571+ nchan_channel_id $1;
572+ nchan_pubsub;
573+ nchan_publisher_upstream_request /upstream_pub;
574+ }
575+
576+ location = /upstream_pub {
577+ proxy_pass http://127.0.0.1:9292/pub;
578+ proxy_set_header X-Publisher-Type $nchan_publisher_type;
579+ proxy_set_header X-Prev-Message-Id $nchan_prev_message_id;
580+ proxy_set_header X-Channel-Id $nchan_channel_id;
581+ proxy_set_header X-Original-URI $request_uri;
582+ }
583+```
584+With this configuration, incoming messages are first `POST`ed to `http://127.0.0.1:9292/pub`.
585+The upstream response code determines how publishing will proceed:
586+ - `304 Not Modified` publishes the message as received, without modifification.
587+ - `204 No Content` discards the message
588+ - `200 OK` is used for modifying the message. Instead of the original incoming message, the message contained in this HTTP response is published.
589+
590+There are two main use cases for `nchan_publisher_upstream_request`: forwarding incoming data from Websocket publishers to an application, and mutating incoming messages.
591+
592+<!-- commands: nchan_publisher_upstream_request -->
593+
594 ## Storage
595
596 Nchan can stores messages in memory, on disk, or via Redis. Memory storage is much faster, whereas Redis has additional overhead as is considerably slower for publishing messages, but offers near unlimited scalability for broadcast use cases with far more subscribers than publishers.
597
598-<!-- tag:storage -->
599-
600 ### Memory Storage
601
602-This storage method uses a segment of shared memory to store messages and channel data. Large messages as determined by Nginx's caching layer are stored on-disk. The size of the memory segment is configured with `nchan_max_reserved_memory`. Data stored here is not persistent, and is lost if Nginx is restarted or reloaded.
603+This default storage method uses a segment of shared memory to store messages and channel data. Large messages as determined by Nginx's caching layer are stored on-disk. The size of the memory segment is configured with `nchan_shared_memory_size`. Data stored here is not persistent, and is lost if Nginx is restarted or reloaded.
604
605 <!-- tag:memstore -->
606
607 ### Redis
608
609-Nchan can also store messages and channels on a Redis server, or in a Redis cluster. To use a Redis server, set `nchan_use_redis on;` and set the server url with `nchan_redis_url`. These two settings are inheritable by nested locations, so it is enough to set them within an `http { }` block to enable Redis for all Nchan locations in that block. Different locations can also use different Redis servers.
610+[Redis](http://redis.io) can be used to add **data persistence** and **horizontal scalability**, **failover** and **high availability** to your Nchan setup.
611+
612+<!-- tag:redis -->
613
614-To use a Redis Cluster, the Redis servers acting as cluster nodes need to be configured in an `upstream { }` block:
615+#### Connecting to a Redis Server
616+To connect to a single Redis master server, use an `upstream` with `nchan_redis_server` and `nchan_redis_pass` settings:
617
618 ```nginx
619+http {
620+ upstream my_redis_server {
621+ nchan_redis_server 127.0.0.1;
622+ }
623+ server {
624+ listen 80;
625+
626+ location ~ /redis_sub/(\w+)$ {
627+ nchan_subscriber;
628+ nchan_channel_id $1;
629+ nchan_redis_pass my_redis_server;
630+ }
631+ location ~ /redis_pub/(\w+)$ {
632+ nchan_redis_pass my_redis_server;
633+ nchan_publisher;
634+ nchan_channel_id $1;
635+ }
636+ }
637+}
638+```
639+
640+All servers with the above configuration connecting to the same redis server share channel and message data.
641+
642+Channels that don't use Redis can be configured side-by-side with Redis-backed channels, provided the endpoints never overlap. (This can be ensured, as above, by setting separate `nchan_channel_group`s.). Different locations can also connect to different Redis servers.
643+
644+Nchan can work with a single Redis master. It can also auto-discover and use Redis slaves to balance PUBSUB traffic.
645+
646+<!-- commands: nchan_redis_server nchan_redis_pass -->
647+
648+#### Redis Cluster
649+Nchan also supports using Redis Cluster, which adds scalability via sharding channels among cluster nodes. Redis cluster also provides **automatic failover**, **high availability**, and eliminates the single point of failure of one shared Redis server. It is configred and used like so:
650+
651+```nginx
652+http {
653 upstream redis_cluster {
654 nchan_redis_server redis://127.0.0.1:7000;
655 nchan_redis_server redis://127.0.0.1:7001;
656 nchan_redis_server redis://127.0.0.1:7002;
657+ # you don't need to specify all the nodes, they will be autodiscovered
658+ # however, it's recommended that you do specify at least a few master nodes.
659 }
660+ server {
661+ listen 80;
662+
663+ location ~ /sub/(\w+)$ {
664+ nchan_subscriber;
665+ nchan_channel_id $1;
666+ nchan_redis_pass redis_cluster;
667+ }
668+ location ~ /pub/(\w+)$ {
669+ nchan_publisher;
670+ nchan_channel_id $1;
671+ nchan_redis_pass redis_cluster;
672+ }
673+ }
674+}
675 ```
676
677-It is best to specify all master cluster nodes, but this is not required -- as long as Nchan can connect to at least 1 node, it will discover and connect to the whole cluster.
678+<!-- commands: nchan_redis_server nchan_redis_pass -->
679+
680+##### High Availability
681+Redis Cluster connections are designed to be resilient and try to recover from errors. Interrupted connections will have their commands queued until reconnection, and Nchan will publish any messages it successfully received while disconnected. Nchan is also adaptive to cluster modifications. It will add new nodes and remove them as needed.
682+
683+All Nchan servers sharing a Redis server or cluster should have their times synchronized (via ntpd or your favorite ntp daemon). Failure to do so may result in missed or duplicate messages.
684+
685+#### Tweaks and Optimizations
686+
687+As of version 1.2.0, Nchan uses Redis slaves to load-balance PUBSUB traffic. By default, there is an equal chance that a channel's PUBSUB subscription will go to any master or slave. The [`nchan_redis_subscribe_weights`](#nchan_redis_subscribe_weights) setting is available to fine-tune this load-balancing.
688+
689+Also from 1.2.0 onward, [`nchan_redis_optimize_target`](#nchan_redis_optimize_target) can be used to prefer optimizing Redis slaves for CPU or bandwidth. For heavy publishing loads, the tradeoff is very roughly 35% replication bandwidth per slave to 30% CPU load on slaves.
690+
691+## Introspection
692+
693+There are several ways to see what's happening inside Nchan. These are useful for debugging application integration and for measuring performance.
694
695-To use Redis Cluster in an Nchan location, use the `nchan_redis_pass` setting:
696+### Channel Events
697+
698+Channel events are messages automatically published by Nchan when certain events occur in a channel. These are very useful for debugging the use of channels. However, they carry a significant performance overhead and should be used during development, and not in production.
699+
700+Channel events are published to special 'meta' channels associated with normal channels. Here's how to configure them:
701
702 ```nginx
703- location ~ /pubsub/(\w+)$ {
704+location ~ /pubsub/(.+)$ {
705+ nchan_pubsub;
706+ nchan_channel_id $1;
707+ nchan_channel_events_channel_id $1; #enables channel events for this location
708+}
709+
710+location ~ /channel_events/(.+) {
711+ #channel events subscriber location
712+ nchan_subscriber;
713+ nchan_channel_group meta; #"meta" is a SPECIAL channel group
714+ nchan_channel_id $1;
715+}
716+```
717+
718+Note the `/channel_events/...` location has a *special* `nchan_channel_group`, `meta`. This group is reserved for accessing "channel events channels", or"metachannels".
719+
720+Now, say I subscribe to `/channel_events/foo` I will refer to this as the channel events subscriber.
721+
722+Let's see what this channel events subscriber receives when I publish messages to
723+
724+Subscribing to `/pubsub/foo` produces the channel event
725+```
726+subscriber_enqueue foo
727+```
728+
729+Publishing a message to `/pubsub/foo`:
730+```
731+channel_publish foo
732+```
733+
734+Unsubscribing from `/pubsub/foo`:
735+```
736+subscriber_dequeue foo
737+```
738+
739+Deleting `/pubsub/foo` (with HTTP `DELETE /pubsub/foo`):
740+```
741+channel_delete foo
742+```
743+
744+The event string itself is configirable with [nchan_channel_event_string](#nchan_channel_event_string). By default, it is set to `$nchan_channel_event $nchan_channel_id`.
745+This string can use any Nginx and [Nchan variables](/#variables).
746+
747+
748+### nchan_stub_status Stats
749+
750+Like Nginx's [stub_status](https://nginx.org/en/docs/http/ngx_http_stub_status_module.html),
751+`nchan_stub_status` is used to get performance metrics.
752+
753+```nginx
754+ location /nchan_stub_status {
755+ nchan_stub_status;
756+ }
757+```
758+
759+Sending a GET request to this location produces the response:
760+
761+```text
762+total published messages: 1906
763+stored messages: 1249
764+shared memory used: 1824K
765+channels: 80
766+subscribers: 90
767+redis pending commands: 0
768+redis connected servers: 0
769+total interprocess alerts received: 1059634
770+interprocess alerts in transit: 0
771+interprocess queued alerts: 0
772+total interprocess send delay: 0
773+total interprocess receive delay: 0
774+nchan version: 1.1.5
775+```
776+
777+Here's what each line means, and how to interpret it:
778+ - `total published messages`: Number of messages published to all channels through this Nchan server.
779+ - `stored messages`: Number of messages currently buffered in memory
780+ - `shared memory used`: Total shared memory used for buffering messages, storing channel information, and other purposes. This value should be comfortably below `nchan_shared_memory_size`.
781+ - `channels`: Number of channels present on this Nchan server.
782+ - `subscribers`: Number of subscribers to all channels on this Nchan server.
783+ - `redis pending commands`: Number of commands sent to Redis that are awaiting a reply. May spike during high load, especially if the Redis server is overloaded. Should tend towards 0.
784+ - `redis connected servers`: Number of redis servers to which Nchan is currently connected.
785+ - `total interprocess alerts received`: Number of interprocess communication packets transmitted between Nginx workers processes for Nchan. Can grow at 100-10000 per second at high load.
786+ - `interprocess alerts in transit`: Number of interprocess communication packets in transit between Nginx workers. May be nonzero during high load, but should always tend toward 0 over time.
787+ - `interprocess queued alerts`: Number of interprocess communication packets waiting to be sent. May be nonzero during high load, but should always tend toward 0 over time.
788+ - `total interprocess send delay`: Total amount of time interprocess communication packets spend being queued if delayed. May increase during high load.
789+ - `total interprocess receive delay`: Total amount of time interprocess communication packets spend in transit if delayed. May increase during high load.
790+ - `nchan_version`: current version of Nchan. Available for version 1.1.5 and above.
791+
792+Additionally, when there is at least one `nchan_stub_status` location, the following Nginx variables are available:
793+ - `$nchan_stub_status_total_published_messages`
794+ - `$nchan_stub_status_stored_messages`
795+ - `$nchan_stub_status_shared_memory_used`
796+ - `$nchan_stub_status_channels`
797+ - `$nchan_stub_status_subscribers`
798+ - `$nchan_stub_status_redis_pending_commands`
799+ - `$nchan_stub_status_redis_connected_servers`
800+ - `$nchan_stub_status_total_ipc_alerts_received`
801+ - `$nchan_stub_status_ipc_queued_alerts`
802+ - `$nchan_stub_status_total_ipc_send_delay`
803+ - `$nchan_stub_status_total_ipc_receive_delay`
804+
805+
806+## Securing Channels
807+
808+### Securing Publisher Endpoints
809+Consider the use case of an application where authenticated users each use a private, dedicated channel for live updates. The configuration might look like this:
810+
811+```nginx
812+http {
813+ server {
814+ #available only on localhost
815+ listen 127.0.0.1:8080;
816+ location ~ /pub/(\w+)$ {
817+ nchan_publisher;
818+ nchan_channel_group my_app_group;
819+ nchan_channel_id $1;
820+ }
821+ }
822+
823+ server {
824+ #available to the world
825+ listen 80;
826+
827+ location ~ /sub/(\w+)$ {
828+ nchan_subscriber;
829+ nchan_channel_group my_app_group;
830+ nchan_channel_id $1;
831+ }
832+ }
833+}
834+
835+```
836+
837+Here, the subscriber endpoint is available on a public-facing port 80, and the publisher endpoint is only available on localhost, so can be accessed only by applications residing on that machine. Another way to limit access to the publisher endpoint is by using the allow/deny settings:
838+
839+```nginx
840+
841+ server {
842+ #available to the world
843+ listen 80;
844+ location ~ /pub/(\w+)$ {
845+ allow 127.0.0.1;
846+ deny all;
847+ nchan_publisher;
848+ nchan_channel_group my_app_group;
849+ nchan_channel_id $1;
850+ }
851+```
852+
853+Here, only the local IP 127.0.0.1 is allowed to use the publisher location, even though it is defined in a non-localhost server block.
854+
855+### Keeping a Channel Private
856+
857+A Channel ID that is meant to be private should be treated with the same care as a session ID token. Considering the above use case of one-channel-per-user, how can we ensure that only the authenticated user, and no one else, is able to access his channel?
858+
859+First, if you intend on securing the channel contents, you must use TLS/SSL:
860+
861+```nginx
862+http {
863+ server {
864+ #available only on localhost
865+ listen 127.0.0.1:8080;
866+ #...publisher endpoint config
867+ }
868+ server {
869+ #available to the world
870+ listen 443 ssl;
871+ #SSL config goes here
872+ location ~ /sub/(\w+)$ {
873+ nchan_subscriber;
874+ nchan_channel_group my_app_group;
875+ nchan_channel_id $1;
876+ }
877+ }
878+}
879+```
880+
881+Now that you have a secure connection between the subscriber client and the server, you don't need to worry about the channel ID or messages being passively intercepted. This is a minimum requirement for secure message delivery, but it is not sufficient.
882+
883+You must also take care to do at least one of the following:
884+ - [Generate good, high-entropy Channel IDs](#good-ids).
885+ - [Authorize all subscribers with the `nchan_authorize_request` config directive](#request-authorization).
886+ - [Authorize subscribers and hide channel IDs with the "`X-Accel-Redirect`" mechanism](#x-accel-redirect).
887+
888+#### Good IDs
889+
890+An ID that can be guessed is an ID that can be hijacked. If you are not authenticating subscribers (as described below), a channel ID should be impossible to guess. Use at least 128 bits of entropy to generate a random token, associate it with the authenticated user, and share it only with the user's client. Do not reuse tokens, just as you would not reuse session IDs.
891+
892+#### X-Accel-Redirect
893+
894+This feature uses the [X-Accel feature](https://www.nginx.com/resources/wiki/start/topics/examples/x-accel) of Nginx upstream proxies to perform an internal request to a subscriber endpoint.
895+It allows a subscriber client to be authenticated by your application, and then redirected by nginx internally to a location chosen by your appplication (such as a publisher or subscriber endpoint). This makes it possible to have securely authenticated clients that are unaware of the channel id they are subscribed to.
896+
897+Consider the following configuration:
898+```nginx
899+upstream upstream_app {
900+ server 127.0.0.1:8080;
901+}
902+
903+server {
904+ listen 80;
905+
906+ location = /sub_upstream {
907+ proxy_pass http://upstream_app/subscriber_x_accel_redirect;
908+ proxy_set_header X-Forwarded-For $remote_addr;
909+ }
910+
911+ location ~ /sub/internal/(\w+)$ {
912+ internal; #this location only accessible for internal nginx redirects
913+ nchan_subscriber;
914 nchan_channel_id $1;
915- nchan_pubsub;
916- nchan_redis_pass redis_cluster;
917+ nchan_channel_group test;
918 }
919+}
920+```
921
922+As commented, `/sub/internal/` is inaccessible from the outside:
923+```console
924+> curl -v http://127.0.0.1/sub/internal/foo
925+
926+ < HTTP/1.1 404 Not Found
927+ < Server: nginx/1.9.5
928+ <
929+ <html>
930+ <head><title>404 Not Found</title></head>
931+ <body bgcolor="white">
932+ <center><h1>404 Not Found</h1></center>
933+ <hr><center>nginx/1.9.5</center>
934+ </body>
935+ </html>
936 ```
937
938-Note that `nchan_redis_pass` implies `nchan_use_redis on;`, and that this setting is *not* inherited by nested locations.
939+But if a request is made to `/sub_upstream`, it gets forwarded to your application (`my_app`) on port 8080 with the url `/subscriber_x_accel_redirect`.
940+Note that you can set any forwarded headers here like any [`proxy_pass`](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass) Nginx location,
941+but unlike the case with `nchan_authorize_request`, Nchan-specific variables are not available.
942
943-When connecting several Nchan servers to the same Redis server (or cluster), the servers **must have their times synced up**. Failure to do so may result in missing and duplicated messages.
944+Now, your application must be set up to handle the request to `/subscriber_x_accel_redirect`. You should make sure the client is properly authenticated (maybe using a session cookie), and generate an associated channel id. If authentication fails, respond with a normal `403 Forbidden` response. You can also pass extra information about the failure in the response body and headers.
945
946-See the [details page](https://nchan.slact.net/details#using-redis) for more information on using Redis.
947+If your application successfully authenticates the subscriber request, you now need to instruct Nginx to issue an internal redirect to `/sub/internal/my_channel_id`.
948+This is accomplished by responding with an empty `200 OK` response that includes two headers:
949+- `X-Accel-Redirect: /sub/internal/my_channel_id`
950+- `X-Accel-Buffering: no`
951
952-<!-- tag:redis -->
953+In the presence of these headers, Nginx will not forward your app's response to the client, and instead will *internally* redirect to `/sub/internal/my_channel_id`.
954+This will behave as if the client had requested the subscriber endpoint location directly.
955+
956+Thus using X-Accel-Redirect it is possible to both authenticate all subscribers *and* keep channel IDs completely hidden from subscribers.
957+
958+This method is especially useful for EventSource and Websocket subscribers. Long-Polling subscribers will need to be re-authenticated for every new message, which may flood your application with too many authentication requests.
959+
960+### Revoking Channel Authorization
961+
962+In some cases, you may want to revoke a particular subscriber's authorization for a given channel (e.g., if the user's permissions are changed). If the channel is unique to the subscriber, this is simply accomplished by deleting the channel. The same can be achieved for shared channels by subscribing each subscriber to both the shared channel and a subscriber-specific channel via a multiplexed connection. Deleting the subscriber-specific channel will terminate the subscriber''s connection, thereby also terminating their subscription to the shared channel. Consider the following configuration:
963+
964+```nginx
965+location ~ /sub/(\w+) {
966+ nchan_subscriber;
967+ nchan_channel_id shared_$1 user_$arg_userid;
968+ nchan_authorize_request /authorize;
969+}
970
971+location /pub/user {
972+ nchan_publisher;
973+ nchan_channel_id user_$arg_userid;
974+}
975+```
976+
977+A request to `/sub/foo?userid=1234` will subscribe to channels "shared_foo" and "user_1234" via a multiplexed connection. If you later send a `DELETE` request to `/pub/user?userid=1234`, this subscriber will be disconnected and therefore unsubscribed from both "user_1234" and "shared_foo".
978+
979 ## Variables
980
981 Nchan makes several variables usabled in the config file:
982@@ -401,13 +1004,16 @@ Nchan makes several variables usabled in the config file:
983 For subscriber locations, this variable is set to the subscriber type (websocket, longpoll, etc.).
984
985 - `$nchan_publisher_type`
986- For subscriber locations, this variable is set to the subscriber type (http or websocket).
987+ For publisher locations, this variable is set to the subscriber type (http or websocket).
988
989 - `$nchan_prev_message_id`, `$nchan_message_id`
990 The current and previous (if applicable) message id for publisher request or subscriber response.
991
992 - `$nchan_channel_event`
993 For channel events, this is the event name. Useful when configuring `nchan_channel_event_string`.
994+
995+- `$nchan_version`
996+ Current Nchan version. Available since 1.1.5.
997
998 Additionally, `nchan_stub_status` data is also exposed as variables. These are available only when `nchan_stub_status` is enabled on at least one location:
999
1000@@ -440,18 +1046,72 @@ Additionally, `nchan_stub_status` data is also exposed as variables. These are a
1001 > Split the channel id into several ids for multiplexing using the delimiter string provided.
1002 [more details](#channel-multiplexing)
1003
1004+- **nchan_deflate_message_for_websocket** `[ on | off ]`
1005+ arguments: 1
1006+ default: `off`
1007+ context: server, location
1008+ > Store a compressed (deflated) copy of the message along with the original to be sent to websocket clients supporting the permessage-deflate protocol extension
1009+
1010 - **nchan_eventsource_event**
1011 arguments: 1
1012 default: `(none)`
1013 context: server, location, if
1014 > Set the EventSource `event:` line to this value. When used in a publisher location, overrides the published message's `X-EventSource-Event` header and associates the message with the given value. When used in a subscriber location, overrides all messages' associated `event:` string with the given value.
1015
1016+- **nchan_eventsource_ping_comment**
1017+ arguments: 1
1018+ default: `(empty)`
1019+ context: server, location, if
1020+ > Set the EventSource comment `: ...` line for periodic pings from server to client. Newlines are not allowed. If empty, no comment is sent with the ping.
1021+
1022+- **nchan_eventsource_ping_data**
1023+ arguments: 1
1024+ default: `(empty)`
1025+ context: server, location, if
1026+ > Set the EventSource `data:` line for periodic pings from server to client. Newlines are not allowed. If empty, no data is sent with the ping.
1027+
1028+- **nchan_eventsource_ping_event**
1029+ arguments: 1
1030+ default: `ping`
1031+ context: server, location, if
1032+ > Set the EventSource `event:` line for periodic pings from server to client. Newlines are not allowed. If empty, no event type is sent with the ping.
1033+
1034+- **nchan_eventsource_ping_interval** `<number> (seconds)`
1035+ arguments: 1
1036+ default: `0 (none)`
1037+ context: server, location, if
1038+ > Interval for sending ping messages to EventSource subscribers. Disabled by default.
1039+
1040 - **nchan_longpoll_multipart_response** `[ off | on | raw ]`
1041 arguments: 1
1042 default: `off`
1043 context: server, location, if
1044 > when set to 'on', enable sending multiple messages in a single longpoll response, separated using the multipart/mixed content-type scheme. If there is only one available message in response to a long-poll request, it is sent unmodified. This is useful for high-latency long-polling connections as a way to minimize round-trips to the server. When set to 'raw', sends multiple messages using the http-raw-stream message separator.
1045
1046+- **nchan_permessage_deflate_compression_level** `[ 0-9 ]`
1047+ arguments: 1
1048+ default: `6`
1049+ context: http
1050+ > Compression level for the `deflate` algorithm used in websocket's permessage-deflate extension. 0: no compression, 1: fastest, worst, 9: slowest, best
1051+
1052+- **nchan_permessage_deflate_compression_memlevel** `[ 1-9 ]`
1053+ arguments: 1
1054+ default: `8`
1055+ context: http
1056+ > Memory level for the `deflate` algorithm used in websocket's permessage-deflate extension. How much memory should be allocated for the internal compression state. 1 - minimum memory, slow and reduces compression ratio; 9 - maximum memory for optimal speed
1057+
1058+- **nchan_permessage_deflate_compression_strategy** `[ default | filtered | huffman-only | rle | fixed ]`
1059+ arguments: 1
1060+ default: `default`
1061+ context: http
1062+ > Compression strategy for the `deflate` algorithm used in websocket's permessage-deflate extension. Use 'default' for normal data, For details see [zlib's section on copression strategies](http://zlib.net/manual.html#Advanced)
1063+
1064+- **nchan_permessage_deflate_compression_window** `[ 9-15 ]`
1065+ arguments: 1
1066+ default: `10`
1067+ context: http
1068+ > Compression window for the `deflate` algorithm used in websocket's permessage-deflate extension. The base two logarithm of the window size (the size of the history buffer). The bigger the window, the better the compression, but the more memory used by the compressor.
1069+
1070 - **nchan_publisher** `[ http | websocket ]`
1071 arguments: 0 - 2
1072 default: `http websocket`
1073@@ -471,7 +1131,7 @@ Additionally, `nchan_stub_status` data is also exposed as variables. These are a
1074 context: server, location, if
1075 > Send POST request to internal location (which may proxy to an upstream server) with published message in the request body. Useful for bridging websocket publishers with HTTP applications, or for transforming message via upstream application before publishing to a channel.
1076 > The upstream response code determines how publishing will proceed. A `200 OK` will publish the message from the upstream response's body. A `304 Not Modified` will publish the message as it was received from the publisher. A `204 No Content` will result in the message not being published.
1077- [more details](https://nchan.slact.net/details#message-publishing-callbacks)
1078+ [more details](#message-forwarding)
1079
1080 - **nchan_pubsub** `[ http | websocket | eventsource | longpoll | intervalpoll | chunked | multipart-mixed | http-raw-stream ]`
1081 arguments: 0 - 6
1082@@ -480,6 +1140,12 @@ Additionally, `nchan_stub_status` data is also exposed as variables. These are a
1083 > Defines a server or location as a pubsub endpoint. For long-polling, GETs subscribe. and POSTs publish. For Websockets, publishing data on a connection does not yield a channel metadata response. Without additional configuration, this turns a location into an echo server.
1084 [more details](#pubsub-endpoint)
1085
1086+- **nchan_subscribe_request** `<url>`
1087+ arguments: 1
1088+ context: server, location, if
1089+ > Send GET request to internal location (which may proxy to an upstream server) after subscribing. Disabled for longpoll and interval-polling subscribers.
1090+ [more details](#subscriber-presence)
1091+
1092 - **nchan_subscriber** `[ websocket | eventsource | longpoll | intervalpoll | chunked | multipart-mixed | http-raw-stream ]`
1093 arguments: 0 - 5
1094 default: `websocket eventsource longpoll chunked multipart-mixed`
1095@@ -535,37 +1201,111 @@ Additionally, `nchan_stub_status` data is also exposed as variables. These are a
1096 legacy name: push_subscriber_timeout
1097 > Maximum time a subscriber may wait for a message before being disconnected. If you don't want a subscriber's connection to timeout, set this to 0. When possible, the subscriber will get a response with a `408 Request Timeout` status; otherwise the subscriber will simply be disconnected.
1098
1099+- **nchan_unsubscribe_request** `<url>`
1100+ arguments: 1
1101+ context: server, location, if
1102+ > Send GET request to internal location (which may proxy to an upstream server) after unsubscribing. Disabled for longpoll and interval-polling subscribers.
1103+ [more details](#subscriber-presence)
1104+
1105+- **nchan_websocket_client_heartbeat** `<heartbeat_in> <heartbeat_out>`
1106+ arguments: 2
1107+ default: `none (disabled)`
1108+ context: server, location, if
1109+ > Most browser Websocket clients do not allow manually sending PINGs to the server. To overcome this limitation, this setting can be used to set up a PING/PONG message/response connection heartbeat. When the client sends the server message *heartbeat_in* (PING), the server automatically responds with *heartbeat_out* (PONG).
1110+
1111 - **nchan_websocket_ping_interval** `<number> (seconds)`
1112 arguments: 1
1113 default: `0 (none)`
1114 context: server, location, if
1115 > Interval for sending websocket ping frames. Disabled by default.
1116
1117+- **nchan_access_control_allow_credentials**
1118+ arguments: 1
1119+ default: `on`
1120+ context: http, server, location, if
1121+ > When enabled, sets the [Cross-Origin Resource Sharing (CORS)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS) `Access-Control-Allow-Credentials` header to `true`.
1122+
1123+- **nchan_access_control_allow_origin** `<string>`
1124+ arguments: 1
1125+ default: `$http_origin`
1126+ context: http, server, location, if
1127+ > Set the [Cross-Origin Resource Sharing (CORS)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS) `Access-Control-Allow-Origin` header to this value. If the incoming request's `Origin` header does not match this value, respond with a `403 Forbidden`. Multiple origins can be provided in a single argument separated with a space.
1128+
1129 - **nchan_authorize_request** `<url>`
1130 arguments: 1
1131 context: server, location, if
1132 > Send GET request to internal location (which may proxy to an upstream server) for authorization of a publisher or subscriber request. A 200 response authorizes the request, a 403 response forbids it.
1133- [more details](https://nchan.slact.net/details#request-authorization)
1134+ [more details](#request-authorization)
1135
1136-- **nchan_subscribe_request** `<url>`
1137+- **nchan_channel_group** `<string>`
1138 arguments: 1
1139+ default: `(none)`
1140 context: server, location, if
1141- > Send GET request to internal location (which may proxy to an upstream server) after subscribing. Disabled for longpoll and interval-polling subscribers.
1142- [more details](https://nchan.slact.net/details#subsribe-and-unsubscribe-callbacks)
1143+ legacy name: push_channel_group
1144+ > The accounting and security group a channel belongs to. Works like a prefix string to the channel id. Can be set with nginx variables.
1145
1146-- **nchan_unsubscribe_request** `<url>`
1147+- **nchan_channel_group_accounting**
1148 arguments: 1
1149- context: server, location, if
1150- > Send GET request to internal location (which may proxy to an upstream server) after unsubscribing. Disabled for longpoll and interval-polling subscribers.
1151- [more details](https://nchan.slact.net/details#subsribe-and-unsubscribe-callbacks)
1152+ default: `off`
1153+ context: server, location
1154+ > Enable tracking channel, subscriber, and message information on a per-channel-group basis. Can be used to place upper limits on channel groups.
1155+
1156+- **nchan_group_location** `[ get | set | delete | off ]`
1157+ arguments: 0 - 3
1158+ default: `get set delete`
1159+ context: location
1160+ > Group information and configuration location. GET request for group info, POST to set limits, DELETE to delete all channels in group.
1161
1162-- **nchan_max_reserved_memory** `<size>`
1163+- **nchan_group_max_channels** `<number>`
1164 arguments: 1
1165- default: `32M`
1166- context: http
1167- legacy name: push_max_reserved_memory
1168- > The size of the shared memory chunk this module will use for message queuing and buffering.
1169- [more details](#memory-storage)
1170+ default: `0 (unlimited)`
1171+ context: location
1172+ > Maximum number of channels allowed in the group.
1173+
1174+- **nchan_group_max_messages** `<number>`
1175+ arguments: 1
1176+ default: `0 (unlimited)`
1177+ context: location
1178+ > Maximum number of messages allowed for all the channels in the group.
1179+
1180+- **nchan_group_max_messages_disk** `<number>`
1181+ arguments: 1
1182+ default: `0 (unlimited)`
1183+ context: location
1184+ > Maximum amount of disk space allowed for the messages of all the channels in the group.
1185+
1186+- **nchan_group_max_messages_memory** `<number>`
1187+ arguments: 1
1188+ default: `0 (unlimited)`
1189+ context: location
1190+ > Maximum amount of shared memory allowed for the messages of all the channels in the group.
1191+
1192+- **nchan_group_max_subscribers** `<number>`
1193+ arguments: 1
1194+ default: `0 (unlimited)`
1195+ context: location
1196+ > Maximum number of subscribers allowed for the messages of all the channels in the group.
1197+
1198+- **nchan_max_channel_id_length** `<number>`
1199+ arguments: 1
1200+ default: `1024`
1201+ context: http, server, location
1202+ legacy name: push_max_channel_id_length
1203+ > Maximum permissible channel id length (number of characters). This settings applies to ids before they may be split by the `nchan_channel_id_split_delimiter` Requests with a channel id that is too long will receive a `403 Forbidden` response.
1204+
1205+- **nchan_max_channel_subscribers** `<number>`
1206+ arguments: 1
1207+ default: `0 (unlimited)`
1208+ context: http, server, location
1209+ legacy name: push_max_channel_subscribers
1210+ > Maximum concurrent subscribers to the channel on this Nchan server. Does not include subscribers on other Nchan instances when using a shared Redis server.
1211+
1212+- **nchan_subscribe_existing_channels_only** `[ on | off ]`
1213+ arguments: 1
1214+ default: `off`
1215+ context: http, server, location
1216+ legacy name: push_authorized_channels_only
1217+ > Whether or not a subscriber may create a channel by sending a request to a subscriber location. If set to on, a publisher must send a POST or PUT request before a subscriber can request messages on the channel. Otherwise, all subscriber requests to nonexistent channels will get a 403 Forbidden response.
1218
1219 - **nchan_message_buffer_length** `[ <number> | <variable> ]`
1220 arguments: 1
1221@@ -574,6 +1314,12 @@ Additionally, `nchan_stub_status` data is also exposed as variables. These are a
1222 legacy names: push_max_message_buffer_length, push_message_buffer_length
1223 > Publisher configuration setting the maximum number of messages to store per channel. A channel's message buffer will retain a maximum of this many most recent messages. An Nginx variable can also be used to set the buffer length dynamically.
1224
1225+- **nchan_message_temp_path** `<path>`
1226+ arguments: 1
1227+ default: `<client_body_temp_path>`
1228+ context: http
1229+ > Large messages are stored in temporary files in the `client_body_temp_path` or the `nchan_message_temp_path` if the former is unavailable. Default is the built-in default `client_body_temp_path`
1230+
1231 - **nchan_message_timeout** `[ <time> | <variable> ]`
1232 arguments: 1
1233 default: `1h`
1234@@ -581,36 +1327,83 @@ Additionally, `nchan_stub_status` data is also exposed as variables. These are a
1235 legacy name: push_message_timeout
1236 > Publisher configuration setting the length of time a message may be queued before it is considered expired. If you do not want messages to expire, set this to 0. Note that messages always expire from oldest to newest, so an older message may prevent a newer one with a shorter timeout from expiring. An Nginx variable can also be used to set the timeout dynamically.
1237
1238+- **nchan_redis_connect_timeout**
1239+ arguments: 1
1240+ default: `600ms`
1241+ context: upstream
1242+ > Redis server connection timeout.
1243+
1244 - **nchan_redis_idle_channel_cache_timeout** `<time>`
1245 arguments: 1
1246 default: `30s`
1247 context: http, server, location
1248 > A Redis-stored channel and its messages are removed from memory (local cache) after this timeout, provided there are no local subscribers.
1249
1250-- **nchan_redis_pass**
1251+- **nchan_redis_namespace** `<string>`
1252+ arguments: 1
1253+ context: http, server, upstream, location
1254+ > Prefix all Redis keys with this string. All Nchan-related keys in redis will be of the form "nchan_redis_namespace:*" . Default is empty.
1255+
1256+- **nchan_redis_nostore_fastpublish** `[ on | off ]`
1257+ arguments: 1
1258+ default: `off`
1259+ context: http, server, upstream
1260+ > Increases publishing capacity by 2-3x for Redis nostore mode at the expense of inaccurate subscriber counts in the publisher response.
1261+
1262+- **nchan_redis_optimize_target** `[ cpu | bandwidth ]`
1263+ arguments: 1
1264+ default: `cpu`
1265+ context: upstream
1266+ > This tweaks whether [effect replication](https://redis.io/commands/eval#replicating-commands-instead-of-scripts) is enabled. Optimizing for CPU usage enables effect replication, costing additional bandwidth (between 1.2 and 2 times more) between all master->slave links. Optimizing for bandwidth increases CPU load on slaves, but keeps outgoing bandwidth used for replication the same as the incoming bandwidth on Master.
1267+
1268+- **nchan_redis_pass** `<upstream-name>`
1269 arguments: 1
1270 context: http, server, location
1271 > Use an upstream config block for Redis servers.
1272- [more details](https://nchan.slact.net/details#using-redis)
1273+ [more details](#connecting-to-a-redis-server)
1274
1275 - **nchan_redis_ping_interval**
1276 arguments: 1
1277 default: `4m`
1278- context: http, server, location
1279+ context: http, server, upstream, location
1280 > Send a keepalive command to redis to keep the Nchan redis clients from disconnecting. Set to 0 to disable.
1281
1282-- **nchan_redis_server**
1283+- **nchan_redis_server** `<redis-url>`
1284+ arguments: 1
1285+ context: upstream
1286+ > Used in upstream { } blocks to set redis servers. Redis url is in the form 'redis://:password@hostname:6379/0'. Shorthands 'host:port' or 'host' are permitted.
1287+ [more details](#connecting-to-a-redis-server)
1288+
1289+- **nchan_redis_storage_mode** `[ distributed | backup | nostore ]`
1290 arguments: 1
1291+ default: `distributed`
1292+ context: http, server, upstream
1293+ > The mode of operation of the Redis server. In `distributed` mode, messages are published directly to Redis, and retrieved in real-time. Any number of Nchan servers in distributed mode can share the Redis server (or cluster). Useful for horizontal scalability, but suffers the latency penalty of all message publishing going through Redis first.
1294+ >
1295+ > In `backup` mode, messages are published locally first, then later forwarded to Redis, and are retrieved only upon chanel initialization. Only one Nchan server should use a Redis server (or cluster) in this mode. Useful for data persistence without sacrificing response times to the latency of a round-trip to Redis.
1296+ >
1297+ > In `nostore` mode, messages are published as in `distributed` mode, but are not stored. Thus Redis is used to broadcast messages to many Nchan instances with no delivery guarantees during connection failure, and only local in-memory storage. This means that there are also no message delivery guarantees for subscribers switching from one Nchan instance to another connected to the same Redis server or cluster. Nostore mode increases Redis publishing capacity by an order of magnitude.
1298+
1299+- **nchan_redis_subscribe_weights** `master=<integer> slave=<integer>`
1300+ arguments: 1 - 2
1301+ default: `master=1 slave=1`
1302 context: upstream
1303- > Used in upstream { } blocks to set redis servers.
1304- [more details](https://nchan.slact.net/details#using-redis)
1305+ > Determines how subscriptions to Redis PUBSUB channels are distributed between master and slave nodes. The higher the number, the more likely that each node of that type will be chosen for each new channel. The weights for slave nodes are cumulative, so an equal 1:1 master:slave weight ratio with two slaves would have a 1/3 chance of picking a master, and 2/3 chance of picking one of the slaves. The weight must be a non-negative integer.
1306
1307-- **nchan_redis_url**
1308+- **nchan_redis_url** `<redis-url>`
1309 arguments: 1
1310 default: `127.0.0.1:6379`
1311 context: http, server, location
1312- > The path to a redis server, of the form 'redis://:password@hostname:6379/0'. Shorthand of the form 'host:port' or just 'host' is also accepted.
1313- [more details](https://nchan.slact.net/details#using-redis)
1314+ > Use of this command is discouraged in favor of upstreams blocks with (`nchan_redis_server`)[#nchan_redis_server]. The path to a redis server, of the form 'redis://:password@hostname:6379/0'. Shorthand of the form 'host:port' or just 'host' is also accepted.
1315+ [more details](#connecting-to-a-redis-server)
1316+
1317+- **nchan_shared_memory_size** `<size>`
1318+ arguments: 1
1319+ default: `128M`
1320+ context: http
1321+ legacy names: push_max_reserved_memory, nchan_max_reserved_memory
1322+ > Shared memory slab pre-allocated for Nchan. Used for channel statistics, message storage, and interprocess communication.
1323+ [more details](#memory-storage)
1324
1325 - **nchan_store_messages** `[ on | off ]`
1326 arguments: 1
1327@@ -623,28 +1416,8 @@ Additionally, `nchan_stub_status` data is also exposed as variables. These are a
1328 arguments: 1
1329 default: `off`
1330 context: http, server, location
1331- > Use redis for message storage at this location.
1332- [more details](https://nchan.slact.net/details#using-redis)
1333-
1334-- **nchan_access_control_allow_origin** `<string>`
1335- arguments: 1
1336- default: `*`
1337- context: http, server, location
1338- > Set the [Cross-Origin Resource Sharing (CORS)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS) `Access-Control-Allow-Origin` header to this value. If the publisher or subscriber request's `Origin` header does not match this value, respond with a `403 Forbidden`.
1339-
1340-- **nchan_channel_group** `<string>`
1341- arguments: 1
1342- default: `(none)`
1343- context: server, location, if
1344- legacy name: push_channel_group
1345- > Because settings are bound to locations and not individual channels, it is useful to be able to have channels that can be reached only from some locations and never others. That's where this setting comes in. Think of it as a prefix string for the channel id.
1346-
1347-- **nchan_subscribe_existing_channels_only** `[ on | off ]`
1348- arguments: 1
1349- default: `off`
1350- context: http, server, location
1351- legacy name: push_authorized_channels_only
1352- > Whether or not a subscriber may create a channel by sending a request to a subscriber location. If set to on, a publisher must send a POST or PUT request before a subscriber can request messages on the channel. Otherwise, all subscriber requests to nonexistent channels will get a 403 Forbidden response.
1353+ > Use of this command is discouraged in favor of (`nchan_redis_pass`)[#nchan_redis_pass]. Use Redis for message storage at this location.
1354+ [more details](#connecting-to-a-redis-server)
1355
1356 - **nchan_channel_event_string** `<string>`
1357 arguments: 1
1358@@ -656,27 +1429,13 @@ Additionally, `nchan_stub_status` data is also exposed as variables. These are a
1359 arguments: 1
1360 context: server, location, if
1361 > Channel id where `nchan_channel_id`'s events should be sent. Events like subscriber enqueue/dequeue, publishing messages, etc. Useful for application debugging. The channel event message is configurable via nchan_channel_event_string. The channel group for events is hardcoded to 'meta'.
1362- [more details](https://nchan.slact.net/details#channel-events)
1363+ [more details](#channel-events)
1364
1365 - **nchan_stub_status**
1366 arguments: 0
1367 context: location
1368 > Similar to Nginx's stub_status directive, requests to an `nchan_stub_status` location get a response with some vital Nchan statistics. This data does not account for information from other Nchan instances, and monitors only local connections, published messages, etc.
1369- [more details](https://nchan.slact.net/details#nchan_stub_status)
1370-
1371-- **nchan_max_channel_id_length** `<number>`
1372- arguments: 1
1373- default: `512`
1374- context: http, server, location
1375- legacy name: push_max_channel_id_length
1376- > Maximum permissible channel id length (number of characters). Longer ids will be truncated.
1377-
1378-- **nchan_max_channel_subscribers** `<number>`
1379- arguments: 1
1380- default: `0 (unlimited)`
1381- context: http, server, location
1382- legacy name: push_max_channel_subscribers
1383- > Maximum concurrent subscribers to the channel on this Nchan server. Does not include subscribers on other Nchan instances when using a shared Redis server.
1384+ [more details](#nchan_stub_status)
1385
1386 - **nchan_channel_timeout**
1387 arguments: 1
1388@@ -690,5 +1449,9 @@ Additionally, `nchan_stub_status` data is also exposed as variables. These are a
1389 context: http, server, location
1390 > Development directive to completely replace default storage engine. Don't use unless you are an Nchan developer.
1391
1392+- **nchan_redis_wait_after_connecting**
1393+ arguments: 1
1394+ context: http, server, location
1395+
1396 ## Contribute
1397-Please support this project with a donation to keep me warm through the winter. I accept bitcoin at 15dLBzRS4HLRwCCVjx4emYkxXcyAPmGxM3 . Other donation methods can be found at https://nchan.slact.net
1398+Please support this project with a donation to keep me warm through the winter. I accept bitcoin at 15dLBzRS4HLRwCCVjx4emYkxXcyAPmGxM3 . Other donation methods can be found at https://nchan.io
1399diff --git a/debian/modules/nchan/changelog.txt b/debian/modules/nchan/changelog.txt
1400index 8eba621..88f481e 100644
1401--- a/debian/modules/nchan/changelog.txt
1402+++ b/debian/modules/nchan/changelog.txt
1403@@ -1,3 +1,208 @@
1404+1.2.7 (Mar. 17 2020)
1405+ fix: unidirectional subscribers have their connection terminated if they send any data to the server
1406+ after the initial request handshake. This applies to all subscribers except Websocket
1407+ feature: periodic pings for EventSource subscribers
1408+ fix: Redis pending commands count may be incorrect in nchan's stub status page
1409+ fix: channel deletion fails to propagate to Redis slaves
1410+ fix: possible stack overflow when using websocket subscribers
1411+1.2.6 (Jun. 18 2019)
1412+ fix: when using Redis, a channel can stop receiving new messages if
1413+ they are published faster than they can be sent to subscribers and the
1414+ message buffer is sufficiently small
1415+ fix: websocket PONG response did not contain PING frame data
1416+ fix: multiplexed channels may stop receiving messages
1417+ fix (security): specially crafted websocket publisher requests when using Redis
1418+ may result in use-after-free memory access
1419+ fix: Nginx config reload may result in crash when using Redis cluster
1420+1.2.5 (Mar. 20 2019)
1421+ fix: using multiplexed channels with Redis in backup mode may result in worker crash
1422+ fix: nchan_publisher_channel_id could not be set exclusively in a publisher location
1423+ fix: Google pagespeed module compatibility
1424+ fix: nchan prevents nginx from starting if no http {} block is configured
1425+1.2.4 (Feb. 25 2019)
1426+ fix: Redis cluster info with zero-length hostname may result in worker crash
1427+ fix: build problems with included hiredis lib in FreeBSD
1428+ feature: nchan_redis_namespace and nchan_redis_ping_interval now work in upstream blocks
1429+ fix: websocket publisher did not publishing channel events
1430+ fix: Redis namespace was limited to 8 bytes
1431+1.2.3 (Oct. 15 2018)
1432+ fix: possible invalid memory access when the initial connection to a Redis cluster node times out
1433+1.2.2 (Oct. 9 2018)
1434+ fix (security): using an unresponsive, overloaded Redis server may result in invalid memory access
1435+ fix: incorrect logging of discovered Redis cluster nodes
1436+ fix: better handling of connection loss when Redis server is unresponsive
1437+ fix: presence of Redis cluster nodes with no known address ("noaddr") nodes could result in worker crash
1438+ fix (security): subscriber may erroneously receive a 400 Bad Request or crash a worker
1439+ based on data from a previous subscriber
1440+ feature: built-in backend benchmark
1441+ feature: add optimized fastpublish option to Redis nostore mode for maximum
1442+ message publishing thoroughput via Redis
1443+ feature: add no-store Redis mode that uses Redis for broadcasting messages, not storage
1444+ fix: connecting to load-balancing Redis proxy resulted in crash
1445+ fix: using longpoll-multipart in "raw" mode cound result in worker crash
1446+ fix: channel events used with Redis resulted in segfault
1447+1.2.1 (Aug. 2 2018)
1448+ fix: channel last_requested was set to 0 instead of -1 on channel creation
1449+ fix: authentication failure body not forwarded for Nginx > 1.13.10
1450+ fix: possible invalid memory access for websocket unsubscribe requests
1451+ fix: building Nchan could interfere with building other modules
1452+1.2.0 (Jul. 23 2018)
1453+ feature: configurable support for CORS Access-Control-Allow-Credentials header
1454+ fix: better compliance with RFC7692 Websocket permessage-deflate parameter negotiation
1455+ fix (security): possible busy-loop denial-of-service for specially crafted
1456+ handshakes from Websocket subscribers using permessage-deflate
1457+ (Thanks, Benjamin Michéle)
1458+ fix: nchan_permessage_deflate_compression_memlevel was not applied when set
1459+ refactor: all publisher and subscriber upstream requests are now more memory-efficient
1460+ fix: Using websocket publisher upstream requests may result in invalid memory access
1461+ fix: publishing Redis-backed messages with 1-second expiration may fail after
1462+ cluster restart
1463+ change: nchan_redis_wait_after_connecting directive is now obsolete, and is ignored
1464+ feature: nchan_redis_optimize_target for "cpu" or "bandwidth". Trades off CPU
1465+ load on Redis slaves for syncronization bandwidth.
1466+ feature: configurable Redis master/slave channel subscribe weights with
1467+ nchan_redis_subscribe_weights
1468+ fix: Compilation issues on OS X and systems lacking non-POSIX memrchr()
1469+ fix: nchan_pubsub CORS Allowed headers did not include headers used by subscribers
1470+ fix: Redis-backed channel buffer length could exceed nchan_message_buffer_length
1471+ fix: Publisher upstream compatibility for Nginx > 1.13.10
1472+ feature: nchan_redis_connect_timeout to configure maximum connection time
1473+ to Redis servers
1474+ feature: Offload Redis SUBSCRIBE traffic to slaves
1475+ (one SUBSCRIBE per channel per worker)
1476+ fix: Redis cluster and master/slave failover and reconnection issues
1477+ refactor: Redis connection handling rewritten from scratch
1478+ fix: subscribers may not receive new messages after reconnecting to Redis
1479+ fix: publishing to an unavailable Redis-backed channel may result in a
1480+ following 400 Bad Request
1481+ change: Old Redis-backed channel messages are now delivered after the message
1482+ buffer is fully loaded into memory. Previously they were delivered
1483+ incrementally while the buffer loaded.
1484+ fix: multiplexed Redis-backed channels may not deliver messages if one or
1485+ more channels' messages all expire
1486+ fix: possible crash when catching up to reconnected Redis channel with
1487+ subscribers waiting for consecutive messages
1488+ fix: possible crash from rapidly creating and deleting channels
1489+1.1.15 (Apr. 27 2018)
1490+ fix: A disconnect from a Redis cluster node can result in a segfault
1491+ fix: Using Redis-backed multiplexed channels can result in a segfault
1492+1.1.14 (Jan. 10 2018)
1493+ feature: added nchan_redis_wait_after_connecting setting
1494+ fix: compatibility with Redis >= 4.0 cluster
1495+1.1.13 (Dec. 4 2017)
1496+ fix: added Redis backwards compatibility with Nchan 1.1.7 and below
1497+ for online upgrades with mixed-version Nchan cluster
1498+1.1.12 (Dec. 1 2017)
1499+ fix: possible "Unexpected spool == nuspool" worker crash
1500+ fix: subscriber messages delivered during active nchan_subscribe subrequest
1501+ may be garbled
1502+1.1.11 (Nov. 29 2017)
1503+ fix: Redis backup-mode not working (since 1.1.9)
1504+ fix: incorrect handling of Redis permessage-deflated messages results in
1505+ missing first char of eventsource event type
1506+ fix: worker crash when unable to create temp file for large websocket
1507+ permessage-deflate message
1508+ fix: CPU-bound overloaded Nginx may result in worker crashes
1509+ (may occur with large Openresty Lua load)
1510+ change: default nchan_shared_memory_size is now 128M
1511+ fix: some channel info from publisher GET requests may be incorrect with Redis
1512+ fix: file descriptor leak when reconnecting to Redis
1513+1.1.10 (Nov. 13 2017)
1514+ feature: nchan_authorize_request failure response forwarded back to subscriber
1515+ Sponsored by Symless (https://symless.com/)
1516+ fix: allow nchan_access_control_allow_origin in if blocks
1517+ fix: longpoll-multipart may read uninitialized memory when receiving
1518+ zero-length message
1519+ fix (security): invalid memory access for aborted websocket subscriber
1520+ after channel existence check via Redis
1521+ fix: websocket handhshake failure handled incorrectly when messages
1522+ are available
1523+ fix (security): websocket subscriber disconnecting before handshake may
1524+ result in invalid memory access
1525+ fix (security): possible invalid memory access for disappearing longpoll sub
1526+ feature: add "shared memory limit" to nchan_stub_status output
1527+1.1.9 (Oct. 30 2017)
1528+ fix: more proper websocket extension negotiation with more
1529+ informative failure messages
1530+ fix: websocket handshake failure response included superfluous CLOSE frame
1531+ feature: websocket deflate-frame and x-webkit-deflate-frame support
1532+1.1.8 (Oct. 26 2017)
1533+ feature: websocket permessage-deflate support
1534+ Sponsored by HYFN (https://hyfn.com/)
1535+ fix (security): websocket publisher may crash worker when publishing with
1536+ channel group accounting on to a new group
1537+ fix: messages published to Nchan via websocket binary frames
1538+ should have content-type set to "application/octet-stream"
1539+ fix: accept websocket publisher binary frames (thanks @rponczkowski)
1540+ fix: multiplexing over exactly 255 channels results in worker crash
1541+ fix (security): Specially crafted invalid subscriber msgid may crash worker
1542+ fix: nchan_subscriber_first_message <= 0 (newest) with existing Redis data
1543+ incorrectly treated as "oldest" for initial subscribers
1544+ fix: 0-length channel name may crash worker
1545+ fix: subscribe/unsubscribe callback requests do not work when used with
1546+ authorization callback request
1547+ fix (security): Messages published with Redis through websocket publisher
1548+ may result in worker crash (bug introduced in 1.1.5)
1549+ fix: nchan_pubsub setting may not be parsed correctly
1550+1.1.7 (Jul. 3 2017)
1551+ fix: possible read-after-free after redis disconnect
1552+ fix: publishing to redis cluster before it is connected results in worker crash
1553+ fix: possible use-after-free for suddenly disconnected longpoll-multipart subscriber
1554+ fix: possible use-after-free when using nchan_authorize_request for slow subscribers and slow upstream
1555+ fix: nchan_stub_status "stored messages" value could be incorrect when using Redis
1556+1.1.6 (May 9 2017)
1557+ fix: messages published through Redis may crash worker (introduced in 1.1.5)
1558+ fix (security): urlencoded message id in url parsed incorrectly can result in worker crash
1559+1.1.5 (May 3 2017)
1560+ feature: get current Nchan version through $nchan_version variable
1561+ and nchan_stub_status
1562+ fix (security): invalid memory access for multiplexed channel subscribers
1563+ with buffered output (Thanks Casey Forbes (@caseyf) for debugging.)
1564+ fix: "redis pending commands" nchan_stub_status stat could be wrong after
1565+ deleting channels
1566+ fix: invalid memory access when using Redis under high load
1567+ fix: possible "message from the past" errors under high publishing load
1568+ fix: graceful publisher/subscriber notifications when out of shared memory
1569+ (via HTTP 507 Insufficient Storage status code)
1570+ fix: compatibility with limit_except directive
1571+1.1.4 (Apr. 25 2017)
1572+ fix (security): possible memory corruption using multiplexed channels
1573+ at high load (Thanks Giovanni Caporaletti (@TrustNoOne) for debugging.)
1574+ fix: possible crash when reconnecting to Redis cluster (introduced in 1.1.3)
1575+1.1.3 (Mar. 25 2017)
1576+ fix (security): incorrect handling of WS optimization could trigger SIGABRT
1577+ fix: Redis cluster reconnect readiness verification
1578+1.1.2 (Mar. 1 2017)
1579+ change: "interprocess alert X delayed by Y sec" log messages downgraded
1580+ from ERROR to NOTICE
1581+ fix: "group info string too short" error
1582+ fix: Incorrect handling of connections to Redis cluster nodes with round-robin
1583+ DNS hostnames (Thanks to ring.com for sponsoring this fix!)
1584+1.1.1 (Feb. 8 2017)
1585+ fix: incorrect stats for nchan_stub_status after reload.
1586+ (Stats are no longer reset after reload)
1587+ fix: websocket subscriber may receive two CLOSE frames
1588+ fix: websocket with ws+meta.nchan subprotocol did not receive empty messages
1589+ feature: websocket client heartbeats with nchan_websocket_client_heartbeat
1590+ fix: websocket now echoes code and reason when connection close initiated from client
1591+1.1.0 (Jan. 4 2017)
1592+ feature: websocket subscribers now receive "application/octet-stream" messages
1593+ in binary frame rather than text
1594+ fix: publisher request variables not always passed to nchan_publisher_upstream_request
1595+ feature: Redis storage 'backup' mode strictly for data persistence
1596+ fix: possible lingering subscriber if connection is closed just before subscribing
1597+ fix: possible memory leak when using multiplexed channels
1598+ security fix: subscribing with If-Modified-Since and without If-None-Match headers
1599+ crashed the Nginx worker (thanks @supertong)
1600+ security fix: sending an empty message to multipart/mixed subscriber crashed Nginx worker
1601+ fix: publisher & subscriber response codes were logged incorrectly
1602+ fix: websocket subscriber memory leak on upstream authentication failure
1603+ fix: possible crash after reloading when using several instances of Nchan with Redis
1604+ feature: nchan_access_control_allow_origin can accept nginx variables
1605+ feature: Redis key namespaces
1606+ feature: all size configurations can now parse decimal values
1607+ fix: off-by-one subscriber count when using multiplexed channels
1608+ feature: accounting and dynamic limits for channel groups
1609 1.0.8 (Nov. 28 2016)
1610 fix: possible crash under severely heavy load, introduced in 1.0.7 with stack-overflow fix
1611 1.0.7 (Nov. 27 2016)
1612diff --git a/debian/modules/nchan/cloc-exclude.txt b/debian/modules/nchan/cloc-exclude.txt
1613index c72f372..fdf0e34 100644
1614--- a/debian/modules/nchan/cloc-exclude.txt
1615+++ b/debian/modules/nchan/cloc-exclude.txt
1616@@ -1,17 +1,16 @@
1617 src/uthash.h
1618 src/hiredis
1619-src/store/redis/cmp.*
1620+src/util/ngx_nchan_hacked_slab.c
1621+src/util/hdr_histogram.c
1622+src/util/hdr_histogram.h
1623+src/store/redis/cmp.c
1624+src/store/redis/cmp.h
1625+src/store/redis/redis_lua_commands.h
1626 src/nginx-source
1627-dev/package
1628-dev/nginx-nchan/*.patch
1629-dev/nginx-nchan/pkg
1630-dev/nginx-nchan/src
1631-dev/nginx-nchan/*.txt
1632-dev/nginx-nchan/nginx.conf
1633-dev/nginx-nchan/nginx.logrotate
1634-dev/nginx-nchan/*.tar
1635-dev/nginx-nchan/*.gz
1636+src/nchan_config_commands.c
1637+dev/bench
1638+dev/nginx-pkg
1639 dev/clang-analyzer
1640 dev/package
1641 dev/src
1642-
1643+dev/redis-trib.rb
1644diff --git a/debian/modules/nchan/config b/debian/modules/nchan/config
1645index 5776d29..324deef 100644
1646--- a/debian/modules/nchan/config
1647+++ b/debian/modules/nchan/config
1648@@ -1,25 +1,68 @@
1649 ngx_addon_name=ngx_nchan_module
1650
1651+nchan_libs=""
1652 #do we have hiredis on the platform?
1653-ngx_feature="hiredis"
1654+# it's currently no longer possible to link the platform's hiredis lib,
1655+# because we now use a hacked connect function
1656+# maybe it can be brought back at some later time...
1657+ngx_feature="hiredis with stored sockaddr"
1658+ngx_feature_name="NCHAN_HAVE_HIREDIS_WITH_SOCKADDR"
1659 ngx_feature_run=yes
1660-ngx_feature_incs="#include <hiredis/hiredis.h>"
1661-ngx_feature_test="if(HIREDIS_SONAME < 0.13) { return 1; }"
1662+ngx_feature_path=
1663+ngx_feature_incs=" \
1664+ #include <hiredis/hiredis.h>
1665+ #include <sys/socket.h>
1666+"
1667 ngx_feature_libs="-lhiredis"
1668+ngx_feature_test=" \
1669+ redisContext c; \
1670+ if(HIREDIS_SONAME < 0.13) { return 1; } \
1671+ if(sizeof(c.sockaddr) != sizeof(struct sockaddr)) { return 1;} \
1672+"
1673 . auto/feature
1674 if [ $ngx_found = no ]; then
1675 _NCHAN_HIREDIS_SRCS="\
1676- ${ngx_addon_dir}/src/hiredis/hiredis.c \
1677- ${ngx_addon_dir}/src/hiredis/read.c \
1678- ${ngx_addon_dir}/src/hiredis/async.c \
1679- ${ngx_addon_dir}/src/hiredis/sds.c \
1680- ${ngx_addon_dir}/src/hiredis/net.c \
1681+ ${ngx_addon_dir}/src/store/redis/hiredis/hiredis.c \
1682+ ${ngx_addon_dir}/src/store/redis/hiredis/read.c \
1683+ ${ngx_addon_dir}/src/store/redis/hiredis/async.c \
1684+ ${ngx_addon_dir}/src/store/redis/hiredis/sds.c \
1685+ ${ngx_addon_dir}/src/store/redis/hiredis/net.c \
1686 "
1687 ngx_feature_libs=""
1688 else
1689+ nchan_libs="$nchan_libs $ngx_feature_libs"
1690 _NCHAN_HIREDIS_SRCS=""
1691 fi
1692
1693+ngx_feature="math lib"
1694+ngx_feature_name="NCHAN_HAVE_MATH"
1695+ngx_feature_run=yes
1696+ngx_feature_path=
1697+ngx_feature_incs="#include <math.h>"
1698+ngx_feature_libs="-lm"
1699+ngx_feature_test="sqrt(20);"
1700+. auto/feature
1701+if [ $ngx_found = yes ]; then
1702+ nchan_libs="$nchan_libs $ngx_feature_libs"
1703+fi
1704+
1705+#do we have memrchr() on the platform?
1706+ngx_feature="memrchr()"
1707+ngx_feature_name="NCHAN_HAVE_MEMRCHR"
1708+ngx_feature_run=yes
1709+ngx_feature_path=
1710+ngx_feature_libs=
1711+ngx_feature_incs=" \
1712+ #include <string.h>
1713+ #include <stddef.h>
1714+"
1715+ngx_feature_test=" \
1716+ const char *str = \"aboobar\"; \
1717+ const void *place = &str[4]; \
1718+ const void *found = memrchr(str, 'b', strlen(str)); \
1719+ if(place != found) { return 1; } \
1720+"
1721+. auto/feature
1722
1723 _NCHAN_SUBSCRIBERS_SRCS="\
1724 ${ngx_addon_dir}/src/subscribers/common.c \
1725@@ -35,24 +78,31 @@ _NCHAN_SUBSCRIBERS_SRCS="\
1726 ${ngx_addon_dir}/src/subscribers/memstore_ipc.c \
1727 ${ngx_addon_dir}/src/subscribers/memstore_multi.c \
1728 ${ngx_addon_dir}/src/subscribers/memstore_redis.c \
1729+ ${ngx_addon_dir}/src/subscribers/getmsg_proxy.c \
1730+ ${ngx_addon_dir}/src/subscribers/benchmark.c \
1731 "
1732
1733 _NCHAN_REDIS_STORE_SRCS="\
1734 ${_NCHAN_HIREDIS_SRCS} \
1735 ${ngx_addon_dir}/src/store/redis/cmp.c \
1736- ${ngx_addon_dir}/src/store/redis/cluster.c \
1737+ ${ngx_addon_dir}/src/store/redis/redis_lua_commands.c \
1738+ ${ngx_addon_dir}/src/store/redis/redis_nodeset_parser.c \
1739+ ${ngx_addon_dir}/src/store/redis/redis_nodeset.c \
1740 ${ngx_addon_dir}/src/store/redis/rdsstore.c \
1741 ${ngx_addon_dir}/src/store/redis/redis_nginx_adapter.c \
1742 "
1743 _NCHAN_MEMORY_STORE_SRCS="\
1744 ${ngx_addon_dir}/src/store/memory/ipc.c \
1745 ${ngx_addon_dir}/src/store/memory/ipc-handlers.c \
1746+ ${ngx_addon_dir}/src/store/memory/groups.c \
1747 ${ngx_addon_dir}/src/store/memory/memstore.c \
1748 "
1749
1750 _nchan_util_dir="${ngx_addon_dir}/src/util"
1751 _NCHAN_UTIL_SRCS=" \
1752+ $_nchan_util_dir/nchan_debug.c \
1753 $_nchan_util_dir/nchan_list.c \
1754+ $_nchan_util_dir/nchan_slist.c \
1755 $_nchan_util_dir/ngx_nchan_hacked_slab.c \
1756 $_nchan_util_dir/shmem.c \
1757 $_nchan_util_dir/nchan_rbtree.c \
1758@@ -62,31 +112,32 @@ _NCHAN_UTIL_SRCS=" \
1759 $_nchan_util_dir/nchan_fake_request.c \
1760 $_nchan_util_dir/nchan_bufchainpool.c \
1761 $_nchan_util_dir/nchan_channel_id.c \
1762- $_nchan_util_dir/nchan_channel_info.c \
1763- $_nchan_util_dir/nchan_msgid.c \
1764+ $_nchan_util_dir/nchan_output_info.c \
1765+ $_nchan_util_dir/nchan_msg.c \
1766 $_nchan_util_dir/nchan_thingcache.c \
1767 $_nchan_util_dir/nchan_reaper.c \
1768 $_nchan_util_dir/nchan_subrequest.c \
1769+ $_nchan_util_dir/nchan_benchmark.c \
1770+ $_nchan_util_dir/hdr_histogram.c \
1771 "
1772
1773 _NCHAN_STORE_SRCS="\
1774 ${ngx_addon_dir}/src/store/spool.c \
1775 ${ngx_addon_dir}/src/store/ngx_rwlock.c \
1776 ${ngx_addon_dir}/src/store/store_common.c \
1777- $_NCHAN_REDIS_STORE_SRCS \
1778 $_NCHAN_MEMORY_STORE_SRCS \
1779+ $_NCHAN_REDIS_STORE_SRCS \
1780 "
1781
1782 _NCHAN_SRCS="\
1783- $_NCHAN_UTIL_SRCS \
1784- $_NCHAN_SUBSCRIBERS_SRCS \
1785- $_NCHAN_STORE_SRCS \
1786 ${ngx_addon_dir}/src/nchan_defs.c \
1787 ${ngx_addon_dir}/src/nchan_variables.c \
1788 ${ngx_addon_dir}/src/nchan_module.c \
1789+ $_NCHAN_UTIL_SRCS \
1790+ $_NCHAN_SUBSCRIBERS_SRCS \
1791+ $_NCHAN_STORE_SRCS \
1792 "
1793
1794-
1795 ngx_module_incs=$ngx_addon_dir/src
1796
1797 have=NGX_HTTP_HEADERS . auto/have
1798@@ -95,11 +146,11 @@ if test -n "$ngx_module_link"; then
1799 ngx_module_type=HTTP
1800 ngx_module_name=$ngx_addon_name
1801 ngx_module_srcs="$_NCHAN_SRCS"
1802- ngx_module_libs=$ngx_feature_libs
1803+ ngx_module_libs=$nchan_libs
1804 . auto/module
1805 else
1806 NGX_ADDON_SRCS="$NGX_ADDON_SRCS $_NCHAN_SRCS"
1807- CORE_LIBS="$CORE_LIBS $ngx_feature_libs"
1808+ CORE_LIBS="$CORE_LIBS $nchan_libs"
1809 CORE_INCS="$CORE_INCS $ngx_module_incs"
1810 HTTP_MODULES="$HTTP_MODULES $ngx_addon_name"
1811 fi
1812diff --git a/debian/modules/nchan/src/nchan_commands.rb b/debian/modules/nchan/src/nchan_commands.rb
1813index 12a88f1..eae0ea9 100644
1814--- a/debian/modules/nchan/src/nchan_commands.rb
1815+++ b/debian/modules/nchan/src/nchan_commands.rb
1816@@ -30,13 +30,24 @@ CfCmd.new do
1817 group: "pubsub",
1818 tags: [ 'publisher', 'hook' ],
1819 value: "<url>",
1820- uri: "/details#message-publishing-callbacks",
1821+ uri: "#message-forwarding",
1822 info: <<-EOS.gsub(/^ {8}/, '')
1823 Send POST request to internal location (which may proxy to an upstream server) with published message in the request body. Useful for bridging websocket publishers with HTTP applications, or for transforming message via upstream application before publishing to a channel.
1824 The upstream response code determines how publishing will proceed. A `200 OK` will publish the message from the upstream response's body. A `304 Not Modified` will publish the message as it was received from the publisher. A `204 No Content` will result in the message not being published.
1825 EOS
1826-
1827
1828+
1829+ nchan_deflate_message_for_websocket [:srv, :loc],
1830+ :nchan_set_message_compression_slot,
1831+ :loc_conf,
1832+ args: 1,
1833+
1834+ group: "pubsub",
1835+ tags: ["publisher", 'subscriber-websocket'],
1836+ value: ['on', 'off'],
1837+ default: "off",
1838+ info: "Store a compressed (deflated) copy of the message along with the original to be sent to websocket clients supporting the permessage-deflate protocol extension"
1839+
1840 nchan_channel_id_split_delimiter [:srv, :loc, :if],
1841 :ngx_conf_set_str_slot,
1842 [:loc_conf, :channel_id_split_delimiter],
1843@@ -62,6 +73,7 @@ CfCmd.new do
1844 :nchan_pubsub_directive,
1845 :loc_conf,
1846 args: 0..6,
1847+ alt: ["nchan_pubsub_location"],
1848
1849 group: "pubsub",
1850 tags: [ 'publisher', 'subscriber', 'pubsub' ],
1851@@ -91,13 +103,53 @@ CfCmd.new do
1852 tags: ['subscriber-eventsource'],
1853 default: "(none)",
1854 info: "Set the EventSource `event:` line to this value. When used in a publisher location, overrides the published message's `X-EventSource-Event` header and associates the message with the given value. When used in a subscriber location, overrides all messages' associated `event:` string with the given value."
1855+
1856+ nchan_eventsource_ping_comment [:srv, :loc, :if],
1857+ :ngx_conf_set_str_slot_no_newlines,
1858+ [:loc_conf, :"eventsource_ping.comment"],
1859+ args: 1,
1860+
1861+ group: "pubsub",
1862+ tags: ['subscriber-eventsource'],
1863+ default: "(empty)",
1864+ info: "Set the EventSource comment `: ...` line for periodic pings from server to client. Newlines are not allowed. If empty, no comment is sent with the ping."
1865+
1866+ nchan_eventsource_ping_event [:srv, :loc, :if],
1867+ :ngx_conf_set_str_slot_no_newlines,
1868+ [:loc_conf, :"eventsource_ping.event"],
1869+ args: 1,
1870+
1871+ group: "pubsub",
1872+ tags: ['subscriber-eventsource'],
1873+ default: "ping",
1874+ info: "Set the EventSource `event:` line for periodic pings from server to client. Newlines are not allowed. If empty, no event type is sent with the ping."
1875+
1876+ nchan_eventsource_ping_data [:srv, :loc, :if],
1877+ :ngx_conf_set_str_slot_no_newlines,
1878+ [:loc_conf, :"eventsource_ping.data"],
1879+ args: 1,
1880+
1881+ group: "pubsub",
1882+ tags: ['subscriber-eventsource'],
1883+ default: "(empty)",
1884+ info: "Set the EventSource `data:` line for periodic pings from server to client. Newlines are not allowed. If empty, no data is sent with the ping."
1885+
1886+ nchan_eventsource_ping_interval [:srv, :loc, :if],
1887+ :ngx_conf_set_sec_slot,
1888+ [:loc_conf, :"eventsource_ping.interval"],
1889
1890+ group: "pubsub",
1891+ tags: ['subscriber-eventsource'],
1892+ value: "<number> (seconds)",
1893+ default: "0 (none)",
1894+ info: "Interval for sending ping messages to EventSource subscribers. Disabled by default."
1895
1896 nchan_subscriber [:srv, :loc, :if],
1897 :nchan_subscriber_directive,
1898 :loc_conf,
1899 args: 0..5,
1900 legacy: "push_subscriber",
1901+ alt: ["nchan_subscriber_location"],
1902
1903 group: "pubsub",
1904 tags: ['subscriber'],
1905@@ -188,11 +240,23 @@ CfCmd.new do
1906 default: "0 (none)",
1907 info: "Interval for sending websocket ping frames. Disabled by default."
1908
1909+ nchan_websocket_client_heartbeat [:srv, :loc, :if],
1910+ :nchan_websocket_heartbeat_directive,
1911+ [:loc_conf, :websocket_heartbeat],
1912+ args: 2,
1913+
1914+ group: "pubsub",
1915+ tags:['subscriber-websocket'],
1916+ value: "<heartbeat_in> <heartbeat_out>",
1917+ default: "none (disabled)",
1918+ info: "Most browser Websocket clients do not allow manually sending PINGs to the server. To overcome this limitation, this setting can be used to set up a PING/PONG message/response connection heartbeat. When the client sends the server message *heartbeat_in* (PING), the server automatically responds with *heartbeat_out* (PONG)."
1919+
1920 nchan_publisher [:srv, :loc, :if],
1921 :nchan_publisher_directive,
1922 :loc_conf,
1923 args: 0..2,
1924 legacy: "push_publisher",
1925+ alt: ["nchan_publisher_location"],
1926
1927 group: "pubsub",
1928 tags: ['publisher'],
1929@@ -221,7 +285,7 @@ CfCmd.new do
1930 tags: ['publisher', 'subscriber', 'hook'],
1931 value: "<url>",
1932 info: "Send GET request to internal location (which may proxy to an upstream server) for authorization of a publisher or subscriber request. A 200 response authorizes the request, a 403 response forbids it.",
1933- uri: "/details#request-authorization"
1934+ uri: "#request-authorization"
1935
1936 nchan_subscribe_request [:srv, :loc, :if],
1937 :ngx_http_set_complex_value_slot,
1938@@ -231,7 +295,7 @@ CfCmd.new do
1939 tags: ['subscriber', 'hook'],
1940 value: "<url>",
1941 info: "Send GET request to internal location (which may proxy to an upstream server) after subscribing. Disabled for longpoll and interval-polling subscribers.",
1942- uri: "/details#subsribe-and-unsubscribe-callbacks"
1943+ uri: "#subscriber-presence"
1944
1945 nchan_unsubscribe_request [:srv, :loc, :if],
1946 :ngx_http_set_unsubscribe_request_url,
1947@@ -241,7 +305,18 @@ CfCmd.new do
1948 tags: ['subscriber', 'hook'],
1949 value: "<url>",
1950 info: "Send GET request to internal location (which may proxy to an upstream server) after unsubscribing. Disabled for longpoll and interval-polling subscribers.",
1951- uri: "/details#subsribe-and-unsubscribe-callbacks"
1952+ uri: "#subscriber-presence"
1953+
1954+ nchan_message_temp_path [:main],
1955+ :ngx_conf_set_path_slot,
1956+ [:main_conf, :message_temp_path],
1957+
1958+ group: "storage",
1959+ tags: ["memstore"],
1960+ value: "<path>",
1961+ default: "<client_body_temp_path>",
1962+ info: "Large messages are stored in temporary files in the `client_body_temp_path` or the `nchan_message_temp_path` if the former is unavailable. Default is the built-in default `client_body_temp_path`"
1963+
1964
1965 nchan_store_messages [:main, :srv, :loc, :if],
1966 :nchan_store_messages_directive,
1967@@ -254,27 +329,68 @@ CfCmd.new do
1968 default: :on,
1969 info: "Publisher configuration. \"`off`\" is equivalent to setting `nchan_message_buffer_length 0`, which disables the buffering of old messages. Using this setting is not recommended when publishing very quickly, as it may result in missed messages."
1970
1971- nchan_max_reserved_memory [:main],
1972- :ngx_conf_set_size_slot,
1973+ nchan_shared_memory_size [:main],
1974+ :nchan_conf_set_size_slot,
1975 [:main_conf, :shm_size],
1976- legacy: "push_max_reserved_memory",
1977+ legacy: ["push_max_reserved_memory", "nchan_max_reserved_memory"],
1978
1979 group: "storage",
1980 tags: ['memstore'],
1981 value: "<size>",
1982- default: "32M",
1983- info: "The size of the shared memory chunk this module will use for message queuing and buffering.",
1984+ default: "128M",
1985+ info: "Shared memory slab pre-allocated for Nchan. Used for channel statistics, message storage, and interprocess communication.",
1986 uri: "#memory-storage"
1987-
1988+
1989+ nchan_permessage_deflate_compression_level [:main],
1990+ :nchan_conf_deflate_compression_level_directive,
1991+ :main_conf,
1992+
1993+ group: "pubsub",
1994+ tags: ['subscriber-websocket'],
1995+ value: ["0-9"],
1996+ default: "6",
1997+ info: "Compression level for the `deflate` algorithm used in websocket's permessage-deflate extension. 0: no compression, 1: fastest, worst, 9: slowest, best"
1998+
1999+ nchan_permessage_deflate_compression_strategy [:main],
2000+ :nchan_conf_deflate_compression_strategy_directive,
2001+ :main_conf,
2002+
2003+ group: "pubsub",
2004+ tags: ['subscriber-websocket'],
2005+ value: ["default", "filtered", "huffman-only", "rle", "fixed"],
2006+ default: "default",
2007+ info: "Compression strategy for the `deflate` algorithm used in websocket's permessage-deflate extension. Use 'default' for normal data, For details see [zlib's section on copression strategies](http://zlib.net/manual.html#Advanced)"
2008+
2009+ nchan_permessage_deflate_compression_window [:main],
2010+ :nchan_conf_deflate_compression_window_directive,
2011+ :main_conf,
2012+
2013+ group: "pubsub",
2014+ tags: ['subscriber-websocket'],
2015+ value: ["9-15"],
2016+ default: "10",
2017+ info: "Compression window for the `deflate` algorithm used in websocket's permessage-deflate extension. The base two logarithm of the window size (the size of the history buffer). The bigger the window, the better the compression, but the more memory used by the compressor."
2018+
2019+ nchan_permessage_deflate_compression_memlevel [:main],
2020+ :nchan_conf_deflate_compression_memlevel_directive,
2021+ :main_conf,
2022+
2023+ group: "pubsub",
2024+ tags: ['subscriber-websocket'],
2025+ value: ["1-9"],
2026+ default: "8",
2027+ info: "Memory level for the `deflate` algorithm used in websocket's permessage-deflate extension. How much memory should be allocated for the internal compression state. 1 - minimum memory, slow and reduces compression ratio; 9 - maximum memory for optimal speed"
2028+
2029 nchan_redis_url [:main, :srv, :loc],
2030 :ngx_conf_set_redis_url,
2031 [:loc_conf, :"redis.url"],
2032
2033 group: "storage",
2034 tags: ['redis'],
2035+ value: "<redis-url>",
2036 default: "127.0.0.1:6379",
2037- info: "The path to a redis server, of the form 'redis://:password@hostname:6379/0'. Shorthand of the form 'host:port' or just 'host' is also accepted.",
2038- uri: "/details#using-redis"
2039+ info: "Use of this command is discouraged in favor of upstreams blocks with (`nchan_redis_server`)[#nchan_redis_server]. The path to a redis server, of the form 'redis://:password@hostname:6379/0'. Shorthand of the form 'host:port' or just 'host' is also accepted.",
2040+ uri: "#connecting-to-a-redis-server"
2041
2042 nchan_redis_pass [:main, :srv, :loc],
2043 :ngx_conf_set_redis_upstream_pass,
2044@@ -282,8 +398,9 @@ CfCmd.new do
2045
2046 group: "storage",
2047 tags: ['publisher', 'subscriber', 'redis'],
2048+ value: "<upstream-name>",
2049 info: "Use an upstream config block for Redis servers.",
2050- uri: "/details#using-redis"
2051+ uri: "#connecting-to-a-redis-server"
2052
2053 nchan_redis_pass_inheritable [:main, :srv, :loc],
2054 :ngx_conf_set_flag_slot,
2055@@ -303,8 +420,36 @@ CfCmd.new do
2056 :loc_conf,
2057 group: "storage",
2058 tags: ['redis'],
2059- info: "Used in upstream { } blocks to set redis servers.",
2060- uri: "/details#using-redis"
2061+ value: "<redis-url>",
2062+ info: "Used in upstream { } blocks to set redis servers. Redis url is in the form 'redis://:password@hostname:6379/0'. Shorthands 'host:port' or 'host' are permitted.",
2063+ uri: "#connecting-to-a-redis-server"
2064+
2065+ nchan_redis_storage_mode [:main, :srv, :upstream],
2066+ :ngx_conf_set_redis_storage_mode_slot,
2067+ [:loc_conf, :"redis.storage_mode"],
2068+
2069+ group: "storage",
2070+ tags: ['redis'],
2071+ value: ["distributed", "backup", "nostore"],
2072+ default: "distributed",
2073+ info: <<-EOS.gsub(/^ {8}/, '')
2074+ The mode of operation of the Redis server. In `distributed` mode, messages are published directly to Redis, and retrieved in real-time. Any number of Nchan servers in distributed mode can share the Redis server (or cluster). Useful for horizontal scalability, but suffers the latency penalty of all message publishing going through Redis first.
2075+
2076+ In `backup` mode, messages are published locally first, then later forwarded to Redis, and are retrieved only upon chanel initialization. Only one Nchan server should use a Redis server (or cluster) in this mode. Useful for data persistence without sacrificing response times to the latency of a round-trip to Redis.
2077+
2078+ In `nostore` mode, messages are published as in `distributed` mode, but are not stored. Thus Redis is used to broadcast messages to many Nchan instances with no delivery guarantees during connection failure, and only local in-memory storage. This means that there are also no message delivery guarantees for subscribers switching from one Nchan instance to another connected to the same Redis server or cluster. Nostore mode increases Redis publishing capacity by an order of magnitude.
2079+ EOS
2080+
2081+ nchan_redis_nostore_fastpublish [:main, :srv, :upstream],
2082+ :ngx_conf_set_flag_slot,
2083+ [:loc_conf, :"redis.nostore_fastpublish"],
2084+
2085+ group: "storage",
2086+ tags: ['redis', 'publisher'],
2087+ value: [:on, :off],
2088+ default: :off,
2089+ info: "Increases publishing capacity by 2-3x for Redis nostore mode at the expense of inaccurate subscriber counts in the publisher response."
2090+
2091
2092 nchan_use_redis [:main, :srv, :loc],
2093 :ngx_conf_enable_redis,
2094@@ -314,10 +459,10 @@ CfCmd.new do
2095 tags: ['redis', 'publisher', 'subscriber'],
2096 value: [ :on, :off ],
2097 default: :off,
2098- info: "Use redis for message storage at this location.",
2099- uri: "/details#using-redis"
2100+ info: "Use of this command is discouraged in favor of (`nchan_redis_pass`)[#nchan_redis_pass]. Use Redis for message storage at this location.",
2101+ uri: "#connecting-to-a-redis-server"
2102
2103- nchan_redis_ping_interval [:main, :srv, :loc],
2104+ nchan_redis_ping_interval [:main, :srv, :upstream, :loc],
2105 :ngx_conf_set_sec_slot,
2106 [:loc_conf, :"redis.ping_interval"],
2107
2108@@ -326,6 +471,55 @@ CfCmd.new do
2109 default: "4m",
2110 info: "Send a keepalive command to redis to keep the Nchan redis clients from disconnecting. Set to 0 to disable."
2111
2112+ nchan_redis_wait_after_connecting [:main, :srv, :loc],
2113+ :nchan_ignore_obsolete_setting,
2114+ :loc_conf,
2115+
2116+ group: "obsolete",
2117+ tags: ['redis'],
2118+ uncocumented: true
2119+
2120+ nchan_redis_connect_timeout [:upstream],
2121+ :ngx_conf_set_msec_slot,
2122+ [:srv_conf, :"redis.connect_timeout"],
2123+
2124+ group: "storage",
2125+ tags: ['redis'],
2126+ default: "600ms",
2127+ info: "Redis server connection timeout."
2128+
2129+ nchan_redis_subscribe_weights [:upstream],
2130+ :ngx_conf_set_redis_subscribe_weights,
2131+ :srv_conf,
2132+ args: 1..2,
2133+
2134+ group: "storage",
2135+ tags: ['redis'],
2136+ value: "master=<integer> slave=<integer>",
2137+ default: "master=1 slave=1",
2138+ info: "Determines how subscriptions to Redis PUBSUB channels are distributed between master and slave nodes. The higher the number, the more likely that each node of that type will be chosen for each new channel. The weights for slave nodes are cumulative, so an equal 1:1 master:slave weight ratio with two slaves would have a 1/3 chance of picking a master, and 2/3 chance of picking one of the slaves. The weight must be a non-negative integer."
2139+
2140+ nchan_redis_optimize_target [:upstream],
2141+ :ngx_conf_set_redis_optimize_target,
2142+ :srv_conf,
2143+
2144+ group: "storage",
2145+ tags: ['redis'],
2146+ value: [:cpu, :bandwidth],
2147+ default: "cpu",
2148+ info: "This tweaks whether [effect replication](https://redis.io/commands/eval#replicating-commands-instead-of-scripts) is enabled. Optimizing for CPU usage enables effect replication, costing additional bandwidth (between 1.2 and 2 times more) between all master->slave links. Optimizing for bandwidth increases CPU load on slaves, but keeps outgoing bandwidth used for replication the same as the incoming bandwidth on Master."
2149+
2150+ nchan_redis_namespace [:main, :srv, :upstream, :loc],
2151+ :ngx_conf_set_str_slot,
2152+ [:loc_conf, :"redis.namespace"],
2153+ args: 1,
2154+ post_handler: "ngx_conf_process_redis_namespace_slot",
2155+
2156+ group: "storage",
2157+ value: "<string>",
2158+ tags: ['redis'],
2159+ info: "Prefix all Redis keys with this string. All Nchan-related keys in redis will be of the form \"nchan_redis_namespace:*\" . Default is empty."
2160+
2161 nchan_redis_fakesub_timer_interval [:main],
2162 :ngx_conf_set_msec_slot,
2163 [:main_conf, :redis_fakesub_timer_interval],
2164@@ -378,27 +572,108 @@ CfCmd.new do
2165 default: :off,
2166 info: "Whether or not a subscriber may create a channel by sending a request to a subscriber location. If set to on, a publisher must send a POST or PUT request before a subscriber can request messages on the channel. Otherwise, all subscriber requests to nonexistent channels will get a 403 Forbidden response."
2167
2168- nchan_access_control_allow_origin [:main, :srv, :loc],
2169- :ngx_conf_set_str_slot,
2170+ nchan_access_control_allow_origin [:main, :srv, :loc, :if],
2171+ :ngx_http_set_complex_value_slot,
2172 [:loc_conf, :allow_origin],
2173 args: 1,
2174
2175 group: "security",
2176 value: "<string>",
2177 tags: ['publisher', 'subscriber'],
2178- default: "*",
2179- info: "Set the [Cross-Origin Resource Sharing (CORS)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS) `Access-Control-Allow-Origin` header to this value. If the publisher or subscriber request's `Origin` header does not match this value, respond with a `403 Forbidden`."
2180+ default: "$http_origin",
2181+ info: "Set the [Cross-Origin Resource Sharing (CORS)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS) `Access-Control-Allow-Origin` header to this value. If the incoming request's `Origin` header does not match this value, respond with a `403 Forbidden`. Multiple origins can be provided in a single argument separated with a space."
2182+
2183+ nchan_access_control_allow_credentials [:main, :srv, :loc, :if],
2184+ :ngx_conf_set_flag_slot,
2185+ [:loc_conf, :allow_credentials],
2186+ args: 1,
2187+
2188+ group: "security",
2189+ tags: ['publisher', 'subscriber'],
2190+ default: "on",
2191+ info: "When enabled, sets the [Cross-Origin Resource Sharing (CORS)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS) `Access-Control-Allow-Credentials` header to `true`."
2192+
2193
2194 nchan_channel_group [:srv, :loc, :if],
2195- :ngx_conf_set_str_slot,
2196+ :ngx_http_set_complex_value_slot,
2197 [:loc_conf, :channel_group],
2198 legacy: "push_channel_group",
2199
2200 group: "security",
2201- tags: ['publisher', 'subscriber', 'channel-events'],
2202+ tags: ['publisher', 'subscriber', 'channel-events', 'group'],
2203 value: "<string>",
2204 default: "(none)",
2205- info: "Because settings are bound to locations and not individual channels, it is useful to be able to have channels that can be reached only from some locations and never others. That's where this setting comes in. Think of it as a prefix string for the channel id."
2206+ info: "The accounting and security group a channel belongs to. Works like a prefix string to the channel id. Can be set with nginx variables."
2207+
2208+ nchan_channel_group_accounting [:srv, :loc],
2209+ :ngx_conf_set_flag_slot,
2210+ [:loc_conf, "group.enable_accounting"],
2211+
2212+ group: "security",
2213+ tags: ['group', 'security'],
2214+ default: "off",
2215+ info: "Enable tracking channel, subscriber, and message information on a per-channel-group basis. Can be used to place upper limits on channel groups."
2216+
2217+ nchan_group_location [:loc],
2218+ :nchan_group_directive,
2219+ [:loc_conf],
2220+ args: 0..3,
2221+
2222+ group: "security",
2223+ tags: ['group'],
2224+ value: ["get", "set", "delete", "off"],
2225+ default: ["get", "set", "delete"],
2226+ info: "Group information and configuration location. GET request for group info, POST to set limits, DELETE to delete all channels in group."
2227+
2228+ nchan_group_max_channels [:loc],
2229+ :ngx_http_set_complex_value_slot,
2230+ [:loc_conf, "group.max_channels"],
2231+
2232+ group: "security",
2233+ tags: ['group', 'security'],
2234+ value: "<number>",
2235+ default: "0 (unlimited)",
2236+ info: "Maximum number of channels allowed in the group."
2237+
2238+ nchan_group_max_messages [:loc],
2239+ :ngx_http_set_complex_value_slot,
2240+ [:loc_conf, "group.max_messages"],
2241+
2242+ group: "security",
2243+ tags: ['group', 'security'],
2244+ value: "<number>",
2245+ default: "0 (unlimited)",
2246+ info: "Maximum number of messages allowed for all the channels in the group."
2247+
2248+ nchan_group_max_messages_memory [:loc],
2249+ :ngx_http_set_complex_value_slot,
2250+ [:loc_conf, "group.max_messages_shm_bytes"],
2251+
2252+ group: "security",
2253+ tags: ['group', 'security'],
2254+ value: "<number>",
2255+ default: "0 (unlimited)",
2256+ info: "Maximum amount of shared memory allowed for the messages of all the channels in the group."
2257+
2258+ nchan_group_max_messages_disk [:loc],
2259+ :ngx_http_set_complex_value_slot,
2260+ [:loc_conf, "group.max_messages_file_bytes"],
2261+
2262+ group: "security",
2263+ tags: ['group', 'security'],
2264+ value: "<number>",
2265+ default: "0 (unlimited)",
2266+ info: "Maximum amount of disk space allowed for the messages of all the channels in the group."
2267+
2268+ nchan_group_max_subscribers [:loc],
2269+ :ngx_http_set_complex_value_slot,
2270+ [:loc_conf, "group.max_subscribers"],
2271+
2272+ group: "security",
2273+ tags: ['group', 'security'],
2274+ value: "<number>",
2275+ default: "0 (unlimited)",
2276+ info: "Maximum number of subscribers allowed for the messages of all the channels in the group."
2277
2278 nchan_channel_events_channel_id [:srv, :loc, :if],
2279 :nchan_set_channel_events_channel_id,
2280@@ -406,8 +681,8 @@ CfCmd.new do
2281 args: 1,
2282
2283 group: "meta",
2284- tags: ['publisher', 'subscriber', 'channel-events'],
2285- uri: "/details#channel-events",
2286+ tags: ['publisher', 'subscriber', 'channel-events', 'introspection'],
2287+ uri: "#channel-events",
2288 info: "Channel id where `nchan_channel_id`'s events should be sent. Events like subscriber enqueue/dequeue, publishing messages, etc. Useful for application debugging. The channel event message is configurable via nchan_channel_event_string. The channel group for events is hardcoded to 'meta'."
2289
2290 nchan_stub_status [:loc],
2291@@ -416,15 +691,16 @@ CfCmd.new do
2292 args: 0,
2293
2294 group: "meta",
2295+ tags: ['introspection'],
2296 info: "Similar to Nginx's stub_status directive, requests to an `nchan_stub_status` location get a response with some vital Nchan statistics. This data does not account for information from other Nchan instances, and monitors only local connections, published messages, etc.",
2297- uri: "/details#nchan_stub_status"
2298+ uri: "#nchan_stub_status"
2299
2300 nchan_channel_event_string [:srv, :loc, :if],
2301 :ngx_http_set_complex_value_slot,
2302 [:loc_conf, :channel_event_string],
2303
2304 group: "meta",
2305- tags: ['publisher', 'subscriber', 'channel-events'],
2306+ tags: ['publisher', 'subscriber', 'channel-events', 'introspection'],
2307 value: "<string>",
2308 default: "\"$nchan_channel_event $nchan_channel_id\"",
2309 info: "Contents of channel event message"
2310@@ -437,8 +713,8 @@ CfCmd.new do
2311 group: "security",
2312 tags: ['publisher', 'subscriber', 'channel-id' ],
2313 value: "<number>",
2314- default: 512,
2315- info: "Maximum permissible channel id length (number of characters). Longer ids will be truncated."
2316+ default: 1024,
2317+ info: "Maximum permissible channel id length (number of characters). This settings applies to ids before they may be split by the `nchan_channel_id_split_delimiter` Requests with a channel id that is too long will receive a `403 Forbidden` response."
2318
2319 nchan_max_channel_subscribers [:main, :srv, :loc],
2320 :ngx_conf_set_num_slot,
2321@@ -468,6 +744,49 @@ CfCmd.new do
2322 default: "memory",
2323 info: "Development directive to completely replace default storage engine. Don't use unless you are an Nchan developer."
2324
2325+ nchan_benchmark [:loc],
2326+ :nchan_benchmark_directive,
2327+ [:loc_conf],
2328+ group: "development",
2329+ undocumented: true,
2330+ args: 0
2331+ nchan_benchmark_time [:loc],
2332+ :ngx_conf_set_sec_slot,
2333+ [:loc_conf, "benchmark.time"],
2334+ group: "development",
2335+ undocumented: true
2336+ nchan_benchmark_messages_per_channel_per_minute [:loc],
2337+ :ngx_conf_set_num_slot,
2338+ [:loc_conf, "benchmark.msgs_per_minute"],
2339+ group: "development",
2340+ undocumented: true
2341+ nchan_benchmark_message_padding_bytes [:loc],
2342+ :ngx_conf_set_num_slot,
2343+ [:loc_conf, "benchmark.msg_padding"],
2344+ group: "development",
2345+ undocumented: true
2346+ nchan_benchmark_channels [:loc],
2347+ :ngx_conf_set_num_slot,
2348+ [:loc_conf, "benchmark.channels"],
2349+ group: "development",
2350+ undocumented: true
2351+ nchan_benchmark_subscribers_per_channel [:loc],
2352+ :ngx_conf_set_num_slot,
2353+ [:loc_conf, "benchmark.subscribers_per_channel"],
2354+ group: "development",
2355+ undocumented: true
2356+ nchan_benchmark_subscriber_distribution [:loc],
2357+ :nchan_benchmark_subscriber_distribution_directive,
2358+ [:loc_conf, "benchmark.subscriber_distribution"],
2359+ group: "development",
2360+ undocumented: true
2361+ nchan_benchmark_publisher_distribution [:loc],
2362+ :nchan_benchmark_publisher_distribution_directive,
2363+ [:loc_conf, "benchmark.publisher_distribution"],
2364+ group: "development",
2365+ undocumented: true
2366+
2367+
2368 push_min_message_buffer_length [:srv, :loc, :if],
2369 :nchan_ignore_obsolete_setting,
2370 :loc_conf,
2371diff --git a/debian/modules/nchan/src/nchan_config_commands.c b/debian/modules/nchan/src/nchan_config_commands.c
2372index 3051f09..4cbde2a 100644
2373--- a/debian/modules/nchan/src/nchan_config_commands.c
2374+++ b/debian/modules/nchan/src/nchan_config_commands.c
2375@@ -1,4 +1,8 @@
2376 //AUTOGENERATED, do not edit! see nchan_commands.rb
2377+static ngx_conf_post_t nchan_redis_namespace__post_handler = {
2378+ ngx_conf_process_redis_namespace_slot
2379+};
2380+
2381 static ngx_command_t nchan_commands[] = {
2382 { ngx_string("nchan_channel_id"),
2383 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1|NGX_CONF_TAKE2|NGX_CONF_TAKE3|NGX_CONF_TAKE4|NGX_CONF_TAKE5|NGX_CONF_TAKE6|NGX_CONF_TAKE7,
2384@@ -33,6 +37,13 @@ static ngx_command_t nchan_commands[] = {
2385 offsetof(nchan_loc_conf_t, publisher_upstream_request_url),
2386 NULL } ,
2387
2388+ { ngx_string("nchan_deflate_message_for_websocket"),
2389+ NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
2390+ nchan_set_message_compression_slot,
2391+ NGX_HTTP_LOC_CONF_OFFSET,
2392+ 0,
2393+ NULL } ,
2394+
2395 { ngx_string("nchan_channel_id_split_delimiter"),
2396 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
2397 ngx_conf_set_str_slot,
2398@@ -59,6 +70,12 @@ static ngx_command_t nchan_commands[] = {
2399 NGX_HTTP_LOC_CONF_OFFSET,
2400 0,
2401 NULL } ,
2402+ { ngx_string("nchan_pubsub_location"), //alt for nchan_pubsub
2403+ NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_NOARGS|NGX_CONF_TAKE1|NGX_CONF_TAKE2|NGX_CONF_TAKE3|NGX_CONF_TAKE4|NGX_CONF_TAKE5|NGX_CONF_TAKE6,
2404+ nchan_pubsub_directive,
2405+ NGX_HTTP_LOC_CONF_OFFSET,
2406+ 0,
2407+ NULL } ,
2408
2409 { ngx_string("nchan_longpoll_multipart_response"),
2410 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
2411@@ -74,6 +91,34 @@ static ngx_command_t nchan_commands[] = {
2412 offsetof(nchan_loc_conf_t, eventsource_event),
2413 NULL } ,
2414
2415+ { ngx_string("nchan_eventsource_ping_comment"),
2416+ NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
2417+ ngx_conf_set_str_slot_no_newlines,
2418+ NGX_HTTP_LOC_CONF_OFFSET,
2419+ offsetof(nchan_loc_conf_t, eventsource_ping.comment),
2420+ NULL } ,
2421+
2422+ { ngx_string("nchan_eventsource_ping_event"),
2423+ NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
2424+ ngx_conf_set_str_slot_no_newlines,
2425+ NGX_HTTP_LOC_CONF_OFFSET,
2426+ offsetof(nchan_loc_conf_t, eventsource_ping.event),
2427+ NULL } ,
2428+
2429+ { ngx_string("nchan_eventsource_ping_data"),
2430+ NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
2431+ ngx_conf_set_str_slot_no_newlines,
2432+ NGX_HTTP_LOC_CONF_OFFSET,
2433+ offsetof(nchan_loc_conf_t, eventsource_ping.data),
2434+ NULL } ,
2435+
2436+ { ngx_string("nchan_eventsource_ping_interval"),
2437+ NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
2438+ ngx_conf_set_sec_slot,
2439+ NGX_HTTP_LOC_CONF_OFFSET,
2440+ offsetof(nchan_loc_conf_t, eventsource_ping.interval),
2441+ NULL } ,
2442+
2443 { ngx_string("nchan_subscriber"),
2444 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_NOARGS|NGX_CONF_TAKE1|NGX_CONF_TAKE2|NGX_CONF_TAKE3|NGX_CONF_TAKE4|NGX_CONF_TAKE5,
2445 nchan_subscriber_directive,
2446@@ -86,6 +131,12 @@ static ngx_command_t nchan_commands[] = {
2447 NGX_HTTP_LOC_CONF_OFFSET,
2448 0,
2449 NULL } ,
2450+ { ngx_string("nchan_subscriber_location"), //alt for nchan_subscriber
2451+ NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_NOARGS|NGX_CONF_TAKE1|NGX_CONF_TAKE2|NGX_CONF_TAKE3|NGX_CONF_TAKE4|NGX_CONF_TAKE5,
2452+ nchan_subscriber_directive,
2453+ NGX_HTTP_LOC_CONF_OFFSET,
2454+ 0,
2455+ NULL } ,
2456
2457 { ngx_string("nchan_subscriber_compound_etag_message_id"),
2458 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
2459@@ -129,6 +180,13 @@ static ngx_command_t nchan_commands[] = {
2460 offsetof(nchan_loc_conf_t, websocket_ping_interval),
2461 NULL } ,
2462
2463+ { ngx_string("nchan_websocket_client_heartbeat"),
2464+ NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE2,
2465+ nchan_websocket_heartbeat_directive,
2466+ NGX_HTTP_LOC_CONF_OFFSET,
2467+ offsetof(nchan_loc_conf_t, websocket_heartbeat),
2468+ NULL } ,
2469+
2470 { ngx_string("nchan_publisher"),
2471 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_NOARGS|NGX_CONF_TAKE1|NGX_CONF_TAKE2,
2472 nchan_publisher_directive,
2473@@ -141,6 +199,12 @@ static ngx_command_t nchan_commands[] = {
2474 NGX_HTTP_LOC_CONF_OFFSET,
2475 0,
2476 NULL } ,
2477+ { ngx_string("nchan_publisher_location"), //alt for nchan_publisher
2478+ NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_NOARGS|NGX_CONF_TAKE1|NGX_CONF_TAKE2,
2479+ nchan_publisher_directive,
2480+ NGX_HTTP_LOC_CONF_OFFSET,
2481+ 0,
2482+ NULL } ,
2483
2484 { ngx_string("nchan_subscriber_timeout"),
2485 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
2486@@ -176,6 +240,13 @@ static ngx_command_t nchan_commands[] = {
2487 offsetof(nchan_loc_conf_t, unsubscribe_request_url),
2488 NULL } ,
2489
2490+ { ngx_string("nchan_message_temp_path"),
2491+ NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
2492+ ngx_conf_set_path_slot,
2493+ NGX_HTTP_MAIN_CONF_OFFSET,
2494+ offsetof(nchan_main_conf_t, message_temp_path),
2495+ NULL } ,
2496+
2497 { ngx_string("nchan_store_messages"),
2498 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
2499 nchan_store_messages_directive,
2500@@ -189,18 +260,52 @@ static ngx_command_t nchan_commands[] = {
2501 0,
2502 NULL } ,
2503
2504- { ngx_string("nchan_max_reserved_memory"),
2505+ { ngx_string("nchan_shared_memory_size"),
2506 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
2507- ngx_conf_set_size_slot,
2508+ nchan_conf_set_size_slot,
2509 NGX_HTTP_MAIN_CONF_OFFSET,
2510 offsetof(nchan_main_conf_t, shm_size),
2511 NULL } ,
2512- { ngx_string("push_max_reserved_memory"), //legacy for nchan_max_reserved_memory
2513+ { ngx_string("push_max_reserved_memory"), //legacy for nchan_shared_memory_size
2514 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
2515- ngx_conf_set_size_slot,
2516+ nchan_conf_set_size_slot,
2517 NGX_HTTP_MAIN_CONF_OFFSET,
2518 offsetof(nchan_main_conf_t, shm_size),
2519 NULL } ,
2520+ { ngx_string("nchan_max_reserved_memory"), //legacy for nchan_shared_memory_size
2521+ NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
2522+ nchan_conf_set_size_slot,
2523+ NGX_HTTP_MAIN_CONF_OFFSET,
2524+ offsetof(nchan_main_conf_t, shm_size),
2525+ NULL } ,
2526+
2527+ { ngx_string("nchan_permessage_deflate_compression_level"),
2528+ NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
2529+ nchan_conf_deflate_compression_level_directive,
2530+ NGX_HTTP_MAIN_CONF_OFFSET,
2531+ 0,
2532+ NULL } ,
2533+
2534+ { ngx_string("nchan_permessage_deflate_compression_strategy"),
2535+ NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
2536+ nchan_conf_deflate_compression_strategy_directive,
2537+ NGX_HTTP_MAIN_CONF_OFFSET,
2538+ 0,
2539+ NULL } ,
2540+
2541+ { ngx_string("nchan_permessage_deflate_compression_window"),
2542+ NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
2543+ nchan_conf_deflate_compression_window_directive,
2544+ NGX_HTTP_MAIN_CONF_OFFSET,
2545+ 0,
2546+ NULL } ,
2547+
2548+ { ngx_string("nchan_permessage_deflate_compression_memlevel"),
2549+ NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
2550+ nchan_conf_deflate_compression_memlevel_directive,
2551+ NGX_HTTP_MAIN_CONF_OFFSET,
2552+ 0,
2553+ NULL } ,
2554
2555 { ngx_string("nchan_redis_url"),
2556 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
2557@@ -237,6 +342,20 @@ static ngx_command_t nchan_commands[] = {
2558 0,
2559 NULL } ,
2560
2561+ { ngx_string("nchan_redis_storage_mode"),
2562+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_UPS_CONF|NGX_CONF_TAKE1,
2563+ ngx_conf_set_redis_storage_mode_slot,
2564+ NGX_HTTP_LOC_CONF_OFFSET,
2565+ offsetof(nchan_loc_conf_t, redis.storage_mode),
2566+ NULL } ,
2567+
2568+ { ngx_string("nchan_redis_nostore_fastpublish"),
2569+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_UPS_CONF|NGX_CONF_TAKE1,
2570+ ngx_conf_set_flag_slot,
2571+ NGX_HTTP_LOC_CONF_OFFSET,
2572+ offsetof(nchan_loc_conf_t, redis.nostore_fastpublish),
2573+ NULL } ,
2574+
2575 { ngx_string("nchan_use_redis"),
2576 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
2577 ngx_conf_enable_redis,
2578@@ -245,12 +364,47 @@ static ngx_command_t nchan_commands[] = {
2579 NULL } ,
2580
2581 { ngx_string("nchan_redis_ping_interval"),
2582- NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
2583+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_UPS_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
2584 ngx_conf_set_sec_slot,
2585 NGX_HTTP_LOC_CONF_OFFSET,
2586 offsetof(nchan_loc_conf_t, redis.ping_interval),
2587 NULL } ,
2588
2589+ { ngx_string("nchan_redis_wait_after_connecting"),
2590+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
2591+ nchan_ignore_obsolete_setting,
2592+ NGX_HTTP_LOC_CONF_OFFSET,
2593+ 0,
2594+ NULL } ,
2595+
2596+ { ngx_string("nchan_redis_connect_timeout"),
2597+ NGX_HTTP_UPS_CONF|NGX_CONF_TAKE1,
2598+ ngx_conf_set_msec_slot,
2599+ NGX_HTTP_SRV_CONF_OFFSET,
2600+ offsetof(nchan_srv_conf_t, redis.connect_timeout),
2601+ NULL } ,
2602+
2603+ { ngx_string("nchan_redis_subscribe_weights"),
2604+ NGX_HTTP_UPS_CONF|NGX_CONF_TAKE1|NGX_CONF_TAKE2,
2605+ ngx_conf_set_redis_subscribe_weights,
2606+ NGX_HTTP_SRV_CONF_OFFSET,
2607+ 0,
2608+ NULL } ,
2609+
2610+ { ngx_string("nchan_redis_optimize_target"),
2611+ NGX_HTTP_UPS_CONF|NGX_CONF_TAKE1,
2612+ ngx_conf_set_redis_optimize_target,
2613+ NGX_HTTP_SRV_CONF_OFFSET,
2614+ 0,
2615+ NULL } ,
2616+
2617+ { ngx_string("nchan_redis_namespace"),
2618+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_UPS_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
2619+ ngx_conf_set_str_slot,
2620+ NGX_HTTP_LOC_CONF_OFFSET,
2621+ offsetof(nchan_loc_conf_t, redis.namespace),
2622+ &nchan_redis_namespace__post_handler } ,
2623+
2624 { ngx_string("nchan_redis_fakesub_timer_interval"),
2625 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
2626 ngx_conf_set_msec_slot,
2627@@ -317,25 +471,81 @@ static ngx_command_t nchan_commands[] = {
2628 NULL } ,
2629
2630 { ngx_string("nchan_access_control_allow_origin"),
2631- NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
2632- ngx_conf_set_str_slot,
2633+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
2634+ ngx_http_set_complex_value_slot,
2635 NGX_HTTP_LOC_CONF_OFFSET,
2636 offsetof(nchan_loc_conf_t, allow_origin),
2637 NULL } ,
2638
2639+ { ngx_string("nchan_access_control_allow_credentials"),
2640+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
2641+ ngx_conf_set_flag_slot,
2642+ NGX_HTTP_LOC_CONF_OFFSET,
2643+ offsetof(nchan_loc_conf_t, allow_credentials),
2644+ NULL } ,
2645+
2646 { ngx_string("nchan_channel_group"),
2647 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
2648- ngx_conf_set_str_slot,
2649+ ngx_http_set_complex_value_slot,
2650 NGX_HTTP_LOC_CONF_OFFSET,
2651 offsetof(nchan_loc_conf_t, channel_group),
2652 NULL } ,
2653 { ngx_string("push_channel_group"), //legacy for nchan_channel_group
2654 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
2655- ngx_conf_set_str_slot,
2656+ ngx_http_set_complex_value_slot,
2657 NGX_HTTP_LOC_CONF_OFFSET,
2658 offsetof(nchan_loc_conf_t, channel_group),
2659 NULL } ,
2660
2661+ { ngx_string("nchan_channel_group_accounting"),
2662+ NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
2663+ ngx_conf_set_flag_slot,
2664+ NGX_HTTP_LOC_CONF_OFFSET,
2665+ offsetof(nchan_loc_conf_t, group.enable_accounting),
2666+ NULL } ,
2667+
2668+ { ngx_string("nchan_group_location"),
2669+ NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS|NGX_CONF_TAKE1|NGX_CONF_TAKE2|NGX_CONF_TAKE3,
2670+ nchan_group_directive,
2671+ NGX_HTTP_LOC_CONF_OFFSET,
2672+ 0,
2673+ NULL } ,
2674+
2675+ { ngx_string("nchan_group_max_channels"),
2676+ NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
2677+ ngx_http_set_complex_value_slot,
2678+ NGX_HTTP_LOC_CONF_OFFSET,
2679+ offsetof(nchan_loc_conf_t, group.max_channels),
2680+ NULL } ,
2681+
2682+ { ngx_string("nchan_group_max_messages"),
2683+ NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
2684+ ngx_http_set_complex_value_slot,
2685+ NGX_HTTP_LOC_CONF_OFFSET,
2686+ offsetof(nchan_loc_conf_t, group.max_messages),
2687+ NULL } ,
2688+
2689+ { ngx_string("nchan_group_max_messages_memory"),
2690+ NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
2691+ ngx_http_set_complex_value_slot,
2692+ NGX_HTTP_LOC_CONF_OFFSET,
2693+ offsetof(nchan_loc_conf_t, group.max_messages_shm_bytes),
2694+ NULL } ,
2695+
2696+ { ngx_string("nchan_group_max_messages_disk"),
2697+ NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
2698+ ngx_http_set_complex_value_slot,
2699+ NGX_HTTP_LOC_CONF_OFFSET,
2700+ offsetof(nchan_loc_conf_t, group.max_messages_file_bytes),
2701+ NULL } ,
2702+
2703+ { ngx_string("nchan_group_max_subscribers"),
2704+ NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
2705+ ngx_http_set_complex_value_slot,
2706+ NGX_HTTP_LOC_CONF_OFFSET,
2707+ offsetof(nchan_loc_conf_t, group.max_subscribers),
2708+ NULL } ,
2709+
2710 { ngx_string("nchan_channel_events_channel_id"),
2711 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
2712 nchan_set_channel_events_channel_id,
2713@@ -403,6 +613,62 @@ static ngx_command_t nchan_commands[] = {
2714 offsetof(nchan_loc_conf_t, storage_engine),
2715 NULL } ,
2716
2717+ { ngx_string("nchan_benchmark"),
2718+ NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
2719+ nchan_benchmark_directive,
2720+ NGX_HTTP_LOC_CONF_OFFSET,
2721+ 0,
2722+ NULL } ,
2723+
2724+ { ngx_string("nchan_benchmark_time"),
2725+ NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
2726+ ngx_conf_set_sec_slot,
2727+ NGX_HTTP_LOC_CONF_OFFSET,
2728+ offsetof(nchan_loc_conf_t, benchmark.time),
2729+ NULL } ,
2730+
2731+ { ngx_string("nchan_benchmark_messages_per_channel_per_minute"),
2732+ NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
2733+ ngx_conf_set_num_slot,
2734+ NGX_HTTP_LOC_CONF_OFFSET,
2735+ offsetof(nchan_loc_conf_t, benchmark.msgs_per_minute),
2736+ NULL } ,
2737+
2738+ { ngx_string("nchan_benchmark_message_padding_bytes"),
2739+ NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
2740+ ngx_conf_set_num_slot,
2741+ NGX_HTTP_LOC_CONF_OFFSET,
2742+ offsetof(nchan_loc_conf_t, benchmark.msg_padding),
2743+ NULL } ,
2744+
2745+ { ngx_string("nchan_benchmark_channels"),
2746+ NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
2747+ ngx_conf_set_num_slot,
2748+ NGX_HTTP_LOC_CONF_OFFSET,
2749+ offsetof(nchan_loc_conf_t, benchmark.channels),
2750+ NULL } ,
2751+
2752+ { ngx_string("nchan_benchmark_subscribers_per_channel"),
2753+ NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
2754+ ngx_conf_set_num_slot,
2755+ NGX_HTTP_LOC_CONF_OFFSET,
2756+ offsetof(nchan_loc_conf_t, benchmark.subscribers_per_channel),
2757+ NULL } ,
2758+
2759+ { ngx_string("nchan_benchmark_subscriber_distribution"),
2760+ NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
2761+ nchan_benchmark_subscriber_distribution_directive,
2762+ NGX_HTTP_LOC_CONF_OFFSET,
2763+ offsetof(nchan_loc_conf_t, benchmark.subscriber_distribution),
2764+ NULL } ,
2765+
2766+ { ngx_string("nchan_benchmark_publisher_distribution"),
2767+ NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
2768+ nchan_benchmark_publisher_distribution_directive,
2769+ NGX_HTTP_LOC_CONF_OFFSET,
2770+ offsetof(nchan_loc_conf_t, benchmark.publisher_distribution),
2771+ NULL } ,
2772+
2773 { ngx_string("push_min_message_buffer_length"),
2774 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
2775 nchan_ignore_obsolete_setting,
2776diff --git a/debian/modules/nchan/src/nchan_defs.c b/debian/modules/nchan/src/nchan_defs.c
2777index c9e07e1..8dbd49c 100644
2778--- a/debian/modules/nchan/src/nchan_defs.c
2779+++ b/debian/modules/nchan/src/nchan_defs.c
2780@@ -15,6 +15,7 @@ const ngx_str_t NCHAN_HEADER_ACCESS_CONTROL_ALLOW_HEADERS = ngx_string("Access-
2781 const ngx_str_t NCHAN_HEADER_ACCESS_CONTROL_ALLOW_METHODS = ngx_string("Access-Control-Allow-Methods");
2782 const ngx_str_t NCHAN_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN = ngx_string("Access-Control-Allow-Origin");
2783 const ngx_str_t NCHAN_HEADER_ACCESS_CONTROL_EXPOSE_HEADERS = ngx_string("Access-Control-Expose-Headers");
2784+const ngx_str_t NCHAN_HEADER_ACCESS_CONTROL_ALLOW_CREDENTIALS = ngx_string("Access-Control-Allow-Credentials");
2785
2786 const ngx_str_t NCHAN_HEADER_EVENTSOURCE_EVENT = ngx_string("X-EventSource-Event");
2787
2788@@ -26,6 +27,7 @@ const ngx_str_t NCHAN_HEADER_SEC_WEBSOCKET_KEY = ngx_string("Sec-WebSocket-Key")
2789 const ngx_str_t NCHAN_HEADER_SEC_WEBSOCKET_ACCEPT = ngx_string("Sec-WebSocket-Accept");
2790 const ngx_str_t NCHAN_HEADER_SEC_WEBSOCKET_VERSION = ngx_string("Sec-WebSocket-Version");
2791 const ngx_str_t NCHAN_HEADERS_SEC_WEBSOCKET_PROTOCOL = ngx_string("Sec-WebSocket-Protocol");
2792+const ngx_str_t NCHAN_HEADER_SEC_WEBSOCKET_EXTENSIONS = ngx_string("Sec-WebSocket-Extensions");
2793
2794 //and some wobsockety values
2795 const ngx_str_t NCHAN_UPGRADE = ngx_string("Upgrade");
2796@@ -39,6 +41,7 @@ const ngx_str_t NCHAN_CACHE_CONTROL_VALUE = ngx_string("no-cache");
2797 const ngx_str_t NCHAN_HTTP_STATUS_101 = ngx_string("101 Switching Protocols");
2798 const ngx_str_t NCHAN_HTTP_STATUS_204 = ngx_string("204 No Content");
2799 const ngx_str_t NCHAN_HTTP_STATUS_304 = ngx_string("304 Not Modified");
2800+const ngx_str_t NCHAN_HTTP_STATUS_400 = ngx_string("400 Bad Request");
2801 const ngx_str_t NCHAN_HTTP_STATUS_408 = ngx_string("408 Request Timeout");
2802 const ngx_str_t NCHAN_HTTP_STATUS_409 = ngx_string("409 Conflict");
2803 const ngx_str_t NCHAN_HTTP_STATUS_410 = ngx_string("410 Gone");
2804@@ -46,10 +49,14 @@ const ngx_str_t NCHAN_HTTP_STATUS_410 = ngx_string("410 Gone");
2805 //other stuff
2806 const ngx_str_t NCHAN_SUBSCRIBER_TIMEOUT= ngx_string("Subscriber Timeout");
2807 const ngx_str_t NCHAN_ANYSTRING= ngx_string("*");
2808+const ngx_str_t NCHAN_ACCESS_CONTROL_ALLOWED_GROUP_HEADERS = ngx_string("Accept, Content-Type");
2809 const ngx_str_t NCHAN_ACCESS_CONTROL_ALLOWED_PUBLISHER_HEADERS = ngx_string("Content-Type");
2810 const ngx_str_t NCHAN_ACCESS_CONTROL_ALLOWED_SUBSCRIBER_HEADERS = ngx_string("If-None-Match, If-Modified-Since, Content-Type, Cache-Control, X-EventSource-Event");
2811+const ngx_str_t NCHAN_ACCESS_CONTROL_ALLOWED_PUBSUB_HEADERS = ngx_string("If-None-Match, If-Modified-Since, Content-Type, Cache-Control, X-EventSource-Event");
2812 const ngx_str_t NCHAN_ALLOW_GET_POST_PUT_DELETE= ngx_string("GET, POST, PUT, DELETE");
2813 const ngx_str_t NCHAN_ALLOW_GET= ngx_string("GET");
2814+const ngx_str_t NCHAN_ALLOW_GET_POST_DELETE= ngx_string("GET, POST, DELETE");
2815+
2816
2817 const ngx_str_t NCHAN_VARY_HEADER_VALUE = ngx_string("If-None-Match, If-Modified-Since");
2818 const ngx_str_t NCHAN_MSG_RESPONSE_ALLOWED_HEADERS = ngx_string("Last-Modified, Etag, Content-Type");
2819@@ -86,6 +93,6 @@ const ngx_str_t NCHAN_CHANNEL_INFO_YAML = ngx_string(
2820 "messages: %ui" CRLF
2821 "requested: %d" CRLF
2822 "subscribers: %ui" CRLF
2823- "last_message_id: %V" CRLF
2824+ "last_message_id: \"%V\"" CRLF
2825 CRLF
2826 "\0");
2827diff --git a/debian/modules/nchan/src/nchan_defs.h b/debian/modules/nchan/src/nchan_defs.h
2828index 8e1af05..0809080 100644
2829--- a/debian/modules/nchan/src/nchan_defs.h
2830+++ b/debian/modules/nchan/src/nchan_defs.h
2831@@ -1,12 +1,13 @@
2832 #ifndef NCHAN_DEFS_H
2833 #define NCHAN_DEFS_H
2834
2835-#define NCHAN_DEFAULT_SHM_SIZE 33554432 //32 megs
2836+#define NCHAN_DEFAULT_SHM_SIZE 134217728 //128 megs
2837 #define NCHAN_DEFAULT_MESSAGE_TIMEOUT 3600
2838 #define NCHAN_DEFAULT_REDIS_IDLE_CHANNEL_CACHE_TIMEOUT 30
2839 #define NCHAN_DEFAULT_SUBSCRIBER_TIMEOUT 0 //default: never timeout
2840+#define NCHAN_DEFAULT_REDIS_NODE_CONNECT_TIMEOUT_MSEC 600
2841 //(liucougar: this is a bit confusing, but it is what's the default behavior before this option is introducecd)
2842-#define NCHAN_DEFAULT_WEBSOCKET_PING_INTERVAL 0
2843+#define NCHAN_DEFAULT_SUBSCRIBER_PING_INTERVAL 0
2844
2845 #define NCHAN_DEFAULT_CHANNEL_TIMEOUT 5 //default: timeout in 5 seconds
2846
2847@@ -62,6 +63,7 @@ extern const ngx_str_t NCHAN_HEADER_ACCESS_CONTROL_ALLOW_HEADERS;
2848 extern const ngx_str_t NCHAN_HEADER_ACCESS_CONTROL_ALLOW_METHODS;
2849 extern const ngx_str_t NCHAN_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN;
2850 extern const ngx_str_t NCHAN_HEADER_ACCESS_CONTROL_EXPOSE_HEADERS;
2851+extern const ngx_str_t NCHAN_HEADER_ACCESS_CONTROL_ALLOW_CREDENTIALS;
2852
2853 extern const ngx_str_t NCHAN_HEADER_EVENTSOURCE_EVENT;
2854
2855@@ -71,6 +73,7 @@ extern const ngx_str_t NCHAN_HEADER_SEC_WEBSOCKET_KEY;
2856 extern const ngx_str_t NCHAN_HEADER_SEC_WEBSOCKET_ACCEPT;
2857 extern const ngx_str_t NCHAN_HEADER_SEC_WEBSOCKET_VERSION;
2858 extern const ngx_str_t NCHAN_HEADERS_SEC_WEBSOCKET_PROTOCOL;
2859+extern const ngx_str_t NCHAN_HEADER_SEC_WEBSOCKET_EXTENSIONS;
2860
2861 //header values
2862 extern const ngx_str_t NCHAN_CACHE_CONTROL_VALUE;
2863@@ -79,6 +82,7 @@ extern const ngx_str_t NCHAN_CACHE_CONTROL_VALUE;
2864 extern const ngx_str_t NCHAN_HTTP_STATUS_101;
2865 extern const ngx_str_t NCHAN_HTTP_STATUS_204;
2866 extern const ngx_str_t NCHAN_HTTP_STATUS_304;
2867+extern const ngx_str_t NCHAN_HTTP_STATUS_400;
2868 extern const ngx_str_t NCHAN_HTTP_STATUS_408;
2869 extern const ngx_str_t NCHAN_HTTP_STATUS_409;
2870 extern const ngx_str_t NCHAN_HTTP_STATUS_410;
2871@@ -91,7 +95,10 @@ extern const ngx_str_t NCHAN_WEBSOCKET;
2872 extern const ngx_str_t NCHAN_ANYSTRING;
2873 extern const ngx_str_t NCHAN_ACCESS_CONTROL_ALLOWED_PUBLISHER_HEADERS;
2874 extern const ngx_str_t NCHAN_ACCESS_CONTROL_ALLOWED_SUBSCRIBER_HEADERS;
2875+extern const ngx_str_t NCHAN_ACCESS_CONTROL_ALLOWED_PUBSUB_HEADERS;
2876+extern const ngx_str_t NCHAN_ACCESS_CONTROL_ALLOWED_GROUP_HEADERS;
2877 extern const ngx_str_t NCHAN_ALLOW_GET_POST_PUT_DELETE;
2878+extern const ngx_str_t NCHAN_ALLOW_GET_POST_DELETE;
2879 extern const ngx_str_t NCHAN_ALLOW_GET;
2880 extern const ngx_str_t NCHAN_VARY_HEADER_VALUE;
2881 extern const ngx_str_t NCHAN_MSG_RESPONSE_ALLOWED_HEADERS;
2882diff --git a/debian/modules/nchan/src/nchan_module.c b/debian/modules/nchan/src/nchan_module.c
2883old mode 100755
2884new mode 100644
2885index ff97852..4555c5d
2886--- a/debian/modules/nchan/src/nchan_module.c
2887+++ b/debian/modules/nchan/src/nchan_module.c
2888@@ -13,11 +13,17 @@
2889 #include <subscribers/http-multipart-mixed.h>
2890 #include <subscribers/http-raw-stream.h>
2891 #include <subscribers/websocket.h>
2892+#include <subscribers/benchmark.h>
2893 #include <store/memory/store.h>
2894 #include <store/redis/store.h>
2895
2896 #include <nchan_setup.c>
2897
2898+#if (NGX_ZLIB)
2899+#include <zlib.h>
2900+#endif
2901+
2902+
2903 #if FAKESHARD
2904 #include <store/memory/ipc.h>
2905 #include <store/memory/shmem.h>
2906@@ -30,6 +36,9 @@ ngx_int_t nchan_worker_processes;
2907 int nchan_stub_status_enabled = 0;
2908
2909
2910+static void nchan_publisher_body_handler(ngx_http_request_t *r);
2911+static void nchan_publisher_unavailable_body_handler(ngx_http_request_t *r);
2912+
2913 //#define DEBUG_LEVEL NGX_LOG_WARN
2914 //#define DEBUG_LEVEL NGX_LOG_DEBUG
2915
2916@@ -55,8 +64,6 @@ ngx_int_t nchan_maybe_send_channel_event_message(ngx_http_request_t *r, channel_
2917 static ngx_str_t evt_chan_publish= ngx_string("channel_publish");
2918 static ngx_str_t evt_chan_delete = ngx_string("channel_delete");
2919
2920- struct timeval tv;
2921-
2922 nchan_loc_conf_t *cf = ngx_http_get_module_loc_conf(r, ngx_nchan_module);
2923 ngx_http_complex_value_t *cv = cf->channel_events_channel_id;
2924 if(cv==NULL) {
2925@@ -70,7 +77,6 @@ ngx_int_t nchan_maybe_send_channel_event_message(ngx_http_request_t *r, channel_
2926 ngx_str_t *id;
2927 u_char *cur;
2928 ngx_str_t evstr;
2929- ngx_buf_t buf;
2930 nchan_msg_t msg;
2931
2932 switch(event_type) {
2933@@ -113,21 +119,22 @@ ngx_int_t nchan_maybe_send_channel_event_message(ngx_http_request_t *r, channel_
2934
2935 //the event message
2936 ngx_http_complex_value(r, cf->channel_event_string, &evstr);
2937- ngx_memzero(&buf, sizeof(buf)); //do we really need this?...
2938- buf.temporary = 1;
2939- buf.memory = 1;
2940- buf.last_buf = 1;
2941- buf.pos = evstr.data;
2942- buf.last = evstr.data + evstr.len;
2943- buf.start = buf.pos;
2944- buf.end = buf.last;
2945
2946 ngx_memzero(&msg, sizeof(msg));
2947- ngx_gettimeofday(&tv);
2948- msg.id.time = tv.tv_sec;
2949- msg.id.tagcount = 1;
2950- msg.buf = &buf;
2951
2952+ msg.buf.temporary = 1;
2953+ msg.buf.memory = 1;
2954+ msg.buf.last_buf = 1;
2955+ msg.buf.pos = evstr.data;
2956+ msg.buf.last = evstr.data + evstr.len;
2957+ msg.buf.start = msg.buf.pos;
2958+ msg.buf.end = msg.buf.last;
2959+
2960+ msg.id.time = 0;
2961+ msg.id.tag.fixed[0] = 0;
2962+ msg.id.tagactive = 0;
2963+ msg.id.tagcount = 1;
2964+ msg.storage = NCHAN_MSG_STACK;
2965
2966 if(evcf == NULL) {
2967 evcf = &evcf_data;
2968@@ -201,9 +208,7 @@ ngx_int_t nchan_loc_conf_max_messages(nchan_loc_conf_t *cf) {
2969 return num;
2970 }
2971
2972-static void nchan_publisher_body_handler(ngx_http_request_t *r);
2973-
2974-static ngx_int_t nchan_http_publisher_handler(ngx_http_request_t * r) {
2975+static ngx_int_t nchan_http_publisher_handler(ngx_http_request_t * r, void (*body_handler)(ngx_http_request_t *r)) {
2976 ngx_int_t rc;
2977 nchan_request_ctx_t *ctx = ngx_http_get_module_ctx(r, ngx_nchan_module);
2978
2979@@ -211,7 +216,7 @@ static ngx_int_t nchan_http_publisher_handler(ngx_http_request_t * r) {
2980
2981 if(ctx) ctx->publisher_type = &publisher_name;
2982
2983- /* Instruct ngx_http_read_subscriber_request_body to store the request
2984+ /* Instruct ngx_http_read_client_request_body to store the request
2985 body entirely in a memory buffer or in a file */
2986 r->request_body_in_single_buf = 1;
2987 r->request_body_in_persistent_file = 1;
2988@@ -221,7 +226,7 @@ static ngx_int_t nchan_http_publisher_handler(ngx_http_request_t * r) {
2989 //don't buffer the request body --send it right on through
2990 //r->request_body_no_buffering = 1;
2991
2992- rc = ngx_http_read_client_request_body(r, nchan_publisher_body_handler);
2993+ rc = ngx_http_read_client_request_body(r, body_handler);
2994 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
2995 return rc;
2996 }
2997@@ -233,11 +238,14 @@ ngx_int_t nchan_stub_status_handler(ngx_http_request_t *r) {
2998 ngx_chain_t out;
2999 nchan_stub_status_t *stats;
3000
3001- float shmem_used;
3002+ nchan_main_conf_t *mcf = ngx_http_get_module_main_conf(r, ngx_nchan_module);
3003+
3004+ float shmem_used, shmem_max;
3005
3006 char *buf_fmt = "total published messages: %ui\n"
3007 "stored messages: %ui\n"
3008 "shared memory used: %fK\n"
3009+ "shared memory limit: %fK\n"
3010 "channels: %ui\n"
3011 "subscribers: %ui\n"
3012 "redis pending commands: %ui\n"
3013@@ -246,7 +254,8 @@ ngx_int_t nchan_stub_status_handler(ngx_http_request_t *r) {
3014 "interprocess alerts in transit: %ui\n"
3015 "interprocess queued alerts: %ui\n"
3016 "total interprocess send delay: %ui\n"
3017- "total interprocess receive delay: %ui\n";
3018+ "total interprocess receive delay: %ui\n"
3019+ "nchan version: %s\n";
3020
3021 if ((b = ngx_pcalloc(r->pool, sizeof(*b) + 800)) == NULL) {
3022 nchan_log_request_error(r, "Failed to allocate response buffer for nchan_stub_status.");
3023@@ -254,13 +263,14 @@ ngx_int_t nchan_stub_status_handler(ngx_http_request_t *r) {
3024 }
3025
3026 shmem_used = (float )((float )nchan_get_used_shmem() / 1024.0);
3027+ shmem_max = (float )((float )mcf->shm_size / 1024.0);
3028
3029 stats = nchan_get_stub_status_stats();
3030
3031 b->start = (u_char *)&b[1];
3032 b->pos = b->start;
3033
3034- b->end = ngx_snprintf(b->start, 800, buf_fmt, stats->total_published_messages, stats->messages, shmem_used, stats->channels, stats->subscribers, stats->redis_pending_commands, stats->redis_connected_servers, stats->ipc_total_alerts_received, stats->ipc_total_alerts_sent - stats->ipc_total_alerts_received, stats->ipc_queue_size, stats->ipc_total_send_delay, stats->ipc_total_receive_delay);
3035+ b->end = ngx_snprintf(b->start, 800, buf_fmt, stats->total_published_messages, stats->messages, shmem_used, shmem_max, stats->channels, stats->subscribers, stats->redis_pending_commands, stats->redis_connected_servers, stats->ipc_total_alerts_received, stats->ipc_total_alerts_sent - stats->ipc_total_alerts_received, stats->ipc_queue_size, stats->ipc_total_send_delay, stats->ipc_total_receive_delay, NCHAN_VERSION);
3036 b->last = b->end;
3037
3038 b->memory = 1;
3039@@ -336,6 +346,133 @@ int nchan_parse_message_buffer_config(ngx_http_request_t *r, nchan_loc_conf_t *c
3040 return 1;
3041 }
3042
3043+static ngx_int_t group_handler_callback(ngx_int_t status, nchan_group_t *group, ngx_http_request_t *r) {
3044+ nchan_request_ctx_t *ctx = ngx_http_get_module_ctx(r, ngx_nchan_module);
3045+
3046+ if(!group) {
3047+ group = ngx_pcalloc(r->pool, sizeof(*group));
3048+ }
3049+
3050+ if(!ctx->request_ran_content_handler) {
3051+ r->main->count--;
3052+ nchan_group_info(r, group);
3053+ }
3054+ else {
3055+ nchan_http_finalize_request(r, nchan_group_info(r, group));
3056+ }
3057+
3058+
3059+
3060+ return NGX_OK;
3061+}
3062+
3063+
3064+static ngx_int_t parse_size_limit(u_char *data, size_t len) {
3065+ ngx_str_t str;
3066+ str.data = data;
3067+ str.len = len;
3068+ return nchan_parse_size(&str);
3069+}
3070+
3071+static ngx_int_t set_group_num_limit(ngx_http_request_t *r, ngx_http_complex_value_t *cv, ngx_atomic_int_t *dst, ngx_int_t (*parsefunc)(u_char *, size_t), char *errstr) {
3072+ ngx_str_t tmp;
3073+ ngx_int_t num;
3074+ if(cv) {
3075+ ngx_http_complex_value(r, cv, &tmp);
3076+ if(tmp.len == 0) {
3077+ *dst = -1;
3078+ return 1;
3079+ }
3080+ else if((num = parsefunc(tmp.data, tmp.len)) == NGX_ERROR || num < 0) {
3081+ nchan_respond_cstring(r, NGX_HTTP_FORBIDDEN, &NCHAN_CONTENT_TYPE_TEXT_PLAIN, errstr, 0);
3082+ return 0;
3083+ }
3084+ *dst = num;
3085+ }
3086+ else {
3087+ *dst = -1;
3088+ }
3089+ return 1;
3090+}
3091+
3092+static ngx_int_t parse_group_limits(ngx_http_request_t *r, nchan_loc_conf_t *cf, nchan_group_limits_t *limits) {
3093+ set_group_num_limit(r, cf->group.max_channels, &limits->channels, ngx_atoi, "invalid nchan_group_max_channels value");
3094+ set_group_num_limit(r, cf->group.max_subscribers, &limits->subscribers, ngx_atoi, "invalid nchan_group_max_subscribers value");
3095+ set_group_num_limit(r, cf->group.max_messages, &limits->messages, ngx_atoi, "invalid nchan_group_max_messages value");
3096+ set_group_num_limit(r, cf->group.max_messages_shm_bytes, &limits->messages_shmem_bytes, parse_size_limit, "invalid nchan_group_max_messages_memory value");
3097+ set_group_num_limit(r, cf->group.max_messages_file_bytes, &limits->messages_file_bytes, parse_size_limit, "invalid nchan_group_max_messages_disk value");
3098+
3099+ return r->headers_out.status != NGX_HTTP_FORBIDDEN ? NGX_OK : NGX_ERROR;
3100+}
3101+
3102+ngx_int_t nchan_group_handler(ngx_http_request_t *r) {
3103+ nchan_loc_conf_t *cf = ngx_http_get_module_loc_conf(r, ngx_nchan_module);
3104+ nchan_request_ctx_t *ctx;
3105+ ngx_int_t rc = NGX_DONE;
3106+ ngx_str_t *group;
3107+
3108+ if((ctx = ngx_pcalloc(r->pool, sizeof(nchan_request_ctx_t))) == NULL) {
3109+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
3110+ }
3111+ ngx_http_set_ctx(r, ctx, ngx_nchan_module);
3112+
3113+ if(r->connection && (r->connection->read->eof || r->connection->read->pending_eof)) {
3114+ ngx_http_finalize_request(r, NGX_HTTP_CLIENT_CLOSED_REQUEST);
3115+ return NGX_ERROR;
3116+ }
3117+
3118+ if(!cf->group.enable_accounting) {
3119+ nchan_respond_cstring(r, NGX_HTTP_FORBIDDEN, &NCHAN_CONTENT_TYPE_TEXT_PLAIN, "Channel group accounting is disabled.", 0);
3120+ return NGX_OK;
3121+ }
3122+
3123+ group = nchan_get_group_name(r, cf, ctx);
3124+ if(group == NULL) {
3125+ nchan_respond_cstring(r, NGX_HTTP_BAD_REQUEST, &NCHAN_CONTENT_TYPE_TEXT_PLAIN, "No group specified", 0);
3126+ return NGX_OK;
3127+ }
3128+
3129+ switch(r->method) {
3130+ case NGX_HTTP_GET:
3131+ if(!cf->group.get) {
3132+ rc = nchan_respond_status(r, NGX_HTTP_FORBIDDEN, NULL, NULL, 0);
3133+ }
3134+ r->main->count++;
3135+ cf->storage_engine->get_group(group, cf, (callback_pt )group_handler_callback, r);
3136+
3137+ break;
3138+
3139+ case NGX_HTTP_POST:
3140+ if(!cf->group.set) {
3141+ rc = nchan_respond_status(r, NGX_HTTP_FORBIDDEN, NULL, NULL, 0);
3142+ }
3143+
3144+ nchan_group_limits_t limits;
3145+
3146+ if(parse_group_limits(r, cf, &limits) != NGX_OK) {
3147+ return NGX_OK;
3148+ }
3149+
3150+ r->main->count++;
3151+ cf->storage_engine->set_group_limits(group, cf, &limits, (callback_pt )group_handler_callback, r);
3152+ break;
3153+
3154+ case NGX_HTTP_DELETE:
3155+ if(!cf->group.delete) {
3156+ rc = nchan_respond_status(r, NGX_HTTP_FORBIDDEN, NULL, NULL, 0);
3157+ }
3158+ r->main->count++;
3159+ cf->storage_engine->delete_group(group, cf, (callback_pt )group_handler_callback, r);
3160+ break;
3161+
3162+ case NGX_HTTP_OPTIONS:
3163+ rc= nchan_OPTIONS_respond(r, &NCHAN_ACCESS_CONTROL_ALLOWED_GROUP_HEADERS, &NCHAN_ALLOW_GET_POST_DELETE);
3164+ break;
3165+ }
3166+ ctx->request_ran_content_handler = 1;
3167+ return rc;
3168+}
3169+
3170 ngx_int_t nchan_pubsub_handler(ngx_http_request_t *r) {
3171 nchan_loc_conf_t *cf = ngx_http_get_module_loc_conf(r, ngx_nchan_module);
3172 ngx_str_t *channel_id;
3173@@ -343,47 +480,55 @@ ngx_int_t nchan_pubsub_handler(ngx_http_request_t *r) {
3174 nchan_msg_id_t *msg_id;
3175 ngx_int_t rc = NGX_DONE;
3176 nchan_request_ctx_t *ctx;
3177- ngx_str_t *origin_header;
3178+ nchan_group_limits_t group_limits;
3179
3180-#if NCHAN_BENCHMARK
3181- struct timeval tv;
3182- ngx_gettimeofday(&tv);
3183-#endif
3184+ if(r->connection && (r->connection->read->eof || r->connection->read->pending_eof)) {
3185+ ngx_http_finalize_request(r, NGX_HTTP_CLIENT_CLOSED_REQUEST);
3186+ return NGX_ERROR;
3187+ }
3188
3189 if((ctx = ngx_pcalloc(r->pool, sizeof(nchan_request_ctx_t))) == NULL) {
3190 return NGX_HTTP_INTERNAL_SERVER_ERROR;
3191 }
3192 ngx_http_set_ctx(r, ctx, ngx_nchan_module);
3193
3194-#if NCHAN_BENCHMARK
3195- ctx->start_tv = tv;
3196-#endif
3197-
3198 //X-Accel-Redirected requests get their method mangled to GET. De-mangle it if necessary
3199 if(r->upstream && r->upstream->headers_in.x_accel_redirect) {
3200 //yep, we got x-accel-redirected. what was the original method?...
3201 nchan_recover_x_accel_redirected_request_method(r);
3202 }
3203
3204- if((origin_header = nchan_get_header_value(r, NCHAN_HEADER_ORIGIN)) != NULL) {
3205- ctx->request_origin_header = *origin_header;
3206- if(!(cf->allow_origin.len == 1 && cf->allow_origin.data[0] == '*')) {
3207- if(!(origin_header->len == cf->allow_origin.len && ngx_strnstr(origin_header->data, (char *)cf->allow_origin.data, origin_header->len) != NULL)) {
3208- //CORS origin match failed! return a 403 forbidden
3209- goto forbidden;
3210- }
3211- }
3212+ if(!nchan_match_origin_header(r, cf, ctx)) {
3213+ goto forbidden;
3214+ }
3215+
3216+ if((msg_id = nchan_subscriber_get_msg_id(r)) == NULL) {
3217+ goto bad_msgid;
3218+ }
3219+
3220+ if(parse_group_limits(r, cf, &group_limits) == NGX_OK) {
3221+ // unless the group already exists, these limits may only be set after this incoming request.
3222+ // TODO: fix this, although that will lead to even gnarlier control flow.
3223+ cf->storage_engine->set_group_limits(nchan_get_group_name(r, cf, ctx), cf, &group_limits, NULL, NULL);
3224 }
3225 else {
3226- ctx->request_origin_header.len=0;
3227- ctx->request_origin_header.data=NULL;
3228+ // there waas an error parsing group limit strings, and it has already been sent in the response.
3229+ // just quit.
3230+ return NGX_OK;
3231 }
3232
3233- if((channel_id = nchan_get_channel_id(r, SUB, 1)) == NULL) {
3234- //just get the subscriber_channel_id for now. the publisher one is handled elsewhere
3235- return r->headers_out.status ? NGX_OK : NGX_HTTP_INTERNAL_SERVER_ERROR;
3236+ if(cf->redis.enabled && !nchan_store_redis_ready(cf)) {
3237+ //using redis, and it's not ready yet
3238+ if(r->method == NGX_HTTP_POST || r->method == NGX_HTTP_PUT) {
3239+ //discard request body before responding
3240+ nchan_http_publisher_handler(r, nchan_publisher_unavailable_body_handler);
3241+ }
3242+ else {
3243+ nchan_respond_status(r, NGX_HTTP_SERVICE_UNAVAILABLE, NULL, NULL, 0);
3244+ }
3245+ return NGX_OK;
3246 }
3247-
3248+
3249 if(cf->pub.websocket || cf->pub.http) {
3250 char *err;
3251 if(!nchan_parse_message_buffer_config(r, cf, &err)) {
3252@@ -392,7 +537,7 @@ ngx_int_t nchan_pubsub_handler(ngx_http_request_t *r) {
3253 return NGX_OK;
3254 }
3255 else {
3256- nchan_respond_status(r, NGX_HTTP_INTERNAL_SERVER_ERROR, NULL, 0);
3257+ nchan_respond_status(r, NGX_HTTP_INTERNAL_SERVER_ERROR, NULL, NULL, 0);
3258 return NGX_OK;
3259 }
3260 }
3261@@ -402,6 +547,10 @@ ngx_int_t nchan_pubsub_handler(ngx_http_request_t *r) {
3262 //want websocket?
3263 if(cf->sub.websocket) {
3264 //we prefer to subscribe
3265+ if((channel_id = nchan_get_channel_id(r, SUB, 1)) == NULL) {
3266+ return r->headers_out.status ? NGX_OK : NGX_HTTP_INTERNAL_SERVER_ERROR;
3267+ }
3268+
3269 #if FAKESHARD
3270 memstore_sub_debug_start();
3271 #endif
3272@@ -412,7 +561,9 @@ ngx_int_t nchan_pubsub_handler(ngx_http_request_t *r) {
3273 nchan_log_request_error(r, "unable to create websocket subscriber");
3274 return NGX_HTTP_INTERNAL_SERVER_ERROR;
3275 }
3276- sub->fn->subscribe(sub, channel_id);
3277+ if(sub->fn->subscribe(sub, channel_id) != NGX_OK) {
3278+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
3279+ }
3280 #if FAKESHARD
3281 memstore_sub_debug_end();
3282 #endif
3283@@ -448,25 +599,31 @@ ngx_int_t nchan_pubsub_handler(ngx_http_request_t *r) {
3284 sub_create = longpoll_subscriber_create;
3285 }
3286 else if(cf->pub.http) {
3287- nchan_http_publisher_handler(r);
3288+ nchan_http_publisher_handler(r, nchan_publisher_body_handler);
3289 }
3290 else {
3291 goto forbidden;
3292 }
3293
3294 if(sub_create) {
3295+ if((channel_id = nchan_get_channel_id(r, SUB, 1)) == NULL) {
3296+ return r->headers_out.status ? NGX_OK : NGX_HTTP_INTERNAL_SERVER_ERROR;
3297+ }
3298 #if FAKESHARD
3299 memstore_sub_debug_start();
3300 #endif
3301 if((msg_id = nchan_subscriber_get_msg_id(r)) == NULL) {
3302 goto bad_msgid;
3303 }
3304+
3305 if((sub = sub_create(r, msg_id)) == NULL) {
3306 nchan_log_request_error(r, "unable to create subscriber");
3307 return NGX_HTTP_INTERNAL_SERVER_ERROR;
3308 }
3309
3310- sub->fn->subscribe(sub, channel_id);
3311+ if(sub->fn->subscribe(sub, channel_id) != NGX_OK) {
3312+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
3313+ }
3314 #if FAKESHARD
3315 memstore_sub_debug_end();
3316 #endif
3317@@ -477,24 +634,27 @@ ngx_int_t nchan_pubsub_handler(ngx_http_request_t *r) {
3318 case NGX_HTTP_POST:
3319 case NGX_HTTP_PUT:
3320 if(cf->pub.http) {
3321- nchan_http_publisher_handler(r);
3322+ nchan_http_publisher_handler(r, nchan_publisher_body_handler);
3323 }
3324 else goto forbidden;
3325 break;
3326
3327 case NGX_HTTP_DELETE:
3328 if(cf->pub.http) {
3329- nchan_http_publisher_handler(r);
3330+ nchan_http_publisher_handler(r, nchan_publisher_body_handler);
3331 }
3332 else goto forbidden;
3333 break;
3334
3335 case NGX_HTTP_OPTIONS:
3336- if(cf->pub.http) {
3337- nchan_OPTIONS_respond(r, &cf->allow_origin, &NCHAN_ACCESS_CONTROL_ALLOWED_PUBLISHER_HEADERS, &NCHAN_ALLOW_GET_POST_PUT_DELETE);
3338+ if(cf->pub.http && (cf->sub.poll || cf->sub.longpoll || cf->sub.eventsource || cf->sub.websocket)) {
3339+ nchan_OPTIONS_respond(r, &NCHAN_ACCESS_CONTROL_ALLOWED_PUBSUB_HEADERS, &NCHAN_ALLOW_GET_POST_PUT_DELETE);
3340+ }
3341+ else if(cf->pub.http) {
3342+ nchan_OPTIONS_respond(r, &NCHAN_ACCESS_CONTROL_ALLOWED_PUBLISHER_HEADERS, &NCHAN_ALLOW_GET_POST_PUT_DELETE);
3343 }
3344 else if(cf->sub.poll || cf->sub.longpoll || cf->sub.eventsource || cf->sub.websocket) {
3345- nchan_OPTIONS_respond(r, &cf->allow_origin, &NCHAN_ACCESS_CONTROL_ALLOWED_SUBSCRIBER_HEADERS, &NCHAN_ALLOW_GET);
3346+ nchan_OPTIONS_respond(r, &NCHAN_ACCESS_CONTROL_ALLOWED_SUBSCRIBER_HEADERS, &NCHAN_ALLOW_GET);
3347 }
3348 else goto forbidden;
3349 break;
3350@@ -504,7 +664,7 @@ ngx_int_t nchan_pubsub_handler(ngx_http_request_t *r) {
3351 return rc;
3352
3353 forbidden:
3354- nchan_respond_status(r, NGX_HTTP_FORBIDDEN, NULL, 0);
3355+ nchan_respond_status(r, NGX_HTTP_FORBIDDEN, NULL, NULL, 0);
3356 ctx->request_ran_content_handler = 1;
3357 return NGX_OK;
3358
3359@@ -520,7 +680,12 @@ static ngx_int_t channel_info_callback(ngx_int_t status, void *rptr, void *pd) {
3360 if(r == NULL) {
3361 return NGX_ERROR;
3362 }
3363- nchan_http_finalize_request(r, nchan_response_channel_ptr_info( (nchan_channel_t *)rptr, r, 0));
3364+ if(status>=500 && status <= 599) {
3365+ nchan_http_finalize_request(r, status);
3366+ }
3367+ else {
3368+ nchan_http_finalize_request(r, nchan_response_channel_ptr_info( (nchan_channel_t *)rptr, r, 0));
3369+ }
3370 return NGX_OK;
3371 }
3372
3373@@ -567,9 +732,10 @@ static ngx_http_request_t *nchan_get_safe_request_ptr(safe_request_ptr_t *d) {
3374 }
3375
3376
3377-static ngx_int_t publish_callback(ngx_int_t status, nchan_channel_t *ch, safe_request_ptr_t *pd) {
3378+static ngx_int_t publish_callback(ngx_int_t status, void *data, safe_request_ptr_t *pd) {
3379 nchan_request_ctx_t *ctx;
3380 static nchan_msg_id_t empty_msgid = NCHAN_ZERO_MSGID;
3381+ nchan_channel_t *ch = data;
3382
3383 ngx_http_request_t *r = nchan_get_safe_request_ptr(pd);
3384
3385@@ -599,19 +765,34 @@ static ngx_int_t publish_callback(ngx_int_t status, nchan_channel_t *ch, safe_re
3386 return NGX_OK;
3387
3388 case NGX_ERROR:
3389+ status = NGX_HTTP_INTERNAL_SERVER_ERROR;
3390+ /*fallthrough*/
3391+ case NGX_HTTP_INSUFFICIENT_STORAGE:
3392 case NGX_HTTP_INTERNAL_SERVER_ERROR:
3393+ case NGX_HTTP_SERVICE_UNAVAILABLE:
3394 //WTF?
3395- nchan_log_request_error(r, "error publishing message");
3396- ctx->prev_msg_id = empty_msgid;;
3397+ nchan_log_request_error(r, "error publishing message (HTTP status code %i)", status);
3398+ ctx->prev_msg_id = empty_msgid;
3399 ctx->msg_id = empty_msgid;
3400- nchan_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
3401+ nchan_http_finalize_request(r, status);
3402 return NGX_ERROR;
3403
3404+ case NGX_HTTP_FORBIDDEN:
3405+ ctx->prev_msg_id = empty_msgid;
3406+ ctx->msg_id = empty_msgid;
3407+ if(data) {
3408+ nchan_respond_cstring(r, NGX_HTTP_FORBIDDEN, &NCHAN_CONTENT_TYPE_TEXT_PLAIN, (char *)data, 1);
3409+ }
3410+ else {
3411+ nchan_http_finalize_request(r, NGX_HTTP_FORBIDDEN);
3412+ }
3413+ return NGX_OK;
3414+
3415 default:
3416 //for debugging, mostly. I don't expect this branch to behit during regular operation
3417 ctx->prev_msg_id = empty_msgid;;
3418 ctx->msg_id = empty_msgid;
3419- nchan_log_request_error(r, "TOTALLY UNEXPECTED error publishing message, status code %i", status);
3420+ nchan_log_request_error(r, "TOTALLY UNEXPECTED error publishing message (HTTP status code %i)", status);
3421 nchan_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
3422 return NGX_ERROR;
3423 }
3424@@ -619,7 +800,6 @@ static ngx_int_t publish_callback(ngx_int_t status, nchan_channel_t *ch, safe_re
3425
3426 static void nchan_publisher_post_request(ngx_http_request_t *r, ngx_str_t *content_type, size_t content_length, ngx_chain_t *request_body_chain, ngx_str_t *channel_id, nchan_loc_conf_t *cf) {
3427 ngx_buf_t *buf;
3428- struct timeval tv;
3429 nchan_msg_t *msg;
3430 ngx_str_t *eventsource_event;
3431
3432@@ -633,19 +813,19 @@ static void nchan_publisher_post_request(ngx_http_request_t *r, ngx_str_t *conte
3433 nchan_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
3434 return;
3435 }
3436- msg->shared = 0;
3437+ msg->storage = NCHAN_MSG_POOL;
3438
3439
3440 if(cf->eventsource_event.len > 0) {
3441- msg->eventsource_event = cf->eventsource_event;
3442+ msg->eventsource_event = &cf->eventsource_event;
3443 }
3444 else if((eventsource_event = nchan_get_header_value(r, NCHAN_HEADER_EVENTSOURCE_EVENT)) != NULL) {
3445- msg->eventsource_event = *eventsource_event;
3446+ msg->eventsource_event = eventsource_event;
3447 }
3448
3449 //content type
3450 if(content_type) {
3451- msg->content_type = *content_type;
3452+ msg->content_type = content_type;
3453 }
3454
3455 if(content_length == 0) {
3456@@ -660,22 +840,16 @@ static void nchan_publisher_post_request(ngx_http_request_t *r, ngx_str_t *conte
3457 return;
3458 }
3459
3460- ngx_gettimeofday(&tv);
3461- msg->id.time = tv.tv_sec;
3462+ msg->id.time = 0;
3463 msg->id.tag.fixed[0] = 0;
3464 msg->id.tagactive = 0;
3465 msg->id.tagcount = 1;
3466
3467- msg->buf = buf;
3468+ msg->buf = *buf;
3469 #if NCHAN_MSG_LEAK_DEBUG
3470 msg->lbl = r->uri;
3471 #endif
3472-#if NCHAN_BENCHMARK
3473- nchan_request_ctx_t *ctx = ngx_http_get_module_ctx(r, ngx_nchan_module);
3474- msg->start_tv = ctx->start_tv;
3475-#endif
3476-
3477-
3478+ nchan_deflate_message_if_needed(msg, cf, r, r->pool);
3479 if((pd = nchan_set_safe_request_ptr(r)) == NULL) {
3480 return;
3481 }
3482@@ -715,7 +889,11 @@ static ngx_int_t nchan_publisher_upstream_handler(ngx_http_request_t *sr, void *
3483 if(sr->upstream) {
3484 content_type = (sr->upstream->headers_in.content_type ? &sr->upstream->headers_in.content_type->value : NULL);
3485 content_length = nchan_subrequest_content_length(sr);
3486+#if nginx_version >= 1013010
3487+ request_chain = sr->out;
3488+#else
3489 request_chain = sr->upstream->out_bufs;
3490+#endif
3491 }
3492 else {
3493 content_type = NULL;
3494@@ -793,7 +971,8 @@ static void nchan_publisher_body_handler_continued(ngx_http_request_t *r, ngx_st
3495 psrd->ch_id = channel_id;
3496
3497 ngx_http_subrequest(r, &publisher_upstream_request_url, NULL, &sr, psr, NGX_HTTP_SUBREQUEST_IN_MEMORY);
3498- nchan_adjust_subrequest(sr, NGX_HTTP_POST, &POST_REQUEST_STRING, r->request_body, r->headers_in.content_length_n, NULL);
3499+ nchan_adjust_subrequest(sr, NGX_HTTP_POST, &POST_REQUEST_STRING, r->request_body, r->headers_in.content_length_n);
3500+ sr->args = r->args;
3501 }
3502 break;
3503
3504@@ -806,7 +985,7 @@ static void nchan_publisher_body_handler_continued(ngx_http_request_t *r, ngx_st
3505 break;
3506
3507 default:
3508- nchan_respond_status(r, NGX_HTTP_FORBIDDEN, NULL, 0);
3509+ nchan_respond_status(r, NGX_HTTP_FORBIDDEN, NULL, NULL, 0);
3510 }
3511
3512 }
3513@@ -841,6 +1020,11 @@ static ngx_int_t nchan_publisher_body_authorize_handler(ngx_http_request_t *r, v
3514 return NGX_OK;
3515 }
3516
3517+static void nchan_publisher_unavailable_body_handler(ngx_http_request_t *r) {
3518+ nchan_http_finalize_request(r, NGX_HTTP_SERVICE_UNAVAILABLE);
3519+ return;
3520+}
3521+
3522 static void nchan_publisher_body_handler(ngx_http_request_t *r) {
3523 ngx_str_t *channel_id;
3524 nchan_loc_conf_t *cf = ngx_http_get_module_loc_conf(r, ngx_nchan_module);
3525@@ -902,72 +1086,22 @@ static void nchan_publisher_body_handler(ngx_http_request_t *r) {
3526 }
3527 }
3528
3529-#if NCHAN_BENCHMARK
3530-int nchan_timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y) {
3531- /* Perform the carry for the later subtraction by updating y. */
3532- if (x->tv_usec < y->tv_usec) {
3533- int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
3534- y->tv_usec -= 1000000 * nsec;
3535- y->tv_sec += nsec;
3536- }
3537- if (x->tv_usec - y->tv_usec > 1000000) {
3538- int nsec = (x->tv_usec - y->tv_usec) / 1000000;
3539- y->tv_usec += 1000000 * nsec;
3540- y->tv_sec -= nsec;
3541- }
3542-
3543- /* Compute the time remaining to wait.
3544- tv_usec is certainly positive. */
3545- result->tv_sec = x->tv_sec - y->tv_sec;
3546- result->tv_usec = x->tv_usec - y->tv_usec;
3547-
3548- /* Return 1 if result is negative. */
3549- return x->tv_sec < y->tv_sec;
3550-}
3551-#endif
3552-
3553-
3554-#if NCHAN_SUBSCRIBER_LEAK_DEBUG
3555-
3556-subscriber_t *subdebug_head = NULL;
3557-
3558-void subscriber_debug_add(subscriber_t *sub) {
3559- if(subdebug_head == NULL) {
3560- sub->dbg_next = NULL;
3561- sub->dbg_prev = NULL;
3562- }
3563- else {
3564- sub->dbg_next = subdebug_head;
3565- sub->dbg_prev = NULL;
3566- assert(subdebug_head->dbg_prev == NULL);
3567- subdebug_head->dbg_prev = sub;
3568- }
3569- subdebug_head = sub;
3570-}
3571-void subscriber_debug_remove(subscriber_t *sub) {
3572- subscriber_t *prev, *next;
3573- prev = sub->dbg_prev;
3574- next = sub->dbg_next;
3575- if(subdebug_head == sub) {
3576- assert(sub->dbg_prev == NULL);
3577- if(next) {
3578- next->dbg_prev = NULL;
3579- }
3580- subdebug_head = next;
3581+ngx_int_t nchan_benchmark_handler(ngx_http_request_t *r) {
3582+ nchan_request_ctx_t *ctx;
3583+
3584+ if(r->connection && (r->connection->read->eof || r->connection->read->pending_eof)) {
3585+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
3586 }
3587- else {
3588- if(prev) {
3589- prev->dbg_next = next;
3590- }
3591- if(next) {
3592- next->dbg_prev = prev;
3593- }
3594+ if((ctx = ngx_pcalloc(r->pool, sizeof(nchan_request_ctx_t))) == NULL) {
3595+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
3596 }
3597+ ngx_http_set_ctx(r, ctx, ngx_nchan_module);
3598
3599- sub->dbg_next = NULL;
3600- sub->dbg_prev = NULL;
3601-}
3602-void subscriber_debug_assert_isempty(void) {
3603- assert(subdebug_head == NULL);
3604+ return nchan_benchmark_ws_initialize(r);
3605+
3606+ /*
3607+ ctx->bcp = ngx_palloc(r->pool, sizeof(nchan_bufchain_pool_t));
3608+ nchan_bufchain_pool_init(ctx->bcp, r->pool);
3609+ return nchan_benchmark_initialize(r);
3610+ */
3611 }
3612-#endif
3613diff --git a/debian/modules/nchan/src/nchan_module.h b/debian/modules/nchan/src/nchan_module.h
3614index 8c1aafb..b337e54 100644
3615--- a/debian/modules/nchan/src/nchan_module.h
3616+++ b/debian/modules/nchan/src/nchan_module.h
3617@@ -3,17 +3,17 @@
3618 //#define NCHAN_SUBSCRIBER_LEAK_DEBUG 1
3619 //#define NCHAN_MSG_RESERVE_DEBUG 1
3620 //#define NCHAN_MSG_LEAK_DEBUG 1
3621-//#define NCHAN_BENCHMARK 1
3622
3623 //debugging config
3624 //#define FAKESHARD 1
3625+//#define FAKE_SHMEM 1
3626 #if FAKESHARD
3627 //#define PUB_FAKE_WORKER 0
3628 //#define SUB_FAKE_WORKER 1
3629 //#define ONE_FAKE_CHANNEL_OWNER 2
3630 #define MAX_FAKE_WORKERS 5
3631 #endif
3632-
3633+#include <nchan_version.h>
3634 #include <ngx_http.h>
3635
3636 //building for old versions
3637@@ -30,9 +30,10 @@
3638 #include <nchan_defs.h>
3639 #include <util/nchan_util.h>
3640 #include <util/nchan_channel_id.h>
3641-#include <util/nchan_channel_info.h>
3642-#include <util/nchan_msgid.h>
3643+#include <util/nchan_output_info.h>
3644+#include <util/nchan_msg.h>
3645 #include <util/nchan_output.h>
3646+#include <util/nchan_debug.h>
3647
3648 extern ngx_pool_t *nchan_pool;
3649 extern ngx_int_t nchan_worker_processes;
3650@@ -43,7 +44,8 @@ extern int nchan_stub_status_enabled;
3651
3652 ngx_int_t nchan_stub_status_handler(ngx_http_request_t *r);
3653 ngx_int_t nchan_pubsub_handler(ngx_http_request_t *r);
3654-ngx_buf_t *nchan_channel_info_buf(ngx_str_t *accept_header, ngx_uint_t messages, ngx_uint_t subscribers, time_t last_seen, nchan_msg_id_t *last_msgid, ngx_str_t **generated_content_type);
3655+ngx_int_t nchan_group_handler(ngx_http_request_t *r);
3656+ngx_int_t nchan_benchmark_handler(ngx_http_request_t *r);
3657
3658 time_t nchan_loc_conf_message_timeout(nchan_loc_conf_t *cf);
3659 ngx_int_t nchan_loc_conf_max_messages(nchan_loc_conf_t *cf);
3660@@ -55,23 +57,13 @@ void __memstore_update_stub_status(off_t offset, int count);
3661 nchan_stub_status_t *nchan_get_stub_status_stats(void);
3662 size_t nchan_get_used_shmem(void);
3663
3664-#if NCHAN_SUBSCRIBER_LEAK_DEBUG
3665-void subscriber_debug_add(subscriber_t *);
3666-void subscriber_debug_remove(subscriber_t *);
3667-void subscriber_debug_assert_isempty(void);
3668-#endif
3669-
3670-#if NCHAN_BENCHMARK
3671-int nchan_timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y);
3672-#endif
3673-
3674-#define nchan_log_notice(fmt, args...) ngx_log_error(NGX_LOG_NOTICE, ngx_cycle->log, 0, "nchan: " fmt, ##args)
3675-#define nchan_log_warning(fmt, args...) ngx_log_error(NGX_LOG_WARN, ngx_cycle->log, 0, "nchan: " fmt, ##args)
3676-#define nchan_log_error(fmt, args...) ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "nchan: " fmt, ##args)
3677-#define nchan_log_request_warning(request, fmt, args...) ngx_log_error(NGX_LOG_WARN, (request)->connection->log, 0, "nchan: " fmt, ##args)
3678-#define nchan_log_request_error(request, fmt, args...) ngx_log_error(NGX_LOG_ERR, (request)->connection->log, 0, "nchan: " fmt, ##args)
3679-
3680 #define nchan_log(level, log, errno, fmt, args...) ngx_log_error(level, log, errno, "nchan: " fmt, ##args)
3681+#define nchan_log_notice(fmt, args...) nchan_log(NGX_LOG_NOTICE, ngx_cycle->log, 0, fmt, ##args)
3682+#define nchan_log_warning(fmt, args...) nchan_log(NGX_LOG_WARN, ngx_cycle->log, 0, fmt, ##args)
3683+#define nchan_log_error(fmt, args...) nchan_log(NGX_LOG_ERR, ngx_cycle->log, 0, fmt, ##args)
3684+#define nchan_log_ooshm_error(fmt, args...) nchan_log(NGX_LOG_ERR, ngx_cycle->log, 0, "Out of shared memory while " fmt ". Increase nchan_max_reserved_memory.", ##args)
3685
3686+#define nchan_log_request_warning(request, fmt, args...) ngx_log_error(NGX_LOG_WARN, (request)->connection->log, 0, "nchan: " fmt, ##args)
3687+#define nchan_log_request_error(request, fmt, args...) ngx_log_error(NGX_LOG_ERR, ((request) ? (request)->connection->log : ngx_cycle->log), 0, "nchan: " fmt, ##args)
3688
3689 #endif /*NCHAN_MODULE_H*/
3690diff --git a/debian/modules/nchan/src/nchan_setup.c b/debian/modules/nchan/src/nchan_setup.c
3691index e083e31..8d416a0 100644
3692--- a/debian/modules/nchan/src/nchan_setup.c
3693+++ b/debian/modules/nchan/src/nchan_setup.c
3694@@ -4,20 +4,46 @@
3695 #include <nchan_variables.h>
3696 #include <store/memory/store.h>
3697 #include <store/redis/store.h>
3698+#if (NGX_ZLIB)
3699+#include <zlib.h>
3700+#endif
3701
3702 static ngx_str_t DEFAULT_CHANNEL_EVENT_STRING = ngx_string("$nchan_channel_event $nchan_channel_id");
3703
3704 nchan_store_t *default_storage_engine = &nchan_store_memory;
3705+ngx_flag_t global_nchan_enabled = 0;
3706 ngx_flag_t global_redis_enabled = 0;
3707+ngx_flag_t global_zstream_needed = 0;
3708+ngx_flag_t global_benchmark_enabled = 0;
3709+void *global_owner_cycle = NULL;
3710+
3711+#define MERGE_UNSET_CONF(conf, prev, unset, default) \
3712+if (conf == unset) { \
3713+ conf = (prev == unset) ? default : prev; \
3714+}
3715
3716 #define MERGE_CONF(cf, prev_cf, name) if((cf)->name == NULL) { (cf)->name = (prev_cf)->name; }
3717
3718 static ngx_int_t nchan_init_module(ngx_cycle_t *cycle) {
3719- ngx_core_conf_t *ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
3720+ if(global_owner_cycle && global_owner_cycle != ngx_cycle) {
3721+ global_nchan_enabled = 0;
3722+ global_redis_enabled = 0;
3723+ global_zstream_needed = 0;
3724+ global_benchmark_enabled = 0;
3725+ }
3726+ global_owner_cycle = (void *)ngx_cycle;
3727+
3728+ if(!global_nchan_enabled) {
3729+ return NGX_OK;
3730+ }
3731+ ngx_core_conf_t *ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
3732 nchan_worker_processes = ccf->worker_processes;
3733
3734 //initialize storage engines
3735 nchan_store_memory.init_module(cycle);
3736+ if(global_benchmark_enabled) {
3737+ nchan_benchmark_init_module(cycle);
3738+ }
3739 if(global_redis_enabled) {
3740 nchan_store_redis.init_module(cycle);
3741 }
3742@@ -25,6 +51,9 @@ static ngx_int_t nchan_init_module(ngx_cycle_t *cycle) {
3743 }
3744
3745 static ngx_int_t nchan_init_worker(ngx_cycle_t *cycle) {
3746+ if(!global_nchan_enabled) {
3747+ return NGX_OK;
3748+ }
3749 if (ngx_process != NGX_PROCESS_WORKER && ngx_process != NGX_PROCESS_SINGLE) {
3750 //not a worker, stop initializing stuff.
3751 return NGX_OK;
3752@@ -33,6 +62,9 @@ static ngx_int_t nchan_init_worker(ngx_cycle_t *cycle) {
3753 if(nchan_store_memory.init_worker(cycle)!=NGX_OK) {
3754 return NGX_ERROR;
3755 }
3756+ if(global_benchmark_enabled) {
3757+ nchan_benchmark_init_worker(cycle);
3758+ }
3759
3760 if(global_redis_enabled && nchan_store_redis.init_worker(cycle)!=NGX_OK) {
3761 return NGX_ERROR;
3762@@ -44,13 +76,30 @@ static ngx_int_t nchan_init_worker(ngx_cycle_t *cycle) {
3763 return NGX_OK;
3764 }
3765
3766+static ngx_int_t nchan_preconfig(ngx_conf_t *cf) {
3767+ global_owner_cycle = (void *)ngx_cycle;
3768+ global_nchan_enabled = 0;
3769+ return nchan_add_variables(cf);
3770+}
3771+
3772 static ngx_int_t nchan_postconfig(ngx_conf_t *cf) {
3773+ global_owner_cycle = (void *)ngx_cycle;
3774 if(nchan_store_memory.init_postconfig(cf)!=NGX_OK) {
3775 return NGX_ERROR;
3776 }
3777 if(global_redis_enabled && nchan_store_redis.init_postconfig(cf)!=NGX_OK) {
3778 return NGX_ERROR;
3779 }
3780+
3781+#if (NGX_ZLIB)
3782+ if(global_zstream_needed) {
3783+ nchan_main_conf_t *mcf = ngx_http_conf_get_module_main_conf(cf, ngx_nchan_module);
3784+ nchan_common_deflate_init(mcf);
3785+ }
3786+#endif
3787+
3788+ global_nchan_enabled = 1;
3789+
3790 return NGX_OK;
3791 }
3792
3793@@ -60,12 +109,45 @@ static void * nchan_create_main_conf(ngx_conf_t *cf) {
3794 if(mcf == NULL) {
3795 return NGX_CONF_ERROR;
3796 }
3797+
3798+ static ngx_path_init_t nchan_temp_path = { ngx_string(NGX_HTTP_CLIENT_TEMP_PATH), { 0, 0, 0 } };
3799+ ngx_conf_merge_path_value(cf, &mcf->message_temp_path, NULL, &nchan_temp_path);
3800+
3801 nchan_store_memory.create_main_conf(cf, mcf);
3802 nchan_store_redis.create_main_conf(cf, mcf);
3803
3804+#if (NGX_ZLIB)
3805+ mcf->zlib_params.level = Z_DEFAULT_COMPRESSION;
3806+ mcf->zlib_params.windowBits = 10;
3807+ mcf->zlib_params.memLevel = 8;
3808+ mcf->zlib_params.strategy = Z_DEFAULT_STRATEGY;
3809+#endif
3810+
3811 return mcf;
3812 }
3813
3814+static void *nchan_create_srv_conf(ngx_conf_t *cf) {
3815+ nchan_srv_conf_t *scf = ngx_pcalloc(cf->pool, sizeof(*scf));
3816+ if(scf == NULL) {
3817+ return NGX_CONF_ERROR;
3818+ }
3819+ scf->redis.connect_timeout = NGX_CONF_UNSET_MSEC;
3820+ scf->redis.optimize_target = NCHAN_REDIS_OPTIMIZE_UNSET;
3821+ scf->redis.master_weight = NGX_CONF_UNSET;
3822+ scf->redis.slave_weight = NGX_CONF_UNSET;
3823+ scf->upstream_nchan_loc_conf = NULL;
3824+ return scf;
3825+}
3826+
3827+static char *nchan_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child) {
3828+ nchan_srv_conf_t *prev = parent, *conf = child;
3829+ ngx_conf_merge_msec_value(conf->redis.connect_timeout, prev->redis.connect_timeout, NCHAN_DEFAULT_REDIS_NODE_CONNECT_TIMEOUT_MSEC);
3830+ MERGE_UNSET_CONF(conf->redis.optimize_target, prev->redis.optimize_target, NCHAN_REDIS_OPTIMIZE_UNSET, NCHAN_REDIS_OPTIMIZE_CPU);
3831+ ngx_conf_merge_value(conf->redis.master_weight, prev->redis.master_weight, 1);
3832+ ngx_conf_merge_value(conf->redis.slave_weight, prev->redis.slave_weight, 1);
3833+ return NGX_CONF_OK;
3834+}
3835+
3836 //location config stuff
3837 static void *nchan_create_loc_conf(ngx_conf_t *cf) {
3838 nchan_loc_conf_t *lcf = ngx_pcalloc(cf->pool, sizeof(*lcf));
3839@@ -82,12 +164,16 @@ static void *nchan_create_loc_conf(ngx_conf_t *cf) {
3840 lcf->sub.websocket=0;
3841 lcf->sub.http_chunked=0;
3842
3843+ // lcf->group is already zeroed
3844+ lcf->group.enable_accounting = NGX_CONF_UNSET;
3845+
3846 lcf->shared_data_index=NGX_CONF_UNSET;
3847
3848 lcf->authorize_request_url = NULL;
3849 lcf->publisher_upstream_request_url = NULL;
3850 lcf->unsubscribe_request_url = NULL;
3851 lcf->subscribe_request_url = NULL;
3852+ lcf->channel_group = NULL;
3853
3854 lcf->message_timeout=NGX_CONF_UNSET;
3855 lcf->max_messages=NGX_CONF_UNSET;
3856@@ -107,11 +193,19 @@ static void *nchan_create_loc_conf(ngx_conf_t *cf) {
3857
3858 lcf->websocket_ping_interval=NGX_CONF_UNSET;
3859
3860+ lcf->eventsource_ping.interval=NGX_CONF_UNSET;
3861+
3862 lcf->msg_in_etag_only = NGX_CONF_UNSET;
3863
3864+ lcf->allow_origin = NULL;
3865+ lcf->allow_credentials = NGX_CONF_UNSET;
3866+
3867 lcf->channel_events_channel_id = NULL;
3868 lcf->channel_event_string = NULL;
3869
3870+ lcf->websocket_heartbeat.enabled=NGX_CONF_UNSET;
3871+
3872+ lcf->message_compression = NCHAN_MSG_COMPRESSION_INVALID;
3873
3874 lcf->longpoll_multimsg=NGX_CONF_UNSET;
3875 lcf->longpoll_multimsg_use_raw_stream_separator=NGX_CONF_UNSET;
3876@@ -125,7 +219,20 @@ static void *nchan_create_loc_conf(ngx_conf_t *cf) {
3877 lcf->redis.url_enabled=NGX_CONF_UNSET;
3878 lcf->redis.ping_interval = NGX_CONF_UNSET;
3879 lcf->redis.upstream_inheritable=NGX_CONF_UNSET;
3880-
3881+ lcf->redis.storage_mode = REDIS_MODE_CONF_UNSET;
3882+ lcf->redis.nostore_fastpublish = NGX_CONF_UNSET;
3883+ lcf->redis.privdata = NULL;
3884+ lcf->redis.nodeset = NULL;
3885+
3886+ lcf->request_handler = NULL;
3887+
3888+ lcf->benchmark.time = NGX_CONF_UNSET;
3889+ lcf->benchmark.msgs_per_minute = NGX_CONF_UNSET;
3890+ lcf->benchmark.msg_padding = NGX_CONF_UNSET;
3891+ lcf->benchmark.channels = NGX_CONF_UNSET;
3892+ lcf->benchmark.subscribers_per_channel = NGX_CONF_UNSET;
3893+ lcf->benchmark.subscriber_distribution = NCHAN_BENCHMARK_SUBSCRIBER_DISTRIBUTION_UNSET;
3894+ lcf->benchmark.publisher_distribution = NCHAN_BENCHMARK_PUBLISHER_DISTRIBUTION_UNSET;
3895 return lcf;
3896 }
3897
3898@@ -153,6 +260,36 @@ static char * create_complex_value_from_ngx_str(ngx_conf_t *cf, ngx_http_complex
3899 return NGX_CONF_OK;
3900 }
3901
3902+static int is_pub_location(nchan_loc_conf_t *lcf) {
3903+ return lcf->pub.http || lcf->pub.websocket;
3904+}
3905+static int is_sub_location(nchan_loc_conf_t *lcf) {
3906+ nchan_conf_subscriber_types_t s = lcf->sub;
3907+ return s.poll || s.http_raw_stream || s.longpoll || s.http_chunked || s.http_multipart || s.eventsource || s.websocket;
3908+}
3909+static int is_group_location(nchan_loc_conf_t *lcf) {
3910+ return lcf->group.get || lcf->group.set || lcf->group.delete;
3911+}
3912+
3913+static int is_valid_location(ngx_conf_t *cf, nchan_loc_conf_t *lcf) {
3914+
3915+ if(is_group_location(lcf)) {
3916+ if(is_pub_location(lcf) && is_sub_location(lcf)) {
3917+ ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "Can't have a publisher and subscriber location and also be a group access location (nchan_group + nchan_publisher, nchan_subscriber or nchan_pubsub)");
3918+ return 0;
3919+ }
3920+ else if(is_pub_location(lcf)) {
3921+ ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "Can't have a publisher location and also be a group access location (nchan_group + nchan_publisher)");
3922+ return 0;
3923+ }
3924+ else if(is_sub_location(lcf)) {
3925+ ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "Can't have a subscriber location and also be a group access location (nchan_group + nchan_subscriber)");
3926+ return 0;
3927+ }
3928+ }
3929+ return 1;
3930+}
3931+
3932 static char *ngx_conf_set_redis_upstream(ngx_conf_t *cf, ngx_str_t *url, void *conf) {
3933 ngx_url_t upstream_url;
3934 nchan_loc_conf_t *lcf = conf;
3935@@ -170,14 +307,43 @@ static char *ngx_conf_set_redis_upstream(ngx_conf_t *cf, ngx_str_t *url, void *c
3936
3937 lcf->redis.enabled = 1;
3938 global_redis_enabled = 1;
3939- nchan_store_redis_add_server_conf(cf, &lcf->redis, lcf);
3940+ nchan_store_redis_add_active_loc_conf(cf, lcf);
3941+
3942+ return NGX_CONF_OK;
3943+}
3944+
3945+static char *nchan_setup_handler(ngx_conf_t *cf, ngx_int_t (*handler)(ngx_http_request_t *)) {
3946+ ngx_http_core_loc_conf_t *clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
3947+ //nchan_loc_conf_t *plcf = conf;
3948+ clcf->handler = handler;
3949+ clcf->if_modified_since = NGX_HTTP_IMS_OFF;
3950
3951 return NGX_CONF_OK;
3952 }
3953
3954+static nchan_loc_conf_t *nchan_loc_conf_get_upstream_lcf(nchan_loc_conf_t *conf, nchan_loc_conf_t *prev) {
3955+ nchan_redis_conf_t *rcf = &conf->redis, *prev_rcf = &prev->redis;
3956+ if(rcf->upstream == prev_rcf->upstream || rcf->upstream == NULL) {
3957+ //same or no upstream, so don't bother
3958+ return NULL;
3959+ }
3960+ else {
3961+ assert(rcf->upstream);
3962+ nchan_srv_conf_t *upstream_scf = ngx_http_conf_upstream_srv_conf(rcf->upstream, ngx_nchan_module);
3963+ if(upstream_scf && upstream_scf->upstream_nchan_loc_conf) {
3964+ return upstream_scf->upstream_nchan_loc_conf;
3965+ }
3966+ else {
3967+ //ngx_conf_log_error(NGX_LOG_WARN, cf, 0, "nchan upstream srv_conf loc_conf ptr is null");
3968+ return NULL;
3969+ }
3970+ }
3971+}
3972+
3973+
3974 static char * nchan_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) {
3975 nchan_loc_conf_t *prev = parent, *conf = child;
3976-
3977+ nchan_loc_conf_t *up = nchan_loc_conf_get_upstream_lcf(conf, prev);
3978 //publisher types
3979 ngx_conf_merge_bitmask_value(conf->pub.http, prev->pub.http, 0);
3980 ngx_conf_merge_bitmask_value(conf->pub.websocket, prev->pub.websocket, 0);
3981@@ -189,6 +355,20 @@ static char * nchan_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) {
3982 ngx_conf_merge_bitmask_value(conf->sub.http_chunked, prev->sub.http_chunked, 0);
3983 ngx_conf_merge_bitmask_value(conf->sub.websocket, prev->sub.websocket, 0);
3984
3985+ //group request types
3986+ ngx_conf_merge_bitmask_value(conf->group.get, prev->group.get, 0);
3987+ ngx_conf_merge_bitmask_value(conf->group.set, prev->group.set, 0);
3988+ ngx_conf_merge_bitmask_value(conf->group.delete, prev->group.delete, 0);
3989+
3990+ ngx_conf_merge_value(conf->group.enable_accounting, prev->group.enable_accounting, 0);
3991+
3992+ //validate location
3993+ if(!is_valid_location(cf, conf)) {
3994+ return NGX_CONF_ERROR;
3995+ }
3996+
3997+ MERGE_UNSET_CONF(conf->message_compression, prev->message_compression, NCHAN_MSG_COMPRESSION_INVALID, NCHAN_MSG_NO_COMPRESSION);
3998+
3999 ngx_conf_merge_sec_value(conf->message_timeout, prev->message_timeout, NCHAN_DEFAULT_MESSAGE_TIMEOUT);
4000 ngx_conf_merge_value(conf->max_messages, prev->max_messages, NCHAN_DEFAULT_MAX_MESSAGES);
4001
4002@@ -199,7 +379,12 @@ static char * nchan_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) {
4003 conf->subscriber_first_message = (prev->subscriber_first_message == NCHAN_SUBSCRIBER_FIRST_MESSAGE_UNSET) ? NCHAN_SUBSCRIBER_DEFAULT_FIRST_MESSAGE : prev->subscriber_first_message;
4004 }
4005
4006- ngx_conf_merge_sec_value(conf->websocket_ping_interval, prev->websocket_ping_interval, NCHAN_DEFAULT_WEBSOCKET_PING_INTERVAL);
4007+ ngx_conf_merge_sec_value(conf->websocket_ping_interval, prev->websocket_ping_interval, NCHAN_DEFAULT_SUBSCRIBER_PING_INTERVAL);
4008+
4009+ ngx_conf_merge_sec_value(conf->eventsource_ping.interval, prev->eventsource_ping.interval, NCHAN_DEFAULT_SUBSCRIBER_PING_INTERVAL);
4010+ ngx_conf_merge_str_value(conf->eventsource_ping.data, prev->eventsource_ping.comment, "");
4011+ ngx_conf_merge_str_value(conf->eventsource_ping.event, prev->eventsource_ping.event, "ping");
4012+ ngx_conf_merge_str_value(conf->eventsource_ping.data, prev->eventsource_ping.data, "");
4013
4014 ngx_conf_merge_sec_value(conf->subscriber_timeout, prev->subscriber_timeout, NCHAN_DEFAULT_SUBSCRIBER_TIMEOUT);
4015 ngx_conf_merge_sec_value(conf->redis_idle_channel_cache_timeout, prev->redis_idle_channel_cache_timeout, NCHAN_DEFAULT_REDIS_IDLE_CHANNEL_CACHE_TIMEOUT);
4016@@ -212,13 +397,18 @@ static char * nchan_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) {
4017 ngx_conf_merge_str_value(conf->subscriber_http_raw_stream_separator, prev->subscriber_http_raw_stream_separator, "\n");
4018
4019 ngx_conf_merge_str_value(conf->channel_id_split_delimiter, prev->channel_id_split_delimiter, "");
4020- ngx_conf_merge_str_value(conf->channel_group, prev->channel_group, "");
4021- ngx_conf_merge_str_value(conf->allow_origin, prev->allow_origin, "*");
4022+ MERGE_CONF(conf, prev, allow_origin);
4023+ ngx_conf_merge_value(conf->allow_credentials, prev->allow_credentials, 1);
4024 ngx_conf_merge_str_value(conf->eventsource_event, prev->eventsource_event, "");
4025 ngx_conf_merge_str_value(conf->custom_msgtag_header, prev->custom_msgtag_header, "");
4026 ngx_conf_merge_value(conf->msg_in_etag_only, prev->msg_in_etag_only, 0);
4027 ngx_conf_merge_value(conf->longpoll_multimsg, prev->longpoll_multimsg, 0);
4028 ngx_conf_merge_value(conf->longpoll_multimsg_use_raw_stream_separator, prev->longpoll_multimsg_use_raw_stream_separator, 0);
4029+
4030+ ngx_conf_merge_value(conf->websocket_heartbeat.enabled, prev->websocket_heartbeat.enabled, 0);
4031+ MERGE_CONF(conf, prev, websocket_heartbeat.in);
4032+ MERGE_CONF(conf, prev, websocket_heartbeat.out);
4033+
4034 MERGE_CONF(conf, prev, channel_events_channel_id);
4035 MERGE_CONF(conf, prev, channel_event_string);
4036
4037@@ -236,6 +426,13 @@ static char * nchan_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) {
4038 MERGE_CONF(conf, prev, publisher_upstream_request_url);
4039 MERGE_CONF(conf, prev, unsubscribe_request_url);
4040 MERGE_CONF(conf, prev, subscribe_request_url);
4041+ MERGE_CONF(conf, prev, channel_group);
4042+
4043+ MERGE_CONF(conf, prev, group.max_channels);
4044+ MERGE_CONF(conf, prev, group.max_subscribers);
4045+ MERGE_CONF(conf, prev, group.max_messages);
4046+ MERGE_CONF(conf, prev, group.max_messages_shm_bytes);
4047+ MERGE_CONF(conf, prev, group.max_messages_file_bytes);
4048
4049 if(conf->pub_chid.n == 0) {
4050 conf->pub_chid = prev->pub_chid;
4051@@ -261,30 +458,54 @@ static char * nchan_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) {
4052 }
4053 conf->last_message_id.n = 2;
4054 }
4055-
4056+
4057 ngx_conf_merge_value(conf->redis.url_enabled, prev->redis.url_enabled, 0);
4058+
4059 ngx_conf_merge_value(conf->redis.upstream_inheritable, prev->redis.upstream_inheritable, 0);
4060 ngx_conf_merge_str_value(conf->redis.url, prev->redis.url, NCHAN_REDIS_DEFAULT_URL);
4061+
4062+ if(up && up->redis.namespace.len > 0) { //upstream has a namespace set
4063+ ngx_conf_merge_str_value(conf->redis.namespace, up->redis.namespace, "");
4064+ }
4065+ else {
4066+ ngx_conf_merge_str_value(conf->redis.namespace, prev->redis.namespace, "");
4067+ }
4068+
4069+ if(up)
4070+ ngx_conf_merge_value(conf->redis.ping_interval, up->redis.ping_interval, NGX_CONF_UNSET);
4071 ngx_conf_merge_value(conf->redis.ping_interval, prev->redis.ping_interval, NCHAN_REDIS_DEFAULT_PING_INTERVAL_TIME);
4072+
4073+ if(up)
4074+ ngx_conf_merge_value(conf->redis.nostore_fastpublish, up->redis.nostore_fastpublish, NGX_CONF_UNSET);
4075+ ngx_conf_merge_value(conf->redis.nostore_fastpublish, prev->redis.nostore_fastpublish, 0);
4076+
4077 if(conf->redis.url_enabled) {
4078 conf->redis.enabled = 1;
4079- nchan_store_redis_add_server_conf(cf, &conf->redis, conf);
4080+ nchan_store_redis_add_active_loc_conf(cf, conf);
4081 }
4082 if(conf->redis.upstream_inheritable && !conf->redis.upstream && prev->redis.upstream && prev->redis.upstream_url.len > 0) {
4083 conf->redis.upstream_url = prev->redis.upstream_url;
4084 ngx_conf_set_redis_upstream(cf, &conf->redis.upstream_url, conf);
4085 }
4086
4087- return NGX_CONF_OK;
4088-}
4089-
4090- //channel id variable
4091-//publisher and subscriber handlers now.
4092-static char *nchan_setup_handler(ngx_conf_t *cf, void * conf, ngx_int_t (*handler)(ngx_http_request_t *)) {
4093- ngx_http_core_loc_conf_t *clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
4094- //nchan_loc_conf_t *plcf = conf;
4095- clcf->handler = handler;
4096- clcf->if_modified_since = NGX_HTTP_IMS_OFF;
4097+ if(up)
4098+ MERGE_UNSET_CONF(conf->redis.storage_mode, up->redis.storage_mode, REDIS_MODE_CONF_UNSET, REDIS_MODE_CONF_UNSET);
4099+ MERGE_UNSET_CONF(conf->redis.storage_mode, prev->redis.storage_mode, REDIS_MODE_CONF_UNSET, REDIS_MODE_DISTRIBUTED);
4100+
4101+ if(prev->request_handler != NULL && conf->request_handler == NULL) {
4102+ conf->request_handler = prev->request_handler;
4103+ }
4104+ if(conf->request_handler != NULL) {
4105+ nchan_setup_handler(cf, conf->request_handler);
4106+ }
4107+
4108+ ngx_conf_merge_value(conf->benchmark.time, prev->benchmark.time, 10);
4109+ ngx_conf_merge_value(conf->benchmark.msgs_per_minute, prev->benchmark.msgs_per_minute, 120);
4110+ ngx_conf_merge_value(conf->benchmark.msg_padding, prev->benchmark.msg_padding, 0);
4111+ ngx_conf_merge_value(conf->benchmark.channels, prev->benchmark.channels, 1000);
4112+ ngx_conf_merge_value(conf->benchmark.subscribers_per_channel, prev->benchmark.subscribers_per_channel, 100);
4113+ MERGE_UNSET_CONF(conf->benchmark.subscriber_distribution, prev->benchmark.subscriber_distribution, NCHAN_BENCHMARK_SUBSCRIBER_DISTRIBUTION_UNSET, NCHAN_BENCHMARK_SUBSCRIBER_DISTRIBUTION_RANDOM);
4114+ MERGE_UNSET_CONF(conf->benchmark.publisher_distribution, prev->benchmark.publisher_distribution, NCHAN_BENCHMARK_PUBLISHER_DISTRIBUTION_UNSET, NCHAN_BENCHMARK_PUBLISHER_DISTRIBUTION_RANDOM);
4115
4116 return NGX_CONF_OK;
4117 }
4118@@ -332,12 +553,12 @@ static char *nchan_set_storage_engine(ngx_conf_t *cf, ngx_command_t *cmd, void *
4119 #define DISABLED_STRINGS "none", "off", "disabled"
4120 #define DISABLED_STRINGS_N 3
4121
4122-
4123 static char *nchan_publisher_directive_parse(ngx_conf_t *cf, ngx_command_t *cmd, void *conf, ngx_int_t fail) {
4124 nchan_loc_conf_t *lcf = conf;
4125 ngx_str_t *val;
4126 ngx_uint_t i;
4127
4128+
4129 nchan_conf_publisher_types_t *pubt = &lcf->pub;
4130
4131 if(cf->args->nelts == 1){ //no arguments
4132@@ -362,7 +583,11 @@ static char *nchan_publisher_directive_parse(ngx_conf_t *cf, ngx_command_t *cmd,
4133 }
4134 }
4135
4136- return nchan_setup_handler(cf, conf, &nchan_pubsub_handler);
4137+ if(!is_valid_location(cf, lcf)) {
4138+ return NGX_CONF_ERROR;
4139+ }
4140+ lcf->request_handler = &nchan_pubsub_handler;
4141+ return NGX_CONF_OK;
4142 }
4143
4144 static char *nchan_publisher_directive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
4145@@ -425,7 +650,12 @@ static char *nchan_subscriber_directive_parse(ngx_conf_t *cf, ngx_command_t *cmd
4146 }
4147 }
4148 }
4149- return nchan_setup_handler(cf, conf, &nchan_pubsub_handler);
4150+
4151+ if(!is_valid_location(cf, lcf)) {
4152+ return NGX_CONF_ERROR;
4153+ }
4154+ lcf->request_handler = &nchan_pubsub_handler;
4155+ return NGX_CONF_OK;
4156 }
4157
4158 static char *nchan_subscriber_directive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
4159@@ -435,18 +665,147 @@ static char *nchan_subscriber_directive(ngx_conf_t *cf, ngx_command_t *cmd, void
4160 static char *nchan_pubsub_directive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
4161 ngx_str_t *val;
4162 ngx_uint_t i;
4163+ nchan_loc_conf_t *lcf = conf;
4164 nchan_publisher_directive_parse(cf, cmd, conf, 0);
4165 nchan_subscriber_directive_parse(cf, cmd, conf, 0);
4166 for(i=1; i < cf->args->nelts; i++) {
4167 val = &((ngx_str_t *) cf->args->elts)[i];
4168 if(! nchan_strmatch(val,
4169- WEBSOCKET_STRINGS_N + EVENTSOURCE_STRINGS_N + HTTP_CHUNKED_STRINGS_N + HTTP_MULTIPART_STRINGS_N + LONGPOLL_STRINGS_N + INTERVALPOLL_STRINGS_N + DISABLED_STRINGS_N,
4170- WEBSOCKET_STRINGS, EVENTSOURCE_STRINGS, HTTP_CHUNKED_STRINGS, HTTP_MULTIPART_STRINGS, LONGPOLL_STRINGS_N, INTERVALPOLL_STRINGS_N, DISABLED_STRINGS_N)) {
4171+ WEBSOCKET_STRINGS_N + EVENTSOURCE_STRINGS_N + HTTP_CHUNKED_STRINGS_N + HTTP_MULTIPART_STRINGS_N + LONGPOLL_STRINGS_N + INTERVALPOLL_STRINGS_N + HTTP_RAW_STREAM_STRINGS_N + DISABLED_STRINGS_N,
4172+ WEBSOCKET_STRINGS, EVENTSOURCE_STRINGS, HTTP_CHUNKED_STRINGS, HTTP_MULTIPART_STRINGS, LONGPOLL_STRINGS, INTERVALPOLL_STRINGS, HTTP_RAW_STREAM_STRINGS, DISABLED_STRINGS)) {
4173 ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "invalid %V value: %V", &cmd->name, val);
4174 return NGX_CONF_ERROR;
4175 }
4176 }
4177- return nchan_setup_handler(cf, conf, &nchan_pubsub_handler);
4178+
4179+ if(!is_valid_location(cf, lcf)) {
4180+ return NGX_CONF_ERROR;
4181+ }
4182+
4183+ return NGX_CONF_OK;
4184+}
4185+
4186+static char *nchan_group_directive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
4187+ nchan_loc_conf_t *lcf = conf;
4188+ ngx_str_t *val;
4189+ ngx_uint_t i;
4190+ nchan_conf_group_t *group = &lcf->group;
4191+
4192+ if(cf->args->nelts == 1){ //no arguments
4193+ group->get=1;
4194+ group->set=1;
4195+ group->delete=1;
4196+ }
4197+ else {
4198+ for(i=1; i < cf->args->nelts; i++) {
4199+ val = &((ngx_str_t *) cf->args->elts)[i];
4200+ if(nchan_strmatch(val, 1, "get")) {
4201+ group->get=1;
4202+ }
4203+ else if(nchan_strmatch(val, 1, "set")) {
4204+ group->set=1;
4205+ }
4206+ else if(nchan_strmatch(val, 1, "delete")) {
4207+ group->delete=1;
4208+ }
4209+ else if(nchan_strmatch(val, 1, "off")) {
4210+ group->get=0;
4211+ group->set=0;
4212+ group->delete=0;
4213+ }
4214+ else {
4215+ ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "invalid %V value: %V", &cmd->name, val);
4216+ return NGX_CONF_ERROR;
4217+ }
4218+ }
4219+ }
4220+
4221+ if(!is_valid_location(cf, lcf)) {
4222+ return NGX_CONF_ERROR;
4223+ }
4224+ lcf->request_handler = &nchan_group_handler;
4225+ return NGX_CONF_OK;
4226+}
4227+
4228+static ngx_int_t set_complex_value(ngx_conf_t *cf, ngx_http_complex_value_t **cv, char *val) {
4229+ ngx_http_compile_complex_value_t ccv;
4230+ ngx_str_t *value = ngx_palloc(cf->pool, sizeof(ngx_str_t));;
4231+ if(value == NULL) {
4232+ return NGX_ERROR;
4233+ }
4234+ value->data = (u_char *)val;
4235+ value->len = strlen(val);
4236+ *cv = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t));
4237+ if (*cv == NULL) {
4238+ return NGX_ERROR;
4239+ }
4240+ ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
4241+ ccv.cf = cf;
4242+ ccv.value = value;
4243+ ccv.complex_value = *cv;
4244+ if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
4245+ return NGX_ERROR;
4246+ }
4247+
4248+ return NGX_OK;
4249+}
4250+
4251+static ngx_int_t set_complex_value_array_size1(ngx_conf_t *cf, nchan_complex_value_arr_t *chid, char *val) {
4252+ chid->n = 1;
4253+ return set_complex_value(cf, &chid->cv[0], val);
4254+}
4255+
4256+static char *nchan_benchmark_directive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
4257+ nchan_loc_conf_t *lcf = conf;
4258+ global_benchmark_enabled = 1;
4259+ lcf->request_handler = &nchan_benchmark_handler;
4260+
4261+
4262+ //set group
4263+ if(set_complex_value(cf, &lcf->channel_group, "benchmark") != NGX_OK) {
4264+ return "error setting benchmark channel group";
4265+ }
4266+ //set pub and sub channel ids
4267+ if(set_complex_value_array_size1(cf, &lcf->pub_chid, "control") != NGX_OK) {
4268+ return "error setting benchmark control channel";
4269+ }
4270+ if(set_complex_value_array_size1(cf, &lcf->sub_chid, "data") != NGX_OK) {
4271+ return "error setting benchmark data channel";
4272+ }
4273+
4274+ lcf->sub.websocket = 1;
4275+ lcf->pub.websocket = 1;
4276+
4277+ return NGX_CONF_OK;
4278+}
4279+
4280+static char *nchan_benchmark_subscriber_distribution_directive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
4281+ ngx_str_t *val = &((ngx_str_t *) cf->args->elts)[1];
4282+ nchan_loc_conf_t *lcf = conf;
4283+ if(nchan_strmatch(val, 1, "random")) {
4284+ lcf->benchmark.subscriber_distribution = NCHAN_BENCHMARK_SUBSCRIBER_DISTRIBUTION_RANDOM;
4285+ }
4286+ else if(nchan_strmatch(val, 2, "optimal", "best")) {
4287+ lcf->benchmark.subscriber_distribution = NCHAN_BENCHMARK_SUBSCRIBER_DISTRIBUTION_OPTIMAL;
4288+ }
4289+ else {
4290+ return "invalid value, must be \"random\" or \"optimal\"";
4291+ }
4292+ return NGX_CONF_OK;
4293+}
4294+static char *nchan_benchmark_publisher_distribution_directive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
4295+ ngx_str_t *val = &((ngx_str_t *) cf->args->elts)[1];
4296+ nchan_loc_conf_t *lcf = conf;
4297+ if(nchan_strmatch(val, 1, "random")) {
4298+ lcf->benchmark.publisher_distribution = NCHAN_BENCHMARK_PUBLISHER_DISTRIBUTION_RANDOM;
4299+ }
4300+ else if(nchan_strmatch(val, 2, "optimal", "best")) {
4301+ lcf->benchmark.publisher_distribution = NCHAN_BENCHMARK_PUBLISHER_DISTRIBUTION_OPTIMAL;
4302+ }
4303+ else {
4304+ return "invalid value, must be \"random\" or \"optimal\"";
4305+ }
4306+ return NGX_CONF_OK;
4307 }
4308
4309 static char *nchan_subscriber_first_message_directive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
4310@@ -482,7 +841,113 @@ static char *nchan_subscriber_first_message_directive(ngx_conf_t *cf, ngx_comman
4311 return NGX_CONF_OK;
4312 }
4313
4314+static char *nchan_websocket_heartbeat_directive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
4315+ nchan_loc_conf_t *lcf = (nchan_loc_conf_t *)conf;
4316+ ngx_str_t *heartbeat_in = &((ngx_str_t *) cf->args->elts)[1];
4317+ ngx_str_t *heartbeat_out = &((ngx_str_t *) cf->args->elts)[2];
4318+ ngx_str_t *in, *out;
4319+ lcf->websocket_heartbeat.enabled = 1;
4320+
4321+ in = ngx_pcalloc(cf->pool, sizeof(ngx_str_t) + heartbeat_in->len);
4322+ in->data = (u_char *)&in[1];
4323+ in->len = heartbeat_in->len;
4324+ ngx_memcpy(in->data, heartbeat_in->data, heartbeat_in->len);
4325+ lcf->websocket_heartbeat.in = in;
4326+
4327+ out = ngx_pcalloc(cf->pool, sizeof(ngx_str_t) + heartbeat_out->len);
4328+ out->data = (u_char *)&out[1];
4329+ out->len = heartbeat_out->len;
4330+ ngx_memcpy(out->data, heartbeat_out->data, heartbeat_out->len);
4331+ lcf->websocket_heartbeat.out = out;
4332+
4333+ return NGX_CONF_OK;
4334+}
4335+
4336+static char *nchan_conf_deflate_compression_level_directive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
4337+#if (NGX_ZLIB)
4338+ nchan_main_conf_t *mcf = (nchan_main_conf_t *)conf;
4339+ ngx_int_t np;
4340+ ngx_str_t *value = cf->args->elts;
4341+ np = ngx_atoi(value[1].data, value[1].len);
4342+ if (np == NGX_ERROR) {
4343+ return "invalid number";
4344+ }
4345+ if(np < 0 || np > 9) {
4346+ return "must be between 0 and 9";
4347+ }
4348+
4349+ mcf->zlib_params.level = np;
4350+#endif
4351+ return NGX_CONF_OK;
4352+}
4353+
4354+static char *nchan_conf_deflate_compression_strategy_directive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
4355+#if (NGX_ZLIB)
4356+ nchan_main_conf_t *mcf = (nchan_main_conf_t *)conf;
4357+ ngx_str_t *val = cf->args->elts;
4358+ if(nchan_strmatch(val, 1, "default")) {
4359+ mcf->zlib_params.strategy = Z_DEFAULT_STRATEGY;
4360+ }
4361+ else if(nchan_strmatch(val, 1, "filtered")) {
4362+ mcf->zlib_params.strategy = Z_FILTERED;
4363+ }
4364+ else if(nchan_strmatch(val, 1, "huffman-only")) {
4365+ mcf->zlib_params.strategy = Z_HUFFMAN_ONLY;
4366+ }
4367+ else if(nchan_strmatch(val, 1, "rle")) {
4368+ mcf->zlib_params.strategy = Z_RLE;
4369+ }
4370+ else if(nchan_strmatch(val, 1, "fixed")) {
4371+ mcf->zlib_params.strategy = Z_FIXED;
4372+ }
4373+ else {
4374+ return "invalid compression strategy";
4375+ }
4376+#endif
4377+ return NGX_CONF_OK;
4378+}
4379+
4380+static char *nchan_conf_deflate_compression_window_directive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
4381+#if (NGX_ZLIB)
4382+ nchan_main_conf_t *mcf = (nchan_main_conf_t *)conf;
4383+ ngx_int_t np;
4384+ ngx_str_t *value = cf->args->elts;
4385+ np = ngx_atoi(value[1].data, value[1].len);
4386+ if (np == NGX_ERROR) {
4387+ return "invalid number";
4388+ }
4389+ if(np < 9 || np > 15) {
4390+ return "must be between 9 and 15";
4391+ }
4392+
4393+ mcf->zlib_params.windowBits = np;
4394+#endif
4395+ return NGX_CONF_OK;
4396+}
4397+
4398+static char *nchan_conf_deflate_compression_memlevel_directive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
4399+#if (NGX_ZLIB)
4400+ nchan_main_conf_t *mcf = (nchan_main_conf_t *)conf;
4401+ ngx_int_t np;
4402+ ngx_str_t *value = cf->args->elts;
4403+ np = ngx_atoi(value[1].data, value[1].len);
4404+ if (np == NGX_ERROR) {
4405+ return "invalid number";
4406+ }
4407+ if(np < 1 || np > 9) {
4408+ return "must be between 1 and 9";
4409+ }
4410+
4411+ mcf->zlib_params.memLevel = np;
4412+#endif
4413+ return NGX_CONF_OK;
4414+}
4415+
4416+
4417 static void nchan_exit_worker(ngx_cycle_t *cycle) {
4418+ if(!global_nchan_enabled) {
4419+ return;
4420+ }
4421 if(global_redis_enabled) {
4422 redis_store_prepare_to_exit_worker();
4423 }
4424@@ -491,16 +956,32 @@ static void nchan_exit_worker(ngx_cycle_t *cycle) {
4425 nchan_store_redis.exit_worker(cycle);
4426 }
4427 nchan_output_shutdown();
4428+#if (NGX_ZLIB)
4429+ if(global_zstream_needed) {
4430+ nchan_common_deflate_shutdown();
4431+ }
4432+#endif
4433 #if NCHAN_SUBSCRIBER_LEAK_DEBUG
4434 subscriber_debug_assert_isempty();
4435 #endif
4436 }
4437
4438 static void nchan_exit_master(ngx_cycle_t *cycle) {
4439+ if(!global_nchan_enabled) {
4440+ return;
4441+ }
4442+ if(global_benchmark_enabled) {
4443+ nchan_benchmark_exit_master(cycle);
4444+ }
4445 nchan_store_memory.exit_master(cycle);
4446 if(global_redis_enabled) {
4447 nchan_store_redis.exit_master(cycle);
4448 }
4449+#if (NGX_ZLIB)
4450+ if(global_zstream_needed) {
4451+ nchan_common_deflate_shutdown();
4452+ }
4453+#endif
4454 }
4455
4456 static char *nchan_set_complex_value_array(ngx_conf_t *cf, ngx_command_t *cmd, void *conf, nchan_complex_value_arr_t *chid) {
4457@@ -668,6 +1149,26 @@ static char *nchan_set_raw_subscriber_separator(ngx_conf_t *cf, ngx_command_t *c
4458 return NGX_CONF_OK;
4459 }
4460
4461+static char *nchan_set_message_compression_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
4462+ ngx_str_t *val = &((ngx_str_t *) cf->args->elts)[1];
4463+ nchan_loc_conf_t *lcf = conf;
4464+#if (NGX_ZLIB)
4465+ if(nchan_strmatch(val, 1, "on")) {
4466+ lcf->message_compression = 1;
4467+ global_zstream_needed = 1;
4468+ }
4469+ else if(nchan_strmatch(val, 1, "off")) {
4470+ lcf->message_compression = 0;
4471+ }
4472+ else {
4473+ return "invalid value: must be 'on' or 'off'";
4474+ }
4475+ return NGX_CONF_OK;
4476+#else
4477+ return "cannot use compression, Nginx was built without zlib";
4478+#endif
4479+}
4480+
4481 static char *nchan_set_longpoll_multipart(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
4482 ngx_str_t *val = &((ngx_str_t *) cf->args->elts)[1];
4483 nchan_loc_conf_t *lcf = conf;
4484@@ -688,11 +1189,59 @@ static char *nchan_set_longpoll_multipart(ngx_conf_t *cf, ngx_command_t *cmd, vo
4485 return NGX_CONF_OK;
4486 }
4487
4488+static char *ngx_conf_set_redis_subscribe_weights(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
4489+ ngx_int_t master = NGX_CONF_UNSET;
4490+ ngx_int_t slave = NGX_CONF_UNSET;
4491+ ngx_str_t *val = cf->args->elts;
4492+ ngx_str_t *cur;
4493+ unsigned i;
4494+ nchan_srv_conf_t *scf = conf;
4495+ for(i=1; i < cf->args->nelts; i++) {
4496+ cur = &val[i];
4497+ if(nchan_str_after(&cur, "master=")) {
4498+ if((master = ngx_atoi(cur->data, cur->len)) == NGX_ERROR) {
4499+ return "has invalid weight for master";
4500+ }
4501+ }
4502+ else if(nchan_str_after(&cur, "slave=")) {
4503+ if((slave = ngx_atoi(cur->data, cur->len)) == NGX_ERROR) {
4504+ return "has invalid weight for slave";
4505+ }
4506+ }
4507+ }
4508+
4509+ if(master != NGX_CONF_UNSET) {
4510+ scf->redis.master_weight = master;
4511+ }
4512+ if(slave != NGX_CONF_UNSET) {
4513+ scf->redis.slave_weight = slave;
4514+ }
4515+
4516+ return NGX_CONF_OK;
4517+}
4518+
4519+static char *ngx_conf_set_redis_optimize_target(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
4520+ ngx_str_t *val = &((ngx_str_t *) cf->args->elts)[1];
4521+ nchan_srv_conf_t *scf = conf;
4522+ if(nchan_strmatch(val, 2, "bandwidth", "bw")) {
4523+ scf->redis.optimize_target = NCHAN_REDIS_OPTIMIZE_BANDWIDTH;
4524+ }
4525+ else if(nchan_strmatch(val, 2, "cpu", "CPU")) {
4526+ scf->redis.optimize_target = NCHAN_REDIS_OPTIMIZE_CPU;
4527+ }
4528+ else {
4529+ return "invalid value, must be \"bandwidth\" or \"cpu\"";
4530+ }
4531+ return NGX_CONF_OK;
4532+}
4533+
4534 static char *ngx_conf_enable_redis(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
4535 char *rc;
4536 ngx_flag_t *fp;
4537 char *p = conf;
4538- nchan_loc_conf_t *lcf = (nchan_loc_conf_t *)conf;
4539+ nchan_loc_conf_t *lcf = conf;
4540+
4541+ ngx_conf_log_error(NGX_LOG_WARN, cf, 0, "Use of %V is discouraged in favor of nchan_redis_pass.", &cmd->name);
4542
4543 rc = ngx_conf_set_flag_slot(cf, cmd, conf);
4544 if(rc == NGX_CONF_ERROR) {
4545@@ -703,20 +1252,22 @@ static char *ngx_conf_enable_redis(ngx_conf_t *cf, ngx_command_t *cmd, void *con
4546 if(*fp) {
4547 if(!lcf->redis.enabled) {
4548 lcf->redis.enabled = 1;
4549- nchan_store_redis_add_server_conf(cf, &lcf->redis, lcf);
4550+ nchan_store_redis_add_active_loc_conf(cf, lcf);
4551 }
4552 global_redis_enabled = 1;
4553 }
4554 else {
4555- nchan_store_redis_remove_server_conf(cf, &lcf->redis);
4556+ nchan_store_redis_remove_active_loc_conf(cf, lcf);
4557 }
4558
4559 return rc;
4560 }
4561
4562 static char *nchan_stub_status_directive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
4563+ nchan_loc_conf_t *lcf = conf;
4564 nchan_stub_status_enabled = 1;
4565- return nchan_setup_handler(cf, conf, &nchan_stub_status_handler);
4566+ lcf->request_handler = &nchan_stub_status_handler;
4567+ return NGX_CONF_OK;
4568 }
4569
4570
4571@@ -728,8 +1279,18 @@ static char *ngx_conf_upstream_redis_server(ngx_conf_t *cf, ngx_command_t *cmd,
4572 ngx_http_upstream_srv_conf_t *uscf;
4573 ngx_str_t *value;
4574 ngx_http_upstream_server_t *usrv;
4575-
4576+ nchan_loc_conf_t *lcf = conf;
4577 uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module);
4578+ nchan_srv_conf_t *scf = NULL;
4579+ scf = ngx_http_conf_upstream_srv_conf(uscf, ngx_nchan_module);
4580+ if(scf->upstream_nchan_loc_conf) {
4581+ assert(scf->upstream_nchan_loc_conf == lcf);
4582+ }
4583+ else {
4584+ //is this even a safe technique? it might break in the future...
4585+ scf->upstream_nchan_loc_conf = lcf;
4586+ }
4587+
4588
4589 if(uscf->servers == NULL) {
4590 uscf->servers = ngx_array_create(cf->pool, 4, sizeof(ngx_http_upstream_server_t));
4591@@ -738,6 +1299,11 @@ static char *ngx_conf_upstream_redis_server(ngx_conf_t *cf, ngx_command_t *cmd,
4592 return NGX_CONF_ERROR;
4593 }
4594 value = cf->args->elts;
4595+
4596+ if(!nchan_store_redis_validate_url(&value[1])) {
4597+ return "url is invalid";
4598+ }
4599+
4600 ngx_memzero(usrv, sizeof(*usrv));
4601 #if nginx_version >= 1007002
4602 usrv->name = value[1];
4603@@ -749,16 +1315,89 @@ static char *ngx_conf_upstream_redis_server(ngx_conf_t *cf, ngx_command_t *cmd,
4604 return NGX_CONF_OK;
4605 }
4606
4607+static char *ngx_conf_set_str_slot_no_newlines(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
4608+ ngx_str_t *val = cf->args->elts;
4609+ ngx_str_t *arg = &val[1];
4610+
4611+ if(nchan_ngx_str_substr(arg, "\n")) {
4612+ return "can't contain any newline characters";
4613+ }
4614+
4615+ return ngx_conf_set_str_slot(cf, cmd, conf);
4616+}
4617+
4618 static char *ngx_conf_set_redis_url(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
4619 nchan_loc_conf_t *lcf = conf;
4620+ ngx_str_t *val = cf->args->elts;
4621+ ngx_str_t *arg = &val[1];
4622+
4623+ ngx_conf_log_error(NGX_LOG_WARN, cf, 0, "Use of %V is discouraged in favor of an upstream { } block with nchan_redis_server %V;", &cmd->name, arg);
4624
4625 if(lcf->redis.upstream) {
4626 return "can't be set here: already using nchan_redis_pass";
4627 }
4628+ if(!nchan_store_redis_validate_url(arg)) {
4629+ return "url is invalid";
4630+ }
4631
4632 return ngx_conf_set_str_slot(cf, cmd, conf);
4633 }
4634
4635+static char *ngx_conf_process_redis_namespace_slot(ngx_conf_t *cf, void *post, void *fld) {
4636+ ngx_str_t *arg = fld;
4637+
4638+ if(memchr(arg->data, '{', arg->len)) {
4639+ return "can't contain character '{'";
4640+ }
4641+
4642+ if(memchr(arg->data, '}', arg->len)) {
4643+ return "can't contain character '}'";
4644+ }
4645+
4646+ if(arg->len > 0 && arg->data[arg->len-1] != ':') {
4647+ u_char *nns;
4648+ if((nns = ngx_palloc(cf->pool, arg->len + 2)) == NULL) {
4649+ return "couldn't allocate redis namespace data";
4650+ }
4651+ ngx_memcpy(nns, arg->data, arg->len);
4652+ nns[arg->len]=':';
4653+ nns[arg->len+1]='\0';
4654+ arg->len++;
4655+ arg->data=nns;
4656+ }
4657+
4658+ return NGX_CONF_OK;
4659+}
4660+
4661+static char *ngx_conf_set_redis_storage_mode_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
4662+ char *p = conf;
4663+ ngx_str_t *val = cf->args->elts;
4664+ ngx_str_t *arg = &val[1];
4665+
4666+ nchan_redis_storage_mode_t *field;
4667+
4668+ field = (nchan_redis_storage_mode_t *) (p + cmd->offset);
4669+
4670+ if(*field != REDIS_MODE_CONF_UNSET) {
4671+ return "is duplicate";
4672+ }
4673+
4674+ if(nchan_strmatch(arg, 1, "backup")) {
4675+ *field = REDIS_MODE_BACKUP;
4676+ }
4677+ else if(nchan_strmatch(arg, 1, "distributed")) {
4678+ *field = REDIS_MODE_DISTRIBUTED;
4679+ }
4680+ else if(nchan_strmatch(arg, 1, "nostore") || nchan_strmatch(arg, 1, "distributed-nostore")) {
4681+ *field = REDIS_MODE_DISTRIBUTED_NOSTORE;
4682+ }
4683+ else {
4684+ return "is invalid, must be one of 'distributed', 'backup' or 'nostore'";
4685+ }
4686+
4687+ return NGX_CONF_OK;
4688+}
4689+
4690 static char *ngx_conf_set_redis_upstream_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
4691 nchan_loc_conf_t *lcf = conf;
4692 ngx_str_t *value = cf->args->elts;
4693@@ -771,12 +1410,12 @@ static char *ngx_conf_set_redis_upstream_pass(ngx_conf_t *cf, ngx_command_t *cmd
4694 #include "nchan_config_commands.c" //hideous but hey, it works
4695
4696 static ngx_http_module_t nchan_module_ctx = {
4697- nchan_add_variables, /* preconfiguration */
4698+ nchan_preconfig, /* preconfiguration */
4699 nchan_postconfig, /* postconfiguration */
4700 nchan_create_main_conf, /* create main configuration */
4701 NULL, /* init main configuration */
4702- NULL, /* create server configuration */
4703- NULL, /* merge server configuration */
4704+ nchan_create_srv_conf, /* create server configuration */
4705+ nchan_merge_srv_conf, /* merge server configuration */
4706 nchan_create_loc_conf, /* create location configuration */
4707 nchan_merge_loc_conf, /* merge location configuration */
4708 };
4709diff --git a/debian/modules/nchan/src/nchan_types.h b/debian/modules/nchan/src/nchan_types.h
4710index 58b8ee1..d8733c0 100644
4711--- a/debian/modules/nchan/src/nchan_types.h
4712+++ b/debian/modules/nchan/src/nchan_types.h
4713@@ -5,8 +5,14 @@
4714
4715 typedef ngx_int_t (*callback_pt)(ngx_int_t, void *, void *);
4716
4717-typedef enum {MSG_CHANNEL_NOTREADY, MSG_NORESPONSE, MSG_INVALID, MSG_PENDING, MSG_NOTFOUND, MSG_FOUND, MSG_EXPECTED, MSG_EXPIRED} nchan_msg_status_t;
4718-typedef enum {INACTIVE, NOTREADY, WAITING, STUBBED, READY} chanhead_pubsub_status_t;
4719+#include <util/nchan_fake_request.h>
4720+
4721+typedef enum {MSG_ERROR, MSG_CHANNEL_NOTREADY, MSG_INVALID, MSG_PENDING, MSG_NOTFOUND, MSG_FOUND, MSG_EXPECTED, MSG_EXPIRED} nchan_msg_status_t;
4722+typedef enum {INACTIVE, NOTREADY, WAITING, STUBBED, READY, DELETED} chanhead_pubsub_status_t;
4723+
4724+typedef enum {NCHAN_CONTENT_TYPE_PLAIN, NCHAN_CONTENT_TYPE_JSON, NCHAN_CONTENT_TYPE_XML, NCHAN_CONTENT_TYPE_YAML, NCHAN_CONTENT_TYPE_HTML} nchan_content_type_t;
4725+
4726+typedef enum {REDIS_MODE_CONF_UNSET = NGX_CONF_UNSET, REDIS_MODE_BACKUP = 1, REDIS_MODE_DISTRIBUTED = 2, REDIS_MODE_DISTRIBUTED_NOSTORE = 3} nchan_redis_storage_mode_t;
4727
4728 typedef enum {
4729 SUB_ENQUEUE, SUB_DEQUEUE, SUB_RECEIVE_MESSAGE, SUB_RECEIVE_STATUS,
4730@@ -17,6 +23,15 @@ typedef struct {
4731 size_t shm_size;
4732 ngx_msec_t redis_fakesub_timer_interval;
4733 size_t redis_publish_message_msgkey_size;
4734+#if (NGX_ZLIB)
4735+ struct {
4736+ int level;
4737+ int windowBits;
4738+ int memLevel;
4739+ int strategy;
4740+ } zlib_params;
4741+#endif
4742+ ngx_path_t *message_temp_path;
4743 } nchan_main_conf_t;
4744
4745
4746@@ -24,10 +39,14 @@ typedef struct {
4747 ngx_str_t url;
4748 ngx_flag_t url_enabled;
4749 time_t ping_interval;
4750+ ngx_str_t namespace;
4751+ nchan_redis_storage_mode_t storage_mode;
4752+ ngx_int_t nostore_fastpublish;
4753 ngx_str_t upstream_url;
4754 ngx_http_upstream_srv_conf_t *upstream;
4755 ngx_flag_t upstream_inheritable;
4756 unsigned enabled:1;
4757+ void *nodeset;
4758 void *privdata;
4759 } nchan_redis_conf_t;
4760
4761@@ -57,8 +76,8 @@ union nchan_msg_multitag {
4762 typedef struct {
4763 time_t time; //tag message by time
4764 union nchan_msg_multitag tag;
4765- unsigned tagactive:16;
4766- unsigned tagcount:16;
4767+ int16_t tagactive;
4768+ int16_t tagcount;
4769 } nchan_msg_id_t;
4770
4771 typedef struct {
4772@@ -80,20 +99,35 @@ struct msg_rsv_dbg_s {
4773 typedef struct nchan_loc_conf_s nchan_loc_conf_t;
4774 typedef struct nchan_msg_s nchan_msg_t;
4775
4776+typedef enum {NCHAN_MSG_SHARED, NCHAN_MSG_HEAP, NCHAN_MSG_POOL, NCHAN_MSG_STACK} nchan_msg_storage_t;
4777+
4778+typedef enum {
4779+ NCHAN_MSG_COMPRESSION_INVALID = -1,
4780+ NCHAN_MSG_NO_COMPRESSION = 0,
4781+ NCHAN_MSG_COMPRESSION_WEBSOCKET_PERMESSAGE_DEFLATE
4782+} nchan_msg_compression_type_t;
4783+
4784+typedef struct {
4785+ ngx_buf_t buf;
4786+ nchan_msg_compression_type_t compression;
4787+} nchan_compressed_msg_t;
4788+
4789 struct nchan_msg_s {
4790 nchan_msg_id_t id;
4791 nchan_msg_id_t prev_id;
4792- ngx_str_t content_type;
4793- ngx_str_t eventsource_event;
4794+ ngx_str_t *content_type;
4795+ ngx_str_t *eventsource_event;
4796 // ngx_str_t charset;
4797- ngx_buf_t *buf;
4798+ ngx_buf_t buf;
4799 time_t expires;
4800+
4801 ngx_atomic_int_t refcount;
4802+ nchan_msg_t *parent;
4803+ nchan_compressed_msg_t *compressed;
4804+ //struct nchan_msg_s *reload_next;
4805
4806- struct nchan_msg_s *reload_next;
4807+ nchan_msg_storage_t storage;
4808
4809- unsigned shared:1; //for debugging
4810- unsigned temp_allocd:1;
4811 #if NCHAN_MSG_RESERVE_DEBUG
4812 struct msg_rsv_dbg_s *rsv;
4813 #endif
4814@@ -102,17 +136,9 @@ struct nchan_msg_s {
4815 struct nchan_msg_s *dbg_prev;
4816 struct nchan_msg_s *dbg_next;
4817 #endif
4818-#if NCHAN_BENCHMARK
4819- struct timeval start_tv;
4820-#endif
4821 }; // nchan_msg_t
4822
4823 typedef struct {
4824- nchan_msg_t copy;
4825- nchan_msg_t *original;
4826-} nchan_msg_copy_t;
4827-
4828-typedef struct {
4829 ngx_str_t id;
4830 ngx_int_t messages;
4831 ngx_int_t subscribers;
4832@@ -150,6 +176,26 @@ typedef struct {
4833
4834 typedef struct subscriber_s subscriber_t;
4835
4836+typedef struct {
4837+ //must be made entirely of ngx_atomic_int_t
4838+ ngx_atomic_int_t channels;
4839+ ngx_atomic_int_t subscribers;
4840+ ngx_atomic_int_t messages;
4841+ ngx_atomic_int_t messages_shmem_bytes;
4842+ ngx_atomic_int_t messages_file_bytes;
4843+} nchan_group_limits_t;
4844+
4845+typedef struct {
4846+ ngx_atomic_int_t channels;
4847+ ngx_atomic_int_t multiplexed_channels;
4848+ ngx_atomic_int_t subscribers;
4849+ ngx_atomic_int_t messages;
4850+ ngx_atomic_int_t messages_shmem_bytes;
4851+ ngx_atomic_int_t messages_file_bytes;
4852+ nchan_group_limits_t limit;
4853+ ngx_str_t name;
4854+} nchan_group_t;
4855+
4856 typedef struct{
4857 //init
4858 ngx_int_t (*init_module)(ngx_cycle_t *cycle);
4859@@ -166,16 +212,14 @@ typedef struct{
4860 ngx_int_t (*subscribe) (ngx_str_t *, subscriber_t *);
4861 ngx_int_t (*publish) (ngx_str_t *, nchan_msg_t *, nchan_loc_conf_t *, callback_pt, void *);
4862
4863+ //channel actions
4864 ngx_int_t (*delete_channel)(ngx_str_t *, nchan_loc_conf_t *, callback_pt, void *);
4865-
4866- //channel actions
4867 ngx_int_t (*find_channel)(ngx_str_t *, nchan_loc_conf_t *, callback_pt, void*);
4868
4869-
4870-
4871- //message actions and properties
4872- ngx_str_t * (*message_etag)(nchan_msg_t *msg, ngx_pool_t *pool);
4873- ngx_str_t * (*message_content_type)(nchan_msg_t *msg, ngx_pool_t *pool);
4874+ //group actions
4875+ ngx_int_t (*get_group)(ngx_str_t *name, nchan_loc_conf_t *, callback_pt, void *);
4876+ ngx_int_t (*set_group_limits)(ngx_str_t *name, nchan_loc_conf_t *, nchan_group_limits_t *limits, callback_pt, void *);
4877+ ngx_int_t (*delete_group)(ngx_str_t *name, nchan_loc_conf_t *, callback_pt, void *);
4878
4879 } nchan_store_t;
4880
4881@@ -196,6 +240,20 @@ typedef struct {
4882 unsigned websocket:1;
4883 } nchan_conf_subscriber_types_t;
4884
4885+typedef struct {
4886+ unsigned get:1;
4887+ unsigned set:1;
4888+ unsigned delete:1;
4889+
4890+ ngx_int_t enable_accounting;
4891+
4892+ ngx_http_complex_value_t *max_channels;
4893+ ngx_http_complex_value_t *max_subscribers;
4894+ ngx_http_complex_value_t *max_messages;
4895+ ngx_http_complex_value_t *max_messages_shm_bytes;
4896+ ngx_http_complex_value_t *max_messages_file_bytes;
4897+} nchan_conf_group_t;
4898+
4899 #define NCHAN_COMPLEX_VALUE_ARRAY_MAX 8
4900 typedef struct {
4901 ngx_http_complex_value_t *cv[NCHAN_COMPLEX_VALUE_ARRAY_MAX];
4902@@ -207,6 +265,40 @@ typedef struct {
4903 ngx_atomic_uint_t max_messages;
4904 } nchan_loc_conf_shared_data_t;
4905
4906+typedef enum {
4907+ NCHAN_REDIS_OPTIMIZE_UNSET = -1,
4908+ NCHAN_REDIS_OPTIMIZE_CPU = 1,
4909+ NCHAN_REDIS_OPTIMIZE_BANDWIDTH = 2
4910+} nchan_redis_optimize_t;
4911+
4912+typedef struct {
4913+ struct {
4914+ ngx_msec_t connect_timeout;
4915+ nchan_redis_optimize_t optimize_target;
4916+ ngx_int_t master_weight;
4917+ ngx_int_t slave_weight;
4918+ } redis;
4919+ nchan_loc_conf_t *upstream_nchan_loc_conf;
4920+} nchan_srv_conf_t;
4921+
4922+typedef struct {
4923+ time_t time;
4924+ ngx_int_t msgs_per_minute;
4925+ ngx_int_t msg_padding;
4926+ ngx_int_t channels;
4927+ ngx_int_t subscribers_per_channel;
4928+ enum {
4929+ NCHAN_BENCHMARK_SUBSCRIBER_DISTRIBUTION_UNSET = -1,
4930+ NCHAN_BENCHMARK_SUBSCRIBER_DISTRIBUTION_RANDOM = 1,
4931+ NCHAN_BENCHMARK_SUBSCRIBER_DISTRIBUTION_OPTIMAL = 2
4932+ } subscriber_distribution;
4933+ enum {
4934+ NCHAN_BENCHMARK_PUBLISHER_DISTRIBUTION_UNSET = -1,
4935+ NCHAN_BENCHMARK_PUBLISHER_DISTRIBUTION_RANDOM = 1,
4936+ NCHAN_BENCHMARK_PUBLISHER_DISTRIBUTION_OPTIMAL = 2
4937+ } publisher_distribution;
4938+} nchan_benchmark_conf_t;
4939+
4940 struct nchan_loc_conf_s { //nchan_loc_conf_t
4941
4942 ngx_int_t shared_data_index;
4943@@ -226,12 +318,14 @@ struct nchan_loc_conf_s { //nchan_loc_conf_t
4944 nchan_complex_value_arr_t pub_chid;
4945 nchan_complex_value_arr_t sub_chid;
4946 nchan_complex_value_arr_t pubsub_chid;
4947- ngx_str_t channel_group;
4948+ ngx_http_complex_value_t *channel_group;
4949+
4950 ngx_str_t channel_id_split_delimiter;
4951
4952 ngx_str_t subscriber_http_raw_stream_separator;
4953
4954- ngx_str_t allow_origin;
4955+ ngx_http_complex_value_t *allow_origin;
4956+ ngx_int_t allow_credentials;
4957
4958 nchan_complex_value_arr_t last_message_id;
4959 ngx_str_t custom_msgtag_header;
4960@@ -239,7 +333,7 @@ struct nchan_loc_conf_s { //nchan_loc_conf_t
4961
4962 nchan_conf_publisher_types_t pub;
4963 nchan_conf_subscriber_types_t sub;
4964-
4965+ nchan_conf_group_t group;
4966 time_t subscriber_timeout;
4967
4968 ngx_int_t longpoll_multimsg;
4969@@ -248,6 +342,20 @@ struct nchan_loc_conf_s { //nchan_loc_conf_t
4970 ngx_str_t eventsource_event;
4971
4972 time_t websocket_ping_interval;
4973+ struct {
4974+ time_t interval;
4975+ ngx_str_t event;
4976+ ngx_str_t data;
4977+ ngx_str_t comment;
4978+ } eventsource_ping;
4979+
4980+ struct {
4981+ ngx_int_t enabled;
4982+ ngx_str_t *in;
4983+ ngx_str_t *out;
4984+ } websocket_heartbeat;
4985+
4986+ nchan_msg_compression_type_t message_compression;
4987
4988 ngx_int_t subscriber_first_message;
4989
4990@@ -263,14 +371,12 @@ struct nchan_loc_conf_s { //nchan_loc_conf_t
4991 ngx_int_t max_channel_subscribers;
4992 time_t channel_timeout;
4993 nchan_store_t *storage_engine;
4994+
4995+ nchan_benchmark_conf_t benchmark;
4996+
4997+ ngx_int_t (*request_handler)(ngx_http_request_t *r);
4998 };// nchan_loc_conf_t;
4999
5000-typedef struct {
The diff has been truncated for viewing.

Subscribers

People subscribed via source and target branches