module Hack.Contrib.ContentSize where

import Hack
import Hack.Utils
import Prelude hiding ((.), (^), (>))
import MPS
import Hack.Constants
import Data.Maybe (isNothing)

content_size :: MiddleWare
content_size app = \env -> do
  response <- app env
  
  if should_size response
    then response.set_header "Content-Length" (response.body.bytesize.show) .return
    else response .return
  
  where 
    should_size response = 
      [  response.header "Content-Length" .isNothing
      ,  response.header "Transfer-Encoding" .isNothing
      ,  not $ status_with_no_entity_body.has(response.status)
      ] .and