Spaces:
Sleeping
Sleeping
Add path-based proxy for clean extension
Browse files
app.py
CHANGED
|
@@ -147,6 +147,65 @@ def proxy_jsonl():
|
|
| 147 |
except Exception as e:
|
| 148 |
return json.dumps({"text": f"error: {str(e)}"}) + '\n', 200, {'Content-Type': 'application/jsonl'}
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
@app.route('/data.csv')
|
| 151 |
def serve_csv():
|
| 152 |
entry = {
|
|
|
|
| 147 |
except Exception as e:
|
| 148 |
return json.dumps({"text": f"error: {str(e)}"}) + '\n', 200, {'Content-Type': 'application/jsonl'}
|
| 149 |
|
| 150 |
+
# Path-based proxy CSV: target URL is in the path, filename is data.csv
|
| 151 |
+
# URL format: /p/<base64_encoded_target>/data.csv
|
| 152 |
+
# This way the basename is always data.csv (passes extension check)
|
| 153 |
+
import base64
|
| 154 |
+
@app.route('/p/<target_b64>/data.csv')
|
| 155 |
+
def path_proxy_csv(target_b64):
|
| 156 |
+
entry = {
|
| 157 |
+
'time': time.time(),
|
| 158 |
+
'method': request.method,
|
| 159 |
+
'path': request.full_path,
|
| 160 |
+
'headers': dict(request.headers),
|
| 161 |
+
'remote_addr': request.remote_addr
|
| 162 |
+
}
|
| 163 |
+
LOG.append(entry)
|
| 164 |
+
try:
|
| 165 |
+
target = base64.urlsafe_b64decode(target_b64 + '==').decode()
|
| 166 |
+
except Exception:
|
| 167 |
+
return 'text\nerror: invalid target encoding\n', 200, {'Content-Type': 'text/csv'}
|
| 168 |
+
if request.method == 'HEAD':
|
| 169 |
+
resp = make_response('', 200)
|
| 170 |
+
resp.headers['Content-Type'] = 'text/csv'
|
| 171 |
+
resp.headers['Content-Length'] = '100'
|
| 172 |
+
return resp
|
| 173 |
+
try:
|
| 174 |
+
r = requests.get(target, timeout=10, verify=False)
|
| 175 |
+
lines = r.text.split('\n')[:100]
|
| 176 |
+
csv_data = 'text\n' + '\n'.join(f'"{line}"' for line in lines if line.strip()) + '\n'
|
| 177 |
+
return csv_data, 200, {'Content-Type': 'text/csv'}
|
| 178 |
+
except Exception as e:
|
| 179 |
+
return f'text\nerror: {str(e)}\n', 200, {'Content-Type': 'text/csv'}
|
| 180 |
+
|
| 181 |
+
# Path-based proxy JSONL
|
| 182 |
+
@app.route('/p/<target_b64>/data.jsonl')
|
| 183 |
+
def path_proxy_jsonl(target_b64):
|
| 184 |
+
entry = {
|
| 185 |
+
'time': time.time(),
|
| 186 |
+
'method': request.method,
|
| 187 |
+
'path': request.full_path,
|
| 188 |
+
'headers': dict(request.headers),
|
| 189 |
+
'remote_addr': request.remote_addr
|
| 190 |
+
}
|
| 191 |
+
LOG.append(entry)
|
| 192 |
+
try:
|
| 193 |
+
target = base64.urlsafe_b64decode(target_b64 + '==').decode()
|
| 194 |
+
except Exception:
|
| 195 |
+
return '{"text": "error: invalid target encoding"}\n', 200, {'Content-Type': 'application/jsonl'}
|
| 196 |
+
if request.method == 'HEAD':
|
| 197 |
+
resp = make_response('', 200)
|
| 198 |
+
resp.headers['Content-Type'] = 'application/jsonl'
|
| 199 |
+
resp.headers['Content-Length'] = '100'
|
| 200 |
+
return resp
|
| 201 |
+
try:
|
| 202 |
+
r = requests.get(target, timeout=10, verify=False)
|
| 203 |
+
lines = r.text.split('\n')[:100]
|
| 204 |
+
jsonl_data = '\n'.join(json.dumps({"text": line}) for line in lines if line.strip()) + '\n'
|
| 205 |
+
return jsonl_data, 200, {'Content-Type': 'application/jsonl'}
|
| 206 |
+
except Exception as e:
|
| 207 |
+
return json.dumps({"text": f"error: {str(e)}"}) + '\n', 200, {'Content-Type': 'application/jsonl'}
|
| 208 |
+
|
| 209 |
@app.route('/data.csv')
|
| 210 |
def serve_csv():
|
| 211 |
entry = {
|