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
|
require "../../../spec_helper"
require "http/server/handler"
require "http/client/response"
private def handle(request, fallthrough = true, directory_listing = true, ignore_body = false, decompress = true)
io = IO::Memory.new
response = HTTP::Server::Response.new(io)
context = HTTP::Server::Context.new(request, response)
handler = HTTP::StaticFileHandler.new datapath("static_file_handler"), fallthrough, directory_listing
handler.call context
response.close
io.rewind
HTTP::Client::Response.from_io(io, ignore_body, decompress)
end
describe HTTP::StaticFileHandler do
file_text = File.read datapath("static_file_handler", "test.txt")
it "serves a file" do
response = handle HTTP::Request.new("GET", "/test.txt"), ignore_body: false
response.status_code.should eq(200)
response.body.should eq(File.read(datapath("static_file_handler", "test.txt")))
end
it "handles forbidden characters in windows paths" do
response = handle HTTP::Request.new("GET", "/foo\\bar.txt"), ignore_body: false
response.status_code.should eq 404
# This file can't be checkout out from git on Windows, thus we need to create it here.
File.touch(Path[datapath("static_file_handler"), Path.posix("back\\slash.txt")])
response = handle HTTP::Request.new("GET", "/back\\slash.txt"), ignore_body: false
response.status_code.should eq 200
ensure
File.delete(Path[datapath("static_file_handler"), Path.posix("back\\slash.txt")])
end
it "adds Etag header" do
response = handle HTTP::Request.new("GET", "/test.txt")
response.headers["Etag"].should match(/W\/"\d+"$/)
end
it "adds Last-Modified header" do
response = handle HTTP::Request.new("GET", "/test.txt")
modification_time = File.info(datapath("static_file_handler", "test.txt")).modification_time
HTTP.parse_time(response.headers["Last-Modified"]).should eq(modification_time.at_beginning_of_second)
end
context "with If-Modified-Since header" do
it "returns 304 Not Modified for equal to Last-Modified" do
initial_response = handle HTTP::Request.new("GET", "/test.txt")
headers = HTTP::Headers.new
headers["If-Modified-Since"] = initial_response.headers["Last-Modified"]
response = handle HTTP::Request.new("GET", "/test.txt", headers), ignore_body: true
response.status_code.should eq(304)
response.headers["Last-Modified"].should eq initial_response.headers["Last-Modified"]
response.headers["Content-Type"]?.should be_nil
response.body.should eq ""
end
it "returns 304 Not Modified for younger than Last-Modified" do
initial_response = handle HTTP::Request.new("GET", "/test.txt")
last_modified = HTTP.parse_time(initial_response.headers["Last-Modified"]).not_nil!
headers = HTTP::Headers.new
headers["If-Modified-Since"] = HTTP.format_time(last_modified + 1.hour)
response = handle HTTP::Request.new("GET", "/test.txt", headers), ignore_body: true
response.headers["Last-Modified"].should eq initial_response.headers["Last-Modified"]
response.status_code.should eq(304)
response.body.should eq ""
end
it "serves content for older than Last-Modified" do
initial_response = handle HTTP::Request.new("GET", "/test.txt")
last_modified = HTTP.parse_time(initial_response.headers["Last-Modified"]).not_nil!
headers = HTTP::Headers.new
headers["If-Modified-Since"] = HTTP.format_time(last_modified - 1.hour)
response = handle HTTP::Request.new("GET", "/test.txt", headers), ignore_body: false
response.headers["Last-Modified"].should eq initial_response.headers["Last-Modified"]
response.status_code.should eq(200)
response.body.should eq(File.read(datapath("static_file_handler", "test.txt")))
end
end
context "with If-None-Match header" do
it "returns 304 Not Modified if header matches etag" do
initial_response = handle HTTP::Request.new("GET", "/test.txt")
headers = HTTP::Headers.new
headers["If-None-Match"] = initial_response.headers["Etag"]
response = handle HTTP::Request.new("GET", "/test.txt", headers), ignore_body: true
response.status_code.should eq(304)
end
it "serves file if header does not match etag" do
headers = HTTP::Headers.new
headers["If-None-Match"] = "some random etag"
response = handle HTTP::Request.new("GET", "/test.txt", headers), ignore_body: false
response.status_code.should eq(200)
response.body.should eq(File.read(datapath("static_file_handler", "test.txt")))
end
it "returns 304 Not Modified if header is *" do
headers = HTTP::Headers.new
headers["If-None-Match"] = "*"
response = handle HTTP::Request.new("GET", "/test.txt", headers), ignore_body: true
response.status_code.should eq(304)
end
it "serves file if header is empty" do
headers = HTTP::Headers.new
headers["If-None-Match"] = ""
response = handle HTTP::Request.new("GET", "/test.txt", headers), ignore_body: false
response.status_code.should eq(200)
response.body.should eq(File.read(datapath("static_file_handler", "test.txt")))
end
it "serves file if header does not contain valid etag" do
headers = HTTP::Headers.new
headers["If-None-Match"] = ", foo"
response = handle HTTP::Request.new("GET", "/test.txt", headers), ignore_body: false
response.status_code.should eq(200)
response.body.should eq(File.read(datapath("static_file_handler", "test.txt")))
end
end
context "with multiple If-None-Match header" do
it "returns 304 Not Modified if at least one header matches etag" do
initial_response = handle HTTP::Request.new("GET", "/test.txt")
headers = HTTP::Headers.new
headers["If-None-Match"] = %(,, ,W/"1234567" , , #{initial_response.headers["Etag"]},"12345678",%)
response = handle HTTP::Request.new("GET", "/test.txt", headers), ignore_body: true
response.status_code.should eq(304)
end
it "serves file if no header matches etag" do
headers = HTTP::Headers.new
headers["If-None-Match"] = "some random etag, 1234567"
response = handle HTTP::Request.new("GET", "/test.txt", headers), ignore_body: false
response.status_code.should eq(200)
response.body.should eq(File.read(datapath("static_file_handler", "test.txt")))
end
end
context "with both If-None-Match and If-Modified-Since headers" do
it "ignores If-Modified-Since as specified in RFC 7232" do
initial_response = handle HTTP::Request.new("GET", "/test.txt")
headers = HTTP::Headers.new
headers["If-Modified-Since"] = HTTP.format_time(File.info(datapath("static_file_handler", "test.txt")).modification_time - 1.hour)
headers["If-None-Match"] = initial_response.headers["Etag"]
response = handle HTTP::Request.new("GET", "/test.txt", headers), ignore_body: true
response.status_code.should eq(304)
end
it "serves a file if header does not match etag even If-Modified-Since is fresh" do
initial_response = handle HTTP::Request.new("GET", "/test.txt")
headers = HTTP::Headers.new
headers["If-Modified-Since"] = initial_response.headers["Last-Modified"]
headers["If-None-Match"] = "some random etag"
response = handle HTTP::Request.new("GET", "/test.txt", headers), ignore_body: false
response.status_code.should eq(200)
response.body.should eq(File.read(datapath("static_file_handler", "test.txt")))
end
end
context "when a Range header is provided" do
context "int range" do
it "serves a byte range" do
headers = HTTP::Headers{"Range" => "bytes=0-2"}
response = handle HTTP::Request.new("GET", "/range.txt", headers)
response.status_code.should eq(206)
response.headers["Content-Range"]?.should eq "bytes 0-2/12"
response.body.should eq "Hel"
end
it "serves a single byte" do
headers = HTTP::Headers{"Range" => "bytes=0-0"}
response = handle HTTP::Request.new("GET", "/range.txt", headers)
response.status_code.should eq(206)
response.headers["Content-Range"]?.should eq "bytes 0-0/12"
response.body.should eq "H"
end
it "serves zero bytes" do
headers = HTTP::Headers{"Range" => "bytes=0-0"}
response = handle HTTP::Request.new("GET", "/empty.txt", headers)
response.status_code.should eq(416)
response.headers["Content-Range"]?.should eq "bytes */0"
response.body.should eq ""
end
it "serves an open-ended byte range" do
headers = HTTP::Headers{"Range" => "bytes=6-"}
response = handle HTTP::Request.new("GET", "/range.txt", headers)
response.status_code.should eq(206)
response.headers["Content-Range"]?.should eq "bytes 6-11/12"
response.body.should eq "world\n"
end
it "serves multiple byte ranges (separator without whitespace)" do
headers = HTTP::Headers{"Range" => "bytes=0-1,6-7"}
response = handle HTTP::Request.new("GET", "/range.txt", headers)
response.status_code.should eq(206)
response.headers["Content-Range"]?.should be_nil
count = 0
MIME::Multipart.parse(response) do |headers, part|
chunk = part.gets_to_end
case range = headers["Content-Range"]
when "bytes 0-1/12"
chunk.should eq "He"
when "bytes 6-7/12"
chunk.should eq "wo"
else
fail "Unknown range: #{range.inspect}"
end
count += 1
end
count.should eq 2
end
it "serves multiple byte ranges (separator with whitespace)" do
headers = HTTP::Headers{"Range" => "bytes=0-1, 6-7"}
response = handle HTTP::Request.new("GET", "/range.txt", headers)
response.status_code.should eq(206)
response.headers["Content-Range"]?.should be_nil
count = 0
MIME::Multipart.parse(response) do |headers, part|
chunk = part.gets_to_end
case range = headers["Content-Range"]
when "bytes 0-1/12"
chunk.should eq "He"
when "bytes 6-7/12"
chunk.should eq "wo"
else
fail "Unknown range: #{range.inspect}"
end
count += 1
end
count.should eq 2
end
it "end of the range is larger than the file size" do
headers = HTTP::Headers{"Range" => "bytes=6-14"}
response = handle HTTP::Request.new("GET", "/range.txt", headers)
response.status_code.should eq 206
response.headers["Content-Range"]?.should eq "bytes 6-11/12"
response.body.should eq "world\n"
end
it "start of the range is larger than the file size" do
headers = HTTP::Headers{"Range" => "bytes=14-15"}
response = handle HTTP::Request.new("GET", "/range.txt", headers)
response.status_code.should eq 416
response.headers["Content-Range"]?.should eq "bytes */12"
end
it "start >= file_size" do
headers = HTTP::Headers{"Range" => "bytes=12-"}
response = handle HTTP::Request.new("GET", "/range.txt", headers)
response.status_code.should eq(416)
response.headers["Content-Range"]?.should eq "bytes */12"
end
end
describe "suffix range" do
it "partial" do
headers = HTTP::Headers{"Range" => "bytes=-6"}
response = handle HTTP::Request.new("GET", "/range.txt", headers)
response.status_code.should eq(206)
response.headers["Content-Range"]?.should eq "bytes 6-11/12"
response.body.should eq "world\n"
end
it "more bytes than content" do
headers = HTTP::Headers{"Range" => "bytes=-15"}
response = handle HTTP::Request.new("GET", "/range.txt", headers)
response.status_code.should eq(206)
response.headers["Content-Range"]?.should eq "bytes 0-11/12"
response.body.should eq "Hello world\n"
end
it "zero" do
headers = HTTP::Headers{"Range" => "bytes=-0"}
response = handle HTTP::Request.new("GET", "/range.txt", headers)
response.status_code.should eq(400)
response.headers["Content-Range"]?.should be_nil
end
it "zero" do
headers = HTTP::Headers{"Range" => "bytes=-0"}
response = handle HTTP::Request.new("GET", "/empty.txt", headers)
response.status_code.should eq(400)
response.headers["Content-Range"]?.should be_nil
end
it "empty file" do
headers = HTTP::Headers{"Range" => "bytes=-1"}
response = handle HTTP::Request.new("GET", "/empty.txt", headers)
response.status_code.should eq(200)
response.headers["Content-Range"]?.should be_nil
end
it "negative size" do
headers = HTTP::Headers{"Range" => "bytes=--2"}
response = handle HTTP::Request.new("GET", "/range.txt", headers)
response.status_code.should eq(400)
response.headers["Content-Range"]?.should be_nil
end
end
describe "invalid Range syntax" do
it "byte number without dash" do
headers = HTTP::Headers{"Range" => "bytes=1"}
response = handle HTTP::Request.new("GET", "/range.txt", headers)
response.status_code.should eq(400)
end
it "start > end" do
headers = HTTP::Headers{"Range" => "bytes=2-1"}
response = handle HTTP::Request.new("GET", "/range.txt", headers)
response.status_code.should eq(400)
end
it "negative end" do
headers = HTTP::Headers{"Range" => "bytes=1--2"}
response = handle HTTP::Request.new("GET", "/range.txt", headers)
response.status_code.should eq(400)
end
it "open range with negative end" do
headers = HTTP::Headers{"Range" => "bytes=--2"}
response = handle HTTP::Request.new("GET", "/range.txt", headers)
response.status_code.should eq(400)
end
it "open range with negative end" do
headers = HTTP::Headers{"Range" => "bytes=--2"}
response = handle HTTP::Request.new("GET", "/empty.txt", headers)
response.status_code.should eq(400)
end
it "unsupported unit" do
headers = HTTP::Headers{"Range" => "chars=1-2"}
response = handle HTTP::Request.new("GET", "/range.txt", headers)
response.status_code.should eq(416)
response.headers["Content-Range"]?.should eq "bytes */12"
end
it "multiple dashes" do
headers = HTTP::Headers{"Range" => "bytes=1-2-3"}
response = handle HTTP::Request.new("GET", "/range.txt", headers)
response.status_code.should eq(400)
end
it "not a number" do
headers = HTTP::Headers{"Range" => "bytes=a-b"}
response = handle HTTP::Request.new("GET", "/range.txt", headers)
response.status_code.should eq(400)
end
it "not a range" do
headers = HTTP::Headers{"Range" => "bytes=-"}
response = handle HTTP::Request.new("GET", "/range.txt", headers)
response.status_code.should eq(400)
end
end
end
it "lists directory's entries" do
response = handle HTTP::Request.new("GET", "/")
response.status_code.should eq(200)
response.body.should match(/test.txt/)
end
it "does not list directory's entries when directory_listing is set to false" do
response = handle HTTP::Request.new("GET", "/"), directory_listing: false
response.status_code.should eq(404)
end
it "does not serve a not found file" do
response = handle HTTP::Request.new("GET", "/not_found_file.txt")
response.status_code.should eq(404)
end
it "does not serve a not found directory" do
response = handle HTTP::Request.new("GET", "/not_found_dir/")
response.status_code.should eq(404)
end
it "does not serve a file as directory" do
response = handle HTTP::Request.new("GET", "/test.txt/")
response.status_code.should eq(404)
end
it "handles only GET and HEAD method" do
%w(GET HEAD).each do |method|
response = handle HTTP::Request.new(method, "/test.txt")
response.status_code.should eq(200)
end
%w(POST PUT DELETE).each do |method|
response = handle HTTP::Request.new(method, "/test.txt")
response.status_code.should eq(404)
response = handle HTTP::Request.new(method, "/test.txt"), false
response.status_code.should eq(405)
response.headers["Allow"].should eq("GET, HEAD")
end
end
it "expands a request path" do
%w(../test.txt ../../test.txt test.txt/../test.txt a/./b/../c/../../test.txt).each do |path|
response = handle HTTP::Request.new("GET", "/#{path}")
response.status_code.should eq(302)
response.headers["Location"].should eq("/test.txt")
end
# directory
%w(.. ../ ../.. a/.. a/.././b/../).each do |path|
response = handle HTTP::Request.new("GET", "/#{path}")
response.status_code.should eq(302)
response.headers["Location"].should eq("/")
end
end
it "unescapes a request path" do
%w(test%2Etxt %74%65%73%74%2E%74%78%74).each do |path|
response = handle HTTP::Request.new("GET", "/#{path}")
response.status_code.should eq(200)
response.body.should eq(file_text)
end
%w(%2E%2E/test.txt found%2F%2E%2E%2Ftest%2Etxt).each do |path|
response = handle HTTP::Request.new("GET", "/#{path}")
response.status_code.should eq(302)
response.headers["Location"].should eq("/test.txt")
end
end
it "returns 400" do
%w(%00 test.txt%00).each do |path|
response = handle HTTP::Request.new("GET", "/#{path}")
response.status_code.should eq(400)
end
end
it "handles invalid redirect path" do
response = handle HTTP::Request.new("GET", "test.txt%0A")
response.status_code.should eq(302)
response.headers["Location"].should eq "/test.txt%0A"
response = handle HTTP::Request.new("GET", "/test.txt%0A")
response.status_code.should eq(404)
end
it "serve compressed content" do
modification_time = File.info(datapath("static_file_handler", "test.txt")).modification_time
File.touch datapath("static_file_handler", "test.txt.gz"), modification_time + 1.second
headers = HTTP::Headers{"Accept-Encoding" => "gzip"}
response = handle HTTP::Request.new("GET", "/test.txt", headers), decompress: false
response.headers["Content-Encoding"].should eq("gzip")
end
it "still serve compressed content when modification time is very close" do
modification_time = File.info(datapath("static_file_handler", "test.txt")).modification_time
File.touch datapath("static_file_handler", "test.txt.gz"), modification_time - 1.millisecond
headers = HTTP::Headers{"Accept-Encoding" => "gzip"}
response = handle HTTP::Request.new("GET", "/test.txt", headers), decompress: false
response.headers["Content-Encoding"].should eq("gzip")
end
it "doesn't serve compressed content if older than raw file" do
modification_time = File.info(datapath("static_file_handler", "test.txt")).modification_time
File.touch datapath("static_file_handler", "test.txt.gz"), modification_time - 1.second
headers = HTTP::Headers{"Accept-Encoding" => "gzip"}
response = handle HTTP::Request.new("GET", "/test.txt", headers)
response.headers["Content-Encoding"]?.should be_nil
end
end
|