Port the outdated pipeline patch against master

This is a slight modification of 220df26ca8908fc1e727423d6f4712ed37c9126b.  Obviously I'm not a C expert, so it might not as correct as before.

One known issue: `repeat_request(..)` allocates a new buffer to create a repeated request buffer instead of `realloc`'ing the original buffer.  If I try to do `realloc` or `free` the original buffer, I always get segfault.  Maybe someone could shed me light on this.