unicorn.git  about / heads / tags
Rack HTTP server for Unix and fast clients
blob 6b9c89d57fe54bc8928afb8e3713a2986cc0bbc3 9676 bytes (raw)
$ git show v0.91.0:test/unit/test_http_parser_ng.rb	# shows this blob on the CLI

  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
 
# coding: binary
require 'test/test_helper'
require 'digest/md5'

include Unicorn

class HttpParserNgTest < Test::Unit::TestCase

  def setup
    @parser = HttpParser.new
  end

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

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

  def test_identity_oneshot_header_with_body
    body = ('a' * 123).freeze
    req = {}
    str = "PUT / HTTP/1.1\r\n" \
          "Content-Length: #{body.length}\r\n" \
          "\r\n#{body}"
    assert_equal req.object_id, @parser.headers(req, str).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 = "PUT / HTTP/1.1\r\nContent-Length: 123\r\n\r\na"
    assert_equal Hash, @parser.headers({}, str).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 = "PUT / HTTP/1.1\r\nContent-Length: 1\r\n\r\naG"
    assert_equal Hash, @parser.headers({}, str).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 = "PUT / HTTP/1.1\r\ntransfer-Encoding: chunked\r\n\r\n"
    req = {}
    assert_equal req, @parser.headers(req, str)
    assert_equal 0, str.size
    tmp = ""
    assert_nil @parser.filter_body(tmp, "6")
    assert_equal 0, tmp.size
    assert_nil @parser.filter_body(tmp, rv = "\r\n")
    assert_equal 0, rv.size
    assert_equal 0, tmp.size
    tmp = ""
    assert_nil @parser.filter_body(tmp, "..")
    assert_equal "..", tmp
    assert_nil @parser.filter_body(tmp, "abcd\r\n0\r\n")
    assert_equal "abcd", tmp
    rv = "PUT"
    assert_equal rv.object_id, @parser.filter_body(tmp, rv).object_id
    assert_equal "PUT", rv
    assert ! @parser.keepalive?
  end

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

  def test_big_chunk
    str = "PUT / HTTP/1.1\r\ntransfer-Encoding: chunked\r\n\r\n" \
          "4000\r\nabcd"
    req = {}
    assert_equal req, @parser.headers(req, str)
    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, "\r\n0\r\n")
    assert @parser.body_eof?
    assert ! @parser.keepalive?
  end

  def test_two_chunks_oneshot
    str = "PUT / HTTP/1.1\r\ntransfer-Encoding: chunked\r\n\r\n" \
          "1\r\na\r\n2\r\n..\r\n0\r\n"
    req = {}
    assert_equal req, @parser.headers(req, str)
    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_trailers
    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 = {}
    assert_equal req, @parser.headers(req, str)
    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
    assert_nil @parser.trailers(req, str << "\r")
    assert_equal req, @parser.trailers(req, str << "\nGET / ")
    assert_equal "GET / ", str
    assert ! @parser.keepalive?
  end

  def test_max_chunk
    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 = {}
    assert_equal req, @parser.headers(req, str)
    assert_nil @parser.content_length
    assert_nothing_raised { @parser.filter_body('', str) }
    assert ! @parser.keepalive?
  end

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

  def test_overflow_chunk
    n = HttpParser::CHUNK_MAX + 1
    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"
    req = {}
    assert_equal req, @parser.headers(req, str)
    assert_nil @parser.content_length
    assert_raise(HttpParserError) { @parser.filter_body('', str) }
    assert ! @parser.keepalive?
  end

  def test_overflow_content_length
    n = HttpParser::LENGTH_MAX + 1
    str = "PUT / HTTP/1.1\r\nContent-Length: #{n}\r\n\r\n"
    assert_raise(HttpParserError) { @parser.headers({}, str) }
    assert ! @parser.keepalive?
  end

  def test_bad_chunk
    str = "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 = {}
    assert_equal req, @parser.headers(req, str)
    assert_nil @parser.content_length
    assert_raise(HttpParserError) { @parser.filter_body('', str) }
    assert ! @parser.keepalive?
  end

  def test_bad_content_length
    str = "PUT / HTTP/1.1\r\nContent-Length: 7ff\r\n\r\n"
    assert_raise(HttpParserError) { @parser.headers({}, str) }
    assert ! @parser.keepalive?
  end

  def test_bad_trailers
    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"
    req = {}
    assert_equal req, @parser.headers(req, str)
    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.trailers(req, str) }
    assert ! @parser.keepalive?
  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 = {}
    assert_equal req, @parser.headers(req, str)
    assert_equal 'Content-MD5,Content-SHA1', req['HTTP_TRAILER']
    assert ! @parser.keepalive?
  end

  def test_parse_simple_request
    parser = HttpParser.new
    req = {}
    http = "GET /read-rfc1945-if-you-dont-believe-me\r\n"
    assert_equal req, parser.headers(req, http)
    assert_equal '', http
    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

end

git clone https://yhbt.net/unicorn.git