Created
July 1, 2014 10:53
-
-
Save matsadler/6027136726c0be80111b to your computer and use it in GitHub Desktop.
Make a HTTP request to unicorn over a UNIX socket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "socket" | |
# edit these | |
socket_path = "tmp/sockets/unicorn.sock" | |
host = "example.com" # HTTP 1.1 requires a Host header | |
path = "/" | |
# connect | |
sock = UNIXSocket.new(socket_path) | |
# make request | |
sock.write("GET #{path} HTTP/1.1\r\nHost: #{host}\r\n\r\n") | |
# read/print the status line | |
status = sock.readline.chomp | |
puts status | |
headers = {} | |
# read/print the headers | |
while true | |
line = sock.readline.chomp | |
puts line | |
break if line.empty? | |
key, value = line.split(/: */, 2) | |
headers[key.downcase] = value | |
end | |
# attempt to read the rest of the request | |
# hopefully we got a Content-Length, and not Transfer-Encoding: chunked | |
sock.read(headers["content-length"].to_i) | |
sock.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment