PageRenderTime 67ms CodeModel.GetById 15ms app.highlight 46ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/test-nginx/cases/lua-tests/066-socket-receiveuntil.t

https://github.com/liyao0409/tengine
Unknown | 1333 lines | 1058 code | 275 blank | 0 comment | 0 complexity | 5854c83dd5fa060d28fd60c3a6348f4a MD5 | raw file
   1# vim:set ft= ts=4 sw=4 et fdm=marker:
   2
   3use lib 'lib';
   4use Test::Nginx::Socket;
   5
   6repeat_each(2);
   7
   8plan tests => repeat_each() * (blocks() * 3);
   9
  10our $HtmlDir = html_dir;
  11
  12$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;
  13
  14no_long_string();
  15#no_diff();
  16#log_level 'warn';
  17
  18run_tests();
  19
  20__DATA__
  21
  22=== TEST 1: memcached read lines
  23--- config
  24    server_tokens off;
  25    location /t {
  26        set $port $TEST_NGINX_MEMCACHED_PORT;
  27
  28        content_by_lua '
  29            local sock = ngx.socket.tcp()
  30            local port = ngx.var.port
  31
  32            local ok, err = sock:connect("127.0.0.1", port)
  33            if not ok then
  34                ngx.say("failed to connect: ", err)
  35                return
  36            end
  37
  38            ngx.say("connected: ", ok)
  39
  40            local req = "flush_all\\r\\n"
  41
  42            local bytes, err = sock:send(req)
  43            if not bytes then
  44                ngx.say("failed to send request: ", err)
  45                return
  46            end
  47            ngx.say("request sent: ", bytes)
  48
  49            local readline = sock:receiveuntil("\\r\\n")
  50            local line, err, part = readline()
  51            if line then
  52                ngx.say("received: ", line)
  53
  54            else
  55                ngx.say("failed to receive a line: ", err, " [", part, "]")
  56            end
  57
  58            ok, err = sock:close()
  59            ngx.say("close: ", ok, " ", err)
  60        ';
  61    }
  62--- request
  63GET /t
  64--- response_body
  65connected: 1
  66request sent: 11
  67received: OK
  68close: 1 nil
  69--- no_error_log
  70[error]
  71
  72
  73
  74=== TEST 2: http read lines
  75--- config
  76    server_tokens off;
  77    location /t {
  78        set $port $TEST_NGINX_SERVER_PORT;
  79
  80        content_by_lua '
  81            local sock = ngx.socket.tcp()
  82            local port = ngx.var.port
  83
  84            local ok, err = sock:connect("127.0.0.1", port)
  85            if not ok then
  86                ngx.say("failed to connect: ", err)
  87                return
  88            end
  89
  90            ngx.say("connected: ", ok)
  91
  92            local req = "GET /foo HTTP/1.0\\r\\nHost: localhost\\r\\nConnection: close\\r\\n\\r\\n"
  93
  94            local bytes, err = sock:send(req)
  95            if not bytes then
  96                ngx.say("failed to send request: ", err)
  97                return
  98            end
  99            ngx.say("request sent: ", bytes)
 100
 101            local readline = sock:receiveuntil("\\r\\n")
 102            local line, err, part
 103
 104            for i = 1, 7 do
 105                line, err, part = readline()
 106                if line then
 107                    ngx.say("read: ", line)
 108
 109                else
 110                    ngx.say("failed to read a line: ", err, " [", part, "]")
 111                end
 112            end
 113
 114            ok, err = sock:close()
 115            ngx.say("close: ", ok, " ", err)
 116        ';
 117    }
 118
 119    location /foo {
 120        content_by_lua 'ngx.say("foo")';
 121        more_clear_headers Date;
 122    }
 123--- request
 124GET /t
 125--- response_body eval
 126qq{connected: 1
 127request sent: 57
 128read: HTTP/1.1 200 OK
 129read: Server: nginx
 130read: Content-Type: text/plain
 131read: Content-Length: 4
 132read: Connection: close
 133read: 
 134failed to read a line: closed [foo
 135]
 136close: nil closed
 137}
 138--- no_error_log
 139[error]
 140
 141
 142
 143=== TEST 3: http read all the headers in a single run
 144--- config
 145    server_tokens off;
 146    location /t {
 147        set $port $TEST_NGINX_SERVER_PORT;
 148
 149        content_by_lua '
 150            local sock = ngx.socket.tcp()
 151            local port = ngx.var.port
 152
 153            local ok, err = sock:connect("127.0.0.1", port)
 154            if not ok then
 155                ngx.say("failed to connect: ", err)
 156                return
 157            end
 158
 159            ngx.say("connected: ", ok)
 160
 161            local req = "GET /foo HTTP/1.0\\r\\nHost: localhost\\r\\nConnection: close\\r\\n\\r\\n"
 162
 163            local bytes, err = sock:send(req)
 164            if not bytes then
 165                ngx.say("failed to send request: ", err)
 166                return
 167            end
 168            ngx.say("request sent: ", bytes)
 169
 170            local read_headers = sock:receiveuntil("\\r\\n\\r\\n")
 171            local line, err, part
 172
 173            for i = 1, 2 do
 174                line, err, part = read_headers()
 175                if line then
 176                    ngx.say("read: ", line)
 177
 178                else
 179                    ngx.say("failed to read a line: ", err, " [", part, "]")
 180                end
 181            end
 182
 183            ok, err = sock:close()
 184            ngx.say("close: ", ok, " ", err)
 185        ';
 186    }
 187
 188    location /foo {
 189        content_by_lua 'ngx.say("foo")';
 190        more_clear_headers Date;
 191    }
 192--- request
 193GET /t
 194--- response_body eval
 195qq{connected: 1
 196request sent: 57
 197read: HTTP/1.1 200 OK\r
 198Server: nginx\r
 199Content-Type: text/plain\r
 200Content-Length: 4\r
 201Connection: close
 202failed to read a line: closed [foo
 203]
 204close: nil closed
 205}
 206--- no_error_log
 207[error]
 208
 209
 210
 211=== TEST 4: ambiguous boundary patterns (abcabd)
 212--- config
 213    server_tokens off;
 214    location /t {
 215        set $port $TEST_NGINX_SERVER_PORT;
 216
 217        content_by_lua '
 218            -- collectgarbage("collect")
 219
 220            local sock = ngx.socket.tcp()
 221            local port = ngx.var.port
 222
 223            local ok, err = sock:connect("127.0.0.1", port)
 224            if not ok then
 225                ngx.say("failed to connect: ", err)
 226                return
 227            end
 228
 229            ngx.say("connected: ", ok)
 230
 231            local req = "GET /foo HTTP/1.0\\r\\nHost: localhost\\r\\nConnection: close\\r\\n\\r\\n"
 232
 233            local bytes, err = sock:send(req)
 234            if not bytes then
 235                ngx.say("failed to send request: ", err)
 236                return
 237            end
 238            ngx.say("request sent: ", bytes)
 239
 240            local read_headers = sock:receiveuntil("\\r\\n\\r\\n")
 241            local headers, err, part = read_headers()
 242            if not headers then
 243                ngx.say("failed to read headers: ", err, " [", part, "]")
 244            end
 245
 246            local reader = sock:receiveuntil("abcabd")
 247
 248            for i = 1, 2 do
 249                line, err, part = reader()
 250                if line then
 251                    ngx.say("read: ", line)
 252
 253                else
 254                    ngx.say("failed to read a line: ", err, " [", part, "]")
 255                end
 256            end
 257
 258            ok, err = sock:close()
 259            ngx.say("close: ", ok, " ", err)
 260        ';
 261    }
 262
 263    location /foo {
 264        content_by_lua 'ngx.say("abcabcabd")';
 265        more_clear_headers Date;
 266    }
 267--- request
 268GET /t
 269--- response_body eval
 270qq{connected: 1
 271request sent: 57
 272read: abc
 273failed to read a line: closed [
 274]
 275close: nil closed
 276}
 277--- no_error_log
 278[error]
 279
 280
 281
 282=== TEST 5: ambiguous boundary patterns (aa)
 283--- config
 284    server_tokens off;
 285    location /t {
 286        set $port $TEST_NGINX_SERVER_PORT;
 287
 288        content_by_lua '
 289            -- collectgarbage("collect")
 290
 291            local sock = ngx.socket.tcp()
 292            local port = ngx.var.port
 293
 294            local ok, err = sock:connect("127.0.0.1", port)
 295            if not ok then
 296                ngx.say("failed to connect: ", err)
 297                return
 298            end
 299
 300            ngx.say("connected: ", ok)
 301
 302            local req = "GET /foo HTTP/1.0\\r\\nHost: localhost\\r\\nConnection: close\\r\\n\\r\\n"
 303
 304            local bytes, err = sock:send(req)
 305            if not bytes then
 306                ngx.say("failed to send request: ", err)
 307                return
 308            end
 309            ngx.say("request sent: ", bytes)
 310
 311            local read_headers = sock:receiveuntil("\\r\\n\\r\\n")
 312            local headers, err, part = read_headers()
 313            if not headers then
 314                ngx.say("failed to read headers: ", err, " [", part, "]")
 315            end
 316
 317            local reader = sock:receiveuntil("aa")
 318
 319            for i = 1, 2 do
 320                line, err, part = reader()
 321                if line then
 322                    ngx.say("read: ", line)
 323
 324                else
 325                    ngx.say("failed to read a line: ", err, " [", part, "]")
 326                end
 327            end
 328
 329            ok, err = sock:close()
 330            ngx.say("close: ", ok, " ", err)
 331        ';
 332    }
 333
 334    location /foo {
 335        content_by_lua 'ngx.say("abcabcaad")';
 336        more_clear_headers Date;
 337    }
 338--- request
 339GET /t
 340--- response_body eval
 341qq{connected: 1
 342request sent: 57
 343read: abcabc
 344failed to read a line: closed [d
 345]
 346close: nil closed
 347}
 348--- no_error_log
 349[error]
 350
 351
 352
 353=== TEST 6: ambiguous boundary patterns (aaa)
 354--- config
 355    server_tokens off;
 356    location /t {
 357        set $port $TEST_NGINX_SERVER_PORT;
 358
 359        content_by_lua '
 360            -- collectgarbage("collect")
 361
 362            local sock = ngx.socket.tcp()
 363            local port = ngx.var.port
 364
 365            local ok, err = sock:connect("127.0.0.1", port)
 366            if not ok then
 367                ngx.say("failed to connect: ", err)
 368                return
 369            end
 370
 371            ngx.say("connected: ", ok)
 372
 373            local req = "GET /foo HTTP/1.0\\r\\nHost: localhost\\r\\nConnection: close\\r\\n\\r\\n"
 374
 375            local bytes, err = sock:send(req)
 376            if not bytes then
 377                ngx.say("failed to send request: ", err)
 378                return
 379            end
 380            ngx.say("request sent: ", bytes)
 381
 382            local read_headers = sock:receiveuntil("\\r\\n\\r\\n")
 383            local headers, err, part = read_headers()
 384            if not headers then
 385                ngx.say("failed to read headers: ", err, " [", part, "]")
 386            end
 387
 388            local reader = sock:receiveuntil("aaa")
 389
 390            for i = 1, 2 do
 391                line, err, part = reader()
 392                if line then
 393                    ngx.say("read: ", line)
 394
 395                else
 396                    ngx.say("failed to read a line: ", err, " [", part, "]")
 397                end
 398            end
 399
 400            ok, err = sock:close()
 401            ngx.say("close: ", ok, " ", err)
 402        ';
 403    }
 404
 405    location /foo {
 406        echo abaabcaaaef;
 407        more_clear_headers Date;
 408    }
 409--- request
 410GET /t
 411--- response_body eval
 412qq{connected: 1
 413request sent: 57
 414read: abaabc
 415failed to read a line: closed [ef
 416]
 417close: nil closed
 418}
 419--- no_error_log
 420[error]
 421
 422
 423
 424=== TEST 7: ambiguous boundary patterns (aaaaad)
 425--- config
 426    server_tokens off;
 427    location /t {
 428        set $port $TEST_NGINX_SERVER_PORT;
 429
 430        content_by_lua '
 431            -- collectgarbage("collect")
 432
 433            local sock = ngx.socket.tcp()
 434            local port = ngx.var.port
 435
 436            local ok, err = sock:connect("127.0.0.1", port)
 437            if not ok then
 438                ngx.say("failed to connect: ", err)
 439                return
 440            end
 441
 442            ngx.say("connected: ", ok)
 443
 444            local req = "GET /foo HTTP/1.0\\r\\nHost: localhost\\r\\nConnection: close\\r\\n\\r\\n"
 445
 446            local bytes, err = sock:send(req)
 447            if not bytes then
 448                ngx.say("failed to send request: ", err)
 449                return
 450            end
 451            ngx.say("request sent: ", bytes)
 452
 453            local read_headers = sock:receiveuntil("\\r\\n\\r\\n")
 454            local headers, err, part = read_headers()
 455            if not headers then
 456                ngx.say("failed to read headers: ", err, " [", part, "]")
 457            end
 458
 459            local reader = sock:receiveuntil("aaaaad")
 460
 461            for i = 1, 2 do
 462                line, err, part = reader()
 463                if line then
 464                    ngx.say("read: ", line)
 465
 466                else
 467                    ngx.say("failed to read a line: ", err, " [", part, "]")
 468                end
 469            end
 470
 471            ok, err = sock:close()
 472            ngx.say("close: ", ok, " ", err)
 473        ';
 474    }
 475
 476    location /foo {
 477        echo baaaaaaaaeaaaaaaadf;
 478        more_clear_headers Date;
 479    }
 480--- request
 481GET /t
 482--- response_body eval
 483qq{connected: 1
 484request sent: 57
 485read: baaaaaaaaeaa
 486failed to read a line: closed [f
 487]
 488close: nil closed
 489}
 490--- no_error_log
 491[error]
 492
 493
 494
 495=== TEST 8: ambiguous boundary patterns (aaaaad), small buffer, 2 bytes
 496--- config
 497    server_tokens off;
 498    lua_socket_buffer_size 2;
 499    location /t {
 500        set $port $TEST_NGINX_SERVER_PORT;
 501
 502        content_by_lua '
 503            -- collectgarbage("collect")
 504
 505            local sock = ngx.socket.tcp()
 506            local port = ngx.var.port
 507
 508            local ok, err = sock:connect("127.0.0.1", port)
 509            if not ok then
 510                ngx.say("failed to connect: ", err)
 511                return
 512            end
 513
 514            ngx.say("connected: ", ok)
 515
 516            local req = "GET /foo HTTP/1.0\\r\\nHost: localhost\\r\\nConnection: close\\r\\n\\r\\n"
 517
 518            local bytes, err = sock:send(req)
 519            if not bytes then
 520                ngx.say("failed to send request: ", err)
 521                return
 522            end
 523            ngx.say("request sent: ", bytes)
 524
 525            local read_headers = sock:receiveuntil("\\r\\n\\r\\n")
 526            local headers, err, part = read_headers()
 527            if not headers then
 528                ngx.say("failed to read headers: ", err, " [", part, "]")
 529            end
 530
 531            local reader = sock:receiveuntil("aaaaad")
 532
 533            for i = 1, 2 do
 534                line, err, part = reader()
 535                if line then
 536                    ngx.say("read: ", line)
 537
 538                else
 539                    ngx.say("failed to read a line: ", err, " [", part, "]")
 540                end
 541            end
 542
 543            ok, err = sock:close()
 544            ngx.say("close: ", ok, " ", err)
 545        ';
 546    }
 547
 548    location /foo {
 549        echo baaaaaaaaeaaaaaaadf;
 550        more_clear_headers Date;
 551    }
 552--- request
 553GET /t
 554--- response_body eval
 555qq{connected: 1
 556request sent: 57
 557read: baaaaaaaaeaa
 558failed to read a line: closed [f
 559]
 560close: nil closed
 561}
 562--- no_error_log
 563[error]
 564
 565
 566
 567=== TEST 9: ambiguous boundary patterns (aaaaad), small buffer, 1 byte
 568--- config
 569    server_tokens off;
 570    lua_socket_buffer_size 1;
 571    location /t {
 572        set $port $TEST_NGINX_SERVER_PORT;
 573
 574        content_by_lua '
 575            -- collectgarbage("collect")
 576
 577            local sock = ngx.socket.tcp()
 578            local port = ngx.var.port
 579
 580            local ok, err = sock:connect("127.0.0.1", port)
 581            if not ok then
 582                ngx.say("failed to connect: ", err)
 583                return
 584            end
 585
 586            ngx.say("connected: ", ok)
 587
 588            local req = "GET /foo HTTP/1.0\\r\\nHost: localhost\\r\\nConnection: close\\r\\n\\r\\n"
 589
 590            local bytes, err = sock:send(req)
 591            if not bytes then
 592                ngx.say("failed to send request: ", err)
 593                return
 594            end
 595            ngx.say("request sent: ", bytes)
 596
 597            local read_headers = sock:receiveuntil("\\r\\n\\r\\n")
 598            local headers, err, part = read_headers()
 599            if not headers then
 600                ngx.say("failed to read headers: ", err, " [", part, "]")
 601            end
 602
 603            local reader = sock:receiveuntil("aaaaad")
 604
 605            for i = 1, 2 do
 606                line, err, part = reader()
 607                if line then
 608                    ngx.say("read: ", line)
 609
 610                else
 611                    ngx.say("failed to read a line: ", err, " [", part, "]")
 612                end
 613            end
 614
 615            ok, err = sock:close()
 616            ngx.say("close: ", ok, " ", err)
 617        ';
 618    }
 619
 620    location /foo {
 621        echo baaaaaaaaeaaaaaaadf;
 622        more_clear_headers Date;
 623    }
 624--- request
 625GET /t
 626--- response_body eval
 627qq{connected: 1
 628request sent: 57
 629read: baaaaaaaaeaa
 630failed to read a line: closed [f
 631]
 632close: nil closed
 633}
 634--- no_error_log
 635[error]
 636
 637
 638
 639=== TEST 10: ambiguous boundary patterns (abcabdabcabe)
 640--- config
 641    server_tokens off;
 642    location /t {
 643        set $port $TEST_NGINX_SERVER_PORT;
 644
 645        content_by_lua '
 646            -- collectgarbage("collect")
 647
 648            local sock = ngx.socket.tcp()
 649            local port = ngx.var.port
 650
 651            local ok, err = sock:connect("127.0.0.1", port)
 652            if not ok then
 653                ngx.say("failed to connect: ", err)
 654                return
 655            end
 656
 657            ngx.say("connected: ", ok)
 658
 659            local req = "GET /foo HTTP/1.0\\r\\nHost: localhost\\r\\nConnection: close\\r\\n\\r\\n"
 660
 661            local bytes, err = sock:send(req)
 662            if not bytes then
 663                ngx.say("failed to send request: ", err)
 664                return
 665            end
 666            ngx.say("request sent: ", bytes)
 667
 668            local read_headers = sock:receiveuntil("\\r\\n\\r\\n")
 669            local headers, err, part = read_headers()
 670            if not headers then
 671                ngx.say("failed to read headers: ", err, " [", part, "]")
 672            end
 673
 674            local reader = sock:receiveuntil("abcabdabcabe")
 675
 676            for i = 1, 2 do
 677                line, err, part = reader()
 678                if line then
 679                    ngx.say("read: ", line)
 680
 681                else
 682                    ngx.say("failed to read a line: ", err, " [", part, "]")
 683                end
 684            end
 685
 686            ok, err = sock:close()
 687            ngx.say("close: ", ok, " ", err)
 688        ';
 689    }
 690
 691    location /foo {
 692        echo abcabdabcabdabcabe;
 693        more_clear_headers Date;
 694    }
 695--- request
 696GET /t
 697--- response_body eval
 698qq{connected: 1
 699request sent: 57
 700read: abcabd
 701failed to read a line: closed [
 702]
 703close: nil closed
 704}
 705--- no_error_log
 706[error]
 707
 708
 709
 710=== TEST 11: ambiguous boundary patterns (abcabdabcabe 2)
 711--- config
 712    server_tokens off;
 713    location /t {
 714        set $port $TEST_NGINX_SERVER_PORT;
 715
 716        content_by_lua '
 717            -- collectgarbage("collect")
 718
 719            local sock = ngx.socket.tcp()
 720            local port = ngx.var.port
 721
 722            local ok, err = sock:connect("127.0.0.1", port)
 723            if not ok then
 724                ngx.say("failed to connect: ", err)
 725                return
 726            end
 727
 728            ngx.say("connected: ", ok)
 729
 730            local req = "GET /foo HTTP/1.0\\r\\nHost: localhost\\r\\nConnection: close\\r\\n\\r\\n"
 731
 732            local bytes, err = sock:send(req)
 733            if not bytes then
 734                ngx.say("failed to send request: ", err)
 735                return
 736            end
 737            ngx.say("request sent: ", bytes)
 738
 739            local read_headers = sock:receiveuntil("\\r\\n\\r\\n")
 740            local headers, err, part = read_headers()
 741            if not headers then
 742                ngx.say("failed to read headers: ", err, " [", part, "]")
 743            end
 744
 745            local reader = sock:receiveuntil("abcabdabcabe")
 746
 747            for i = 1, 2 do
 748                line, err, part = reader()
 749                if line then
 750                    ngx.say("read: ", line)
 751
 752                else
 753                    ngx.say("failed to read a line: ", err, " [", part, "]")
 754                end
 755            end
 756
 757            ok, err = sock:close()
 758            ngx.say("close: ", ok, " ", err)
 759        ';
 760    }
 761
 762    location /foo {
 763        echo abcabdabcabcabdabcabe;
 764        more_clear_headers Date;
 765    }
 766--- request
 767GET /t
 768--- response_body eval
 769qq{connected: 1
 770request sent: 57
 771read: abcabdabc
 772failed to read a line: closed [
 773]
 774close: nil closed
 775}
 776--- no_error_log
 777[error]
 778
 779
 780
 781=== TEST 12: ambiguous boundary patterns (abcabdabcabe 3)
 782--- config
 783    server_tokens off;
 784    location /t {
 785        set $port $TEST_NGINX_SERVER_PORT;
 786
 787        content_by_lua '
 788            -- collectgarbage("collect")
 789
 790            local sock = ngx.socket.tcp()
 791            local port = ngx.var.port
 792
 793            local ok, err = sock:connect("127.0.0.1", port)
 794            if not ok then
 795                ngx.say("failed to connect: ", err)
 796                return
 797            end
 798
 799            ngx.say("connected: ", ok)
 800
 801            local req = "GET /foo HTTP/1.0\\r\\nHost: localhost\\r\\nConnection: close\\r\\n\\r\\n"
 802
 803            local bytes, err = sock:send(req)
 804            if not bytes then
 805                ngx.say("failed to send request: ", err)
 806                return
 807            end
 808            ngx.say("request sent: ", bytes)
 809
 810            local read_headers = sock:receiveuntil("\\r\\n\\r\\n")
 811            local headers, err, part = read_headers()
 812            if not headers then
 813                ngx.say("failed to read headers: ", err, " [", part, "]")
 814            end
 815
 816            local reader = sock:receiveuntil("abcabdabcabe")
 817
 818            for i = 1, 2 do
 819                line, err, part = reader()
 820                if line then
 821                    ngx.say("read: ", line)
 822
 823                else
 824                    ngx.say("failed to read a line: ", err, " [", part, "]")
 825                end
 826            end
 827
 828            ok, err = sock:close()
 829            ngx.say("close: ", ok, " ", err)
 830        ';
 831    }
 832
 833    location /foo {
 834        echo abcabcabdabcabe;
 835        more_clear_headers Date;
 836    }
 837--- request
 838GET /t
 839--- response_body eval
 840qq{connected: 1
 841request sent: 57
 842read: abc
 843failed to read a line: closed [
 844]
 845close: nil closed
 846}
 847--- no_error_log
 848[error]
 849
 850
 851
 852=== TEST 13: ambiguous boundary patterns (abcabdabcabe 4)
 853--- config
 854    server_tokens off;
 855    location /t {
 856        set $port $TEST_NGINX_SERVER_PORT;
 857
 858        content_by_lua '
 859            -- collectgarbage("collect")
 860
 861            local sock = ngx.socket.tcp()
 862            local port = ngx.var.port
 863
 864            local ok, err = sock:connect("127.0.0.1", port)
 865            if not ok then
 866                ngx.say("failed to connect: ", err)
 867                return
 868            end
 869
 870            ngx.say("connected: ", ok)
 871
 872            local req = "GET /foo HTTP/1.0\\r\\nHost: localhost\\r\\nConnection: close\\r\\n\\r\\n"
 873
 874            local bytes, err = sock:send(req)
 875            if not bytes then
 876                ngx.say("failed to send request: ", err)
 877                return
 878            end
 879            ngx.say("request sent: ", bytes)
 880
 881            local read_headers = sock:receiveuntil("\\r\\n\\r\\n")
 882            local headers, err, part = read_headers()
 883            if not headers then
 884                ngx.say("failed to read headers: ", err, " [", part, "]")
 885            end
 886
 887            local reader = sock:receiveuntil("abcabdabcabe")
 888
 889            for i = 1, 2 do
 890                line, err, part = reader()
 891                if line then
 892                    ngx.say("read: ", line)
 893
 894                else
 895                    ngx.say("failed to read a line: ", err, " [", part, "]")
 896                end
 897            end
 898
 899            ok, err = sock:close()
 900            ngx.say("close: ", ok, " ", err)
 901        ';
 902    }
 903
 904    location /foo {
 905        echo ababcabdabcabe;
 906        more_clear_headers Date;
 907    }
 908--- request
 909GET /t
 910--- response_body eval
 911qq{connected: 1
 912request sent: 57
 913read: ab
 914failed to read a line: closed [
 915]
 916close: nil closed
 917}
 918--- no_error_log
 919[error]
 920
 921
 922
 923=== TEST 14: ambiguous boundary patterns (--abc)
 924--- config
 925    server_tokens off;
 926    location /t {
 927        set $port $TEST_NGINX_SERVER_PORT;
 928
 929        content_by_lua '
 930            -- collectgarbage("collect")
 931
 932            local sock = ngx.socket.tcp()
 933            local port = ngx.var.port
 934
 935            local ok, err = sock:connect("127.0.0.1", port)
 936            if not ok then
 937                ngx.say("failed to connect: ", err)
 938                return
 939            end
 940
 941            ngx.say("connected: ", ok)
 942
 943            local req = "GET /foo HTTP/1.0\\r\\nHost: localhost\\r\\nConnection: close\\r\\n\\r\\n"
 944
 945            local bytes, err = sock:send(req)
 946            if not bytes then
 947                ngx.say("failed to send request: ", err)
 948                return
 949            end
 950            ngx.say("request sent: ", bytes)
 951
 952            local read_headers = sock:receiveuntil("\\r\\n\\r\\n")
 953            local headers, err, part = read_headers()
 954            if not headers then
 955                ngx.say("failed to read headers: ", err, " [", part, "]")
 956            end
 957
 958            local reader = sock:receiveuntil("--abc")
 959
 960            for i = 1, 2 do
 961                line, err, part = reader()
 962                if line then
 963                    ngx.say("read: ", line)
 964
 965                else
 966                    ngx.say("failed to read a line: ", err, " [", part, "]")
 967                end
 968            end
 969
 970            ok, err = sock:close()
 971            ngx.say("close: ", ok, " ", err)
 972        ';
 973    }
 974
 975    location /foo {
 976        echo -- ----abc;
 977        more_clear_headers Date;
 978    }
 979--- request
 980GET /t
 981--- response_body eval
 982qq{connected: 1
 983request sent: 57
 984read: --
 985failed to read a line: closed [
 986]
 987close: nil closed
 988}
 989--- no_error_log
 990[error]
 991
 992
 993
 994=== TEST 15: ambiguous boundary patterns (--abc)
 995--- config
 996    server_tokens off;
 997    location /t {
 998        set $port $TEST_NGINX_SERVER_PORT;
 999
1000        content_by_lua '
1001            -- collectgarbage("collect")
1002
1003            local sock = ngx.socket.tcp()
1004            local port = ngx.var.port
1005
1006            local ok, err = sock:connect("127.0.0.1", port)
1007            if not ok then
1008                ngx.say("failed to connect: ", err)
1009                return
1010            end
1011
1012            ngx.say("connected: ", ok)
1013
1014            local req = "GET /foo HTTP/1.0\\r\\nHost: localhost\\r\\nConnection: close\\r\\n\\r\\n"
1015
1016            local bytes, err = sock:send(req)
1017            if not bytes then
1018                ngx.say("failed to send request: ", err)
1019                return
1020            end
1021            ngx.say("request sent: ", bytes)
1022
1023            local read_headers = sock:receiveuntil("\\r\\n\\r\\n")
1024            local headers, err, part = read_headers()
1025            if not headers then
1026                ngx.say("failed to read headers: ", err, " [", part, "]")
1027            end
1028
1029            local reader = sock:receiveuntil("--abc")
1030
1031            for i = 1, 7 do
1032                line, err, part = reader(4)
1033                if line then
1034                    ngx.say("read: ", line)
1035
1036                else
1037                    ngx.say("failed to read a line: ", err, " [", part, "]")
1038                end
1039            end
1040
1041            ok, err = sock:close()
1042            ngx.say("close: ", ok, " ", err)
1043        ';
1044    }
1045
1046    location /foo {
1047        echo "hello, world ----abc";
1048        more_clear_headers Date;
1049    }
1050--- request
1051GET /t
1052--- response_body eval
1053qq{connected: 1
1054request sent: 57
1055read: hell
1056read: o, w
1057read: orld
1058read:  --
1059read: 
1060failed to read a line: nil [nil]
1061failed to read a line: closed [
1062]
1063close: nil closed
1064}
1065--- no_error_log
1066[error]
1067
1068
1069
1070=== TEST 16: ambiguous boundary patterns (--abc), small buffer
1071--- config
1072    server_tokens off;
1073    location /t {
1074        set $port $TEST_NGINX_SERVER_PORT;
1075        lua_socket_buffer_size 1;
1076
1077        content_by_lua '
1078            -- collectgarbage("collect")
1079
1080            local sock = ngx.socket.tcp()
1081            local port = ngx.var.port
1082
1083            local ok, err = sock:connect("127.0.0.1", port)
1084            if not ok then
1085                ngx.say("failed to connect: ", err)
1086                return
1087            end
1088
1089            ngx.say("connected: ", ok)
1090
1091            local req = "GET /foo HTTP/1.0\\r\\nHost: localhost\\r\\nConnection: close\\r\\n\\r\\n"
1092
1093            local bytes, err = sock:send(req)
1094            if not bytes then
1095                ngx.say("failed to send request: ", err)
1096                return
1097            end
1098            ngx.say("request sent: ", bytes)
1099
1100            local read_headers = sock:receiveuntil("\\r\\n\\r\\n")
1101            local headers, err, part = read_headers()
1102            if not headers then
1103                ngx.say("failed to read headers: ", err, " [", part, "]")
1104            end
1105
1106            local reader = sock:receiveuntil("--abc")
1107
1108            for i = 1, 7 do
1109                line, err, part = reader(4)
1110                if line then
1111                    ngx.say("read: ", line)
1112
1113                else
1114                    ngx.say("failed to read a line: ", err, " [", part, "]")
1115                end
1116            end
1117
1118            ok, err = sock:close()
1119            ngx.say("close: ", ok, " ", err)
1120        ';
1121    }
1122
1123    location /foo {
1124        echo "hello, world ----abc";
1125        more_clear_headers Date;
1126    }
1127--- request
1128GET /t
1129--- response_body eval
1130qq{connected: 1
1131request sent: 57
1132read: hell
1133read: o, w
1134read: orld
1135read:  --
1136read: 
1137failed to read a line: nil [nil]
1138failed to read a line: closed [
1139]
1140close: nil closed
1141}
1142--- no_error_log
1143[error]
1144
1145
1146
1147=== TEST 17: ambiguous boundary patterns (--abc), small buffer, mixed by other reading calls
1148--- config
1149    server_tokens off;
1150    location /t {
1151        set $port $TEST_NGINX_SERVER_PORT;
1152        lua_socket_buffer_size 1;
1153
1154        content_by_lua '
1155            -- collectgarbage("collect")
1156
1157            local sock = ngx.socket.tcp()
1158            local port = ngx.var.port
1159
1160            local ok, err = sock:connect("127.0.0.1", port)
1161            if not ok then
1162                ngx.say("failed to connect: ", err)
1163                return
1164            end
1165
1166            ngx.say("connected: ", ok)
1167
1168            local req = "GET /foo HTTP/1.0\\r\\nHost: localhost\\r\\nConnection: close\\r\\n\\r\\n"
1169
1170            local bytes, err = sock:send(req)
1171            if not bytes then
1172                ngx.say("failed to send request: ", err)
1173                return
1174            end
1175            ngx.say("request sent: ", bytes)
1176
1177            local read_headers = sock:receiveuntil("\\r\\n\\r\\n")
1178            local headers, err, part = read_headers()
1179            if not headers then
1180                ngx.say("failed to read headers: ", err, " [", part, "]")
1181            end
1182
1183            local reader = sock:receiveuntil("--abc")
1184
1185            for i = 1, 7 do
1186                line, err, part = reader(4)
1187                if line then
1188                    ngx.say("read: ", line)
1189
1190                else
1191                    ngx.say("failed to read a chunk: ", err, " [", part, "]")
1192                end
1193
1194                local data, err, part = sock:receive(1)
1195                if not data then
1196                    ngx.say("failed to read a byte: ", err, " [", part, "]")
1197                    break
1198                else
1199                    ngx.say("read one byte: ", data)
1200                end
1201            end
1202
1203            ok, err = sock:close()
1204            ngx.say("close: ", ok, " ", err)
1205        ';
1206    }
1207
1208    location /foo {
1209        echo "hello, world ----abc";
1210        more_clear_headers Date;
1211    }
1212--- request
1213GET /t
1214--- response_body eval
1215qq{connected: 1
1216request sent: 57
1217read: hell
1218read one byte: o
1219read: , wo
1220read one byte: r
1221read: ld -
1222read one byte: -
1223read: 
1224read one byte: 
1225
1226failed to read a chunk: nil [nil]
1227failed to read a byte: closed []
1228close: nil closed
1229}
1230--- no_error_log
1231[error]
1232
1233
1234
1235=== TEST 18: ambiguous boundary patterns (abcabd), small buffer
1236--- config
1237    server_tokens off;
1238    lua_socket_buffer_size 3;
1239    location /t {
1240        set $port $TEST_NGINX_SERVER_PORT;
1241
1242        content_by_lua '
1243            -- collectgarbage("collect")
1244
1245            local sock = ngx.socket.tcp()
1246            local port = ngx.var.port
1247
1248            local ok, err = sock:connect("127.0.0.1", port)
1249            if not ok then
1250                ngx.say("failed to connect: ", err)
1251                return
1252            end
1253
1254            ngx.say("connected: ", ok)
1255
1256            local req = "GET /foo HTTP/1.0\\r\\nHost: localhost\\r\\nConnection: close\\r\\n\\r\\n"
1257
1258            local bytes, err = sock:send(req)
1259            if not bytes then
1260                ngx.say("failed to send request: ", err)
1261                return
1262            end
1263            ngx.say("request sent: ", bytes)
1264
1265            local read_headers = sock:receiveuntil("\\r\\n\\r\\n")
1266            local headers, err, part = read_headers()
1267            if not headers then
1268                ngx.say("failed to read headers: ", err, " [", part, "]")
1269            end
1270
1271            local reader = sock:receiveuntil("abcabd")
1272
1273            for i = 1, 2 do
1274                line, err, part = reader()
1275                if line then
1276                    ngx.say("read: ", line)
1277
1278                else
1279                    ngx.say("failed to read a line: ", err, " [", part, "]")
1280                end
1281            end
1282
1283            ok, err = sock:close()
1284            ngx.say("close: ", ok, " ", err)
1285        ';
1286    }
1287
1288    location /foo {
1289        echo abcabcabd;
1290        more_clear_headers Date;
1291    }
1292--- request
1293GET /t
1294--- response_body eval
1295qq{connected: 1
1296request sent: 57
1297read: abc
1298failed to read a line: closed [
1299]
1300close: nil closed
1301}
1302--- no_error_log
1303[error]
1304
1305
1306
1307=== TEST 19: long patterns
1308this exposed a memory leak in receiveuntil
1309--- config
1310    location /t {
1311        content_by_lua '
1312            local sock, err = ngx.req.socket()
1313            if not sock then
1314                ngx.say("failed to get req socket: ", err)
1315                return
1316            end
1317            local reader, err = sock:receiveuntil("------------------------------------------- abcdefghijklmnopqrstuvwxyz")
1318            if not reader then
1319                ngx.say("failed to get reader: ", err)
1320                return
1321            end
1322            ngx.say("ok")
1323        ';
1324    }
1325--- request
1326    POST /t
1327
1328--- more_headers: Content-Length: 1024
1329--- response_body
1330ok
1331--- no_error_log
1332[error]
1333