agrafix / Spock

Another Haskell web framework for rapid development
https://www.spock.li
679 stars 56 forks source link

Clean default response #48

Closed ulfsauer0815 closed 9 years ago

ulfsauer0815 commented 9 years ago

Spock initializes the response state with the error page with status 200 and an empty error message, which results in the following response body:

<html><head><title></title></head><body><h1></h1></body></html>

Especially with REST, empty responses are quite common and with this patch you would not need to specify an explicit response body and reset the content-type header.

agrafix commented 9 years ago

I think the better approach would be to make this configurable in the SpockCfg. Although you can already set a default using the hookAny hook.

ulfsauer0815 commented 9 years ago

How can I set a default with the hookAny hook? It looks like it is only called when no other route matches.

What I want to do is return an empty response (no headers, no body) for routes that do not explicitly set the response body, e.g.

post "foo" $
    return ()

Additionally, how can I reset the headers and the response body this way?

is there a way to remove a header? I still think that the default response should not include an html body and header. (This will also be the default header for a response created with bytes if it is not set by the user.)

Being able to configure that with the SpockCfg would be ideal, but that does not look quite as straight forward to implement as I would like it to be... ;)

agrafix commented 9 years ago

Ah, sorry I didn't understand you correctly! Yes, that's not possible yet. We could solve this using posthooks, analog to the prehooks. But the question there is: When do they fire? If you set a response, the handling currently terminates.

I think the empty response for routes that do not set anything is correct, but we should make sure that non-matched requests return a 404. That seems the case so I will merge the pull request.

For removing headers we'll need a new function, if you like you can contribute one. It will be a bit "tricky" because you will have to check the multiheader (headers that appear multiple times) and the headers and decide what to delete.