packages feed

ngx-export 1.0.1 → 1.1.0

raw patch · 3 files changed

+95/−41 lines, 3 files

Files

Changelog.md view
@@ -1,3 +1,9 @@+### 1.1.0++- Added an asynchronous content handler with direct access to request body+  chunks exported with *ngxExportAsyncHandlerOnReqBody*.+- Reading buffered request body from a temporary file when it's provided.+ ### 1.0.1  - Using deepseq for really deep evaluation of resulting lazy bytestrings to
NgxExport.hs view
@@ -17,8 +17,11 @@ -----------------------------------------------------------------------------  module NgxExport (+    -- * Type declarations+                  ContentHandlerResult+                 ,UnsafeContentHandlerResult      -- * Exporters-                  ngxExportSS+                 ,ngxExportSS                  ,ngxExportSSS                  ,ngxExportSLS                  ,ngxExportBS@@ -34,6 +37,7 @@                  ,ngxExportDefHandler                  ,ngxExportUnsafeHandler                  ,ngxExportAsyncHandler+                 ,ngxExportAsyncHandlerOnReqBody     -- * Re-exported data constructors from /"Foreign.C"/     --   (for marshalling in foreign calls)                  ,Foreign.C.CInt (..)@@ -93,23 +97,34 @@ {-# COMPLETE ToBool :: CUInt #-} #endif -type ContentHandlerData       = (L.ByteString, B.ByteString, Int)-type UnsafeContentHandlerData = (B.ByteString, B.ByteString, Int)+-- | The first element of the /3-tuple/ is /content/, the second is+-- /content type/, and the third is /HTTP status/.+type ContentHandlerResult = (L.ByteString, B.ByteString, Int) -data NgxExport = SS            (String -> String)-               | SSS           (String -> String -> String)-               | SLS           ([String] -> String)-               | BS            (String -> Bool)-               | BSS           (String -> String -> Bool)-               | BLS           ([String] -> Bool)-               | YY            (B.ByteString -> L.ByteString)-               | BY            (B.ByteString -> Bool)-               | IOYY          (B.ByteString -> Bool -> IO L.ByteString)-               | IOYYY         (L.ByteString -> B.ByteString -> IO L.ByteString)-               | Handler       (B.ByteString -> ContentHandlerData)-               | UnsafeHandler (B.ByteString -> UnsafeContentHandlerData)-               | AsyncHandler  (B.ByteString -> IO ContentHandlerData)+-- | The first element of the /3-tuple/ is /content/, the second is+-- /content type/, and the third is /HTTP status/. Both the content and the+-- content type are supposed to be referring to low-level string literals which+-- do not need to be freed upon request termination and must not be+-- garbage-collected in the Haskell RTS.+type UnsafeContentHandlerResult = (B.ByteString, B.ByteString, Int) +data NgxExport = SS              (String -> String)+               | SSS             (String -> String -> String)+               | SLS             ([String] -> String)+               | BS              (String -> Bool)+               | BSS             (String -> String -> Bool)+               | BLS             ([String] -> Bool)+               | YY              (B.ByteString -> L.ByteString)+               | BY              (B.ByteString -> Bool)+               | IOYY            (B.ByteString -> Bool -> IO L.ByteString)+               | IOYYY           (L.ByteString -> B.ByteString ->+                                     IO L.ByteString)+               | Handler         (B.ByteString -> ContentHandlerResult)+               | UnsafeHandler   (B.ByteString -> UnsafeContentHandlerResult)+               | AsyncHandler    (B.ByteString -> IO ContentHandlerResult)+               | AsyncHandlerRB  (L.ByteString -> B.ByteString ->+                                     IO ContentHandlerResult)+ let name = mkName "exportType" in do     TyConI (DataD _ _ _ EXTRA_WILDCARD_BEFORE_CON cs _) <- reify ''NgxExport     let cons = map (\(NormalC con [(_, typ)]) -> (con, typ)) cs@@ -248,7 +263,8 @@ ngxExportAsyncOnReqBody :: Name -> Q [Dec] ngxExportAsyncOnReqBody =     ngxExport 'IOYYY 'asyncIOYYY-    [t|Ptr NgxStrType -> CInt -> CString -> CInt -> CInt -> CUInt ->+    [t|Ptr NgxStrType -> Ptr NgxStrType -> CInt ->+       CString -> CInt -> CInt -> CUInt ->        Ptr (Ptr NgxStrType) -> Ptr CInt ->        Ptr CUInt -> Ptr (StablePtr L.ByteString) -> IO (StablePtr (Async ()))|] @@ -266,12 +282,8 @@        Ptr CUInt -> Ptr (StablePtr L.ByteString) -> IO (StablePtr (Async ()))|]  -- | Exports a function of type--- /'B.ByteString' -> ('L.ByteString', 'B.ByteString', 'Int')/+-- /'B.ByteString' -> 'ContentHandlerResult'/ -- for using in directives /haskell_content/ and /haskell_static_content/.------ The first element in the returned /3-tuple/ of the exported function is--- the /content/, the second is the /content type/, and the third is the--- /HTTP status/. ngxExportHandler :: Name -> Q [Dec] ngxExportHandler =     ngxExport 'Handler 'handler@@ -290,14 +302,8 @@        Ptr (StablePtr L.ByteString) -> IO CUInt|]  -- | Exports a function of type--- /'B.ByteString' -> ('B.ByteString', 'B.ByteString', 'Int')/+-- /'B.ByteString' -> 'UnsafeContentHandlerResult'/ -- for using in directive /haskell_unsafe_content/.------ The first element in the returned /3-tuple/ of the exported function is--- the /content/, the second is the /content type/, and the third is the--- /HTTP status/. Both the content and the content type are supposed to be--- referring to low-level string literals which do not need to be freed upon--- the request termination and must not be garbage-collected in the Haskell RTS. ngxExportUnsafeHandler :: Name -> Q [Dec] ngxExportUnsafeHandler =     ngxExport 'UnsafeHandler 'unsafeHandler@@ -305,12 +311,8 @@        Ptr CString -> Ptr CSize -> Ptr CInt -> IO CUInt|]  -- | Exports a function of type--- /'B.ByteString' -> 'IO' ('L.ByteString', 'B.ByteString', 'Int')/+-- /'B.ByteString' -> 'IO' 'ContentHandlerResult'/ -- for using in directive /haskell_async_content/.------ The first element in the returned /3-tuple/ of the exported function is--- the /content/, the second is the /content type/, and the third is the--- /HTTP status/. ngxExportAsyncHandler :: Name -> Q [Dec] ngxExportAsyncHandler =     ngxExport 'AsyncHandler 'asyncHandler@@ -319,6 +321,21 @@        Ptr (Ptr NgxStrType) -> Ptr CInt ->        Ptr CUInt -> Ptr (StablePtr L.ByteString) -> IO (StablePtr (Async ()))|] +-- | Exports a function of type+-- /'L.ByteString' -> 'B.ByteString' -> 'IO' 'ContentHandlerResult'/+-- for using in directive /haskell_async_content_on_request_body/.+--+-- The first argument of the exported function contains buffers of the client+-- request body.+ngxExportAsyncHandlerOnReqBody :: Name -> Q [Dec]+ngxExportAsyncHandlerOnReqBody =+    ngxExport 'AsyncHandlerRB 'asyncHandlerRB+    [t|Ptr NgxStrType -> Ptr NgxStrType -> CInt ->+       CString -> CInt -> CInt -> CUInt ->+       Ptr CString -> Ptr CSize -> Ptr (StablePtr B.ByteString) -> Ptr CInt ->+       Ptr (Ptr NgxStrType) -> Ptr CInt ->+       Ptr CUInt -> Ptr (StablePtr L.ByteString) -> IO (StablePtr (Async ()))|]+ data NgxStrType = NgxStrType CSize CString  instance Storable NgxStrType where@@ -409,6 +426,23 @@     PtrLen sct lct <- B.unsafeUseAsCStringLen ct return     pokeCStringLen sct lct pct plct >> poke pst st +peekRequestBodyChunks :: Ptr NgxStrType -> Ptr NgxStrType -> Int ->+    IO L.ByteString+peekRequestBodyChunks tmpf b m =+    if tmpf /= nullPtr+        then do+            c <- peek tmpf >>=+                (\(NgxStrType (I l) s) -> peekCStringLen (s, l)) >>=+                    L.readFile+            L.length c `seq` return c+        else peekNgxStringArrayLenY b m++pokeAsyncHandlerData :: B.ByteString -> Ptr CString -> Ptr CSize ->+    Ptr (StablePtr B.ByteString) -> Ptr CInt -> CInt -> IO ()+pokeAsyncHandlerData ct pct plct spct pst st = do+    pokeContentTypeAndStatus ct pct plct pst st+    newStablePtr ct >>= poke spct+ safeHandler :: Ptr CString -> Ptr CInt -> IO CUInt -> IO CUInt safeHandler p pl = handle $ \e -> do     PtrLen x l <- safeNewCStringLen $ show (e :: SomeException)@@ -541,13 +575,13 @@                 flip (,) False <$> f x' fstRun     ) fd efd -asyncIOYYY :: IOYYY -> Ptr NgxStrType -> CInt -> CString -> CInt ->-    CInt -> CUInt -> Ptr (Ptr NgxStrType) -> Ptr CInt ->+asyncIOYYY :: IOYYY -> Ptr NgxStrType -> Ptr NgxStrType -> CInt ->+    CString -> CInt -> CInt -> CUInt -> Ptr (Ptr NgxStrType) -> Ptr CInt ->     Ptr CUInt -> Ptr (StablePtr L.ByteString) -> IO (StablePtr (Async ()))-asyncIOYYY f b (I m) x (I n) fd (ToBool efd) =+asyncIOYYY f tmpf b (I m) x (I n) fd (ToBool efd) =     asyncIOCommon     (do-        b' <- peekNgxStringArrayLenY b m+        b' <- peekRequestBodyChunks tmpf b m         x' <- B.unsafePackCStringLen (x, n)         flip (,) False <$> f b' x'     ) fd efd@@ -562,8 +596,22 @@     (do         x' <- B.unsafePackCStringLen (x, n)         (s, ct, I st) <- f x'-        (return $!! s) >> pokeContentTypeAndStatus ct pct plct pst st-        newStablePtr ct >>= poke spct+        (return $!! s) >> pokeAsyncHandlerData ct pct plct spct pst st+        return (s, False)+    ) fd efd++asyncHandlerRB :: AsyncHandlerRB -> Ptr NgxStrType -> Ptr NgxStrType -> CInt ->+    CString -> CInt -> CInt -> CUInt ->+    Ptr CString -> Ptr CSize -> Ptr (StablePtr B.ByteString) -> Ptr CInt ->+    Ptr (Ptr NgxStrType) -> Ptr CInt ->+    Ptr CUInt -> Ptr (StablePtr L.ByteString) -> IO (StablePtr (Async ()))+asyncHandlerRB f tmpf b (I m) x (I n) fd (ToBool efd) pct plct spct pst =+    asyncIOCommon+    (do+        b' <- peekRequestBodyChunks tmpf b m+        x' <- B.unsafePackCStringLen (x, n)+        (s, ct, I st) <- f b' x'+        (return $!! s) >> pokeAsyncHandlerData ct pct plct spct pst st         return (s, False)     ) fd efd 
ngx-export.cabal view
@@ -1,5 +1,5 @@ name:                       ngx-export-version:                    1.0.1+version:                    1.1.0 synopsis:                   Helper module for Nginx haskell module description:                Helper module for         <http://github.com/lyokha/nginx-haskell-module Nginx haskell module>