# HG changeset patch
# User Matt Harbison <matt_harbi...@yahoo.com>
# Date 1523651553 14400
#      Fri Apr 13 16:32:33 2018 -0400
# Node ID 075edc6d6add061ac13578bc3301c581de347412
# Parent  6645ae6f5e2a248f23ec05df1ff6f48f3377cc47
lfs: update the HTTP status codes in error cases

I'm not bothering with validating PUT requests (for now), because the spec
doesn't explicitly call out a Content-Type (though the example transcript does
use the sensible 'application/octet-stream').

diff --git a/hgext/lfs/wireprotolfsserver.py b/hgext/lfs/wireprotolfsserver.py
--- a/hgext/lfs/wireprotolfsserver.py
+++ b/hgext/lfs/wireprotolfsserver.py
@@ -26,6 +26,9 @@ HTTP_OK = hgwebcommon.HTTP_OK
 HTTP_CREATED = hgwebcommon.HTTP_CREATED
 HTTP_BAD_REQUEST = hgwebcommon.HTTP_BAD_REQUEST
 HTTP_NOT_FOUND = hgwebcommon.HTTP_NOT_FOUND
+HTTP_METHOD_NOT_ALLOWED = hgwebcommon.HTTP_METHOD_NOT_ALLOWED
+HTTP_NOT_ACCEPTABLE = hgwebcommon.HTTP_NOT_ACCEPTABLE
+HTTP_UNSUPPORTED_MEDIA_TYPE = hgwebcommon.HTTP_UNSUPPORTED_MEDIA_TYPE
 
 def handlewsgirequest(orig, rctx, req, res, checkperm):
     """Wrap wireprotoserver.handlewsgirequest() to possibly process an LFS
@@ -105,12 +108,16 @@ def _processbatchrequest(repo, req, res)
     #     "operation": "upload"
     #  }
 
-    if (req.method != b'POST'
-        or req.headers[b'Content-Type'] != b'application/vnd.git-lfs+json'
-        or req.headers[b'Accept'] != b'application/vnd.git-lfs+json'):
-        # TODO: figure out what the proper handling for a bad request to the
-        #       Batch API is.
-        _sethttperror(res, HTTP_BAD_REQUEST, b'Invalid Batch API request')
+    if req.method != b'POST':
+        _sethttperror(res, HTTP_METHOD_NOT_ALLOWED)
+        return True
+
+    if req.headers[b'Content-Type'] != b'application/vnd.git-lfs+json':
+        _sethttperror(res, HTTP_UNSUPPORTED_MEDIA_TYPE)
+        return True
+
+    if req.headers[b'Accept'] != b'application/vnd.git-lfs+json':
+        _sethttperror(res, HTTP_NOT_ACCEPTABLE)
         return True
 
     # XXX: specify an encoding?
@@ -315,6 +322,6 @@ def _processbasictransfer(repo, req, res
 
         return True
     else:
-        _sethttperror(res, HTTP_BAD_REQUEST,
+        _sethttperror(res, HTTP_METHOD_NOT_ALLOWED,
                       message=b'Unsupported LFS transfer method: %s' % method)
         return True
diff --git a/mercurial/hgweb/common.py b/mercurial/hgweb/common.py
--- a/mercurial/hgweb/common.py
+++ b/mercurial/hgweb/common.py
@@ -30,6 +30,8 @@ HTTP_UNAUTHORIZED = 401
 HTTP_FORBIDDEN = 403
 HTTP_NOT_FOUND = 404
 HTTP_METHOD_NOT_ALLOWED = 405
+HTTP_NOT_ACCEPTABLE = 406
+HTTP_UNSUPPORTED_MEDIA_TYPE = 415
 HTTP_SERVER_ERROR = 500
 
 
_______________________________________________
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Reply via email to