34 """Send JSON response with appropriate headers."""
35 json_data = json.dumps(data, indent=2)
36 self.send_response(status_code)
37 self.send_header(
"Content-Type",
"application/json")
38 self.send_header(
"Content-Length", str(len(json_data)))
39 self.send_header(
"Access-Control-Allow-Origin",
"*")
41 self.wfile.write(json_data.encode())
52 """Handle GET requests."""
55 html =
"""<!DOCTYPE html>
57<head><title>FastLED Test Server</title></head>
59<h1>FastLED HTTP Test Server</h1>
60<p>This server mimics httpbin.org endpoints for testing FastLED fetch API.</p>
61<h2>Available Endpoints:</h2>
63<li><a href="/json">/json</a> - Sample JSON slideshow data</li>
64<li><a href="/get">/get</a> - Echo request information</li>
65<li><a href="/ping">/ping</a> - Health check (returns "pong")</li>
71 elif self.
path ==
"/json":
75 "author":
"FastLED Community",
76 "title":
"FastLED Tutorial",
79 "title":
"Introduction to FastLED",
83 "title":
"LED Basics",
87 "title":
"HTTP Fetch API",
93 console.print(
"[green]→ /json - Returning slideshow data[/green]")
96 elif self.
path.startswith(
"/get"):
98 query_params: dict[str, str] = {}
100 query_string = self.
path.split(
"?", 1)[1]
101 for param
in query_string.split(
"&"):
103 key, value = param.split(
"=", 1)
104 query_params[key] = value
106 headers: dict[str, str] = {}
107 for header_name, header_value
in self.headers.items():
108 headers[header_name] = header_value
110 data: dict[str, Any] = {
111 "args": query_params,
113 "origin": self.client_address[0],
114 "url": f
"http://{self.headers.get('Host', 'localhost')}{self.path}",
116 console.print(
"[green]→ /get - Returning request info[/green]")
119 elif self.
path ==
"/ping":
121 console.print(
"[green]→ /ping - Health check[/green]")
126 console.print(f
"[red]→ {self.path} - Not Found[/red]")
128 {
"error":
"Not Found",
"path": self.
path}, status_code=404
132 """Handle POST requests."""
133 content_length = int(self.headers.get(
"Content-Length", 0))
134 post_data = self.rfile.read(content_length).decode(
"utf-8")
136 if self.
path ==
"/post":
138 headers: dict[str, str] = {}
139 for header_name, header_value
in self.headers.items():
140 headers[header_name] = header_value
142 json_data: Any =
None
145 json_data = json.loads(post_data)
148 except json.JSONDecodeError:
151 data: dict[str, Any] = {
156 "origin": self.client_address[0],
157 "url": f
"http://{self.headers.get('Host', 'localhost')}{self.path}",
159 console.print(
"[green]→ POST /post - Returning echo data[/green]")
162 console.print(f
"[red]→ POST {self.path} - Not Found[/red]")
164 {
"error":
"Not Found",
"path": self.
path}, status_code=404