Skip to content

Commit 124f268

Browse files
committed
feat: new config, HTTPBasicAuth.cache_timeout
1 parent 5007e87 commit 124f268

3 files changed

Lines changed: 91 additions & 66 deletions

File tree

asgi_webdav/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from asgi_webdav.cache import DAVCacheType
99
from asgi_webdav.constants import (
1010
DEFAULT_FILENAME_CONTENT_TYPE_MAPPING,
11+
DEFAULT_HTTP_BASIC_AUTH_CACHE_TIMEOUT,
1112
DEFAULT_SUFFIX_CONTENT_TYPE_MAPPING,
1213
AppEntryParameters,
1314
DAVCompressLevel,
@@ -27,6 +28,7 @@ class User(BaseModel):
2728
class HTTPBasicAuth(BaseModel):
2829
# enable: bool = True
2930
cache_type: DAVCacheType = DAVCacheType.MEMORY
31+
cache_timeout: int = DEFAULT_HTTP_BASIC_AUTH_CACHE_TIMEOUT # x second
3032

3133

3234
class HTTPDigestAuth(BaseModel):

asgi_webdav/constants.py

Lines changed: 79 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,9 @@
99

1010
import arrow
1111

12-
ASGIHeaders = Iterable[tuple[bytes, bytes]]
13-
12+
# Common ---
1413

15-
CLIENT_USER_AGENT_RE_FIREFOX = r"^Mozilla/5.0.+Gecko/.+Firefox/"
16-
CLIENT_USER_AGENT_RE_SAFARI = r"^Mozilla/5.0.+Version/.+Safari/"
17-
CLIENT_USER_AGENT_RE_CHROME = r"^Mozilla/5.0.+Chrome/.+Safari/"
18-
CLIENT_USER_AGENT_RE_MACOS_FINDER = r"^WebDAVFS/"
19-
CLIENT_USER_AGENT_RE_WINDOWS_EXPLORER = r"^Microsoft-WebDAV-MiniRedir/"
14+
ASGIHeaders = Iterable[tuple[bytes, bytes]]
2015

2116

2217
class DAVUpperEnumAbc(Enum):
@@ -28,6 +23,8 @@ def _missing_(cls, value: str): # type: ignore
2823
return cls[value.upper()]
2924

3025

26+
# WebDAV protocol ---
27+
3128
DAV_METHODS = {
3229
# rfc4918:9.1
3330
"PROPFIND",
@@ -283,6 +280,74 @@ def __repr__(self):
283280
return f"DAVLockInfo({s})"
284281

285282

283+
DAV_PROPERTY_BASIC_KEYS = {
284+
# Identify
285+
"displayname",
286+
"getetag",
287+
# Date Time
288+
"creationdate",
289+
"getlastmodified",
290+
# File Properties
291+
"getcontenttype",
292+
"getcontentlength",
293+
# 'getcontentlanguage',
294+
# is dir
295+
"resourcetype",
296+
"encoding",
297+
# 'supportedlock', 'lockdiscovery'
298+
# 'executable'
299+
}
300+
301+
DAVPropertyIdentity = NewType(
302+
# (namespace, key)
303+
"DAVPropertyIdentity",
304+
tuple[str, str],
305+
)
306+
DAVPropertyPatches = NewType(
307+
"DAVPropertyPatches",
308+
list[
309+
# (DAVPropertyIdentity(sn_key), value, set<True>/remove<False>)
310+
tuple[DAVPropertyIdentity, str, bool]
311+
],
312+
)
313+
314+
# HTTP protocol ---
315+
316+
RESPONSE_DATA_BLOCK_SIZE = 64 * 1024
317+
318+
319+
class DAVAcceptEncoding:
320+
# https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Encoding
321+
# https://caniuse.com/?search=gzip
322+
# identity
323+
gzip: bool = False
324+
br: bool = False
325+
326+
def __repr__(self):
327+
return f"gzip:{self.gzip}, br:{self.br}"
328+
329+
330+
DEFAULT_COMPRESSION_CONTENT_MINIMUM_LENGTH = 1000 # bytes
331+
DEFAULT_COMPRESSION_CONTENT_TYPE_RULE = r"^application/xml$|^text/"
332+
333+
334+
class DAVCompressLevel(Enum):
335+
"""
336+
http://mattmahoney.net/dc/text.html
337+
https://quixdb.github.io/squash-benchmark/
338+
https://sites.google.com/site/powturbo/home/benchmark
339+
"""
340+
341+
FAST = "fast"
342+
RECOMMEND = "recommend"
343+
BEST = "best"
344+
345+
346+
# Authentication ---
347+
348+
DEFAULT_HTTP_BASIC_AUTH_CACHE_TIMEOUT = 60 * 60 # 1 hour
349+
350+
286351
@dataclass
287352
class DAVUser:
288353
username: str
@@ -333,36 +398,13 @@ def __str__(self):
333398
)
334399

335400

336-
DAV_PROPERTY_BASIC_KEYS = {
337-
# Identify
338-
"displayname",
339-
"getetag",
340-
# Date Time
341-
"creationdate",
342-
"getlastmodified",
343-
# File Properties
344-
"getcontenttype",
345-
"getcontentlength",
346-
# 'getcontentlanguage',
347-
# is dir
348-
"resourcetype",
349-
"encoding",
350-
# 'supportedlock', 'lockdiscovery'
351-
# 'executable'
352-
}
401+
# Extra ---
353402

354-
DAVPropertyIdentity = NewType(
355-
# (namespace, key)
356-
"DAVPropertyIdentity",
357-
tuple[str, str],
358-
)
359-
DAVPropertyPatches = NewType(
360-
"DAVPropertyPatches",
361-
list[
362-
# (DAVPropertyIdentity(sn_key), value, set<True>/remove<False>)
363-
tuple[DAVPropertyIdentity, str, bool]
364-
],
365-
)
403+
CLIENT_USER_AGENT_RE_FIREFOX = r"^Mozilla/5.0.+Gecko/.+Firefox/"
404+
CLIENT_USER_AGENT_RE_SAFARI = r"^Mozilla/5.0.+Version/.+Safari/"
405+
CLIENT_USER_AGENT_RE_CHROME = r"^Mozilla/5.0.+Chrome/.+Safari/"
406+
CLIENT_USER_AGENT_RE_MACOS_FINDER = r"^WebDAVFS/"
407+
CLIENT_USER_AGENT_RE_WINDOWS_EXPLORER = r"^Microsoft-WebDAV-MiniRedir/"
366408

367409
DEFAULT_FILENAME_CONTENT_TYPE_MAPPING = {
368410
"README": "text/plain",
@@ -410,34 +452,8 @@ def __str__(self):
410452
CLIENT_USER_AGENT_RE_WINDOWS_EXPLORER: HIDE_FILE_IN_DIR_RULE_MACOS,
411453
}
412454

413-
RESPONSE_DATA_BLOCK_SIZE = 64 * 1024
414-
415455

416-
class DAVAcceptEncoding:
417-
# https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Encoding
418-
# https://caniuse.com/?search=gzip
419-
# identity
420-
gzip: bool = False
421-
br: bool = False
422-
423-
def __repr__(self):
424-
return f"gzip:{self.gzip}, br:{self.br}"
425-
426-
427-
DEFAULT_COMPRESSION_CONTENT_MINIMUM_LENGTH = 1000 # bytes
428-
DEFAULT_COMPRESSION_CONTENT_TYPE_RULE = r"^application/xml$|^text/"
429-
430-
431-
class DAVCompressLevel(Enum):
432-
"""
433-
http://mattmahoney.net/dc/text.html
434-
https://quixdb.github.io/squash-benchmark/
435-
https://sites.google.com/site/powturbo/home/benchmark
436-
"""
437-
438-
FAST = "fast"
439-
RECOMMEND = "recommend"
440-
BEST = "best"
456+
# Development ---
441457

442458

443459
class DevMode(Enum):

docs/reference/config-file.en.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,22 @@ Example
160160
- Introduced in 1.5.0
161161
- Last updated in 1.5.0
162162

163-
| Key | Value Type | Default Value |
164-
| ---------- | ---------- | ------------- |
165-
| cache_type | str | `memory` |
163+
| Key | Value Type | Default Value |
164+
| ------------- | ---------- | ------------- |
165+
| cache_type | str | `memory` |
166+
| cache_timeout | int | 360 |
166167

167168
#### `cache_type` allowed value
168169

169170
- `bypass`
170171
- `memory`
171172

173+
#### `cache_timeout`
174+
175+
- Unit: second
176+
- Supported `cache_type`:
177+
- not yet
178+
172179
### `HTTPDigestAuth` Object
173180

174181
- Introduced in 0.7.0

0 commit comments

Comments
 (0)