Search code examples
erlanghttp-headersyawshttp-response-codes

changing a specific header in yaws respond


I would like to change the http-header "Server: " value dynamically (while building the respond) in yaws. I tried:-

[{status, 200},
 {allheaders,
      [{header,
          ["Server: ","ASP.NET Development Server/10.0.0.0"]
      }]
 },
 {html,Body}
]

it returned two "Server: " headers instead of one. But it worked for all other headers.


Solution

  • 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