Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active August 29, 2015 14:17
Show Gist options
  • Save JoshCheek/9d742243fca618e1db30 to your computer and use it in GitHub Desktop.
Save JoshCheek/9d742243fca618e1db30 to your computer and use it in GitHub Desktop.
HTTP quiz!

Quiz time!

  • What does the dot mean in in StringIO.new, what does the hash mean in StringIO#puts?
  • How do you make a String literal?
  • How do you make a String without using a literal?

This is an HTTP request, it is a String that is sent across a Socket, which your program can read from as if it were a file or $stdin.

01 POST /users/5?omg=wtf&bbq=lol HTTP/1.1\r\n
02 Host: localhost:8080\r\n
03 Connection: keep-alive\r\n
04 Cache-Control: max-age=0\r\n
05 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n
06 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36\r\n
07 Accept-Encoding: gzip, deflate, sdch\r\n
08 Accept-Language: en-US,en;q=0.8\r\n
09 Content-Length: 13\r\n
10 Content-Type: application/x-www-form-urlencoded\r\n
11 \r\n
12 abc=123&def=4
  • What sends this text?
  • Where does it send it to?
  • What are some things it might receive in response?
  • What is that \r\n all over the place?
  • What is line 01? (we didn't have a formal name)
  • What are lines 02-10?
  • what is the significance of line 11?
  • What is line 12?
  • What is the request method?
  • What is the path?
  • What is the query string?
  • What kind of data structure might the query string be parsed into? In this case, what would that look like? (ie the literal that would be equal to it)
  • How would you access the query parameter "omg" from Rails? Prove this to yourself by making a Rails app, making a request to this query string, and showing yourself that you can access it (in whatever way you like -- Prying into the controller is probably a good choice).
  • What kind of data structure might the headers be parsed into? Verify this by prying into a Rails controller, making a request to it in your browser, and then typing env in pry.
  • What format is the body?
  • What else has the same format as the body?
  • Without looking at the body on line 12, how many characters are on line 12?
  • What port was this server on when I sent it this request?
  • Based on the HTTP method, you can infer the existence of line 12 (though you can't necessarily infer its contents), why is this?
  • Scaffold out a user class with a name, which is a String, and an age, which is an integer. Run the rails server on port 3000, visit /users/new, stop the Rails server and instead run nc -l 3000, and now submit the form.
    • What will the method be?
    • What will the path be?
    • What will the body be?
    • Look at the body, what will the Content-Length be?
    • What will the Content-Type be?
  • Now cancel the nc (control-c), and run your server again. Put a pry in UsersController#create, Visit /users/new, fill out the form with the same values you filled out in the previous question. What will be in params? How does that correlate to what you saw in nc -l?
  • In your console, run this: rackup --builder 'run lambda { |env| [302, {"Location" => "http://www.turing.io", "Hello" => "Teresa!"}, []] }' --port 4567 If you were to go to "http://localhost:4567" in your browser, what would happen? Open the developer tools (in Chrome: command+option+i) and then go to the "Network" tab. Now, make the request. Look at the first entry in this view (or whichever has the 302) Were you right? For both the "Request Headers" and "Response Headers", click "View Raw" what's interesting in the response? Notice that the request looks exactly like what you have to parse for the challenge... that's because it is! Chrome sent that exact request to the rack server.
  • Some characters cannot be placed in url encoded arguments. For example, a left brace: '[' How do we pass a left brace, given that we can't directly use it?
  • You can turn hexidecimal into a Ruby integer with String#to_i(16) why did we pass 16 here?
  • You can turn a Ruby integer into its ASCII character with Fixnum#chr
  • Combine these two pieces of information together to find out what character the hexadecimal string "5b" represents.
  • In one terminal window, run rackup -r sinatra -b 'get("/") { params.inspect }; run Sinatra::Application' -p 4567 In another terminal window, run curl -i 'localhost:4567/?hello=%74%65%72%65%73%61' What came back and why?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment