Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4ca1d0a

Browse files
authored
Merge pull request#13417 from mkurz/enrich_req_onClientError
request passed to `onClientError` now has cookies, flash and session set
2 parents702da35 +f8fc461 commit4ca1d0a

File tree

4 files changed

+81
-6
lines changed

4 files changed

+81
-6
lines changed

‎core/play-integration-test/src/test/scala/play/it/http/BadClientHandlingSpec.scala

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,57 @@ trait BadClientHandlingSpec extends PlaySpecification with ServerIntegrationSpec
8989
responseBody must startWith(expectedBodyTrailing)
9090
responseBody.substring(expectedBodyTrailing.length).matches("[0-9]+") must_==true// must have request id
9191
}
92+
93+
"allow accessing (empty) cookies, (empty) session and (empty) flash from an error handler if no headers are given" in withServer(
94+
newHttpErrorHandler() {
95+
defonClientError(request:RequestHeader,statusCode:Int,message:String)=
96+
Future.successful(
97+
Results.BadRequest(
98+
"cookies:"+ request.cookies+" session:"+ request.session+" flash:"+ request.flash
99+
)
100+
)
101+
defonServerError(request:RequestHeader,exception:Throwable):Future[Result]=Future.successful(Results.Ok)
102+
}
103+
) { port=>
104+
valresponse=BasicHttpClient.makeRequests(port)(
105+
BasicRequest("GET","/[","HTTP/1.1",Map(),"")
106+
)(0)
107+
108+
response.status must_==400
109+
response.body must beLeft("cookies: Map() session: Session(Map()) flash: Flash(Map())")
110+
}
111+
112+
"allow accessing cookies, session and flash from an error handler if headers are set" in withServer(
113+
newHttpErrorHandler() {
114+
defonClientError(request:RequestHeader,statusCode:Int,message:String)=
115+
Future.successful(
116+
Results.BadRequest(
117+
"cookies:"+ request.cookies+" session:"+ request.session+" flash:"+ request.flash
118+
)
119+
)
120+
defonServerError(request:RequestHeader,exception:Throwable):Future[Result]=Future.successful(Results.Ok)
121+
}
122+
) { port=>
123+
valresponse=BasicHttpClient.makeRequests(port)(
124+
BasicRequest(
125+
"GET",
126+
"/[",
127+
"HTTP/1.1",
128+
Map(
129+
"Cookie"->
130+
("PLAY_SESSION=eyJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7InNlc3Npb25mb28iOiJzZXNzaW9uYmFyIn0sIm5iZiI6MTc1MjY2ODA1NSwiaWF0IjoxNzUyNjY4MDU1fQ.HgN1CB4OqFE7NlAwuOKMpn5733_wXq295wC_gX34VvU;"+
131+
"PLAY_FLASH=eyJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7ImZsYXNoZm9vIjoiZmxhc2hiYXIifSwibmJmIjoxNzUyNjY3OTg0LCJpYXQiOjE3NTI2Njc5ODR9.LXzAn-N8BnlodhFhG3Q4YGAVd47jqq7gGAGrYCrLCEQ")
132+
),
133+
""
134+
)
135+
)(0)
136+
137+
response.status must_==400
138+
response.body must beLeft(
139+
"cookies: Map(PLAY_SESSION -> Cookie(PLAY_SESSION,eyJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7InNlc3Npb25mb28iOiJzZXNzaW9uYmFyIn0sIm5iZiI6MTc1MjY2ODA1NSwiaWF0IjoxNzUyNjY4MDU1fQ.HgN1CB4OqFE7NlAwuOKMpn5733_wXq295wC_gX34VvU,None,/,None,false,true,None,false), PLAY_FLASH -> Cookie(PLAY_FLASH,eyJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7ImZsYXNoZm9vIjoiZmxhc2hiYXIifSwibmJmIjoxNzUyNjY3OTg0LCJpYXQiOjE3NTI2Njc5ODR9.LXzAn-N8BnlodhFhG3Q4YGAVd47jqq7gGAGrYCrLCEQ,None,/,None,false,true,None,false))"+
140+
"session: Session(Map(sessionfoo -> sessionbar))"+
141+
"flash: Flash(Map(flashfoo -> flashbar))"
142+
)
143+
}
92144
}
93145
}

