changemewtf / dumb_server

A step-by-step implementation of basic HTTP server-side protocol using ruby's TCPServer.
6 stars 1 forks source link

Mysterious apparently-empty request issue when idling #1

Open changemewtf opened 10 years ago

changemewtf commented 10 years ago

Sometimes the server crashes without any apparent browser-driven activity with the following error:

step7.rb:73:in `<main>': undefined method `split' for nil:NilClass (NoMethodError)

Not sure why yet.

Trevoke commented 10 years ago

I decided to wrap line 73 in a begin/rescue block, like so:

    begin
      method, path, protocol = lines[0].split(" ")
    rescue Exception => e
      require 'pp'
      pp connection
      puts "==="
      pp e.backtrace
    end

And here's what I got when it would have crashed:

#<TCPSocket:fd 8>
===
["step7.rb:74:in `<main>'"]
==================================
====== NEW REQUEST INCOMING ======
==================================

------ REQUEST -------------------

----------------------------------

------ RESPONSE ------------------
HTTP/1.1 200 OK
Content-Type: text/html

OK! Have some pudding.
----------------------------------

#<TCPSocket:fd 8>
===
["step7.rb:74:in `<main>'"]
==================================
====== NEW REQUEST INCOMING ======
==================================

------ REQUEST -------------------

----------------------------------

------ RESPONSE ------------------
HTTP/1.1 200 OK
Content-Type: text/html

OK! Have some pudding.
----------------------------------

Here are a few use cases I've observed. And a theory below.

Use case 1: Use Chrome. Go to localhost:9090 and enter a word, then press submit. Wait forever. Nothing bad happens.

Use case 2: Use Chrome. Go to localhost:9090 and enter a word, then press submit. THEN PRESS THE BACK BUTTON. Crash comes.

Use case 3: Use Chrome. Go to localhost:9090 and enter a word. Do not press submit. Crash comes.

Use case 4: Use firefox (or any other browser). Do the normal thing. Nothing bad happens.

Theory: A few years ago, Chrome decided to be clever and PRELOAD PAGES...