-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
85 lines (63 loc) · 2.21 KB
/
api.py
File metadata and controls
85 lines (63 loc) · 2.21 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
import time
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
# Import internal modules
from src.requester import fetch_headers
from src.analyzer import analyze_headers
from src.schemas import AnalyzeRequest
# Create FastAPI app
app = FastAPI(
title="HTTP Header Analyzer API",
description="An API to fetch and analyze HTTP headers for security issues.",
version="1.0.0"
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # frontend access
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/analyze", summary="Fetch and analyze HTTP headers from a URL")
def analyze_url(url: str):
start = time.perf_counter()
response_data = fetch_headers(url)
if not response_data.get("success"):
raise HTTPException(
status_code=400,
detail=response_data.get("error", "Failed to fetch headers.")
)
findings = analyze_headers(response_data)
end = time.perf_counter()
backend_time = get_backend_time(start, end)
return {
"url": response_data.get("url"),
"status": response_data.get("status_code"),
"headers": response_data.get("headers"),
"analysis": findings ,
"timing": {
"backend_seconds": backend_time
}
}
@app.post("/analyze", summary="Fetch and analyze HTTP headers from a URL (POST)")
def analyze_url_post(payload: AnalyzeRequest):
start = time.perf_counter()
response_data = fetch_headers(str(payload.url))
if not response_data.get("success"):
raise HTTPException(
status_code=400,
detail=response_data.get("error", "Failed to fetch headers.")
)
findings = analyze_headers(response_data)
end = time.perf_counter()
backend_time = get_backend_time(start, end)
return {
"response" : response_data,
"analysis": findings,
"timing": {
"backend_seconds": backend_time
}
}
def get_backend_time(start_time: float, end_time: float) -> float:
"""Calculate backend processing time in seconds."""
return round(end_time - start_time, 4)