Skip to content

Commit d179da8

Browse files
authored
Merge pull request #1018 from nickburlett/fix-upload-time-parse
Fix parsing of ISO times
2 parents dad14bb + 41c8cb3 commit d179da8

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

Distribution/Server/Features/Mirror.hs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,15 @@ mirrorFeature ServerEnv{serverBlobStore = store}
221221
-- curl -H 'Content-Type: text/plain' -u admin:admin -X PUT -d "Tue Oct 18 20:54:28 UTC 2010" http://localhost:8080/package/edit-distance-0.2.1/upload-time
222222
uploadTimePut :: DynamicPath -> ServerPartE Response
223223
uploadTimePut dpath = do
224+
let altParseTimeMaybe timeStr = (
225+
parseTimeMaybe "%c" timeStr
226+
<|> parseTimeMaybe "%Y-%m-%dT%H:%M:%SZ" timeStr
227+
)
224228
guardAuthorised_ [InGroup mirrorGroup]
225229
pkgid <- packageInPath dpath
226230
timeContent <- expectTextPlain
227-
case parseTimeMaybe "%c" (unpackUTF8 timeContent) of
228-
Nothing -> errBadRequest "Could not parse upload time" []
231+
case altParseTimeMaybe (unpackUTF8 timeContent) of
232+
Nothing -> errBadRequest "Could not parse upload time" [MText $ show timeContent]
229233
Just t -> do
230234
existed <- updateSetPackageUploadTime pkgid t
231235
if existed

tests/HackageClientUtils.hs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,29 @@ mkPostReq url vals =
269269
setRequestBody (postRequest (mkUrl url))
270270
("application/x-www-form-urlencoded", urlEncodeVars vals)
271271

272+
mkPutReq :: RelativeURL -> [(String, String)] -> Request_String
273+
mkPutReq url vals =
274+
setRequestBody (putRequest (mkUrl url))
275+
("application/x-www-form-urlencoded", urlEncodeVars vals)
276+
277+
-- Like mkPutReq, but posts the given body text directly as text/plain
278+
mkPutTextReq :: RelativeURL -> String -> Request_String
279+
mkPutTextReq url body =
280+
setRequestBody (putRequest (mkUrl url))
281+
("text/plain", body)
282+
283+
-- | A convenience constructor for a PUT 'Request'.
284+
--
285+
-- If the URL isn\'t syntactically valid, the function raises an error.
286+
putRequest
287+
:: String -- ^URL to POST to
288+
-> Request_String -- ^The constructed request
289+
putRequest urlString =
290+
case parseURI urlString of
291+
Nothing -> error ("putRequest: Not a valid URL - " ++ urlString)
292+
Just u -> mkRequest PUT u
293+
294+
272295
getUrl :: Authorization -> RelativeURL -> IO String
273296
getUrl auth url = Http.execRequest auth (mkGetReq url)
274297

@@ -313,6 +336,18 @@ post auth url vals = void $
313336
where
314337
expectedCode code = isOk code || isSeeOther code || isAccepted code
315338

339+
put :: Authorization -> RelativeURL -> [(String, String)] -> IO ()
340+
put auth url vals = void $
341+
Http.execRequest' auth (mkPutReq url vals) expectedCode
342+
where
343+
expectedCode code = isOk code || isSeeOther code || isAccepted code
344+
345+
putText :: Authorization -> RelativeURL -> String -> IO ()
346+
putText auth url body = void $
347+
Http.execRequest' auth (mkPutTextReq url body) expectedCode
348+
where
349+
expectedCode code = isOk code || isSeeOther code || isAccepted code
350+
316351
postFile :: ExpectedCode
317352
-> Authorization -> RelativeURL
318353
-> String -> (FilePath, String)

tests/HighLevelTest.hs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,34 @@ runPackageUploadTests = do
162162
(Auth "HackageTestUser1" "testpass1")
163163
"/packages/" "package"
164164
(testpackageTarFilename, testpackageTarFileContent)
165+
do info "Adding HackageTestUser1 to mirrorers"
166+
post (Auth "admin" "admin") "/packages/mirrorers/" [
167+
("user", "HackageTestUser1")
168+
]
169+
do info "Checking Package Exists"
170+
xs <- validate NoAuth "/package/testpackage-1.0.0.0"
171+
unless (">testpackage</a>: <small>test package testpackage</small></h1>" `isInfixOf` xs) $
172+
die ("Bad package info: " ++ show xs)
173+
do info "Setting upload time"
174+
putText (Auth "HackageTestUser1" "testpass1")
175+
"/package/testpackage-1.0.0.0/upload-time"
176+
uploadTime
177+
xs <- getUrl NoAuth "/package/testpackage-1.0.0.0/upload-time"
178+
unless (xs == uploadTimeISO) $
179+
die ("Bad upload time: " ++ show xs)
180+
do info "Setting upload time (ISO)"
181+
putText (Auth "HackageTestUser1" "testpass1")
182+
"/package/testpackage-1.0.0.0/upload-time"
183+
uploadTimeISO2
184+
xs <- getUrl NoAuth "/package/testpackage-1.0.0.0/upload-time"
185+
unless (xs == uploadTimeISO2) $
186+
die ("Bad upload time: " ++ show xs)
165187
where
166188
(testpackageTarFilename, testpackageTarFileContent, _, _, _, _) =
167189
testpackage
190+
uploadTime = "Tue Oct 18 20:54:28 UTC 2010"
191+
uploadTimeISO = "2010-10-18T20:54:28Z"
192+
uploadTimeISO2 = "2020-10-18T20:54:28Z"
168193

169194
runPackageTests :: IO ()
170195
runPackageTests = do

0 commit comments

Comments
 (0)