Repository navigation
Expand file tree
/
Copy pathprotocol_handler.py
More file actions
126 lines (103 loc) · 3.87 KB
/
Copy pathprotocol_handler.py
File metadata and controls
126 lines (103 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""
Protocol helpers for scanlink:// URLs and single-instance forwarding.
"""
from urllib.parse import parse_qs, urlparse
try:
import requests
except ImportError:
requests = None
from utils import HTTP_HOST, HTTP_PORT, get_logger
logger = get_logger()
PROTOCOL_SCHEME = "scanlink"
INTERNAL_PROTOCOL_ENDPOINT = f"http://{HTTP_HOST}:{HTTP_PORT}/internal/protocol/scan"
HEALTH_ENDPOINT = f"http://{HTTP_HOST}:{HTTP_PORT}/health"
def parse_protocol_url(url):
"""
Parse a scanlink:// URL and return a structured command object.
"""
if not url:
return None
try:
parsed = urlparse(url)
if parsed.scheme.lower() != PROTOCOL_SCHEME:
logger.warning(f"Invalid protocol scheme: {parsed.scheme}")
return None
action = parsed.netloc.lower() if parsed.netloc else "scan"
params = parse_qs(parsed.query)
doc_id = params.get("docId", params.get("doc_id", [None]))[0]
callback_url = params.get("callback", params.get("callback_url", [None]))[0]
result = {
"action": action,
"doc_id": doc_id,
"callback_url": callback_url,
}
logger.info("Parsed protocol URL: %r", result)
return result
except Exception as exc:
logger.error("Failed to parse protocol URL %r: %s", url, exc)
return None
def is_protocol_url(arg):
"""Check if a command-line argument is a scanlink:// URL."""
if not arg:
return False
return arg.lower().startswith(f"{PROTOCOL_SCHEME}://")
def extract_protocol_url_from_args(args):
"""Extract the first scanlink:// URL from command-line arguments."""
for arg in args:
if is_protocol_url(arg):
return arg
return None
def _json_object(response):
try:
payload = response.json()
except ValueError:
return {}
return payload if isinstance(payload, dict) else {}
def is_existing_instance_running(timeout=1.0):
"""Probe the loopback HTTP health endpoint for an already-running helper."""
if requests is None:
logger.warning("requests is not installed; cannot probe for an existing ScanLink instance")
return False
try:
response = requests.get(HEALTH_ENDPOINT, timeout=timeout)
except requests.RequestException:
return False
# Another program on port 5000 must not make ScanLink exit as a duplicate.
health = _json_object(response)
return (
response.ok
and health.get("status") == "ok"
and "version" in health
and "twain_available" in health
)
def forward_protocol_command(command, timeout=3.0):
"""
Forward a protocol-triggered scan request into the already-running instance.
Returns True when the request was handed off successfully.
"""
if not command or command.get("action") != "scan":
return False
if requests is None:
logger.warning("requests is not installed; cannot forward protocol command to an existing instance")
return False
try:
response = requests.post(INTERNAL_PROTOCOL_ENDPOINT, json=command, timeout=timeout)
payload = _json_object(response)
error = payload.get("error")
if (response.status_code == 202 and payload.get("job_id")) or (
response.status_code == 409
and isinstance(error, dict)
and error.get("code") == "busy"
):
logger.info(
"Forwarded protocol command to existing instance with response %s",
response.status_code,
)
return True
logger.warning(
"Existing instance responded with unexpected status %s while forwarding protocol command",
response.status_code,
)
except requests.RequestException as exc:
logger.debug(f"Could not forward protocol command to existing instance: {exc}")
return False