Comments (13)
Comment by ktoso
Tuesday Jul 05, 2016 at 10:59 GMT
Thanks for the ticket, I agree it's better to provide a directive whcih does this "safely" than assuming people will do the right thing (and get 20 "almost correct" impls ;-))
from akka-http.
Comment by ktoso
Tuesday Jul 05, 2016 at 11:00 GMT
PRs very welcome to this one, should be rather simple :)
from akka-http.
Comment by Hawstein
Tuesday Jul 12, 2016 at 17:32 GMT
@Joe-Edwards @ktoso I will work on this if you don't mind. And AFAICS, just adding a extractStrictRequestEntity
method to BasicDirectives
solves this issue. Correct me if I miss something.
from akka-http.
Comment by Joe-Edwards
Tuesday Jul 12, 2016 at 18:36 GMT
Go for it. If it's any help, what I'd come up with so far was:
def extractStrictEntity(timeout: FiniteDuration): Directive1[HttpEntity.Strict] = {
Directive { inner => ctx =>
ctx.request.entity match {
case strict: HttpEntity.Strict =>
inner(Tuple1(strict))(ctx)
case nonStrict =>
import ctx.{executionContext, materializer}
nonStrict.toStrict(timeout).flatMap(strict => inner(Tuple1(strict))(ctx.mapRequest(_.copy(entity = strict))))
}
}
}
I'm not sure if the optimization in the strict case is really necessary (perhaps using FastFuture will make it redundant anyway?)
from akka-http.
Comment by ktoso
Tuesday Jul 12, 2016 at 21:08 GMT
The optimisation is not needed, toStrict does the right thing (returns "this") if the entity is Strict already.
I was more thinking of making a directive that works via mapRequest, i.e. it transforms the entity transparently.
strictEntity { // toStricts, but we can do various things inside
extractDataBytes { // works since strict already
// ..
} ~ get {
extractRequest { r => r.entity.dataBytes .... }
}
}
from akka-http.
Comment by ktoso
Tuesday Jul 12, 2016 at 21:09 GMT
For inspiration look at mapRequest
val route =
mapRequest(transformToPostRequest) {
extractRequest { req =>
complete(s"The request method was ${req.method.name}")
}
}
from akka-http.
Comment by Hawstein
Wednesday Jul 13, 2016 at 20:00 GMT
@ktoso just make a PR for this issue.
from akka-http.
Comment by Joe-Edwards
Thursday Jul 14, 2016 at 09:42 GMT
The benefit of extracting the entity is that you get don't need to match/cast it. Usually you want a strict entity so you can get the data explicitly:
strictEntity(...) {
extractRequest { request =>
val entity: HttpEntity = request.entity // don't know it's strict yet :(
entity match {
case strict: HttpEntity.Strict => strict.data // now we do, but have an non-exhaustive match :(
}
}
}
vs
extractStrictEntity(...) { strict =>
strict.data // immediately available in a type safe way
}
from akka-http.
Comment by ktoso
Thursday Jul 14, 2016 at 09:59 GMT
Sure, however it does not compose as nicely.
My proposal composes more nicely though, and is beneficial for other directives as well (e.g. formFields
which people keep getting wrong), instead of just this single case.
I stand by the need of doing a directive like I proposed, and optionally adding a extractStrictEntity
.
from akka-http.
Comment by ktoso
Friday Jul 22, 2016 at 09:33 GMT
Thanks a lot @Hawstein for implementing this feature :-)
from akka-http.
Was this not already implemented by akka/akka#20953?
from akka-http.
Correct, thanks a lot for noticing @catalin-ursachi !
from akka-http.
Duplicate of akka/akka#20881 (already solved)
from akka-http.
Related Issues (20)
- Cannot Unmarshall Seq[String] Outside HOT 1
- Release 10.6.0-M1
- Release 10.6.0-M2
- CVE-2023-44487 HOT 3
- Release 10.5.3
- Release 10.6.0
- Failed: Http2ClientSpec should send RST_STREAM if entity stream is canceled HOT 2
- Failed: Http2ServerSpec send RST_STREAM if entity stream is canceled
- Remove dependency on `sbt-dependency-graph` HOT 1
- Release 10.6.1
- Failed: Native image tests HOT 1
- SSE (and maybe more) marshalling/unmarshalling require classic system HOT 1
- fail: CI is failing to publish docs HOT 2
- The akka.http.cors package is not exist HOT 1
- Release 10.6.2
- failed: JwtDirectivesSpec HOT 4
- Failed: OutOfMemoryError HOT 1
- Release 10.6.3 HOT 1
- Explicit typed actor system to Http.apply does not compile on Scala 3
- `Content-Disposition` header parsing for `form-data` doesn't follow RFC7578 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from akka-http.