-
Notifications
You must be signed in to change notification settings - Fork 423
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
Support ranges request handling #1527
Merged
Merged
Changes from 14 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
7cca165
Implemented accept range support
768edb8
Added test
3f70387
Added handling of HEAD requests for Akka Http
c704d72
Changed way of handling HEAD requests with empty content-length
6781147
Added handling of HEAD requests for Play server
b06f697
Refactor: replaced string with enum value
be312a2
Refactor: renamed static endpoint methods
0859152
Broaden the scope of MonadError in Files
b4df79a
Added method to created head and get enpoints for static file
bc5000c
Added example for static files streaming
3c2dce6
Replace hardcoded values with enums
a424004
Fixed compilation error in docs
5aa30ff
Fixed blocking effect in Files
9b267aa
Renamed method and added scala doc
8a49d7e
Minor refactor
eefc5d9
Fixed static content tests setup
432b280
Fixed handling of paths in head endpoint
ef6a368
Fixed setup of double endpoints test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
examples/src/main/scala/sttp/tapir/examples/StaticContentAkkaServer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package sttp.tapir.examples | ||
|
||
import akka.actor.ActorSystem | ||
import akka.http.scaladsl.Http | ||
import akka.http.scaladsl.server.Route | ||
import sttp.client3._ | ||
import sttp.model.{ContentRangeUnits, Header, HeaderNames, StatusCode} | ||
import sttp.tapir._ | ||
import sttp.tapir.server.akkahttp.AkkaHttpServerInterpreter | ||
|
||
import java.nio.file.{Files, Path, StandardOpenOption} | ||
import scala.concurrent.duration.DurationInt | ||
import scala.concurrent.{Await, Future} | ||
|
||
object StaticContentAkkaServer extends App { | ||
private val parent: Path = Files.createTempDirectory("akka-static-example") | ||
|
||
parent.resolve("d1/d2").toFile.mkdirs() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not needed |
||
|
||
Files.write(parent.resolve("f1"), "f1 content".getBytes, StandardOpenOption.CREATE_NEW) | ||
|
||
private val exampleFile = parent.resolve("f1").toFile | ||
private val exampleFilePath = exampleFile.getAbsolutePath | ||
|
||
private val fileEndpoints = fileServerEndpoints[Future]("range-example")(exampleFilePath) | ||
private val route: Route = AkkaHttpServerInterpreter().toRoute(fileEndpoints) | ||
|
||
// starting the server | ||
private implicit val actorSystem: ActorSystem = ActorSystem() | ||
import actorSystem.dispatcher | ||
|
||
private val bindAndCheck: Future[Unit] = Http().newServerAt("localhost", 8080).bindFlow(route).map { _ => | ||
// testing | ||
val backend: SttpBackend[Identity, Any] = HttpURLConnectionBackend() | ||
val headResponse = basicRequest | ||
.head(uri"http://localhost:8080/range-example") | ||
.response(asStringAlways) | ||
.send(backend) | ||
|
||
assert(headResponse.code == StatusCode.Ok) | ||
assert(headResponse.headers.contains(Header(HeaderNames.AcceptRanges, ContentRangeUnits.Bytes))) | ||
assert(headResponse.headers contains Header(HeaderNames.ContentLength, exampleFile.length.toString)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. different syntax |
||
|
||
val getResponse = basicRequest | ||
.headers(Header(HeaderNames.Range, "bytes=3-6")) | ||
.get(uri"http://localhost:8080/range-example") | ||
.response(asStringAlways) | ||
.send(backend) | ||
|
||
assert(getResponse.body == "cont") | ||
assert(getResponse.code == StatusCode.PartialContent) | ||
assert(getResponse.body.length == 4) | ||
assert(getResponse.headers contains Header(HeaderNames.ContentRange, "bytes 3-6/10")) | ||
|
||
} | ||
|
||
Await.result(bindAndCheck.transformWith { r => actorSystem.terminate().transform(_ => r) }, 1.minute) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if the file doesn't exist?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
besides ... you are ignoring the input path :D and always returning the length of the root file/direcotry