Modify Http Headers on System Level (Linux)

How to send a header using a HTTP request through a cURL call?

GET:

with JSON:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" http://hostname/resource

with XML:

curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource

POST:

For posting data:

curl --data "param1=value1¶m2=value2" http://hostname/resource

For file upload:

curl --form "fileupload=@filename.txt" http://hostname/resource

RESTful HTTP Post:

curl -X POST -d @filename http://hostname/resource

For logging into a site (auth):

curl -d "username=admin&password=admin&submit=Login" --dump-header headers http://localhost/Login
curl -L -b headers http://localhost/

How to parse HTTP headers by /bin/sh?

You can trim whitespace by using read with a here document. Use a named pipe to "simulate" the process substitution. (Process substitution may in fact be implemented with named pipes on some operating systems.)

mkfifo headers
curl -sI http://www.google.com > headers &

{
# This line is guaranteed to be first, before any headers.
# Read it separately.
read -r PROTO STATUS MSG
while IFS=':' read -r key value; do
# trim whitespace in "value"
read -r value <<EOF
$value
EOF

case $key in
Server) SERVER="$value"
;;
Content-Type) CT="$value"
;;
esac
done
} < headers
rm headers

I leave it as an exercise to research how to indent the body of the here document properly.

changing a specific header in yaws respond

For Older Versions of yaws (yaws-1.90 and below) the answer below works


Now, by default, any header specified as [{header,"Server: MyOther Server Name"}] , yaws will enforce and tag its name and version before your server header value, like this: Server: Yaws 1.91, MyOther Server Name. This occurs when in your out/1 function you end with a header value, server like this:


out(_A)->
[{header,"Server: ASP.NET Server"},
{html,"<p>I love klacke and yaws ! </p>"}].

Checking your browser, using Firebug you see the Response headers specified as follows:



Date Thu, 17 Nov 2011 07:59:17 GMT
Content-Length 22
Content-Type text/html
Server Yaws 1.91, ASP.NET Server

Now the solution:


In the yaws src, there is a module called yaws.erl. In this module, check around line: 1332, you will find a function like this:


make_server_header() ->
HasDav = ?sc_has_dav(get(sc)),
["Server: ", (get(gc))#gconf.yaws, "\r\n" |
if HasDav == true ->
["DAV: 1\r\n"];
true ->
[]
end].

This is the piece of code that appends the yaws version and server name on top of your server header specified value. We need to change it and comment out the lines and let it return an empty list [ ] or empty string "" to look like this:


make_server_header() ->
%%HasDav = ?sc_has_dav(get(sc)),
%%["Server: ", (get(gc))#gconf.yaws, "\r\n" |
%% if HasDav == true ->
%% ["DAV: 1\r\n"];
%% true ->
%% []
%% end].
[]. %% force it to just return empty

Last step

Now we need to re-compile this module and replace the existing yaws.beam file in YAWS-{VSN}/ebin with the new resulting .beam file we get after compiling this new source. Then reload it in the VM. I hope you can find your way in this last step. One way of pulling this off is by changing the current working directory of your yaws running erlang shell


1> cd("PATH/TO/YAWS/src").
PATH/TO/YAWS/src
Then, execute this (still in the yaws shell) after changing the source of the yaws.erl as we have mentioned above

2> c(yaws).
{ok,yaws}

Now move the generated yaws.beam file from src to ebin. Overwrite/ replace the existing yaws.beam file in ebin with the one from src


$ mv PATH/TO/YAWS/src/yaws.beam PATH/TO/YAWS/ebin/
So, if yaws is running, reload that code by entering in its shell:


3> l(yaws).


For Newer versions of yaws (yaws-1.91 and above)



There is a parameter you will just add to your yaws.conf file

server_signature = "MY SERVER SIGNATURE"

And so the headers will come with Server: MY SERVER SIGNATURE



Related Topics



Leave a reply



Submit