Permalink
Browse files

info requests split out into a separate lib file

  • Loading branch information...
1 parent 0702612 commit d1f64c747bf80bdc3b1fdac5343f8b3e7237a888 @substack committed Sep 23, 2012
Showing with 78 additions and 69 deletions.
  1. +1 −69 lib/handle.js
  2. +72 −0 lib/info.js
  3. +5 −0 lib/no_cache.js
View
@@ -7,8 +7,8 @@ var spawn = require('child_process').spawn;
var services = [ 'upload-pack', 'receive-pack' ]
var createAction = require('./service');
-var onexit = require('./onexit');
var noCache = require('./no_cache');
+var infoResponse = require('./info');
module.exports = function (req, res) {
var self = this;
@@ -128,71 +128,3 @@ handlers.push(function (req, res) {
res.statusCode = 404;
res.end('not found');
});
-
-function serviceRespond (service, file, res) {
- function pack (s) {
- var n = (4 + s.length).toString(16);
- return Array(4 - n.length + 1).join('0') + n + s;
- }
- res.write(pack('# service=git-' + service + '\n'));
- res.write('0000');
-
- var ps = spawn('git-' + service, [
- '--stateless-rpc',
- '--advertise-refs',
- file
- ]);
- ps.stdout.pipe(res, { end : false });
- ps.stderr.pipe(res, { end : false });
-
- onexit(ps, function () { res.end() });
-}
-
-var httpDuplex = require('http-duplex');
-
-function infoResponse (opts, req, res) {
- var self = opts.repos;
- var dup = httpDuplex(req, res);
-
- dup.accept = function () { dup.emit('accept') };
- dup.reject = function (code) { dup.emit('reject', code) };
-
- dup.once('reject', function (code) {
- res.statusCode = code || 500;
- res.end();
- });
-
- self.exists(opts.repo, function (ex) {
- var anyListeners = self.listeners('info').length > 0;
-
- if (!ex && self.autoCreate) {
- dup.exists = ex;
-
- dup.on('accept', function () {
- self.create(opts.repo, next)
- });
-
- self.emit('info', dup);
- if (!anyListeners) dup.accept();
- }
- else if (!ex) {
- res.statusCode = 404;
- res.setHeader('content-type', 'text/plain');
- res.end('repository not found');
- }
- else {
- dup.on('accept', next);
- self.emit('info', dup);
-
- if (!anyListeners) dup.accept();
- }
- });
-
- function next () {
- res.setHeader('content-type',
- 'application/x-git-' + opts.service + '-advertisement'
- );
- noCache(res);
- serviceRespond(opts.service, path.join(self.repoDir, opts.repo), res);
- }
-}
View
@@ -0,0 +1,72 @@
+var httpDuplex = require('http-duplex');
+var path = require('path');
+var spawn = require('child_process').spawn;
+
+var noCache = require('./no_cache');
+var onexit = require('./onexit');
+
+module.exports = function (opts, req, res) {
+ var self = opts.repos;
+ var dup = httpDuplex(req, res);
+
+ dup.accept = function () { dup.emit('accept') };
+ dup.reject = function (code) { dup.emit('reject', code) };
+
+ dup.once('reject', function (code) {
+ res.statusCode = code || 500;
+ res.end();
+ });
+
+ self.exists(opts.repo, function (ex) {
+ var anyListeners = self.listeners('info').length > 0;
+
+ if (!ex && self.autoCreate) {
+ dup.exists = ex;
+
+ dup.on('accept', function () {
+ self.create(opts.repo, next)
+ });
+
+ self.emit('info', dup);
+ if (!anyListeners) dup.accept();
+ }
+ else if (!ex) {
+ res.statusCode = 404;
+ res.setHeader('content-type', 'text/plain');
+ res.end('repository not found');
+ }
+ else {
+ dup.on('accept', next);
+ self.emit('info', dup);
+
+ if (!anyListeners) dup.accept();
+ }
+ });
+
+ function next () {
+ res.setHeader('content-type',
+ 'application/x-git-' + opts.service + '-advertisement'
+ );
+ noCache(res);
+ serviceRespond(opts.service, path.join(self.repoDir, opts.repo), res);
+ }
+}
+
+function serviceRespond (service, file, res) {
+ function pack (s) {
+ var n = (4 + s.length).toString(16);
+ return Array(4 - n.length + 1).join('0') + n + s;
+ }
+ res.write(pack('# service=git-' + service + '\n'));
+ res.write('0000');
+
+ var ps = spawn('git-' + service, [
+ '--stateless-rpc',
+ '--advertise-refs',
+ file
+ ]);
+ ps.stdout.pipe(res, { end : false });
+ ps.stderr.pipe(res, { end : false });
+
+ onexit(ps, function () { res.end() });
+}
View
@@ -0,0 +1,5 @@
+module.exports = function (res) {
+ res.setHeader('expires', 'Fri, 01 Jan 1980 00:00:00 GMT');
+ res.setHeader('pragma', 'no-cache');
+ res.setHeader('cache-control', 'no-cache, max-age=0, must-revalidate');
+};

0 comments on commit d1f64c7

Please sign in to comment.