unicorn Ruby/Rack server user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
425d5ad0d9378778dcd714fbf4a142bbeb9f91f3 blob 22133 bytes (raw)

  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
 
# -*- encoding: binary -*-

require './test/test_helper'
require 'digest/md5'

include Unicorn

class HttpParserNgTest < Test::Unit::TestCase

  def setup
    @parser = HttpParser.new
  end

  # RFC 7230 allows gzip/deflate/compress Transfer-Encoding,
  # but "chunked" must be last if used
  def test_is_chunked
    [ 'chunked,chunked', 'chunked,gzip', 'chunked,gzip,chunked' ].each do |x|
      assert_raise(HttpParserError) { HttpParser.is_chunked?(x) }
    end
    [ 'gzip, chunked', 'gzip,chunked', 'gzip ,chunked' ].each do |x|
      assert HttpParser.is_chunked?(x)
    end
    [ 'gzip', 'xhunked', 'xchunked' ].each do |x|
      assert !HttpParser.is_chunked?(x)
    end
  end

  def test_parser_max_len
    assert_raises(RangeError) do
      HttpParser.max_header_len = 0xffffffff + 1
    end
  end

  def test_next_clear
    r = "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n"
    @parser.buf << r
    @parser.parse
    @parser.response_start_sent = true
    assert @parser.keepalive?
    assert @parser.next?
    assert @parser.response_start_sent

    # persistent client makes another request:
    @parser.buf << r
    @parser.parse
    assert @parser.keepalive?
    assert @parser.next?
    assert_equal false, @parser.response_start_sent
  end

  def test_response_start_sent
    assert_equal false, @parser.response_start_sent, "default is false"
    @parser.response_start_sent = true
    assert_equal true, @parser.response_start_sent
    @parser.response_start_sent = false
    assert_equal false, @parser.response_start_sent
    @parser.response_start_sent = true
    @parser.clear
    assert_equal false, @parser.response_start_sent
  end

  def test_connection_TE
    @parser.buf << "GET / HTTP/1.1\r\nHost: example.com\r\nConnection: TE\r\n"
    @parser.buf << "TE: trailers\r\n\r\n"
    @parser.parse
    assert @parser.keepalive?
    assert @parser.next?
  end

  def test_keepalive_requests_with_next?
    req = "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n".freeze
    expect = {
      "SERVER_NAME" => "example.com",
      "HTTP_HOST" => "example.com",
      "rack.url_scheme" => "http",
      "REQUEST_PATH" => "/",
      "SERVER_PROTOCOL" => "HTTP/1.1",
      "PATH_INFO" => "/",
      "HTTP_VERSION" => "HTTP/1.1",
      "REQUEST_URI" => "/",
      "SERVER_PORT" => "80",
      "REQUEST_METHOD" => "GET",
      "QUERY_STRING" => ""
    }.freeze
    100.times do |nr|
      @parser.buf << req
      assert_equal expect, @parser.parse
      assert @parser.next?
    end
  end

  def test_default_keepalive_is_off
    assert ! @parser.keepalive?
    assert ! @parser.next?
    @parser.buf << "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n"
    @parser.parse
    assert @parser.keepalive?
    @parser.clear
    assert ! @parser.keepalive?
    assert ! @parser.next?
  end

  def test_identity_byte_headers
    req = @parser.env
    str = "PUT / HTTP/1.1\r\n"
    str << "Content-Length: 123\r\n"
    str << "\r"
    hdr = @parser.buf
    str.each_byte { |byte|
      hdr << byte.chr
      assert_nil @parser.parse
    }
    hdr << "\n"
    assert_equal req.object_id, @parser.parse.object_id
    assert_equal '123', req['CONTENT_LENGTH']
    assert_equal 0, hdr.size
    assert ! @parser.keepalive?
    assert @parser.headers?
    assert_equal 123, @parser.content_length
    dst = ""
    buf = '.' * 123
    @parser.filter_body(dst, buf)
    assert_equal '.' * 123, dst
    assert_equal "", buf
    assert @parser.keepalive?
  end

  def test_identity_step_headers
    req = @parser.env
    str = @parser.buf
    str << "PUT / HTTP/1.1\r\n"
    assert ! @parser.parse
    str << "Content-Length: 123\r\n"
    assert ! @parser.parse
    str << "\r\n"
    assert_equal req.object_id, @parser.parse.object_id
    assert_equal '123', req['CONTENT_LENGTH']
    assert_equal 0, str.size
    assert ! @parser.keepalive?
    assert @parser.headers?
    dst = ""
    buf = '.' * 123
    @parser.filter_body(dst, buf)
    assert_equal '.' * 123, dst
    assert_equal "", buf
    assert @parser.keepalive?
  end

  def test_identity_oneshot_header
    req = @parser.env
    str = @parser.buf
    str << "PUT / HTTP/1.1\r\nContent-Length: 123\r\n\r\n"
    assert_equal req.object_id, @parser.parse.object_id
    assert_equal '123', req['CONTENT_LENGTH']
    assert_equal 0, str.size
    assert ! @parser.keepalive?
    assert @parser.headers?
    dst = ""
    buf = '.' * 123
    @parser.filter_body(dst, buf)
    assert_equal '.' * 123, dst
    assert_equal "", buf
  end

  def test_identity_oneshot_header_with_body
    body = ('a' * 123).freeze
    req = @parser.env
    str = @parser.buf
    str << "PUT / HTTP/1.1\r\n" \
           "Content-Length: #{body.length}\r\n" \
           "\r\n#{body}"
    assert_equal req.object_id, @parser.parse.object_id
    assert_equal '123', req['CONTENT_LENGTH']
    assert_equal 123, str.size
    assert_equal body, str
    tmp = ''
    assert_nil @parser.filter_body(tmp, str)
    assert_equal 0, str.size
    assert_equal tmp, body
    assert_equal "", @parser.filter_body(tmp, str)
    assert @parser.keepalive?
  end

  def test_identity_oneshot_header_with_body_partial
    str = @parser.buf
    str << "PUT / HTTP/1.1\r\nContent-Length: 123\r\n\r\na"
    assert_equal Hash, @parser.parse.class
    assert_equal 1, str.size
    assert_equal 'a', str
    tmp = ''
    assert_nil @parser.filter_body(tmp, str)
    assert_equal "", str
    assert_equal "a", tmp
    str << ' ' * 122
    rv = @parser.filter_body(tmp, str)
    assert_equal 122, tmp.size
    assert_nil rv
    assert_equal "", str
    assert_equal str.object_id, @parser.filter_body(tmp, str).object_id
    assert @parser.keepalive?
  end

  def test_identity_oneshot_header_with_body_slop
    str = @parser.buf
    str << "PUT / HTTP/1.1\r\nContent-Length: 1\r\n\r\naG"
    assert_equal Hash, @parser.parse.class
    assert_equal 2, str.size
    assert_equal 'aG', str
    tmp = ''
    assert_nil @parser.filter_body(tmp, str)
    assert_equal "G", str
    assert_equal "G", @parser.filter_body(tmp, str)
    assert_equal 1, tmp.size
    assert_equal "a", tmp
    assert @parser.keepalive?
  end

  def test_chunked
    str = @parser.buf
    req = @parser.env
    str << "PUT / HTTP/1.1\r\ntransfer-Encoding: chunked\r\n\r\n"
    assert_equal req, @parser.parse, "msg=#{str}"
    assert_equal 0, str.size
    tmp = ""
    assert_nil @parser.filter_body(tmp, str << "6")
    assert_equal 0, tmp.size
    assert_nil @parser.filter_body(tmp, str << "\r\n")
    assert_equal 0, str.size
    assert_equal 0, tmp.size
    tmp = ""
    assert_nil @parser.filter_body(tmp, str << "..")
    assert_equal "..", tmp
    assert_nil @parser.filter_body(tmp, str << "abcd\r\n0\r\n")
    assert_equal "abcd", tmp
    assert_equal str.object_id, @parser.filter_body(tmp, str << "PUT").object_id
    assert_equal "PUT", str
    assert ! @parser.keepalive?
    str << "TY: FOO\r\n\r\n"
    assert_equal req, @parser.parse
    assert_equal "FOO", req["HTTP_PUTTY"]
    assert @parser.keepalive?
  end

  def test_chunked_empty
    str = @parser.buf
    req = @parser.env
    str << "PUT / HTTP/1.1\r\ntransfer-Encoding: chunked\r\n\r\n"
    assert_equal req, @parser.parse, "msg=#{str}"
    assert_equal 0, str.size
    tmp = ""
    assert_equal str, @parser.filter_body(tmp, str << "0\r\n\r\n")
    assert_equal "", tmp
  end

  def test_two_chunks
    str = @parser.buf
    str << "PUT / HTTP/1.1\r\ntransfer-Encoding: chunked\r\n\r\n"
    req = @parser.env
    assert_equal req, @parser.parse
    assert_equal 0, str.size
    tmp = ""
    assert_nil @parser.filter_body(tmp, str << "6")
    assert_equal 0, tmp.size
    assert_nil @parser.filter_body(tmp, str << "\r\n")
    assert_equal "", str
    assert_equal 0, tmp.size
    tmp = ""
    assert_nil @parser.filter_body(tmp, str << "..")
    assert_equal 2, tmp.size
    assert_equal "..", tmp
    assert_nil @parser.filter_body(tmp, str << "abcd\r\n1")
    assert_equal "abcd", tmp
    assert_nil @parser.filter_body(tmp, str << "\r")
    assert_equal "", tmp
    assert_nil @parser.filter_body(tmp, str << "\n")
    assert_equal "", tmp
    assert_nil @parser.filter_body(tmp, str << "z")
    assert_equal "z", tmp
    assert_nil @parser.filter_body(tmp, str << "\r\n")
    assert_nil @parser.filter_body(tmp, str << "0")
    assert_nil @parser.filter_body(tmp, str << "\r")
    rv = @parser.filter_body(tmp, str << "\nGET")
    assert_equal "GET", rv
    assert_equal str.object_id, rv.object_id
    assert ! @parser.keepalive?
  end

  def test_big_chunk
    str = @parser.buf
    str << "PUT / HTTP/1.1\r\ntransfer-Encoding: chunked\r\n\r\n" \
           "4000\r\nabcd"
    req = @parser.env
    assert_equal req, @parser.parse
    tmp = ''
    assert_nil @parser.filter_body(tmp, str)
    assert_equal '', str
    str << ' ' * 16300
    assert_nil @parser.filter_body(tmp, str)
    assert_equal '', str
    str << ' ' * 80
    assert_nil @parser.filter_body(tmp, str)
    assert_equal '', str
    assert ! @parser.body_eof?
    assert_equal "", @parser.filter_body(tmp, str << "\r\n0\r\n")
    assert_equal "", tmp
    assert @parser.body_eof?
    str << "\r\n"
    assert_equal req, @parser.parse
    assert_equal "", str
    assert @parser.body_eof?
    assert @parser.keepalive?
  end

  def test_two_chunks_oneshot
    str = @parser.buf
    req = @parser.env
    str << "PUT / HTTP/1.1\r\ntransfer-Encoding: chunked\r\n\r\n" \
           "1\r\na\r\n2\r\n..\r\n0\r\n"
    assert_equal req, @parser.parse
    tmp = ''
    assert_nil @parser.filter_body(tmp, str)
    assert_equal 'a..', tmp
    rv = @parser.filter_body(tmp, str)
    assert_equal rv.object_id, str.object_id
    assert ! @parser.keepalive?
  end

  def test_chunks_bytewise
    chunked = "10\r\nabcdefghijklmnop\r\n11\r\n0123456789abcdefg\r\n0\r\n"
    str = "PUT / HTTP/1.1\r\ntransfer-Encoding: chunked\r\n\r\n"
    buf = @parser.buf
    buf << str
    req = @parser.env
    assert_equal req, @parser.parse
    assert_equal "", buf
    tmp = ''
    body = ''
    str = chunked[0..-2]
    str.each_byte { |byte|
      assert_nil @parser.filter_body(tmp, buf << byte.chr)
      body << tmp
    }
    assert_equal 'abcdefghijklmnop0123456789abcdefg', body
    rv = @parser.filter_body(tmp, buf<< "\n")
    assert_equal rv.object_id, buf.object_id
    assert ! @parser.keepalive?
  end

  def test_trailers
    req = @parser.env
    str = @parser.buf
    str << "PUT / HTTP/1.1\r\n" \
           "Trailer: Content-MD5\r\n" \
           "transfer-Encoding: chunked\r\n\r\n" \
           "1\r\na\r\n2\r\n..\r\n0\r\n"
    assert_equal req, @parser.parse
    assert_equal 'Content-MD5', req['HTTP_TRAILER']
    assert_nil req['HTTP_CONTENT_MD5']
    tmp = ''
    assert_nil @parser.filter_body(tmp, str)
    assert_equal 'a..', tmp
    md5_b64 = [ Digest::MD5.digest(tmp) ].pack('m').strip.freeze
    rv = @parser.filter_body(tmp, str)
    assert_equal rv.object_id, str.object_id
    assert_equal '', str
    md5_hdr = "Content-MD5: #{md5_b64}\r\n".freeze
    str << md5_hdr
    assert_nil @parser.trailers(req, str)
    assert_equal md5_b64, req['HTTP_CONTENT_MD5']
    assert_equal "CONTENT_MD5: #{md5_b64}\r\n", str
    str << "\r"
    assert_nil @parser.parse
    str << "\nGET / "
    assert_equal req, @parser.parse
    assert_equal "GET / ", str
    assert @parser.keepalive?
  end

  def test_trailers_slowly
    str = @parser.buf
    str << "PUT / HTTP/1.1\r\n" \
           "Trailer: Content-MD5\r\n" \
           "transfer-Encoding: chunked\r\n\r\n" \
           "1\r\na\r\n2\r\n..\r\n0\r\n"
    req = @parser.env
    assert_equal req, @parser.parse
    assert_equal 'Content-MD5', req['HTTP_TRAILER']
    assert_nil req['HTTP_CONTENT_MD5']
    tmp = ''
    assert_nil @parser.filter_body(tmp, str)
    assert_equal 'a..', tmp
    md5_b64 = [ Digest::MD5.digest(tmp) ].pack('m').strip.freeze
    rv = @parser.filter_body(tmp, str)
    assert_equal rv.object_id, str.object_id
    assert_equal '', str
    assert_nil @parser.trailers(req, str)
    md5_hdr = "Content-MD5: #{md5_b64}\r\n".freeze
    md5_hdr.each_byte { |byte|
      str << byte.chr
      assert_nil @parser.trailers(req, str)
    }
    assert_equal md5_b64, req['HTTP_CONTENT_MD5']
    assert_equal "CONTENT_MD5: #{md5_b64}\r\n", str
    str << "\r"
    assert_nil @parser.parse
    str << "\n"
    assert_equal req, @parser.parse
  end

  def test_max_chunk
    str = @parser.buf
    str << "PUT / HTTP/1.1\r\n" \
           "transfer-Encoding: chunked\r\n\r\n" \
           "#{HttpParser::CHUNK_MAX.to_s(16)}\r\na\r\n2\r\n..\r\n0\r\n"
    req = @parser.env
    assert_equal req, @parser.parse
    assert_nil @parser.content_length
    @parser.filter_body('', str)
    assert ! @parser.keepalive?
  end

  def test_max_body
    n = HttpParser::LENGTH_MAX
    @parser.buf << "PUT / HTTP/1.1\r\nContent-Length: #{n}\r\n\r\n"
    req = @parser.env
    @parser.headers(req, @parser.buf)
    assert_equal n, req['CONTENT_LENGTH'].to_i
    assert ! @parser.keepalive?
  end

  def test_overflow_chunk
    n = HttpParser::CHUNK_MAX + 1
    str = @parser.buf
    req = @parser.env
    str << "PUT / HTTP/1.1\r\n" \
           "transfer-Encoding: chunked\r\n\r\n" \
           "#{n.to_s(16)}\r\na\r\n2\r\n..\r\n0\r\n"
    assert_equal req, @parser.parse
    assert_nil @parser.content_length
    assert_raise(HttpParserError) { @parser.filter_body('', str) }
  end

  def test_overflow_content_length
    n = HttpParser::LENGTH_MAX + 1
    @parser.buf << "PUT / HTTP/1.1\r\nContent-Length: #{n}\r\n\r\n"
    assert_raise(HttpParserError) { @parser.parse }
  end

  def test_bad_chunk
    @parser.buf << "PUT / HTTP/1.1\r\n" \
                   "transfer-Encoding: chunked\r\n\r\n" \
                   "#zzz\r\na\r\n2\r\n..\r\n0\r\n"
    req = @parser.env
    assert_equal req, @parser.parse
    assert_nil @parser.content_length
    assert_raise(HttpParserError) { @parser.filter_body("", @parser.buf) }
  end

  def test_bad_content_length
    @parser.buf << "PUT / HTTP/1.1\r\nContent-Length: 7ff\r\n\r\n"
    assert_raise(HttpParserError) { @parser.parse }
  end

  def test_bad_trailers
    str = @parser.buf
    req = @parser.env
    str << "PUT / HTTP/1.1\r\n" \
           "Trailer: Transfer-Encoding\r\n" \
           "transfer-Encoding: chunked\r\n\r\n" \
           "1\r\na\r\n2\r\n..\r\n0\r\n"
    assert_equal req, @parser.parse
    assert_equal 'Transfer-Encoding', req['HTTP_TRAILER']
    tmp = ''
    assert_nil @parser.filter_body(tmp, str)
    assert_equal 'a..', tmp
    assert_equal '', str
    str << "Transfer-Encoding: identity\r\n\r\n"
    assert_raise(HttpParserError) { @parser.parse }
  end

  def test_repeat_headers
    str = "PUT / HTTP/1.1\r\n" \
          "Trailer: Content-MD5\r\n" \
          "Trailer: Content-SHA1\r\n" \
          "transfer-Encoding: chunked\r\n\r\n" \
          "1\r\na\r\n2\r\n..\r\n0\r\n"
    req = @parser.env
    @parser.buf << str
    assert_equal req, @parser.parse
    assert_equal 'Content-MD5,Content-SHA1', req['HTTP_TRAILER']
    assert ! @parser.keepalive?
  end

  def test_parse_simple_request
    parser = HttpParser.new
    req = parser.env
    parser.buf << "GET /read-rfc1945-if-you-dont-believe-me\r\n"
    assert_equal req, parser.parse
    assert_equal '', parser.buf
    expect = {
      "SERVER_NAME"=>"localhost",
      "rack.url_scheme"=>"http",
      "REQUEST_PATH"=>"/read-rfc1945-if-you-dont-believe-me",
      "PATH_INFO"=>"/read-rfc1945-if-you-dont-believe-me",
      "REQUEST_URI"=>"/read-rfc1945-if-you-dont-believe-me",
      "SERVER_PORT"=>"80",
      "SERVER_PROTOCOL"=>"HTTP/0.9",
      "REQUEST_METHOD"=>"GET",
      "QUERY_STRING"=>""
    }
    assert_equal expect, req
    assert ! parser.headers?
  end

  def test_path_info_semicolon
    qs = "QUERY_STRING"
    pi = "PATH_INFO"
    req = {}
    str = "GET %s HTTP/1.1\r\nHost: example.com\r\n\r\n"
    {
      "/1;a=b?c=d&e=f" => { qs => "c=d&e=f", pi => "/1;a=b" },
      "/1?c=d&e=f" => { qs => "c=d&e=f", pi => "/1" },
      "/1;a=b" => { qs => "", pi => "/1;a=b" },
      "/1;a=b?" => { qs => "", pi => "/1;a=b" },
      "/1?a=b;c=d&e=f" => { qs => "a=b;c=d&e=f", pi => "/1" },
      "*" => { qs => "", pi => "" },
    }.each do |uri,expect|
      assert_equal req, @parser.headers(req.clear, str % [ uri ])
      req = req.dup
      @parser.clear
      assert_equal uri, req["REQUEST_URI"], "REQUEST_URI mismatch"
      assert_equal expect[qs], req[qs], "#{qs} mismatch"
      assert_equal expect[pi], req[pi], "#{pi} mismatch"
      next if uri == "*"
      uri = URI.parse("http://example.com#{uri}")
      assert_equal uri.query.to_s, req[qs], "#{qs} mismatch URI.parse disagrees"
      assert_equal uri.path, req[pi], "#{pi} mismatch URI.parse disagrees"
    end
  end

  def test_path_info_semicolon_absolute
    qs = "QUERY_STRING"
    pi = "PATH_INFO"
    req = {}
    str = "GET http://example.com%s HTTP/1.1\r\nHost: www.example.com\r\n\r\n"
    {
      "/1;a=b?c=d&e=f" => { qs => "c=d&e=f", pi => "/1;a=b" },
      "/1?c=d&e=f" => { qs => "c=d&e=f", pi => "/1" },
      "/1;a=b" => { qs => "", pi => "/1;a=b" },
      "/1;a=b?" => { qs => "", pi => "/1;a=b" },
      "/1?a=b;c=d&e=f" => { qs => "a=b;c=d&e=f", pi => "/1" },
    }.each do |uri,expect|
      assert_equal req, @parser.headers(req.clear, str % [ uri ])
      req = req.dup
      @parser.clear
      assert_equal uri, req["REQUEST_URI"], "REQUEST_URI mismatch"
      assert_equal "example.com", req["HTTP_HOST"], "Host: mismatch"
      assert_equal expect[qs], req[qs], "#{qs} mismatch"
      assert_equal expect[pi], req[pi], "#{pi} mismatch"
    end
  end

  def test_negative_content_length
    req = {}
    str = "PUT / HTTP/1.1\r\n" \
          "Content-Length: -1\r\n" \
          "\r\n"
    assert_raises(HttpParserError) do
      @parser.headers(req, str)
    end
  end

  def test_invalid_content_length
    req = {}
    str = "PUT / HTTP/1.1\r\n" \
          "Content-Length: zzzzz\r\n" \
          "\r\n"
    assert_raises(HttpParserError) do
      @parser.headers(req, str)
    end
  end

  def test_duplicate_content_length
    str = "PUT / HTTP/1.1\r\n" \
          "Content-Length: 1\r\n" \
          "Content-Length: 9\r\n" \
          "\r\n"
    assert_raises(HttpParserError) { @parser.headers({}, str) }
  end

  def test_chunked_overrides_content_length
    order = [ 'Transfer-Encoding: chunked', 'Content-Length: 666' ]
    %w(a b).each do |x|
      str = "PUT /#{x} HTTP/1.1\r\n" \
            "#{order.join("\r\n")}" \
            "\r\n\r\na\r\nhelloworld\r\n0\r\n\r\n"
      order.reverse!
      env = @parser.headers({}, str)
      assert_nil @parser.content_length
      assert_equal 'chunked', env['HTTP_TRANSFER_ENCODING']
      assert_equal '666', env['CONTENT_LENGTH'],
        'Content-Length logged so the app can log a possible client bug/attack'
      @parser.filter_body(dst = '', str)
      assert_equal 'helloworld', dst
      @parser.parse # handle the non-existent trailer
      assert @parser.next?
    end
  end

  def test_chunked_order_good
    str = "PUT /x HTTP/1.1\r\n" \
          "Transfer-Encoding: gzip\r\n" \
          "Transfer-Encoding: chunked\r\n" \
          "\r\n"
    env = @parser.headers({}, str)
    assert_equal 'gzip,chunked', env['HTTP_TRANSFER_ENCODING']
    assert_nil @parser.content_length

    @parser.clear
    str = "PUT /x HTTP/1.1\r\n" \
          "Transfer-Encoding: gzip, chunked\r\n" \
          "\r\n"
    env = @parser.headers({}, str)
    assert_equal 'gzip, chunked', env['HTTP_TRANSFER_ENCODING']
    assert_nil @parser.content_length
  end

  def test_chunked_order_bad
    str = "PUT /x HTTP/1.1\r\n" \
          "Transfer-Encoding: chunked\r\n" \
          "Transfer-Encoding: gzip\r\n" \
          "\r\n"
    assert_raise(HttpParserError) { @parser.headers({}, str) }
  end

  def test_double_chunked
    str = "PUT /x HTTP/1.1\r\n" \
          "Transfer-Encoding: chunked\r\n" \
          "Transfer-Encoding: chunked\r\n" \
          "\r\n"
    assert_raise(HttpParserError) { @parser.headers({}, str) }

    @parser.clear
    str = "PUT /x HTTP/1.1\r\n" \
          "Transfer-Encoding: chunked,chunked\r\n" \
          "\r\n"
    assert_raise(HttpParserError) { @parser.headers({}, str) }
  end

  def test_backtrace_is_empty
    begin
      @parser.headers({}, "AAADFSFDSFD\r\n\r\n")
      assert false, "should never get here line:#{__LINE__}"
    rescue HttpParserError => e
      assert_equal [], e.backtrace
      return
    end
    assert false, "should never get here line:#{__LINE__}"
  end

  def test_ignore_version_header
    @parser.buf << "GET / HTTP/1.1\r\nVersion: hello\r\n\r\n"
    req = @parser.env
    assert_equal req, @parser.parse
    assert_equal '', @parser.buf
    expect = {
      "SERVER_NAME" => "localhost",
      "rack.url_scheme" => "http",
      "REQUEST_PATH" => "/",
      "SERVER_PROTOCOL" => "HTTP/1.1",
      "PATH_INFO" => "/",
      "HTTP_VERSION" => "HTTP/1.1",
      "REQUEST_URI" => "/",
      "SERVER_PORT" => "80",
      "REQUEST_METHOD" => "GET",
      "QUERY_STRING" => ""
    }
    assert_equal expect, req
  end

  def test_pipelined_requests
    host = "example.com"
    expect = {
      "HTTP_HOST" => host,
      "SERVER_NAME" => host,
      "REQUEST_PATH" => "/",
      "rack.url_scheme" => "http",
      "SERVER_PROTOCOL" => "HTTP/1.1",
      "PATH_INFO" => "/",
      "HTTP_VERSION" => "HTTP/1.1",
      "REQUEST_URI" => "/",
      "SERVER_PORT" => "80",
      "REQUEST_METHOD" => "GET",
      "QUERY_STRING" => ""
    }
    req1 = "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n"
    req2 = "GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n"
    @parser.buf << (req1 + req2)
    env1 = @parser.parse.dup
    assert_equal expect, env1
    assert_equal req2, @parser.buf
    assert ! @parser.env.empty?
    assert @parser.next?
    assert @parser.keepalive?
    assert @parser.headers?
    assert_equal expect, @parser.env
    env2 = @parser.parse.dup
    host.replace "www.example.com"
    assert_equal "www.example.com", expect["HTTP_HOST"]
    assert_equal "www.example.com", expect["SERVER_NAME"]
    assert_equal expect, env2
    assert_equal "", @parser.buf
  end
end
debug log:

solving 425d5ad ...
found 425d5ad in https://yhbt.net/unicorn.git

Code repositories for project(s) associated with this inbox:

	../../../unicorn.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).