Skip to content
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

Add modeled header for Content-Location #2540

Merged
merged 4 commits into from Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,22 @@
/*
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com>
*/

package akka.http.javadsl.model.headers;

import akka.http.javadsl.model.Uri;

/**
* Model for the `ContentLocation` header.
* Specification: https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
*/
public abstract class ContentLocation extends akka.http.scaladsl.model.HttpHeader {
public abstract Uri getUri();

public static ContentLocation create(Uri uri) {
return new akka.http.scaladsl.model.headers.Content$minusLocation(uri.asScala());
}
public static ContentLocation create(String uri) {
return create(Uri.create(uri));
}
}
@@ -0,0 +1,2 @@
# change to internal class, only needed for Scala 2.11
ProblemFilters.exclude[ReversedMissingMethodProblem]("akka.http.impl.model.parser.SimpleHeaders.content-location")
Expand Up @@ -146,6 +146,7 @@ private[http] object HeaderParser {
"content-disposition",
"content-encoding",
"content-length",
"content-location",
"content-range",
"content-type",
"cookie",
Expand Down
Expand Up @@ -91,6 +91,13 @@ private[parser] trait SimpleHeaders { this: Parser with CommonRules with CommonA
longNumberCapped ~> (`Content-Length`(_)) ~ EOI
}

// https://tools.ietf.org/html/rfc7231#section-3.1.4.2
def `content-location` = rule {
// we are bit more relaxed than the spec here by also parsing a potential fragment
// but catch it in the `Content-Location` instance validation (with a `require` in the constructor)
uriReference ~ EOI ~> (`Content-Location`(_))
}

// http://tools.ietf.org/html/rfc7233#section-4.2
def `content-range` = rule {
(`byte-content-range` | `other-content-range`) ~ EOI ~> (`Content-Range`(_, _))
Expand Down
Expand Up @@ -436,6 +436,19 @@ final case class `Content-Length` private[http] (length: Long) extends jm.header
protected def companion = `Content-Length`
}

// https://tools.ietf.org/html/rfc7231#section-3.1.4.2
object `Content-Location` extends ModeledCompanion[`Content-Location`]
final case class `Content-Location`(uri: Uri) extends jm.headers.ContentLocation with ResponseHeader {
require(uri.fragment.isEmpty, "Content-Location header URI must not contain a fragment")
require(uri.authority.userinfo.isEmpty, "Content-Location header URI must not contain a userinfo component")

def renderValue[R <: Rendering](r: R): r.type = { import UriRendering.UriRenderer; r ~~ uri }
protected def companion = `Content-Location`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, caught by the tests and fixed ;)


/** Java API */
def getUri: akka.http.javadsl.model.Uri = uri.asJava
}

/**
* Document http://tools.ietf.org/html/rfc6266 updates document https://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html. Between these
* two there is slight but important difference regarding how parameter values are formatted. In RFC6266 parameters values are without quotes and
Expand Down
Expand Up @@ -208,6 +208,14 @@ class HttpHeaderSpec extends FreeSpec with Matchers {
.renderedTo("999999999999999999")
}

"Content-Location" in {
"Content-Location: https://spray.io/secure" =!= `Content-Location`(Uri("https://spray.io/secure"))
"Content-Location: /en-us/default.aspx?foo=bar" =!= `Content-Location`(Uri("/en-us/default.aspx?foo=bar"))
"Content-Location: https://akka.io/#sec" =!= ErrorInfo(
"Illegal HTTP header 'Content-Location': requirement failed",
"Content-Location header URI must not contain a fragment")
}

"Content-Type" in {
"Content-Type: application/pdf" =!=
`Content-Type`(`application/pdf`)
Expand Down