Haskell WAI middleware to remove a header #feedbase #haskell #programming

Fe The website of Feedbase is a Haskell application, built using the Spock framework. It's my first Haskell program that does something non-trivial in the "real" world.

Recently I was mucking about with cookies in another context, and noticed that the Feedbase website sets a cookie, spockcookie.

This is part of Spock's built-in session handling. I don't use sessions, so I'd rather not set the cookie (given the EU's weird cookie rules and what have you).

You can't turn them off in Spock easily, so I started thinking that maybe some "middleware" could be used.

After some searching I found a StackOverflow question, and combined with looking at the source code of the Network.WAI.Middleware.Gzip module I cobbled together my own little "NoCookies" module:

{-# LANGUAGE OverloadedStrings #-}
module NoCookies where
-- Remove all Set-Cookie headers on responses.
import Network.Wai (Middleware)
import Network.Wai.Internal (Response(..))
import Network.HTTP.Types (Header)
-- Function to hook into middleware:
nocookies :: Middleware
nocookies application request sendResponse = application request $ sendResponse . removeHeader
-- Handle all the various kinds of responses:
removeHeader :: Response -> Response
removeHeader (ResponseFile s h b1 b2) = ResponseFile s (filterSetCookie h) b1 b2
removeHeader (ResponseBuilder s h b) = ResponseBuilder s (filterSetCookie h) b
removeHeader (ResponseStream s h b) = ResponseStream s (filterSetCookie h) b
removeHeader r@(ResponseRaw _ _) = r
-- Remove Set-Cookie from headers:
filterSetCookie :: [Header] -> [Header]
filterSetCookie hs = filter notSetCookie hs
    notSetCookie (x, _) = x /= "Set-Cookie"

A couple of odd things: the Network.HTTP.Types module defines a bunch of constants for headers, but not hSetCookie.

As you can see, most of my module consists of lines pattern matching the Response type - it seems odd that there should be no smarter way of doing this.

As usual in Haskell the reader of documentation is assumed to always know how to put things together. Unfortunately I don't always know that. So here's how I added my module to the Main.hs of my application:

import NoCookies
app :: SpockCtxM ctx Pg.Connection session state ()
app = do
  logger <- liftIO $ mkRequestLogger def { outputFormat = Apache FromFallback }
  middleware logger
  middleware $ gzip def
  middleware (staticPolicy (addBase "static"))
  middleware nocookies


EBR-I tour

First four light bulbs lit by an nuclear reactor This series of 5 "home videos" from a tour of one of the first nuclear reactors in the world, EBR-I, is fascinating:

Ray Haroldsen, who worked there, explains and reminisces brilliantly, while the tour guide listens.

Via Atlas Obscura.


Feedbase documentation #feedbase

Last year I created Feedbase, providing Atom/RSS-feeds via nntp.

Today I finally got my act together and updated the documentation-page with a short section on how to set up Gnus, a screencast on how to use Feedbase in Thunderbird and a section on using slrn to access Feedbase.

I hope this will help anyone interested to get started. I'm enjoying Feedbase a lot.


Dear New York Times #email

nytimes.com says a valid email address is invalid This email address is perfectly valid - '+' is allowed in the local-part, i.e. before the '@' sign.


I ♥ Free Software #free software #ilovefs

I ♥ Free Software Debian, GNU Emacs, Linux, Gnus, XMonad, Perl, Python, Apache, jabber.el, PostgreSQL, Xorg, git, GHC, LaTeX, Gimp, mpd, vlc, Firefox, etc. etc.


XMonad 0.13 released #haskell #x

XMonad 0.13 has been released - and it includes a fix for the bug where window borders would be transparent for windows that use RGBA color scheme (such as the terminal emulator Sakura).

Xmonad logo I started trying to fix the bug more than a year ago, but my Haskell skills weren't up to finishing the job. Fortunately Peter J. Jones jumped in and did the rest!

In 2013 I lamented not being able to switch to XMonad. What happened in the meantime is that I got a high resolution laptop and switched at home. After some months I switched at work as well, and now I'm very happy with it. The most important thing in making the switch was that I bound going to the previous/next workspace to super-z and super-x, which I find quite convenient.


Writing long numbers in Python 3.6 #language #perl #programming #python

I love this new innovation in Python - you can make long numbers more readable with underscores (e.g. 1_000_000).

Perl has had this for... how long? At least 26 years (Perl 4.0).

 (1 comment)

Archive... Search... Keywords...
Publisher at Google+
Sommerhus 2015


Gabriel Daniel Fahrenheit (331).

Queen Victoria (198).

Bob Dylan (76).


Jazz trumpeter Miles Davis (91).

Towel day.


Kronprins Frederik (49).


John Berchtold (34).

Mathias Rust lands on the Red Square (30).


Bob Hope (114).


Clint Eastwood (87).

World Smokefree Day (30).

0.0333 s