Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#396) Fixed unwated behaviour of files and filesOpts while not uploading file. #397

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Web/Scotty/Body.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import qualified Data.ByteString.Lazy.Char8 as BL
import qualified GHC.Exception as E (throw)
import Network.Wai (Request(..), getRequestBodyChunk)
import qualified Network.Wai.Handler.Warp as Warp (InvalidRequest(..))
import qualified Network.Wai.Parse as W (File, Param, getRequestBodyType, tempFileBackEnd, RequestBodyType(..), sinkRequestBodyEx, RequestParseException(..), ParseRequestBodyOptions)
import qualified Network.Wai.Parse as W (File, Param, getRequestBodyType, tempFileBackEnd, RequestBodyType(..), sinkRequestBodyEx, RequestParseException(..), ParseRequestBodyOptions,FileInfo(..))
-- import UnliftIO (MonadUnliftIO(..))
import UnliftIO.Exception (Handler(..), catches, throwIO)

Expand Down Expand Up @@ -64,7 +64,10 @@ getFormParamsAndFilesAction istate prbo req bodyInfo opts = do
let
wholeBody = BL.toChunks bs
(formparams, fs) <- parseRequestBodyExBS istate prbo wholeBody (W.getRequestBodyType req) `catches` handleWaiParseSafeExceptions
return (convertBoth <$> formparams, convertKey <$> fs)
let
fs' = filter emptyFile fs
return (convertBoth <$> formparams, convertKey <$> fs')
where emptyFile (_,fInfo) = ("\"\"" /= (W.fileName fInfo))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do HTML forms behave if the name attribute is absent? i.e. how does the payload sent by the browser look like.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ocramz,
I will add the test-cases.

payload when I upload style.css file with name attribute in HTML.
Screenshot from 2024-04-30 12-10-21

Payload when I upload style.css file without name attribute in HTML.
image


-- | Wrap exceptions from upstream libraries into 'ScottyException'
handleWaiParseSafeExceptions :: MonadIO m => [Handler m a]
Expand Down
2 changes: 1 addition & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
### Breaking changes
* Remove dependency on data-default class (#386). We have been exporting constants for default config values since 0.20, and this dependency was simply unnecessary.
* Remove re-export of `Network.Wai.Parse.ParseRequestBodyOptions` from `Web.Scotty` and `Web.Scotty.Trans`. This is a type from `wai-extra` so exporting it is unnecessary.

* (#396) Fixed unwated behaviour of `files` and `filesOpts` while not uploading any file.


## 0.22 [2024.03.09]
Expand Down
8 changes: 7 additions & 1 deletion test/Web/ScottySpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,9 @@ spec = do
(FMFile "file1.txt", "text/plain;charset=UTF-8", "file", "xxx"),
(FMFile "file1.txt", "text/plain;charset=UTF-8", "file", "xxx")
] `shouldRespondWith` 413

context "Not uploading file should return empty list (#396)" $ do
it "Length of list should be 0" $ do
postMultipartForm "/files" "ABC123" [(FMFile "", "", "", "")] `shouldRespondWith` "0"

describe "filesOpts" $ do
let
Expand All @@ -401,6 +403,10 @@ spec = do
Scotty.post "/files" processForm) $ do
it "loads uploaded files in memory" $ do
postMpForm `shouldRespondWith` 200 { matchBody = "2"}
context "Not uploading file should return empty list (#396)" $ do
withApp (Scotty.post "/files" processForm) $ do
it "Length of list should be 0" $ do
postMultipartForm "/files" "ABC123" [(FMFile "", "", "", "")] `shouldRespondWith` "0"


describe "text" $ do
Expand Down
Loading