‎transport/server/play-netty-server/src/main/scala/play/core/server/netty/PlayRequestHandler.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,17 @@ private[play] class PlayRequestHandler(
111111
valrequestHeader= modelConversion(tryApp).createRequestHeader(channel, request, unparsedTarget)
112112
valdebugHeader= attachDebugInfo(requestHeader)
113113
valcleanMessage=if (message==null)""else message
114+
valmaybeEnriched=Server.tryToEnrichHeader(tryApp, debugHeader)
114115
valresult=
115116
if (bypassErrorHandler)Future.successful(Results.Status(statusCode)(cleanMessage))
116117
else
117118
errorHandler(tryApp).onClientError(
118-
debugHeader.addAttr(HttpErrorHandler.Attrs.HttpErrorInfo,HttpErrorInfo("server-backend")),
119+
maybeEnriched.addAttr(HttpErrorHandler.Attrs.HttpErrorInfo,HttpErrorInfo("server-backend")),
119120
statusCode,
120121
cleanMessage
121122
)
122123
// If there's a problem in parsing the request, then we should close the connection, once done with it
123-
debugHeader->Server.actionForResult(result.map(_.withHeaders(HeaderNames.CONNECTION->"close")))
124+
maybeEnriched->Server.actionForResult(result.map(_.withHeaders(HeaderNames.CONNECTION->"close")))
124125
}
125126

126127
val (requestHeader, handler): (RequestHeader,Handler)= tryRequestmatch {

‎transport/server/play-pekko-http-server/src/main/scala/play/core/server/PekkoHttpServer.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,14 +337,15 @@ class PekkoHttpServer(context: PekkoHttpServer.Context) extends Server {
337337
valunparsedTarget=Server.createUnparsedRequestTarget(headers.uri)
338338
valrequestHeader=
339339
modelConversion(tryApp).createRequestHeader(headers, secure, remoteAddress, unparsedTarget, request)
340-
valdebugHeader= attachDebugInfo(requestHeader)
341-
valresult= errorHandler(tryApp).onClientError(
342-
debugHeader.addAttr(HttpErrorHandler.Attrs.HttpErrorInfo,HttpErrorInfo("server-backend")),
340+
valdebugHeader= attachDebugInfo(requestHeader)
341+
valmaybeEnriched=Server.tryToEnrichHeader(tryApp, debugHeader)
342+
valresult= errorHandler(tryApp).onClientError(
343+
maybeEnriched.addAttr(HttpErrorHandler.Attrs.HttpErrorInfo,HttpErrorInfo("server-backend")),
343344
statusCode,
344345
if (message==null)""else message
345346
)
346347
// If there's a problem in parsing the request, then we should close the connection, once done with it
347-
debugHeader->Server.actionForResult(result.map(_.withHeaders(HeaderNames.CONNECTION->"close")))
348+
maybeEnriched->Server.actionForResult(result.map(_.withHeaders(HeaderNames.CONNECTION->"close")))
348349
}
349350

350351
val (taggedRequestHeader, handler): (RequestHeader,Handler)= convertedRequestHeadermatch {

‎transport/server/play-server/src/main/scala/play/core/server/Server.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import java.util.function.{ Function => JFunction }
1010
importscala.concurrent.duration.FiniteDuration
1111
importscala.concurrent.Future
1212
importscala.language.postfixOps
13+
importscala.util.control.NonFatal
14+
importscala.util.Failure
15+
importscala.util.Success
1316
importscala.util.Try
1417

1518
importcom.typesafe.config.Config
@@ -186,6 +189,24 @@ object Server {
186189
}
187190
}
188191

192+
/**
193+
* Tries to add the following typed attributes to requests:
194+
* - request id (if not existing yet)
195+
* - cookie
196+
* - session cookie
197+
* - flash cookie
198+
*
199+
* If, for some reason, this is not possible, the passed header will be returned as fallback.
200+
*/
201+
private[server]deftryToEnrichHeader(tryApp:Try[Application],header:RequestHeader):RequestHeader=try {
202+
tryAppmatch {
203+
caseSuccess(application)=> application.requestFactory.copyRequestHeader(header)
204+
caseFailure(_)=> header
205+
}
206+
}catch {
207+
caseNonFatal(_)=> header// just in case something went wrong in the requestFactory above
208+
}
209+
189210
/**
190211
* Parses the config setting `infinite` as `Long.MaxValue` otherwise uses Config's built-in
191212
* parsing of byte values.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp