Skip to content

Instantly share code, notes, and snippets.

@progrium
Created November 13, 2011 11:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save progrium/1362014 to your computer and use it in GitHub Desktop.
Save progrium/1362014 to your computer and use it in GitHub Desktop.
parse_http for nc http
# writes host to a host file
nc -l -p 9998 -c "./parse_http>/tmp/cgi;. /tmp/cgi>/dev/null && echo $HTTP_HOST > host"
# pipemill the request body
nc -l -p 9998 -c "./parse_http>/tmp/cgi;. /tmp/cgi | while read line; do something; done"
# route on the path
# uhh, well, sure, just use if on $HTTP_PATHINFO
#!/usr/bin/ruby
first_line = true
headers = true
ARGF.each_line do |line|
if first_line
method, uri, version = line.split(' ')
path, query_string = uri.split('?', 2)
puts "REQUEST_METHOD=#{method}"
puts "PATH_INFO=#{path}"
puts "QUERY_STRING=?#{query_string}"
first_line = false
else
if headers
if line.length > 2
name, value = line.split(':', 2) << ''
puts "HTTP_#{name.gsub('-', '_').upcase}=\"#{value.strip}\""
else
headers = false
end
else
puts "echo #{line}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment