@@ -74,6 +74,51 @@ async def _normalize_filename(filename):
74
74
return filename
75
75
76
76
77
+ async def content_by_hash (request , digest = None , file_digest = None ):
78
+ project_name = request .match_info ["project_name" ]
79
+ json_url = urllib .parse .urljoin (
80
+ request .app ["settings" ]["endpoint" ],
81
+ "/pypi/{}/json" .format (project_name ),
82
+ )
83
+
84
+ async with request .app ["http.session" ].get (json_url ) as resp :
85
+ if 400 <= resp .status < 500 :
86
+ return web .Response (status = resp .status )
87
+ elif 500 <= resp .status < 600 :
88
+ return web .Response (status = 503 )
89
+
90
+ # It shouldn't be possible to get a status code other than 200 here.
91
+ assert resp .status == 200
92
+
93
+ # Get the JSON data from our request.
94
+ data = await resp .json ()
95
+
96
+ # Look at all of the files listed in the JSON response, and see if one
97
+ # matches our filename and Python version. If we find one, then return a
98
+ # 302 redirect to that URL.
99
+ for release in data .get ("releases" , {}).values ():
100
+ for file_ in release :
101
+ if file_ ['digests' ][digest ] == file_digest :
102
+ return web .Response (
103
+ status = 302 ,
104
+ headers = {
105
+ "Location" : file_ ["url" ],
106
+ "Cache-Control" : "max-age=604800, public" ,
107
+ },
108
+ )
109
+
110
+ # If we've gotten to this point, it means that we couldn't locate an url
111
+ # to redirect to so we'll jsut 404.
112
+ return web .Response (status = 404 , headers = {"Reason" : "no file found" })
113
+
114
+
115
+ async def content_sha256 (request ):
116
+ return await content_by_hash (request , digest = "sha256" , file_digest = request .match_info ["file_sha256" ])
117
+
118
+ async def content_blake2b_256 (request ):
119
+ return await content_by_hash (request , digest = "blake2b_256" , file_digest = request .match_info ["file_blake2b_256" ])
120
+
121
+
77
122
async def redirect (request ):
78
123
python_version = request .match_info ["python_version" ]
79
124
project_l = request .match_info ["project_l" ]
0 commit comments