Skip to content

Commit 0588ca1

Browse files
authored
Merge pull request #4 from chainstack/bug/fix-reading-chunked-data
Bug/fix reading chunked data
2 parents a45e146 + fd57897 commit 0588ca1

26 files changed

+356
-1148
lines changed

.github/workflows/test.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Test
2+
3+
on: [pull_request]
4+
5+
jobs:
6+
test:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- uses: actions/checkout@v2
11+
- name: Unit
12+
run: |
13+
make test
14+
- name: E2E
15+
run: |
16+
make test_e2e

.travis.yml

Lines changed: 0 additions & 23 deletions
This file was deleted.

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.PHONY: test test_e2e
2+
3+
test:
4+
docker-compose build test && docker-compose run --rm test
5+
docker-compose down -t 0
6+
7+
test_e2e:
8+
docker-compose build test_e2e && docker-compose run --rm test_e2e
9+
docker-compose down -t 0

README.md

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[![Travis Build Status](https://travis-ci.org/refinitiv/ngx_http_websocket_stat_module.svg?branch=master)](https://travis-ci.org/refinitiv/ngx_http_websocket_stat_module.svg?branch=master)
1+
![Tests](https://github.com/github/docs/actions/workflows/test.yml/badge.svg)
22

33

44
# NGINX module websocket connection and traffic statistics
@@ -9,31 +9,20 @@ Nginx module developed for logging and displaying statistic of websocket proxy c
99

1010
1. Configure nginx adding this module with or build this module as a dynamic module:
1111
```sh
12-
./configure (...) --add-module=./ngx_http_websocket_stat_module
12+
./configure (...) --add-module=./src/ngx_http_websocket_stat_module
1313
# or
14-
./configure (...) --add-dynamic-module=./ngx_http_websocket_stat_module && make modules
14+
./configure (...) --add-dynamic-module=./src/ngx_http_websocket_stat_module && make modules
1515
```
16-
2. Build nginx with make -j<n> command where n is number of cpu cores on your build machine
17-
18-
Alternatively could be used build script shipped along with module:
19-
From module directory run
20-
```sh
21-
test/build_helper.py build
22-
```
23-
It would download and build nginx and all required libraries (openssl, pcre and zlib) and generate nginx configuration file.
2416

2517
## Usage
2618

2719
To enable websocket frames logging specify `log_enabled on` and `ws_log_format` in server section of nginx config file. Additionally, specify `ws_log` to override the log file, which is used to log ws frames.
2820

2921
To customize connection open and close log messages use "open" and "close" parameter for ws_log_format directive.
30-
To log only when the full message is received/sent use "message" parameter for ws_log_format directive.
31-
32-
Maximum number of concurrent websocket connections could be specified with ws_max_connections on server section. This value applies to whole connections that are on nginx. Argument should be integer representing maximum connections. When client tries to open more connections it recevies close framee with 1013 error code and connection is closed on nginx side. If zero number of connections is given there would be no limit on websocket connections.
22+
To log only when the full message is received/sent use "server"/"client" parameters for ws_log_format directive.
3323

3424
To set maximum single connection lifetime use ws_conn_age parameter. Argument is time given in nginx time format (e.g. 1s, 1m 1h and so on). When connection's lifetime is exceeding specified value there is close websocket packet with 4001 error code generated and connection is closed.
3525

36-
3726
Here is a list of variables you can use in log format string:
3827

3928
* $ws_opcode - websocket packet opcode. Look into https://tools.ietf.org/html/rfc6455 Section 5.2, Base Framing Protocol.
@@ -52,22 +41,7 @@ Here is a list of variables you can use in log format string:
5241
* $server_port - Server's port
5342
* $upstream_addr - websocket backend address
5443

55-
## Example of configuration
56-
57-
```
58-
59-
server
60-
{
61-
ws_log <path/to/logfile>;
62-
ws_log_format "$time_local: packet of type $ws_opcode received from $ws_packet_source, packet size is $ws_payload_size";
63-
ws_log_format open "$time_local: Connection opened";
64-
ws_log_format close "$time_local: Connection closed";
65-
ws_max_connections 200;
66-
ws_conn_age 12h;
67-
...
68-
}
69-
70-
```
44+
See [nginx sample configuraion](docker/nginx.conf).
7145

7246
## Copyright
7347

docker-compose.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
version: "3.6"
2+
services:
3+
nginx:
4+
command: nginx -g "daemon off;"
5+
build:
6+
context: .
7+
dockerfile: ./docker/Dockerfile.nginx
8+
volumes:
9+
- ./docker/nginx.conf:/etc/nginx/nginx.conf
10+
- logs:/var/log/nginx
11+
12+
test_e2e:
13+
build:
14+
context: .
15+
dockerfile: ./docker/Dockerfile.e2e
16+
environment:
17+
- ENDPOINT=nginx
18+
- LOG_FILE_PATH=/var/log/nginx/ws.log
19+
volumes:
20+
- logs:/var/log/nginx
21+
depends_on:
22+
- nginx
23+
24+
test:
25+
build:
26+
context: .
27+
dockerfile: ./docker/Dockerfile.unit
28+
29+
volumes:
30+
logs:

docker/Dockerfile.e2e

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM rust:alpine3.13
2+
3+
USER root
4+
5+
RUN apk add --no-cache musl-dev && cargo install websocat
6+
7+
COPY ./docker/test_e2e.sh /usr/local/bin/test_e2e
8+
RUN chmod +x /usr/local/bin/test_e2e
9+
10+
CMD ["test_e2e"]

docker/Dockerfile.nginx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FROM k8s.gcr.io/ingress-nginx/controller:v0.44.0 as builder
2+
3+
USER root
4+
5+
WORKDIR /tmp
6+
7+
RUN apk add git openssl-dev pcre-dev zlib-dev libc-dev gcc make
8+
RUN NGINX_VERSION=$(nginx -v 2>&1 | sed 's/nginx version: nginx\///') && \
9+
wget -qO- https://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz | tar xvz && \
10+
mv nginx-${NGINX_VERSION} nginx
11+
COPY ./src /tmp/ngx_http_websocket_stat_module
12+
RUN cd ./nginx && \
13+
./configure --with-compat \
14+
--add-dynamic-module=../ngx_http_websocket_stat_module && \
15+
make modules
16+
17+
FROM k8s.gcr.io/ingress-nginx/controller:v0.44.0
18+
COPY --from=builder /tmp/nginx/objs/ngx_http_websocket_stat_module.so /etc/nginx/modules/

docker/Dockerfile.unit

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FROM alpine:3.10
2+
3+
USER root
4+
5+
RUN apk add gcc check-dev musl-dev
6+
7+
COPY ./src /tmp/ngx_http_websocket_stat_format/src
8+
COPY ./tests /tmp/ngx_http_websocket_stat_format/tests
9+
10+
WORKDIR /tmp/ngx_http_websocket_stat_format
11+
12+
ENV SRC_DIR="./src" \
13+
DIST_DIR="./dist" \
14+
TEST_DIR="./tests" \
15+
TEST_DIST_DIR="./tests/dist"
16+
17+
RUN mkdir ${DIST_DIR} ${TEST_DIST_DIR} && \
18+
gcc -g -DTEST -g -c ${SRC_DIR}/ngx_http_websocket_stat_format.c -o ${DIST_DIR}/ngx_http_websocket_stat_format.o && \
19+
gcc -g -DTEST ${TEST_DIR}/test_format.c -c -o ${TEST_DIST_DIR}/test_format.o -lcheck && \
20+
gcc -g -DTEST ${TEST_DIST_DIR}/test_format.o ${DIST_DIR}/ngx_http_websocket_stat_format.o -lcheck -lrt -o ${TEST_DIST_DIR}/test_format && \
21+
cp ${TEST_DIST_DIR}/test_format /usr/local/bin/ngx_http_websocket_stat_test && chmod +x /usr/local/bin/ngx_http_websocket_stat_test
22+
23+
CMD ["ngx_http_websocket_stat_test"]

docker/nginx.conf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
pid /tmp/nginx.pid;
2+
3+
load_module /etc/nginx/modules/ngx_http_websocket_stat_module.so;
4+
events {}
5+
http {
6+
server {
7+
listen 0.0.0.0:80;
8+
ws_log /var/log/nginx/ws.log;
9+
ws_log_format open "OPEN";
10+
ws_log_format server "SERVER->CLIENT: $ws_message_size ($http_upgrade)";
11+
ws_log_format client "CLIENT->SERVER: $ws_message_size ($http_upgrade)";
12+
ws_log_format close "CLOSE";
13+
ws_log_enabled on;
14+
location / {
15+
proxy_buffering off;
16+
proxy_request_buffering off;
17+
proxy_pass https://echo.websocket.org;
18+
proxy_ssl_server_name on;
19+
proxy_http_version 1.1;
20+
proxy_set_header Connection "upgrade";
21+
proxy_set_header Upgrade $http_upgrade;
22+
}
23+
}
24+
}

docker/test_e2e.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env sh
2+
3+
set -exo pipefail
4+
5+
> ${LOG_FILE_PATH}
6+
7+
cat <<EOF > /tmp/ws-expected.log
8+
OPEN
9+
CLIENT->SERVER: 1311 (websocket)
10+
SERVER->CLIENT: 1311 (websocket)
11+
CLOSE
12+
EOF
13+
14+
echo "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?" \
15+
| tr -d \\n \
16+
| websocat --text -1 ws://${ENDPOINT}
17+
18+
diff ${LOG_FILE_PATH} /tmp/ws-expected.log

0 commit comments

Comments
 (0)