headers snippets

How to print all HTTP headers with PHP

Tagged php, http, headers, request  Languages php
   foreach($_SERVER as $h=>$v)
       echo "<li>$h = $v</li>\n";
   header('Content-type: text/html');

Example of how to fetch a URL with Net:HTTP and Ruby

Tagged net, http, ruby, example, headers  Languages ruby
require 'net/http'
require 'net/https'

url = URI.parse('http://www.google.com/yo?query=yahoo')

http = Net::HTTP.new(url.host, url.port)

http.open_timeout = http.read_timeout = 10  # Set open and read timeout to 10 seconds
http.use_ssl = (url.scheme == "https")
headers = {
  'User-Agent'          => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv: Gecko/20080201 Firefox/',
  'If-Modified-Since'   => '',
  'If-None-Match'       => ''

# Note to self, use request_uri not path: http://www.ruby-doc.org/core/classes/URI/HTTP.html#M004934
response, body = http.get(url.request_uri, headers)

puts response.code
puts response.message

response.each {|key, val| puts key + ' = ' + val}

How to hide X-Powered-By and Server headers

Tagged servertokens, server, x-powered-by, mod_rails, passenger, apache, apache2, headers  Languages apacheconf

First enable the mod_headers module:

sudo a2enmod headers

Then add this to your apache2.conf:

# Hide X-Powered-By and Server headers
Header always unset "X-Powered-By"
ServerTokens Prod
ServerSignature Off

Now restart Apache:

/etc/init.d/apache2 force-reload

This is security through obscurity at it's finest...

Barebone Web Development With Rack

Tagged rack, ruby, dump, headers  Languages ruby

Create a file named config.ru:

run lambda { |env|
  [200, {"Content-Type" => "text/html"}, [view(env)]]

def view(env)
  res = ""
  res << "<html><body><pre>"
  env.sort.each do |key, value|
    res << "#{key}: #{value}"
    res << "\n"
  res << "</pre></body></html>"

Start the server with e.g. rackup, puma, or thin:

$ rackup

Access http://localhost:9292 to see the request headers.