var express = require('express');
var redis = require('redis');

// The regular expresion for UUID paths
var hexClass = "[0-9a-fA-F]";
var uuidRoute = '/:path('+ hexClass + "{8}-" + hexClass + "{4}-"
  + hexClass + "{4}-" + hexClass + "{4}-"
  + hexClass + "{12})/:index(\\d+)?";

module.exports = function(cfg){
  // The duration to wait before responding to a poll.
  var pollWait = cfg.pollWait || 10000;
  // The duration to wait for a re-request after responding to a poll.
  var afterWait = cfg.afterWait || 10000;
  // The TTL of an item that is intended to be polled on.
  var itemTTL = pollWait + afterWait;

  var app = express();

  // Set up interface for Redis and channel-based pub/sub callbacks
  var db = redis.createClient(cfg.redis.port, cfg.redis.hostname,
      {no_ready_check: true});
  if (cfg.redis.password) db.auth(cfg.redis.password);

  // Middleware to handle rewriting responses.
  app.use(function(req,res,next){
    req.urlPrefix = req.originalUrl.slice(0,-req.url.length);
    next();
  });

  // Middleware to handle text bodies.
  function parseTextBody(req, res, next){
    if (req.is('text/*') && req.body === undefined) {
      req.body = '';
      req.setEncoding('utf8');
      req.on('data', function(chunk){ req.body += chunk });
      req.on('end', next);
    } else {
      next();
    }
  }

  app.use(parseTextBody);

  // Export wrapped offerPost
  app.post('/offers/:phrase', require('./routes/offerPost.js')(db, itemTTL));

  // offerGet doesn't need req.body, so it can be exposed
  // without middleware wrapping
  app.get('/offers/:phrase', require('./routes/offerGet.js')(db));

  // UUID PUT handler, for offer/answer updates
  // (deprecated in favor of update message POSTs)
  app.put(uuidRoute, require('./routes/uuidGet.js')(db));

  // UUID POST handler, for answers and messages
  app.post(uuidRoute, require('./routes/uuidPost.js')(db, pollWait, itemTTL));

  // UUID GET handler, for listening for updates to an offer/answer
  app.get(uuidRoute, require('./routes/uuidGet.js')(db,
    require('./subscriber.js')(cfg.redis), pollWait, itemTTL));
    
  return app;
};

@@ -1111,28 +1111,8 @@
 if (
-req.is('text/*') && 
 req.
@@ -1158,39 +1158,8 @@
 '';%0A
-      req.setEncoding('utf8');%0A
     
@@ -1315,32 +1315,56 @@
  // 
-Export wrapped
+Offer POST handler, for receiving initial
  offer
-Post
+s
 %0A  a
@@ -1448,93 +1448,57 @@
  // 
-o
+O
 ffer
-Get doesn't need req.body, so it can be exposed%0A  // without middleware wrapping
+ POST handler, for announcing initial offers
 %0A  a

Remove text/* content-type requirement for body parsing