diff --git a/cbits/nix.cpp b/cbits/nix.cpp index 654488d..5717365 100644 --- a/cbits/nix.cpp +++ b/cbits/nix.cpp @@ -176,30 +176,26 @@ void dumpPath(char const * const hashPart, struct string * const output) { } } -void dumpLog(char const * const hashPart, struct string * const output) { +void dumpLog(char const * const baseName, struct string * const output) { ref store = getStore(); - std::optional storePath = - store->queryPathFromHashPart(hashPart); + StorePath storePath(baseName); - if (storePath.has_value()) { - auto subs = getDefaultSubstituters(); + auto subs = getDefaultSubstituters(); - subs.push_front(store); + subs.push_front(store); - for (auto & sub : subs) { - LogStore * logStore = dynamic_cast(&*sub); + *output = emptyString; - std::optional log = logStore->getBuildLog(storePath.value()); + for (auto & sub : subs) { + LogStore * logStore = dynamic_cast(&*sub); - if (log.has_value()) { - copyString(log.value(), output); - } else { - *output = emptyString; - } + std::optional log = logStore->getBuildLog(storePath); + + if (log.has_value()) { + copyString(log.value(), output); } - } else { - *output = emptyString; } } + } diff --git a/src/Main.hs b/src/Main.hs index 534f574..3038e11 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -88,23 +88,26 @@ makeApplication ApplicationOptions{..} request respond = do done response + let invalidPath = do + let headers = [ ("Content-Type", "text/plain") ] + + let builder = "Invalid path.\n" + + let response = + Wai.responseBuilder + Types.status400 + headers + builder + + done response + result <- Except.runExceptT do let rawPath = Wai.rawPathInfo request if | Just prefix <- ByteString.stripSuffix ".narinfo" rawPath , Just hashPart <- ByteString.stripPrefix "/" prefix -> do Monad.unless (ByteString.length hashPart == 32 && validHashPart hashPart) do - let headers = [ ("Content-Type", "text/plain") ] - - let builder = "Invalid hash part.\n" - - let response = - Wai.responseBuilder - Types.status400 - headers - builder - - done response + invalidPath maybeStorePath <- liftIO (Nix.queryPathFromHashPart hashPart) @@ -219,30 +222,10 @@ makeApplication ApplicationOptions{..} request respond = do return (interior, Nothing) | otherwise -> do - let headers = [ ("Content-Type", "text/plain") ] - - let builder = "Invalid path component.\n" - - let response = - Wai.responseBuilder - Types.status400 - headers - builder - - done response + invalidPath Monad.unless (validHashPart hashPart) do - let headers = [ ("Content-Type", "text/plain") ] - - let builder = "Invalid hash part.\n" - - let response = - Wai.responseBuilder - Types.status400 - headers - builder - - done response + invalidPath maybeStorePath <- liftIO (Nix.queryPathFromHashPart hashPart) @@ -281,11 +264,14 @@ makeApplication ApplicationOptions{..} request respond = do done response - | Just suffix <- ByteString.stripPrefix "/log/" rawPath - , 32 <= ByteString.length suffix -> do + | Just suffix <- ByteString.stripPrefix "/log/" rawPath -> do let hashPart = ByteString.take 32 suffix - maybeBytes <- liftIO (Nix.dumpLog hashPart) + Monad.unless (ByteString.length hashPart == 32 && validHashPart hashPart) do + invalidPath + + liftIO (print suffix) + maybeBytes <- liftIO (Nix.dumpLog suffix) bytes <- case maybeBytes of Nothing -> noSuchPath diff --git a/src/Nix.hsc b/src/Nix.hsc index 94b7ddf..e54da88 100644 --- a/src/Nix.hsc +++ b/src/Nix.hsc @@ -280,10 +280,10 @@ foreign import ccall "dumpLog" dumpLog_ :: CString -> Ptr String_ -> IO () dumpLog :: ByteString -> IO (Maybe ByteString) -dumpLog hashPart = do - ByteString.useAsCString hashPart \cHashPart -> do +dumpLog baseName = do + ByteString.useAsCString baseName \cBaseName -> do Foreign.alloca \output -> do - let open = dumpLog_ cHashPart output + let open = dumpLog_ cBaseName output let close = freeString output Exception.bracket_ open close do string_@String_{ data_} <- peek output