Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,66 +1,44 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
import
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
@app.get("/")
|
8 |
-
async def root():
|
9 |
-
# Return a help message or documentation as HTML
|
10 |
-
help_html = """
|
11 |
-
<html>
|
12 |
-
<head>
|
13 |
-
<title>Hugging Face App Help</title>
|
14 |
-
</head>
|
15 |
-
<body>
|
16 |
-
<h2>Welcome to the Hugging Face Forwarder App</h2>
|
17 |
-
<p>To use this app, make a GET request to <code>/forward/?param1=YOUR_URL&otherParams=...</code></p>
|
18 |
-
<ul>
|
19 |
-
<li><code>param1</code> is the URL to which the other parameters will be forwarded.</li>
|
20 |
-
<li>Include any other parameters you wish to forward in the query string.</li>
|
21 |
-
</ul>
|
22 |
-
<p>For example: <code>/forward/?param1=https://example.com&data=someData</code></p>
|
23 |
-
</body>
|
24 |
-
</html>
|
25 |
"""
|
26 |
-
return
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
if not forward_url:
|
34 |
-
return JSONResponse(
|
35 |
-
status_code=400,
|
36 |
-
content={"error": "param1 (forward URL) is required. Visit the main page for usage instructions."}
|
37 |
-
)
|
38 |
-
|
39 |
-
if not (forward_url.startswith('http://') or forward_url.startswith('https://')):
|
40 |
-
return JSONResponse(
|
41 |
-
status_code=400,
|
42 |
-
content={"error": "Invalid URL. URL must start with http:// or https://"}
|
43 |
-
)
|
44 |
-
|
45 |
-
if query_params:
|
46 |
-
params_as_str = "&".join([f"{key}={value}" for key, value in query_params.items()])
|
47 |
-
forward_url_with_params = f"{forward_url}?{params_as_str}"
|
48 |
-
else:
|
49 |
-
forward_url_with_params = forward_url
|
50 |
-
|
51 |
-
try:
|
52 |
-
async with httpx.AsyncClient() as client:
|
53 |
-
response = await client.get(forward_url_with_params)
|
54 |
-
response.raise_for_status()
|
55 |
-
except httpx.RequestError as e:
|
56 |
-
return JSONResponse(status_code=500, content={"error": f"Request error: {str(e)}"})
|
57 |
-
except httpx.HTTPStatusError as e:
|
58 |
-
return JSONResponse(status_code=e.response.status_code, content={"error": f"Error response from forwarded URL: {str(e)}"})
|
59 |
-
|
60 |
try:
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
-
|
66 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
from urllib.parse import urlencode, quote_plus
|
4 |
|
5 |
+
def forward_parameters(target_url, extra_params):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
"""
|
7 |
+
Forward parameters to the target URL and return the response.
|
8 |
|
9 |
+
:param target_url: The URL to which the parameters should be forwarded.
|
10 |
+
:param extra_params: A dictionary of additional parameters to forward.
|
11 |
+
:return: The response from the target URL.
|
12 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
try:
|
14 |
+
# Construct the full URL with extra parameters
|
15 |
+
full_url = f"{target_url}?{urlencode(extra_params, quote_via=quote_plus)}"
|
16 |
+
response = requests.get(full_url) # Using GET request for simplicity
|
17 |
+
return response.text
|
18 |
+
except requests.exceptions.RequestException as e:
|
19 |
+
# Return error message in case of request failure
|
20 |
+
return f"An error occurred while forwarding parameters: {str(e)}"
|
21 |
+
|
22 |
+
# Streamlit interface
|
23 |
+
st.title('HTTPS Streamlit Parameters Forwarder')
|
24 |
+
|
25 |
+
# Extracting parameters from the URL
|
26 |
+
params = st.experimental_get_query_params()
|
27 |
+
target_url = params.get('param1', [None])[0] # Extract the target URL
|
28 |
|
29 |
+
if target_url:
|
30 |
+
# Remove 'param1' to forward the remaining parameters
|
31 |
+
if 'param1' in params:
|
32 |
+
del params['param1']
|
33 |
+
response = forward_parameters(target_url, params)
|
34 |
+
st.text_area("Response from Target URL", value=response, height=300)
|
35 |
+
else:
|
36 |
+
st.error("No target URL provided.")
|
37 |
+
st.markdown("""
|
38 |
+
Please append `?param1=URL` to the query string in the address bar,
|
39 |
+
replacing `URL` with your desired target URL.
|
40 |
+
|
41 |
+
Example: `?param1=http://example.com¶m2=value`
|
42 |
+
|
43 |
+
This will forward `param2=value` to `http://example.com`.
|
44 |
+
""")
|