瀏覽代碼

fix unicode handling

Daniel Sheffield 1 年之前
父節點
當前提交
038e68d610
共有 3 個文件被更改,包括 19 次插入7 次删除
  1. 9 5
      app/rest/pyapi.py
  2. 1 1
      app/rest/route_decorators.py
  3. 9 1
      app/rest/templates/clip-form.tpl

+ 9 - 5
app/rest/pyapi.py

@@ -129,12 +129,16 @@ def clip():
 
         # TODO: what is correct overhead for form content?
         OVERHEAD = 1024
-        if 'paste' not in request.query and request.content_length == -1 or request.content_length > CLIP_SIZE_LIMIT + OVERHEAD:
-            return abort(418, f"Paste size can not exceed {CLIP_SIZE_LIMIT}")
-
-        content = request.params['paste'].encode('utf-8')
+        content = request.query.get("paste", None)
+        content_length = request.content_length
+        if content_length == -1:
+            return abort(418, f"Content-Length must be specified")
+        if content_length > CLIP_SIZE_LIMIT + OVERHEAD:
+            return abort(418, f"Content-Length can not exceed {CLIP_SIZE_LIMIT+OVERHEAD}")
+
+        content: bytes = content or request.params["paste"].encode('latin-1')
         if len(content) > CLIP_SIZE_LIMIT:
-            return abort(418, f"Paste size can not exceed {CLIP_SIZE_LIMIT}")
+            return abort(418, f"Paste can not exceed {CLIP_SIZE_LIMIT}")
 
         _bytes = blake(content, person='clip'.encode('utf-8'))
         _b32 = bytes_to_base32(_bytes)

+ 1 - 1
app/rest/route_decorators.py

@@ -51,7 +51,7 @@ def normalize(*args, **kwargs):
 
 def _poison_decorator(func: Callable, cache: Cache = None):
     def wrap(*args, **kwargs):
-        if request.params.get('reload') == 'true':
+        if request.params.reload == 'true':
             normalized = normalize_query(request.params, allow=PARAMS)
             cache.remove(normalized)
         return func(cache, *args, **kwargs)

+ 9 - 1
app/rest/templates/clip-form.tpl

@@ -21,7 +21,15 @@ textarea::-webkit-scrollbar-thumb {
   border: 3px solid var(--scrollbarBG);
 }
   </style>
-  <textarea style="width: 80%" id="paste-text-area" name="paste" rows="30" {{!disabled}}>{{ content }}</textarea>
+  <textarea
+    style="width: 80%"
+    id="paste-text-area"
+    name="paste"
+    rows="30"
+    {{!disabled}}
+    required="true"
+    autofocus="true"
+  >{{ content }}</textarea>
 </form>
 <form id="new" method="get" action="{{ action }}">
 </form>