-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
ngx_http_websockify_module.c
1166 lines (864 loc) · 33.2 KB
/
ngx_http_websockify_module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Nginx Websockify Module
* Embed websockify into Nginx
* https://github.com/tg123/websockify-nginx-module
*
* WebSocket to TCP Protocol Bridge/Proxy
* SEE ALSO: websockify https://github.com/kanaka/websockify
*
* Copyright (C) 2014 - 2015
*
* The MIT License (MIT)
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <ngx_sha1.h>
#include "websocket.h"
#define BUFFER_SIZE (MAX_WEBSOCKET_FRAME_SIZE + MAX_WEBSOCKET_FRAME_HEADER_SIZE + WEBSOCKET_FRAME_MASK_SIZE)
#define BUFFER_FLUSH_TIMEOUT 20
#ifdef _MSC_VER
#define WEBSOCKIFY_FUNC __FUNCTION__
#else
#define WEBSOCKIFY_FUNC __func__
#endif
typedef struct ngx_http_websockify_loc_conf_s {
ngx_http_upstream_conf_t upstream;
ngx_array_t *websockify_lengths;
ngx_array_t *websockify_values;
} ngx_http_websockify_loc_conf_t;
typedef enum {
WEBSOCKIFY_ENCODING_PROTOCOL_UNSET = 0,
WEBSOCKIFY_ENCODING_PROTOCOL_BASE64,
WEBSOCKIFY_ENCODING_PROTOCOL_BINARY
} websockify_encoding_protocol_e;
typedef struct ngx_http_websockify_request_ctx_s {
ngx_http_request_t *request;
ngx_flag_t need_cleanup_fake_recv_buff;
ngx_flag_t closed;
websockify_encoding_protocol_e encoding_protocol;
ngx_buf_t *encode_send_buf;
ngx_buf_t *decode_send_buf;
ngx_event_t flush_all_ev;
ngx_send_pt original_ngx_downstream_send;
ngx_send_pt original_ngx_upstream_send;
ngx_recv_pt original_ngx_upstream_recv;
} ngx_http_websockify_ctx_t ;
ngx_module_t ngx_http_websockify_module;
static ngx_int_t ngx_http_websockify_handler(ngx_http_request_t *r);
static char *ngx_http_websockify(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static void *ngx_http_websockify_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_websockify_merge_loc_conf(ngx_conf_t *cf, void *parent,
void *child);
static ngx_int_t ngx_http_websockify_create_request(ngx_http_request_t *r);
static ngx_int_t ngx_http_websockify_reinit_request(ngx_http_request_t *r);
static ngx_int_t ngx_http_websockify_process_header(ngx_http_request_t *r);
static void ngx_http_websockify_abort_request(ngx_http_request_t *r);
static void ngx_http_websockify_finalize_request(ngx_http_request_t *r,
ngx_int_t rc);
static void ngx_http_websockify_flush_all(ngx_event_t *ev);
static ssize_t ngx_http_websockify_flush(ngx_connection_t *c, ngx_buf_t *b,
ngx_send_pt send);
static ngx_inline ssize_t ngx_http_websockify_flush_downstream(
ngx_http_websockify_ctx_t *ctx);
static ngx_inline ssize_t ngx_http_websockify_flush_upstream(
ngx_http_websockify_ctx_t *ctx);
typedef ssize_t (*ngx_http_websockify_flush_pt)(ngx_http_websockify_ctx_t *ctx);
static ngx_inline size_t ngx_http_websockify_freesize(ngx_buf_t *b,
size_t size);
static ssize_t
ngx_http_websockify_send_and_transform(ngx_connection_t *c, u_char *buf,
const size_t size, ngx_send_pt transform, ngx_http_websockify_flush_pt flush);
static ssize_t ngx_http_websockify_send_downstream_with_encode(
ngx_connection_t *c,
u_char *buf, const size_t size);
static ssize_t
ngx_http_websockify_encode_one_frame(ngx_connection_t *c, u_char *buf,
const size_t size);
static ssize_t ngx_http_websockify_send_downstream_frame(
ngx_http_websockify_ctx_t *ctx, u_char opcode, u_char *payload, size_t size);
static ssize_t ngx_http_websockify_send_upstream_with_decode(
ngx_connection_t *c, u_char *buf, const size_t size);
static ssize_t
ngx_http_websockify_decode_one_frame(ngx_connection_t *c, u_char *buf,
const size_t size);
static ssize_t ngx_http_websockify_empty_recv(ngx_connection_t *c, u_char *buf,
size_t size);
static ngx_inline size_t ngx_http_websockify_freesize(ngx_buf_t *b, size_t size)
{
size_t free_size;
free_size = b->end - b->last;
if (free_size >= size) {
return free_size;
}
return 0;
}
static ngx_inline ssize_t
ngx_http_websockify_flush_downstream(ngx_http_websockify_ctx_t *ctx)
{
ngx_http_request_t *r;
r = ctx->request;
if (ctx->closed) {
return NGX_ERROR;
}
if ( r->connection ) {
return ngx_http_websockify_flush(r->connection, ctx->encode_send_buf,
ctx->original_ngx_downstream_send);
}
return NGX_ERROR;
}
static ngx_inline ssize_t
ngx_http_websockify_flush_upstream(ngx_http_websockify_ctx_t *ctx)
{
ngx_http_request_t *r;
r = ctx->request;
if (ctx->closed) {
return NGX_ERROR;
}
if ( r->upstream->peer.connection ) {
return ngx_http_websockify_flush(r->upstream->peer.connection,
ctx->decode_send_buf, ctx->original_ngx_upstream_send);
}
return NGX_ERROR;
}
static void
ngx_http_websockify_flush_all(ngx_event_t *ev)
{
ngx_http_websockify_ctx_t *ctx;
ctx = ev->data;
if (ngx_http_websockify_flush_downstream(ctx) == NGX_ERROR
|| ngx_http_websockify_flush_upstream(ctx) == NGX_ERROR) {
ctx->closed = 1;
}
}
static ssize_t
ngx_http_websockify_flush(ngx_connection_t *c, ngx_buf_t *b,
ngx_send_pt send)
{
ngx_http_websockify_ctx_t *ctx;
ngx_http_request_t *r;
ssize_t n;
r = c->data;
ctx = ngx_http_get_module_ctx(r, ngx_http_websockify_module);
if (b->last == b->pos) {
return 0;
}
for (;;) {
n = send(c, b->pos, b->last - b->pos);
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0, "%s: sent buffer : %d / %d",
WEBSOCKIFY_FUNC, n, b->last - b->pos);
if (n > 0) {
b->pos += n;
if (b->pos == b->last) {
b->pos = b->start;
b->last = b->start;
}
}
if (n == NGX_AGAIN) {
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "%s: add timer", WEBSOCKIFY_FUNC);
ngx_add_timer(&(ctx->flush_all_ev), BUFFER_FLUSH_TIMEOUT);
}
if ( (n <= 0) || (b->pos == b->last) ) {
break;
}
}
return n;
}
static ssize_t
ngx_http_websockify_send_downstream_frame(ngx_http_websockify_ctx_t *ctx,
u_char opcode, u_char *payload, size_t size)
{
ngx_buf_t *b;
size_t header_length;
if (ngx_http_websockify_flush_downstream(ctx) == NGX_ERROR ) {
return NGX_ERROR;
}
header_length = websocket_server_encoded_header_length(size);
b = ctx->encode_send_buf;
if ( !ngx_http_websockify_freesize(b, (header_length + size))) {
return NGX_AGAIN;
}
websocket_server_write_frame_header(b->last, opcode, size);
if (size > 0) {
ngx_memcpy(b->last + header_length, payload, size);
}
if (ngx_http_websockify_flush_downstream(ctx) == NGX_ERROR ) {
return NGX_ERROR;
}
b->last += header_length + size;
return header_length + size;
}
static ssize_t
ngx_http_websockify_send_and_transform(ngx_connection_t *c, u_char *buf,
const size_t size, ngx_send_pt transform, ngx_http_websockify_flush_pt flush)
{
ngx_http_websockify_ctx_t *ctx;
ngx_http_request_t *r;
size_t total = 0;
ssize_t consumed;
r = c->data;
ctx = ngx_http_get_module_ctx(r, ngx_http_websockify_module);
if (flush(ctx) == NGX_ERROR ) {
return NGX_ERROR;
}
while (size > total) {
consumed = transform(c, buf + total, size - total);
if (consumed == NGX_ERROR) {
return NGX_ERROR;
} else if (consumed == NGX_AGAIN) {
if (flush(ctx) > 0) {
continue;
}
if (total == 0) {
return NGX_AGAIN;
}
break;
} else if (consumed > 0) {
total += consumed;
} else {
// should never happen
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "%s: assert failed",
WEBSOCKIFY_FUNC);
return NGX_ERROR;
}
}
if (flush(ctx) == NGX_ERROR ) {
return NGX_ERROR;
}
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0, "%s: %d / %d", WEBSOCKIFY_FUNC,
total , size);
return total;
}
static ssize_t
ngx_http_websockify_send_downstream_with_encode(ngx_connection_t *c,
u_char *buf,
const size_t size)
{
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "%s: [%d]", WEBSOCKIFY_FUNC,
size);
return ngx_http_websockify_send_and_transform(c, buf, size,
ngx_http_websockify_encode_one_frame, ngx_http_websockify_flush_downstream);
}
static ssize_t
ngx_http_websockify_encode_one_frame(ngx_connection_t *c,
u_char *buf,
const size_t size)
{
ngx_http_websockify_ctx_t *ctx;
ngx_buf_t *b;
ngx_http_request_t *r;
size_t payload_length;
size_t header_length;
size_t free_size;
size_t consumed_size = 0;
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "%s: [%d]", WEBSOCKIFY_FUNC,
size);
r = c->data;
ctx = ngx_http_get_module_ctx(r, ngx_http_websockify_module);
b = ctx->encode_send_buf;
free_size = ngx_http_websockify_freesize(b, MIN_SERVER_FRAME_SIZE);
if ( !free_size ) {
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
"%s: no enough buffer, try again... ", WEBSOCKIFY_FUNC);
return NGX_AGAIN;
}
free_size = ngx_min(free_size,
MAX_WEBSOCKET_FRAME_SIZE + MAX_WEBSOCKET_FRAME_HEADER_SIZE);
switch (ctx->encoding_protocol) {
case WEBSOCKIFY_ENCODING_PROTOCOL_BASE64:
// inverse of ngx_base64_encoded_length.
consumed_size = ngx_min(websocket_payload_consume_size(free_size) / 4 * 3 - 2,
size);
payload_length = ngx_base64_encoded_length(consumed_size);
header_length = websocket_server_encoded_header_length(payload_length);
websocket_server_write_frame_header(b->last, WEBSOCKET_OPCODE_TEXT,
payload_length);
// base64 encode
ngx_str_t src;
ngx_str_t dst;
src.data = buf;
src.len = consumed_size;
dst.data = b->last + header_length;
ngx_encode_base64(&dst, &src);
break;
case WEBSOCKIFY_ENCODING_PROTOCOL_BINARY:
consumed_size = ngx_min(websocket_payload_consume_size(free_size), size);
payload_length = consumed_size;
header_length = websocket_server_encoded_header_length(payload_length);
websocket_server_write_frame_header(b->last, WEBSOCKET_OPCODE_BINARY,
payload_length);
ngx_memcpy(b->last + header_length, buf, consumed_size);
break;
default:
// not support
return NGX_ERROR;
break;
}
b->last += header_length + payload_length; // push encoded data into buffer
return (ssize_t)consumed_size;
}
static ssize_t
ngx_http_websockify_send_upstream_with_decode(ngx_connection_t *c, u_char *buf,
const size_t size)
{
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "%s: [%d]", WEBSOCKIFY_FUNC,
size);
return ngx_http_websockify_send_and_transform(c, buf, size,
ngx_http_websockify_decode_one_frame, ngx_http_websockify_flush_upstream);
}
static ssize_t
ngx_http_websockify_decode_one_frame(ngx_connection_t *c, u_char *buf,
const size_t size)
{
ngx_http_websockify_ctx_t *ctx;
ngx_buf_t *b;
ngx_http_request_t *r;
websocket_frame_t frame;
ssize_t header_length;
size_t used_buf_size;
size_t need_buf_size;
ssize_t reply;
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "%s: [%d]", WEBSOCKIFY_FUNC,
size);
r = c->data;
ctx = ngx_http_get_module_ctx(r, ngx_http_websockify_module);
b = ctx->decode_send_buf;
header_length = websocket_server_decode_next_frame(&frame, buf, size);
if (header_length == NGX_ERROR) {
ngx_log_error(NGX_LOG_ERR, c->log, 0,
"%s: decoding websocket frame > 65535 is not supported!",
WEBSOCKIFY_FUNC);
return NGX_ERROR;
}
if (header_length == NGX_AGAIN) {
return NGX_AGAIN;
}
used_buf_size = 0;
switch (frame.opcode) {
case WEBSOCKET_OPCODE_CONTINUATION:
case WEBSOCKET_OPCODE_PONG:
// do nothing
break;
case WEBSOCKET_OPCODE_TEXT:
case WEBSOCKET_OPCODE_BINARY:
switch (ctx->encoding_protocol) {
case WEBSOCKIFY_ENCODING_PROTOCOL_BASE64:
need_buf_size = ngx_base64_decoded_length(frame.payload_length);
break;
case WEBSOCKIFY_ENCODING_PROTOCOL_BINARY:
need_buf_size = frame.payload_length;
break;
default:
return NGX_ERROR;
break;
}
if ( !ngx_http_websockify_freesize(b, need_buf_size) ) {
if (need_buf_size + b->start > b->end) {
ngx_log_error(NGX_LOG_ERR, c->log, 0, "%s: buffer size too small",
WEBSOCKIFY_FUNC);
return NGX_ERROR;
}
return NGX_AGAIN;
}
websocket_server_decode_unmask_payload(&frame);
switch (ctx->encoding_protocol) {
case WEBSOCKIFY_ENCODING_PROTOCOL_BASE64:;
// base64 decode the data
ngx_str_t src;
ngx_str_t dst;
src.data = frame.payload;
src.len = frame.payload_length;
dst.data = b->last;
if ( ngx_decode_base64(&dst, &src) != NGX_OK ) {
ngx_log_error(NGX_LOG_ERR, c->log, 0,
"%s: decode websocket base64 frame payload error!", WEBSOCKIFY_FUNC);
return NGX_ERROR;
}
used_buf_size = dst.len;
break;
case WEBSOCKIFY_ENCODING_PROTOCOL_BINARY:
ngx_memcpy(b->last, frame.payload, frame.payload_length);
used_buf_size = frame.payload_length;
break;
default:
return NGX_ERROR;
break;
}
break;
case WEBSOCKET_OPCODE_CLOSE:
// TODO testcases
// TODO status code hardcoded (1000 = 03e8)
reply = ngx_http_websockify_send_downstream_frame(ctx, WEBSOCKET_OPCODE_CLOSE,
(u_char *)"\x03\xe8 Closed", 9);
if (reply < 0) {
return reply;
}
ctx->closed = 1;
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "%s: CLOSE replied [%d]",
WEBSOCKIFY_FUNC,
size);
return NGX_ERROR; // let ngx close connection
break;
case WEBSOCKET_OPCODE_PING:
// TODO testcases
if ( !ngx_http_websockify_freesize(ctx->encode_send_buf,
websocket_server_encoded_length(frame.payload_length))) {
return NGX_AGAIN;
}
websocket_server_decode_unmask_payload(&frame);
reply = ngx_http_websockify_send_downstream_frame(ctx, WEBSOCKET_OPCODE_PONG,
frame.payload, frame.payload_length);
if (reply < 0) {
return reply;
}
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "%s: PING replied [%d]",
WEBSOCKIFY_FUNC,
size);
break;
default:
ngx_log_error(NGX_LOG_ALERT, c->log, 0, "%s: unsupported opcode: [%d] ",
WEBSOCKIFY_FUNC, frame.opcode);
break;
}
b->last += used_buf_size; // push decoded data into buffer
return (ssize_t)(header_length + frame.payload_length);
}
static ngx_command_t ngx_http_websockify_commands[] = {
{
ngx_string("websockify_pass"),
NGX_HTTP_LOC_CONF | NGX_HTTP_LIF_CONF | NGX_CONF_TAKE1,
&ngx_http_websockify,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
{
ngx_string("websockify_buffer_size"),
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_websockify_loc_conf_t, upstream.buffer_size),
NULL
},
{
ngx_string("websockify_connect_timeout"),
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_websockify_loc_conf_t, upstream.connect_timeout),
NULL
},
{
ngx_string("websockify_send_timeout"),
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_websockify_loc_conf_t, upstream.send_timeout),
NULL
},
{
ngx_string("websockify_read_timeout"),
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_websockify_loc_conf_t, upstream.read_timeout),
NULL
},
ngx_null_command
};
static void *
ngx_http_websockify_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_websockify_loc_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_websockify_loc_conf_t));
if (conf == NULL) {
return NULL;
}
/*
* set by ngx_pcalloc():
*
* conf->upstream.bufs.num = 0;
* conf->upstream.next_upstream = 0;
* conf->upstream.temp_path = NULL;
* conf->upstream.uri = { 0, NULL };
* conf->upstream.location = NULL;
*/
conf->upstream.local = NGX_CONF_UNSET_PTR;
conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
//conf->upstream.buffer_size = BUFFER_SIZE;
/* the hardcoded values */
conf->upstream.cyclic_temp_file = 0;
conf->upstream.buffering = 0;
//conf->upstream.buffer_size = 0;
//conf->upstream.busy_buffers_size = 0;
conf->upstream.ignore_client_abort = 0;
conf->upstream.send_lowat = 0;
conf->upstream.bufs.num = 0;
conf->upstream.busy_buffers_size = 0;
conf->upstream.max_temp_file_size = 0;
conf->upstream.temp_file_write_size = 0;
conf->upstream.intercept_errors = 1;
conf->upstream.intercept_404 = 1;
conf->upstream.pass_request_headers = 0;
conf->upstream.pass_request_body = 0;
return conf;
}
static char *
ngx_http_websockify_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_websockify_loc_conf_t *prev = parent;
ngx_http_websockify_loc_conf_t *conf = child;
ngx_conf_merge_ptr_value(conf->upstream.local,
prev->upstream.local, NULL);
ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
prev->upstream.connect_timeout, 60000);
ngx_conf_merge_msec_value(conf->upstream.send_timeout,
prev->upstream.send_timeout, 60000);
ngx_conf_merge_msec_value(conf->upstream.read_timeout,
prev->upstream.read_timeout, 60000);
ngx_conf_merge_size_value(conf->upstream.buffer_size,
prev->upstream.buffer_size,
(size_t) BUFFER_SIZE);
ngx_conf_merge_bitmask_value(conf->upstream.next_upstream,
prev->upstream.next_upstream,
(NGX_CONF_BITMASK_SET
| NGX_HTTP_UPSTREAM_FT_ERROR
| NGX_HTTP_UPSTREAM_FT_TIMEOUT));
if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) {
conf->upstream.next_upstream = NGX_CONF_BITMASK_SET
| NGX_HTTP_UPSTREAM_FT_OFF;
}
if (conf->upstream.upstream == NULL) {
conf->upstream.upstream = prev->upstream.upstream;
}
if (conf->websockify_lengths == NULL) {
conf->websockify_lengths = prev->websockify_lengths;
conf->websockify_values = prev->websockify_values;
}
if (conf->upstream.buffer_size == 0) {
conf->upstream.buffer_size = BUFFER_SIZE;
}
return NGX_CONF_OK;
}
static ngx_http_module_t ngx_http_websockify_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_websockify_create_loc_conf, /* create location configuration */
ngx_http_websockify_merge_loc_conf /* merge location configuration */
};
ngx_module_t ngx_http_websockify_module = {
NGX_MODULE_V1,
&ngx_http_websockify_module_ctx, /* module context */
ngx_http_websockify_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_int_t
ngx_http_websockify_handler(ngx_http_request_t *r)
{
ngx_int_t rc;
ngx_http_upstream_t *u;
ngx_http_websockify_loc_conf_t *wlcf;
ngx_http_websockify_ctx_t *ctx;
ngx_str_t var_pass;
ngx_url_t url;
wlcf = ngx_http_get_module_loc_conf(r, ngx_http_websockify_module);
ctx = ngx_http_get_module_ctx(r, ngx_http_websockify_module);
u = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_t));
if (u == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
if ( wlcf->websockify_lengths ) { // parse from var
if (ngx_http_script_run(r, &var_pass, wlcf->websockify_lengths->elts, 0,
wlcf->websockify_values->elts) == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
u->resolved = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_resolved_t));
if (u->resolved == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_memzero(&url, sizeof(ngx_url_t));
url.url.len = var_pass.len;
url.url.data = var_pass.data;
url.uri_part = 1;
url.no_resolve = 1;
if (ngx_parse_url(r->pool, &url) != NGX_OK) {
if (url.err) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"%s in upstream \"%V\"", url.err, &url.url);
}
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
u->resolved->host = url.host;
u->resolved->port = (in_port_t) (url.no_port ? 5900 : url.port);
u->resolved->no_port = url.no_port;
if (url.addrs && url.addrs[0].sockaddr) {
u->resolved->sockaddr = url.addrs[0].sockaddr;
u->resolved->socklen = url.addrs[0].socklen;
u->resolved->naddrs = 1;
}
}
u->schema.len = sizeof("websockify://") - 1;
u->schema.data = (u_char *) "websockify://";
u->peer.log = r->connection->log;
u->peer.log_error = NGX_ERROR_ERR;
u->output.tag = (ngx_buf_tag_t) &ngx_http_websockify_module;
u->conf = &wlcf->upstream;
u->create_request = ngx_http_websockify_create_request;
u->reinit_request = ngx_http_websockify_reinit_request;
u->process_header = ngx_http_websockify_process_header;
u->abort_request = ngx_http_websockify_abort_request;
u->finalize_request = ngx_http_websockify_finalize_request;
r->upstream = u;
ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_websockify_ctx_t));
if (ctx == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ctx->request = r;
ctx->encode_send_buf = ngx_create_temp_buf(r->pool, u->conf->buffer_size);
ctx->decode_send_buf = ngx_create_temp_buf(r->pool, u->conf->buffer_size);
ctx->encoding_protocol = WEBSOCKIFY_ENCODING_PROTOCOL_UNSET;
ctx->flush_all_ev.log = r->connection->log;
ctx->flush_all_ev.data = ctx;
ctx->flush_all_ev.handler = ngx_http_websockify_flush_all;
ctx-> closed = 0;
if (!ctx->encode_send_buf || !ctx->decode_send_buf) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_http_set_ctx(r, ctx, ngx_http_websockify_module);
//ngx_http_upstream_init(r);
rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init);
if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
return rc;
}
return NGX_DONE;
}
static ngx_int_t
ngx_http_websockify_create_request(ngx_http_request_t *r)
{
ngx_http_websockify_ctx_t *ctx;
ctx = ngx_http_get_module_ctx(r, ngx_http_websockify_module);
if ( ctx == NULL ) {
return NGX_ERROR;
}
if ( r->method != NGX_HTTP_GET ) {
r->headers_out.status = NGX_HTTP_BAD_REQUEST;
return NGX_OK;
}
// tricky, let nginx call my reinit
// do nothing to tcp connections
r->upstream->request_sent = 1;
return NGX_OK;
}
static ssize_t
ngx_http_websockify_empty_recv(ngx_connection_t *c, u_char *buf, size_t size)
{
ngx_http_request_t *r;
ngx_http_websockify_ctx_t *ctx;
ssize_t n;
r = c->data;
ctx = ngx_http_get_module_ctx(r, ngx_http_websockify_module);
n = ctx->original_ngx_upstream_recv(c, buf, 1);
c->recv = ctx->original_ngx_upstream_recv;
// tricky, if n == NGX_ERROR upstream might not have connections now
// return control to nginx and let nginx deal with it
if (n == NGX_ERROR) {
return NGX_AGAIN;
}
if (n == NGX_AGAIN) {
ctx->need_cleanup_fake_recv_buff = 1;
return 1;
}
ctx->need_cleanup_fake_recv_buff = 0;
return n;
}
static ngx_int_t
ngx_http_websockify_reinit_request(ngx_http_request_t *r)
{
ngx_http_upstream_t *u;
ngx_http_websockify_ctx_t *ctx;
ctx = ngx_http_get_module_ctx(r, ngx_http_websockify_module);
if (r->header_sent) {
return NGX_OK;
}
u = r->upstream;
// hack for empty reply tcp connection
ctx->original_ngx_upstream_recv = u->peer.connection->recv;
u->peer.connection->recv = ngx_http_websockify_empty_recv;
u->read_event_handler(r, r->upstream);
// this happens if some error occurs during read_event_handler
if ( u->peer.connection == NULL ) {
return NGX_ERROR;
}
return NGX_OK;
}
static ngx_int_t
ngx_http_websockify_process_header(ngx_http_request_t *r)
{
ngx_http_websockify_ctx_t *ctx;
ngx_http_upstream_t *u;
ngx_table_elt_t *h;
ngx_list_part_t *part;
ngx_uint_t i;
ngx_sha1_t sha1;
ngx_str_t ws_key = {0, NULL};
ngx_flag_t accept_binary = 0;
ngx_flag_t accept_base64 = 0;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"websockify : ngx_http_websockify_process_header");
ctx = ngx_http_get_module_ctx(r, ngx_http_websockify_module);
if ( ctx == NULL ) {
return NGX_ERROR;
}
u = r->upstream;
if (ctx->need_cleanup_fake_recv_buff) {
u->buffer.last = u->buffer.start;
}
part = &r->headers_in.headers.part;
h = part->elts;
for (i = 0; /* void */; i++) {
if (i >= part->nelts) {
if (part->next == NULL) {
break;
}
part = part->next;
h = part->elts;
i = 0;
}
if (ngx_strncasecmp(h[i].key.data, (u_char *) "Sec-WebSocket-Key",
h[i].key.len) == 0) {