-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathserver.py
More file actions
878 lines (797 loc) · 33.2 KB
/
server.py
File metadata and controls
878 lines (797 loc) · 33.2 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
#!/usr/bin/env python3
"""
Mock Websites Server for AI Agent Evaluation
Serves mock websites for evaluation with event tracking capabilities.
Usage:
python server.py
The server will:
1. Start on fixed port 16605
2. Serve the mocked websites
3. Collect tracking events from agent interactions
4. Export events via /api/events endpoint
"""
import html
import http.server
import json
import os
import socketserver
import threading
from copy import deepcopy
from datetime import datetime
from urllib.parse import parse_qs, urlparse
# Configuration
PORT = 16605
EVAL_DIR = os.path.dirname(os.path.abspath(__file__))
# In-memory event storage
events_store = {"events": [], "sessions": {}, "sites": {}}
events_store_lock = threading.Lock()
SITE_NAME_TO_BUCKET = {
"globalbusinessreview.com": "gbr",
"techforum.com": "techforum",
"cloudstack.com": "cloudstack",
"dataflow.io": "dataflow",
"finviz": "finviz",
"bluebook.life": "bluebook",
"northstaroutfitters.com": "northstar",
}
def _normalize_site_bucket(raw_value):
"""Normalize a site/path/domain into a mock-site bucket key."""
if not raw_value or not isinstance(raw_value, str):
return None
parsed = urlparse(raw_value)
candidate = parsed.path if parsed.scheme or parsed.netloc else raw_value
candidate = candidate.strip()
if candidate.startswith("/"):
segments = [segment for segment in candidate.split("/") if segment]
if segments:
return segments[0]
normalized = candidate.strip().lower()
return SITE_NAME_TO_BUCKET.get(normalized, normalized or None)
def _get_event_site_bucket(event):
"""Infer the mock-site bucket for one tracked event."""
for key in ("page", "url", "site"):
bucket = _normalize_site_bucket(event.get(key))
if bucket:
return bucket
return "unknown"
def _get_or_create_site_store(site_bucket):
"""Return the per-site event store, creating it if needed."""
return events_store["sites"].setdefault(site_bucket, {"events": [], "sessions": {}})
def _snapshot_events(site_bucket=None):
"""Return a JSON-safe snapshot of tracked events."""
with events_store_lock:
if site_bucket:
site_store = events_store["sites"].get(
site_bucket, {"events": [], "sessions": {}}
)
return {
"site": site_bucket,
"events": deepcopy(site_store["events"]),
"sessions": deepcopy(site_store["sessions"]),
}
return deepcopy(events_store)
def _clear_events(site_bucket=None):
"""Clear tracked events globally or for a specific site."""
with events_store_lock:
if site_bucket:
events_store["sites"][site_bucket] = {"events": [], "sessions": {}}
return
events_store["events"] = []
events_store["sessions"] = {}
events_store["sites"] = {}
# URL mappings
URL_MAPPINGS = {
"/": ("/gbr/index.html", "text/html"),
"/gbr/": ("/gbr/index.html", "text/html"),
"/gbr/index.html": ("/gbr/index.html", "text/html"),
"/gbr/world.html": ("/gbr/world.html", "text/html"),
"/gbr/business.html": ("/gbr/business.html", "text/html"),
"/gbr/markets.html": ("/gbr/markets.html", "text/html"),
"/gbr/tech.html": ("/gbr/tech.html", "text/html"),
"/gbr/politics.html": ("/gbr/politics.html", "text/html"),
"/gbr/opinion.html": ("/gbr/opinion.html", "text/html"),
"/techforum/": ("/techforum/index.html", "text/html"),
"/techforum/index.html": ("/techforum/index.html", "text/html"),
"/techforum/questions.html": ("/techforum/questions.html", "text/html"),
"/cloudstack/": ("/cloudstack/index.html", "text/html"),
"/cloudstack/index.html": ("/cloudstack/index.html", "text/html"),
"/cloudstack/rds.html": ("/cloudstack/rds.html", "text/html"),
"/cloudstack/oss.html": ("/cloudstack/oss.html", "text/html"),
"/cloudstack/vpc.html": ("/cloudstack/vpc.html", "text/html"),
"/cloudstack/slb.html": ("/cloudstack/slb.html", "text/html"),
"/cloudstack/cms.html": ("/cloudstack/cms.html", "text/html"),
"/cloudstack/actiontrail.html": ("/cloudstack/actiontrail.html", "text/html"),
"/cloudstack/config.html": ("/cloudstack/config.html", "text/html"),
"/cloudstack/security.html": ("/cloudstack/security.html", "text/html"),
"/cloudstack/billing.html": ("/cloudstack/billing.html", "text/html"),
"/cloudstack/budget.html": ("/cloudstack/budget.html", "text/html"),
"/cloudstack/placeholder.html": ("/cloudstack/placeholder.html", "text/html"),
"/gbr/articles/business-article1.html": (
"/gbr/articles/business-article1.html",
"text/html",
),
"/gbr/articles/business-article2.html": (
"/gbr/articles/business-article2.html",
"text/html",
),
"/gbr/articles/business-article3.html": (
"/gbr/articles/business-article3.html",
"text/html",
),
"/gbr/articles/business-article4.html": (
"/gbr/articles/business-article4.html",
"text/html",
),
"/gbr/articles/world-article1.html": (
"/gbr/articles/world-article1.html",
"text/html",
),
"/gbr/articles/world-article2.html": (
"/gbr/articles/world-article2.html",
"text/html",
),
"/gbr/articles/world-article3.html": (
"/gbr/articles/world-article3.html",
"text/html",
),
"/gbr/articles/world-article4.html": (
"/gbr/articles/world-article4.html",
"text/html",
),
"/gbr/articles/markets-article1.html": (
"/gbr/articles/markets-article1.html",
"text/html",
),
"/gbr/articles/markets-article2.html": (
"/gbr/articles/markets-article2.html",
"text/html",
),
"/gbr/articles/markets-article3.html": (
"/gbr/articles/markets-article3.html",
"text/html",
),
"/gbr/articles/markets-article4.html": (
"/gbr/articles/markets-article4.html",
"text/html",
),
"/gbr/articles/tech-article1.html": (
"/gbr/articles/tech-article1.html",
"text/html",
),
"/gbr/articles/tech-article2.html": (
"/gbr/articles/tech-article2.html",
"text/html",
),
"/gbr/articles/tech-article3.html": (
"/gbr/articles/tech-article3.html",
"text/html",
),
"/gbr/articles/tech-article4.html": (
"/gbr/articles/tech-article4.html",
"text/html",
),
"/gbr/articles/politics-article1.html": (
"/gbr/articles/politics-article1.html",
"text/html",
),
"/gbr/articles/politics-article2.html": (
"/gbr/articles/politics-article2.html",
"text/html",
),
"/gbr/articles/politics-article3.html": (
"/gbr/articles/politics-article3.html",
"text/html",
),
"/gbr/articles/politics-article4.html": (
"/gbr/articles/politics-article4.html",
"text/html",
),
"/gbr/articles/opinion-article1.html": (
"/gbr/articles/opinion-article1.html",
"text/html",
),
"/gbr/articles/opinion-article2.html": (
"/gbr/articles/opinion-article2.html",
"text/html",
),
"/gbr/articles/opinion-article3.html": (
"/gbr/articles/opinion-article3.html",
"text/html",
),
"/gbr/articles/opinion-article4.html": (
"/gbr/articles/opinion-article4.html",
"text/html",
),
"/gbr/articles/home-article1.html": (
"/gbr/articles/home-article1.html",
"text/html",
),
"/gbr/articles/home-article2.html": (
"/gbr/articles/home-article2.html",
"text/html",
),
"/gbr/articles/home-article3.html": (
"/gbr/articles/home-article3.html",
"text/html",
),
"/gbr/articles/home-article4.html": (
"/gbr/articles/home-article4.html",
"text/html",
),
"/gbr/articles/home-article5.html": (
"/gbr/articles/home-article5.html",
"text/html",
),
"/dataflow/": ("/dataflow/index.html", "text/html"),
"/dataflow/index.html": ("/dataflow/index.html", "text/html"),
"/finviz/": ("/finviz/index.html", "text/html"),
"/finviz/index.html": ("/finviz/index.html", "text/html"),
"/bluebook/": ("/bluebook/index.html", "text/html"),
"/bluebook/index.html": ("/bluebook/index.html", "text/html"),
"/northstar/": ("/northstar/index.html", "text/html"),
"/northstar/index.html": ("/northstar/index.html", "text/html"),
}
CSS_MIMETYPE = "text/css"
JS_MIMETYPE = "application/javascript"
SVG_MIMETYPE = "image/svg+xml"
PNG_MIMETYPE = "image/png"
JPG_MIMETYPE = "image/jpeg"
GIF_MIMETYPE = "image/gif"
class MockWebsiteHandler(http.server.SimpleHTTPRequestHandler):
"""Custom HTTP handler for mock websites with tracking API"""
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=EVAL_DIR, **kwargs)
def do_GET(self):
"""Handle GET requests"""
parsed_path = urlparse(self.path)
path = parsed_path.path
query_params = parse_qs(parsed_path.query)
site_bucket = _normalize_site_bucket(query_params.get("site", [None])[0])
# API endpoints
if path == "/api/events":
self.send_json_response(_snapshot_events(site_bucket))
return
elif path == "/api/events/clear":
_clear_events(site_bucket)
self.send_json_response(
{
"status": "cleared",
"site": site_bucket,
"message": (
f"Events cleared for site '{site_bucket}'"
if site_bucket
else "All events cleared"
),
}
)
return
elif path == "/api/sites":
sites = {
"sites": [
{
"name": "globalbusinessreview.com",
"difficulty": "easy",
"url": "/gbr/",
"description": "News website - test navigation and information gathering",
},
{
"name": "techforum.com",
"difficulty": "medium",
"url": "/techforum/",
"description": "Q&A forum - test interactions (like, collect, comment)",
},
{
"name": "cloudstack.com",
"difficulty": "hard",
"url": "/cloudstack/",
"description": "Cloud console - test complex UI with spam popups",
},
{
"name": "dataflow.io",
"difficulty": "medium",
"url": "/dataflow/",
"description": "Analytics dashboard - test visual understanding (spatial, charts, state)",
},
{
"name": "finviz.com",
"difficulty": "hard",
"url": "/finviz/",
"description": "Stock screener - test complex filters, selects, and data tables",
},
{
"name": "bluebook.life",
"difficulty": "hard",
"url": "/bluebook/",
"description": "Xiaohongshu-like feed - test search, note modal, comment actions, and dense visual layouts",
},
{
"name": "northstaroutfitters.com",
"difficulty": "hard",
"url": "/northstar/",
"description": "Apparel product page - test geometry-first scrolling, sticky UI, and drawer-scoped scrolling",
},
]
}
self.send_json_response(sites)
return
elif path == "/api/help":
help_text = {
"endpoints": {
"GET /api/events": "Get tracked events (optional ?site=<bucket>)",
"GET /api/events/clear": "Clear tracked events (optional ?site=<bucket>)",
"GET /api/sites": "List available mock sites",
"GET /api/help": "Show this help",
"POST /api/track": "Submit tracking event (from browser)",
},
"sites": {
"/gbr/": "Global Business Review mock (easy)",
"/techforum/": "TechForum Q&A mock (medium)",
"/cloudstack/": "CloudStack console mock (hard)",
"/dataflow/": "DataFlow analytics dashboard mock (medium)",
"/finviz/": "Finviz stock screener mock (hard)",
"/bluebook/": "BlueBook lifestyle feed mock (hard)",
"/northstar/": "Northstar Outfitters product page mock (hard)",
},
}
self.send_json_response(help_text)
return
# Search results page
if path == "/gbr/search.html":
self.handle_search(parsed_path)
return
# Redirect directory access without trailing slash to with slash
# This ensures relative paths in HTML work correctly
if not path.endswith("/"):
fs_path = os.path.join(EVAL_DIR, path.lstrip("/"))
if os.path.isdir(fs_path):
# Send 301 redirect to add trailing slash
self.send_response(301)
self.send_header("Location", path + "/")
self.end_headers()
return
# Static file serving
# Check URL mappings
# Debug: print path and mapping
# print(f"DEBUG: path={path}, in URL_MAPPINGS={path in URL_MAPPINGS}")
if path in URL_MAPPINGS:
file_path, content_type = URL_MAPPINGS[path]
# print(f"DEBUG: mapping {path} -> {file_path}")
self.send_file(file_path, content_type)
return
# Check for CSS files
if path.startswith("/css/") and path.endswith(".css"):
self.send_file(path, CSS_MIMETYPE)
return
# Check for JS files (including site-specific JS folders)
if path.startswith("/js/") and path.endswith(".js"):
self.send_file(path, JS_MIMETYPE)
return
# Check for site-specific JS files (e.g., /techforum/js/, /gbr/js/, /cloudstack/js/)
for site in [
"techforum",
"gbr",
"cloudstack",
"dataflow",
"finviz",
"bluebook",
"northstar",
]:
if path.startswith(f"/{site}/js/") and path.endswith(".js"):
self.send_file(path, JS_MIMETYPE)
return
# Check for site-specific CSS files
for site in [
"techforum",
"gbr",
"cloudstack",
"dataflow",
"finviz",
"bluebook",
"northstar",
]:
if path.startswith(f"/{site}/css/") and path.endswith(".css"):
self.send_file(path, CSS_MIMETYPE)
return
# Check for image files
if path.endswith(".svg"):
self.send_file(path, SVG_MIMETYPE)
return
elif path.endswith(".png"):
self.send_file(path, PNG_MIMETYPE)
return
elif path.endswith(".jpg") or path.endswith(".jpeg"):
self.send_file(path, JPG_MIMETYPE)
return
elif path.endswith(".gif"):
self.send_file(path, GIF_MIMETYPE)
return
# Default: try to serve as-is
self.send_file(path, "text/html")
def do_POST(self):
"""Handle POST requests"""
parsed_path = urlparse(self.path)
path = parsed_path.path
if path == "/api/track":
content_length = int(self.headers.get("Content-Length", 0))
post_data = self.rfile.read(content_length)
try:
event = json.loads(post_data.decode("utf-8"))
event["received_at"] = datetime.now().isoformat()
site_bucket = _get_event_site_bucket(event)
session_id = event.get("sessionId", "unknown")
with events_store_lock:
events_store["events"].append(event)
if session_id not in events_store["sessions"]:
events_store["sessions"][session_id] = {
"sessionId": session_id,
"site": event.get("site", "unknown"),
"site_bucket": site_bucket,
"difficulty": event.get("difficulty", "unknown"),
"start_time": event.get("timestamp"),
"events_count": 0,
}
events_store["sessions"][session_id]["events_count"] += 1
events_store["sessions"][session_id]["last_activity"] = event.get(
"timestamp"
)
site_store = _get_or_create_site_store(site_bucket)
site_store["events"].append(event)
if session_id not in site_store["sessions"]:
site_store["sessions"][session_id] = {
"sessionId": session_id,
"site": event.get("site", "unknown"),
"site_bucket": site_bucket,
"difficulty": event.get("difficulty", "unknown"),
"start_time": event.get("timestamp"),
"events_count": 0,
}
site_store["sessions"][session_id]["events_count"] += 1
site_store["sessions"][session_id]["last_activity"] = event.get(
"timestamp"
)
self.send_json_response({"status": "ok", "message": "Event tracked"})
except Exception as e:
self.send_error_response(400, str(e))
return
self.send_error_response(404, "Not Found")
def send_file(self, file_path, content_type):
"""Send a file with appropriate headers"""
full_path = os.path.join(EVAL_DIR, file_path.lstrip("/"))
if not os.path.exists(full_path):
self.send_error_response(404, f"File not found: {file_path}")
return
# If path is a directory, try to serve index.html
if os.path.isdir(full_path):
index_path = os.path.join(full_path, "index.html")
if os.path.exists(index_path):
full_path = index_path
else:
self.send_error_response(404, f"Directory index not found: {file_path}")
return
try:
with open(full_path, "rb") as f:
content = f.read()
self.send_response(200)
self.send_header("Content-Type", content_type)
self.send_header("Content-Length", len(content))
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
self.wfile.write(content)
except Exception as e:
self.send_error_response(500, str(e))
def handle_search(self, parsed_path):
"""Handle search requests and display results"""
# Parse query parameters
query_params = parse_qs(parsed_path.query)
search_query = query_params.get("q", [""])[0].strip().lower()
# Load article manifest
manifest_path = os.path.join(EVAL_DIR, "gbr", "articles", "manifest.json")
try:
with open(manifest_path, "r") as f:
articles = json.load(f)
except Exception as e:
self.send_error_response(500, f"Failed to load articles: {str(e)}")
return
# Filter articles based on search query
matching_articles = []
if search_query:
for article in articles:
# Search in title (case-insensitive)
title = article.get("title", "").lower()
category = article.get("category", "").lower()
# Check if query appears in title or category
if search_query in title or search_query in category:
matching_articles.append(article)
# Generate HTML for search results
html = self.generate_search_results_html(search_query, matching_articles)
# Send response
self.send_response(200)
self.send_header("Content-Type", "text/html; charset=utf-8")
self.send_header("Content-Length", len(html))
self.end_headers()
self.wfile.write(html)
def generate_search_results_html(self, search_query, articles):
"""Generate HTML for search results page"""
# Escape HTML in search query
escaped_query = html.escape(search_query) if search_query else ""
# Generate results content
if not articles:
results_content = """
<div class="no-results">
<h3>No articles found</h3>
<p>Try different keywords or browse our sections above.</p>
</div>
"""
else:
results_items = []
for article in articles:
title = html.escape(article.get("title", "Untitled"))
url = html.escape(article.get("url", "#"))
category = html.escape(article.get("category", "general"))
# Format category for display
category_display = category.capitalize()
if category == "home":
category_display = "Featured"
elif category == "tech":
category_display = "Technology"
item_html = f"""
<div class="search-result-item">
<div class="result-category">{category_display}</div>
<h3 class="result-title"><a href="{url}">{title}</a></h3>
<div class="result-meta">Click to read full article</div>
</div>
"""
results_items.append(item_html)
results_content = (
f'<div class="results-grid">{"".join(results_items)}</div>'
)
# Format plural
plural = "s" if len(articles) != 1 else ""
# Build the complete HTML page
html_content = f"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Search Results - Global Business Review</title>
<link rel="stylesheet" href="/css/gbr.css">
<style>
.search-results {{
max-width: 1000px;
margin: 0 auto;
padding: 40px 20px;
}}
.search-header {{
margin-bottom: 30px;
border-bottom: 1px solid #ddd;
padding-bottom: 20px;
}}
.search-query {{
font-size: 24px;
font-weight: 700;
color: #333;
margin-bottom: 10px;
}}
.results-count {{
font-size: 16px;
color: #777;
font-family: 'Arial', sans-serif;
}}
.no-results {{
text-align: center;
padding: 40px;
background: #f9f9f9;
border-radius: 4px;
}}
.no-results h3 {{
font-size: 20px;
margin-bottom: 10px;
color: #333;
}}
.no-results p {{
color: #777;
line-height: 1.6;
}}
.results-grid {{
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
gap: 20px;
margin-top: 20px;
}}
.search-result-item {{
background: #fff;
border: 1px solid #ddd;
border-radius: 4px;
padding: 20px;
transition: box-shadow 0.2s;
}}
.search-result-item:hover {{
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}}
.result-title {{
font-size: 18px;
font-weight: 600;
margin-bottom: 10px;
line-height: 1.3;
}}
.result-title a {{
color: #000;
text-decoration: none;
}}
.result-title a:hover {{
color: #555;
}}
.result-meta {{
font-size: 13px;
color: #777;
margin-bottom: 10px;
font-family: 'Arial', sans-serif;
}}
.result-category {{
display: inline-block;
background: #f0f0f0;
color: #333;
padding: 2px 8px;
border-radius: 12px;
font-size: 12px;
margin-right: 8px;
}}
.back-to-home {{
display: inline-block;
margin-top: 30px;
color: #0066cc;
text-decoration: none;
font-weight: 600;
font-family: 'Arial', sans-serif;
}}
.back-to-home:hover {{
text-decoration: underline;
}}
</style>
</head>
<body>
<header class="gbr-header">
<div class="header-container">
<div class="logo">
<a href="/gbr/">
<img src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 200 40'%3E%3Crect fill='%23000' width='200' height='40'/%3E%3Ctext fill='white' font-family='Georgia, serif' font-size='24' x='10' y='28'%3EGlobal Business Review%3C/text%3E%3C/svg%3E" alt="GBR Logo" class="gbr-logo">
</a>
</div>
<nav class="main-nav">
<ul>
<li><a href="/gbr/">Home</a></li>
<li><a href="/gbr/world.html">World</a></li>
<li><a href="/gbr/business.html">Business</a></li>
<li><a href="/gbr/markets.html">Markets</a></li>
<li><a href="/gbr/tech.html">Tech</a></li>
<li><a href="/gbr/politics.html">Politics</a></li>
<li><a href="/gbr/opinion.html">Opinion</a></li>
</ul>
</nav>
<div class="header-actions">
<button class="search-btn" id="search-toggle">🔍</button>
<button class="subscribe-btn">Subscribe</button>
<button class="sign-in-btn">Sign In</button>
</div>
</div>
<div class="search-bar" id="search-bar" style="display:flex;">
<input type="text" placeholder="Search GBR..." id="search-input" value="{escaped_query}">
<button id="search-submit" type="button">Search</button>
</div>
</header>
<main class="gbr-main">
<div class="search-results">
<div class="search-header">
<h1 class="search-query">Search Results for "{escaped_query}"</h1>
<div class="results-count">{len(articles)} article{plural} found</div>
</div>
{results_content}
<a href="/gbr/" class="back-to-home">← Back to Home</a>
</div>
</main>
<footer class="gbr-footer">
<div class="footer-container">
<div class="footer-links">
<div class="footer-column">
<h4>Sections</h4>
<ul>
<li><a href="/gbr/world.html">World</a></li>
<li><a href="/gbr/business.html">Business</a></li>
<li><a href="/gbr/markets.html">Markets</a></li>
<li><a href="/gbr/tech.html">Technology</a></li>
</ul>
</div>
<div class="footer-column">
<h4>More</h4>
<ul>
<li><a href="/gbr/podcasts.html">Podcasts</a></li>
<li><a href="/gbr/videos.html">Videos</a></li>
<li><a href="/gbr/newsletters.html">Newsletters</a></li>
</ul>
</div>
<div class="footer-column">
<h4>Support</h4>
<ul>
<li><a href="/gbr/contact.html">Contact Us</a></li>
<li><a href="/gbr/subscribers.html">Subscribers</a></li>
<li><a href="/gbr/help.html">Help Center</a></li>
</ul>
</div>
</div>
<div class="footer-bottom">
<p>© 2025 Global Business Review Inc. All Rights Reserved.</p>
</div>
</div>
</footer>
<script src="/js/tracker.js"></script>
<script src="/js/gbr.js"></script>
</body>
</html>"""
return html_content.encode("utf-8")
def send_json_response(self, data):
"""Send JSON response"""
content = json.dumps(data, indent=2, ensure_ascii=False).encode("utf-8")
self.send_response(200)
self.send_header("Content-Type", "application/json; charset=utf-8")
self.send_header("Content-Length", len(content))
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
self.send_header("Access-Control-Allow-Headers", "Content-Type")
self.end_headers()
self.wfile.write(content)
def send_error_response(self, code, message):
"""Send error response"""
content = json.dumps({"error": message}, indent=2).encode("utf-8")
self.send_response(code)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", len(content))
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
self.wfile.write(content)
def do_OPTIONS(self):
"""Handle CORS preflight requests"""
self.send_response(200)
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
self.send_header("Access-Control-Allow-Headers", "Content-Type")
self.end_headers()
def log_message(self, format, *args):
"""Custom log format"""
print(f"[{datetime.now().strftime('%H:%M:%S')}] {args[0]}")
def print_startup_info(port):
"""Print startup information"""
print("\n" + "=" * 60)
print("Mock Websites Server for AI Agent Evaluation")
print("=" * 60)
print(f"\nServer started at: http://localhost:{port}")
print("\nAvailable Sites:")
print(f" - GBR (Easy): http://localhost:{port}/gbr/")
print(f" - TechForum (Medium): http://localhost:{port}/techforum/")
print(f" - CloudStack (Hard): http://localhost:{port}/cloudstack/")
print(f" - DataFlow (Medium): http://localhost:{port}/dataflow/")
print(f" - Finviz (Hard): http://localhost:{port}/finviz/")
print(f" - BlueBook (Hard): http://localhost:{port}/bluebook/")
print("\nAPI Endpoints:")
print(
f" - GET http://localhost:{port}/api/events - Get tracked events (?site=gbr)"
)
print(
f" - GET http://localhost:{port}/api/events/clear - Clear tracked events (?site=gbr)"
)
print(f" - GET http://localhost:{port}/api/sites - List available sites")
print(f" - GET http://localhost:{port}/api/help - API help")
print(f" - POST http://localhost:{port}/api/track - Submit tracking event")
print("\nPress Ctrl+C to stop the server")
print("=" * 60 + "\n")
def main():
"""Main entry point"""
with socketserver.ThreadingTCPServer(("", PORT), MockWebsiteHandler) as httpd:
httpd.allow_reuse_address = True
httpd.daemon_threads = True
print_startup_info(PORT)
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nServer stopped.")
if __name__ == "__main__":
main()