Skip to content

Commit a99a073

Browse files
committed
refactor: use only Lazy.ByteString to calculate the response body length
1 parent 84d896d commit a99a073

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

src/PostgREST/Response.hs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ actionResponse :: DbResult -> ApiRequest -> (Text, Text) -> AppConfig -> SchemaC
6565
actionResponse (DbCrudResult WrappedReadPlan{pMedia, wrHdrsOnly=headersOnly, crudQi=identifier} RSStandard{..}) ctxApiRequest@ApiRequest{iPreferences=Preferences{..},..} _ _ _ _ _ = do
6666
let
6767
(status, contentRange) = RangeQuery.rangeStatusHeader iTopLevelRange rsQueryTotal rsTableTotal
68-
cLHeader = if headersOnly then mempty else [contentLengthHeaderLazy bod]
68+
cLHeader = if headersOnly then mempty else [ contentLengthHeader bod ]
6969
prefHeader = maybeToList . prefAppliedHeader $ Preferences Nothing Nothing preferCount preferTransaction Nothing preferHandling preferTimezone Nothing []
7070
headers =
7171
[ contentRange
@@ -120,7 +120,7 @@ actionResponse (DbCrudResult MutateReadPlan{mrMutation=MutationCreate, mrMutateP
120120
Just HeadersOnly -> (headers, mempty)
121121
Nothing -> (headers, mempty)
122122

123-
(ovStatus, ovHeaders) <- overrideStatusHeaders rsGucStatus rsGucHeaders status $ contentLengthHeaderLazy bod:headers'
123+
(ovStatus, ovHeaders) <- overrideStatusHeaders rsGucStatus rsGucHeaders status $ contentLengthHeader bod:headers'
124124

125125
Right $ PgrstResponse ovStatus ovHeaders bod
126126

@@ -131,10 +131,11 @@ actionResponse (DbCrudResult MutateReadPlan{mrMutation=MutationUpdate, pMedia} R
131131
if shouldCount preferCount then Just rsQueryTotal else Nothing
132132
prefHeader = prefAppliedHeader $ Preferences Nothing preferRepresentation preferCount preferTransaction preferMissing preferHandling preferTimezone preferMaxAffected []
133133
headers = catMaybes [contentRangeHeader, prefHeader]
134+
lbsBody = LBS.fromStrict rsBody
134135

135136
let (status, headers', body) =
136137
case preferRepresentation of
137-
Just Full -> (HTTP.status200, headers ++ [contentLengthHeaderStrict rsBody] ++ contentTypeHeaders pMedia ctxApiRequest, LBS.fromStrict rsBody)
138+
Just Full -> (HTTP.status200, headers ++ [contentLengthHeader lbsBody] ++ contentTypeHeaders pMedia ctxApiRequest, lbsBody)
138139
Just None -> (HTTP.status204, headers, mempty)
139140
_ -> (HTTP.status204, headers, mempty)
140141

@@ -145,14 +146,15 @@ actionResponse (DbCrudResult MutateReadPlan{mrMutation=MutationUpdate, pMedia} R
145146
actionResponse (DbCrudResult MutateReadPlan{mrMutation=MutationSingleUpsert, pMedia} RSStandard{..}) ctxApiRequest@ApiRequest{iPreferences=Preferences{..}} _ _ _ _ _ = do
146147
let
147148
prefHeader = maybeToList . prefAppliedHeader $ Preferences Nothing preferRepresentation preferCount preferTransaction Nothing preferHandling preferTimezone Nothing []
148-
cLHeader = [contentLengthHeaderStrict rsBody]
149+
lbsBody = LBS.fromStrict rsBody
150+
cLHeader = [contentLengthHeader lbsBody]
149151
cTHeader = contentTypeHeaders pMedia ctxApiRequest
150152

151153
let isInsertIfGTZero i = if i > 0 then HTTP.status201 else HTTP.status200
152154
upsertStatus = isInsertIfGTZero $ fromJust rsInserted
153155
(status, headers, body) =
154156
case preferRepresentation of
155-
Just Full -> (upsertStatus, cLHeader ++ cTHeader ++ prefHeader, LBS.fromStrict rsBody)
157+
Just Full -> (upsertStatus, cLHeader ++ cTHeader ++ prefHeader, lbsBody)
156158
Just None -> (HTTP.status204, prefHeader, mempty)
157159
_ -> (HTTP.status204, prefHeader, mempty)
158160
(ovStatus, ovHeaders) <- overrideStatusHeaders rsGucStatus rsGucHeaders status headers
@@ -164,9 +166,10 @@ actionResponse (DbCrudResult MutateReadPlan{mrMutation=MutationDelete, pMedia} R
164166
contentRangeHeader = RangeQuery.contentRangeH 1 0 $ if shouldCount preferCount then Just rsQueryTotal else Nothing
165167
prefHeader = maybeToList . prefAppliedHeader $ Preferences Nothing preferRepresentation preferCount preferTransaction Nothing preferHandling preferTimezone preferMaxAffected []
166168
headers = contentRangeHeader : prefHeader
169+
lbsBody = LBS.fromStrict rsBody
167170
(status, headers', body) =
168171
case preferRepresentation of
169-
Just Full -> (HTTP.status200, headers ++ [contentLengthHeaderStrict rsBody] ++ contentTypeHeaders pMedia ctxApiRequest, LBS.fromStrict rsBody)
172+
Just Full -> (HTTP.status200, headers ++ [contentLengthHeader lbsBody] ++ contentTypeHeaders pMedia ctxApiRequest, lbsBody)
170173
Just None -> (HTTP.status204, headers, mempty)
171174
_ -> (HTTP.status204, headers, mempty)
172175

@@ -184,7 +187,7 @@ actionResponse (DbCrudResult CallReadPlan{pMedia, crInvMthd=invMethod, crProc=pr
184187
else LBS.fromStrict rsBody
185188
isHeadMethod = invMethod == InvRead True
186189
prefHeader = maybeToList . prefAppliedHeader $ Preferences Nothing Nothing preferCount preferTransaction Nothing preferHandling preferTimezone preferMaxAffected []
187-
cLHeader = if isHeadMethod then mempty else [contentLengthHeaderLazy rsOrErrBody]
190+
cLHeader = if isHeadMethod then mempty else [contentLengthHeader rsOrErrBody]
188191
headers = contentRange : prefHeader
189192
(status', headers', body) =
190193
if Routine.funcReturnsVoid proc then
@@ -199,12 +202,13 @@ actionResponse (DbCrudResult CallReadPlan{pMedia, crInvMthd=invMethod, crProc=pr
199202
Right $ PgrstResponse ovStatus ovHeaders body
200203

201204
actionResponse (DbPlanResult media plan) ctxApiRequest _ _ _ _ _ =
202-
Right $ PgrstResponse HTTP.status200 (contentLengthHeaderStrict plan : contentTypeHeaders media ctxApiRequest) $ LBS.fromStrict plan
205+
let body = LBS.fromStrict plan in
206+
Right $ PgrstResponse HTTP.status200 (contentLengthHeader body : contentTypeHeaders media ctxApiRequest) body
203207

204208
actionResponse (MaybeDbResult InspectPlan{ipHdrsOnly=headersOnly} body) _ versions conf sCache schema negotiatedByProfile =
205209
let
206210
rsBody = maybe mempty (\(x, y, z) -> if headersOnly then mempty else OpenAPI.encode versions conf sCache x y z) body
207-
cLHeader = if headersOnly then mempty else [contentLengthHeaderLazy rsBody]
211+
cLHeader = if headersOnly then mempty else [contentLengthHeader rsBody]
208212
in
209213
Right $ PgrstResponse HTTP.status200 (MediaType.toContentType MTOpenAPI : cLHeader ++ maybeToList (profileHeader schema negotiatedByProfile)) rsBody
210214

@@ -231,7 +235,7 @@ actionResponse (NoDbResult SchemaInfoPlan) _ _ _ _ _ _ = respondInfo "OPTIONS,GE
231235
respondInfo :: ByteString -> Either Error.Error PgrstResponse
232236
respondInfo allowHeader =
233237
let allOrigins = ("Access-Control-Allow-Origin", "*") in
234-
Right $ PgrstResponse HTTP.status200 [contentLengthHeaderStrict mempty, allOrigins, (HTTP.hAllow, allowHeader)] mempty
238+
Right $ PgrstResponse HTTP.status200 [contentLengthHeader mempty, allOrigins, (HTTP.hAllow, allowHeader)] mempty
235239

236240
-- Status and headers can be overridden as per https://postgrest.org/en/stable/references/transactions.html#response-headers
237241
overrideStatusHeaders :: Maybe Text -> Maybe BS.ByteString -> HTTP.Status -> [HTTP.Header]-> Either Error.Error (HTTP.Status, [HTTP.Header])
@@ -248,14 +252,8 @@ decodeGucStatus :: Maybe Text -> Either Error.Error (Maybe HTTP.Status)
248252
decodeGucStatus =
249253
maybe (Right Nothing) $ first (const . Error.ApiRequestError $ Error.GucStatusError) . fmap (Just . toEnum . fst) . decimal
250254

251-
contentLengthHeader :: Show b => (a -> b) -> a -> HTTP.Header
252-
contentLengthHeader lenFn body = ("Content-Length", show (lenFn body))
253-
254-
contentLengthHeaderStrict :: BS.ByteString -> HTTP.Header
255-
contentLengthHeaderStrict = contentLengthHeader BS.length
256-
257-
contentLengthHeaderLazy :: LBS.ByteString -> HTTP.Header
258-
contentLengthHeaderLazy = contentLengthHeader LBS.length
255+
contentLengthHeader :: LBS.ByteString -> HTTP.Header
256+
contentLengthHeader body = ("Content-Length", show (LBS.length body))
259257

260258
contentTypeHeaders :: MediaType -> ApiRequest -> [HTTP.Header]
261259
contentTypeHeaders mediaType ApiRequest{..} =

0 commit comments

Comments
 (0)