@@ -9,6 +9,10 @@ var fixturez = require('fixturez')
99var backend = require ( 'git-http-backend' )
1010var htpasswd = require ( 'htpasswd-js' )
1111
12+ function pad ( str ) {
13+ return ( str + ' ' ) . slice ( 0 , 7 )
14+ }
15+
1216function factory ( config ) {
1317 if ( ! config . root ) throw new Error ( 'Missing required "gitHttpServer.root" config option' )
1418 if ( ! config . route ) throw new Error ( 'Missing required "gitHttpServer.route" config option' )
@@ -39,13 +43,20 @@ function factory (config) {
3943 }
4044
4145 return async function middleware ( req , res , next ) {
46+ // handle pre-flight OPTIONS
47+ if ( req . method === 'OPTIONS' ) {
48+ res . statusCode = 204
49+ res . end ( '' )
50+ console . log ( chalk . green ( '[git-http-server] 204 ' + pad ( req . method ) + ' ' + req . url ) )
51+ return
52+ }
4253 if ( ! next ) next = ( ) => void ( 0 )
4354 try {
4455 var gitdir = getGitDir ( req )
4556 } catch ( err ) {
4657 res . statusCode = 404
4758 res . end ( err . message + '\n' )
48- console . log ( chalk . red ( '[git-http-server] 404 ' + req . url ) )
59+ console . log ( chalk . red ( '[git-http-server] 404 ' + pad ( req . method ) + ' ' + req . url ) )
4960 return
5061 }
5162 if ( gitdir == null ) return next ( )
@@ -63,9 +74,12 @@ function factory (config) {
6374 let cred = auth . parse ( req . headers [ 'authorization' ] )
6475 if ( cred === undefined ) {
6576 res . statusCode = 401
77+ // The default reason phrase used in Node is "Unauthorized", but
78+ // we will use "Authorization Required" to match what Github uses.
79+ res . statusMessage = 'Authorization Required'
6680 res . setHeader ( 'WWW-Authenticate' , 'Basic' )
6781 res . end ( 'Unauthorized' + '\n' )
68- console . log ( chalk . red ( '[git-http-server] 401 ' + req . url ) )
82+ console . log ( chalk . green ( '[git-http-server] 401 ' + pad ( req . method ) + ' ' + req . url ) )
6983 return
7084 }
7185 let valid = await htpasswd . authenticate ( {
@@ -75,9 +89,12 @@ function factory (config) {
7589 } )
7690 if ( ! valid ) {
7791 res . statusCode = 401
92+ // The default reason phrase used in Node is "Unauthorized", but
93+ // we will use "Authorization Required" to match what Github uses.
94+ res . statusMessage = 'Authorization Required'
7895 res . setHeader ( 'WWW-Authenticate' , 'Basic' )
7996 res . end ( 'Bad credentials' + '\n' )
80- console . log ( chalk . red ( '[git-http-server] 401 ' + req . url ) )
97+ console . log ( chalk . green ( '[git-http-server] 401 ' + pad ( req . method ) + ' ' + req . url ) )
8198 return
8299 }
83100 }
@@ -86,12 +103,12 @@ function factory (config) {
86103 if ( err ) {
87104 res . statusCode = 500
88105 res . end ( err + '\n' )
89- console . log ( chalk . red ( '[git-http-server] 500 ' + req . url ) )
106+ console . log ( chalk . red ( '[git-http-server] 500 ' + pad ( req . method ) + ' ' + req . url ) )
90107 return
91108 }
92109
93110 res . setHeader ( 'content-type' , service . type )
94- console . log ( chalk . green ( '[git-http-server] 200 ' + req . url ) )
111+ console . log ( chalk . green ( '[git-http-server] 200 ' + pad ( req . method ) + ' ' + req . url ) )
95112 // console.log('[git-http-server] ' + service.cmd + ' ' + service.args.concat(gitdir).join(' '))
96113 var ps = spawn ( service . cmd , service . args . concat ( gitdir ) )
97114 ps . stdout . pipe ( service . createStream ( ) ) . pipe ( ps . stdin )
0 commit comments