[PATCH] hgweb: return 501 instead of 400 on unknown command

Martin von Zweigbergk martinvonz at google.com
Thu Jun 4 15:15:39 CDT 2015


# HG changeset patch
# User Martin von Zweigbergk <martinvonz at google.com>
# Date 1433439984 25200
#      Thu Jun 04 10:46:24 2015 -0700
# Node ID af314daf5f45ebbaa78d370bfc7982767f5fe5ee
# Parent  c0995cd8ff6fdc44ff20835e005771f08452a353
hgweb: return 501 instead of 400 on unknown command

When an unrecognized command is received, we return a 400 (Bad
Request), which means that the client is to blame. Since the set of
valid commands is not fixed, and it may just be a newer client sending
a new command, it is more fair use 501 (Not Implemented).

Found while writing a custom Mercurial server. I picked 501 for the
server before realizing it was 400 from upstream hg.

diff --git a/mercurial/hgweb/common.py b/mercurial/hgweb/common.py
--- a/mercurial/hgweb/common.py
+++ b/mercurial/hgweb/common.py
@@ -16,6 +16,7 @@
 HTTP_NOT_FOUND = 404
 HTTP_METHOD_NOT_ALLOWED = 405
 HTTP_SERVER_ERROR = 500
+HTTP_NOT_IMPLEMENTED = 501
 
 
 def ismember(ui, username, userlist):
diff --git a/mercurial/hgweb/hgweb_mod.py b/mercurial/hgweb/hgweb_mod.py
--- a/mercurial/hgweb/hgweb_mod.py
+++ b/mercurial/hgweb/hgweb_mod.py
@@ -11,8 +11,8 @@
 from mercurial.templatefilters import websub
 from mercurial.i18n import _
 from common import get_stat, ErrorResponse, permhooks, caching
-from common import HTTP_OK, HTTP_NOT_MODIFIED, HTTP_BAD_REQUEST
-from common import HTTP_NOT_FOUND, HTTP_SERVER_ERROR
+from common import HTTP_OK, HTTP_NOT_MODIFIED
+from common import HTTP_NOT_FOUND, HTTP_SERVER_ERROR, HTTP_NOT_IMPLEMENTED
 from request import wsgirequest
 import webcommands, protocol, webutil
 
@@ -245,7 +245,7 @@
                 caching(self, req) # sets ETag header or raises NOT_MODIFIED
             if cmd not in webcommands.__all__:
                 msg = 'no such method: %s' % cmd
-                raise ErrorResponse(HTTP_BAD_REQUEST, msg)
+                raise ErrorResponse(HTTP_NOT_IMPLEMENTED, msg)
             elif cmd == 'file' and 'raw' in req.form.get('style', []):
                 self.ctype = ctype
                 content = webcommands.rawfile(self, req, tmpl)
diff --git a/tests/test-hgweb.t b/tests/test-hgweb.t
--- a/tests/test-hgweb.t
+++ b/tests/test-hgweb.t
@@ -113,31 +113,31 @@
   error: revision not found: spam
   [1]
 
-should give a 400 - bad command
+should give a 501 - bad command
 
   $ "$TESTDIR/get-with-headers.py" localhost:$HGPORT 'file/tip/foo?cmd=spam&style=raw'
-  400* (glob)
+  501* (glob)
   
   
   error: no such method: spam
   [1]
 
   $ "$TESTDIR/get-with-headers.py" --headeronly localhost:$HGPORT '?cmd=spam'
-  400 no such method: spam
+  501 no such method: spam
   [1]
 
-should give a 400 - bad command as a part of url path (issue4071)
+should give a 501 - bad command as a part of url path (issue4071)
 
   $ "$TESTDIR/get-with-headers.py" --headeronly localhost:$HGPORT 'spam'
-  400 no such method: spam
+  501 no such method: spam
   [1]
 
   $ "$TESTDIR/get-with-headers.py" --headeronly localhost:$HGPORT 'raw-spam'
-  400 no such method: spam
+  501 no such method: spam
   [1]
 
   $ "$TESTDIR/get-with-headers.py" --headeronly localhost:$HGPORT 'spam/tip/foo'
-  400 no such method: spam
+  501 no such method: spam
   [1]
 
 should give a 404 - file does not exist


More information about the Mercurial-devel mailing list