-
Notifications
You must be signed in to change notification settings - Fork 370
Substitute headers #46
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.twitter.diffy.proxy | ||
|
||
import com.twitter.finagle.Filter | ||
import com.twitter.util.Future | ||
import org.jboss.netty.handler.codec.http.{HttpHeaders, DefaultHttpRequest, HttpResponse, HttpRequest} | ||
import scala.collection.JavaConverters._ | ||
|
||
object CloneHttpRequestFilter { | ||
|
||
type Req = HttpRequest | ||
type Res = HttpResponse | ||
|
||
private def mkCloneRequest(reqIn:Req, filter: (Req => Future[Res])):Future[Res] = { | ||
def copyHeader(headers:HttpHeaders)(k: String, v:String) : Unit = { headers.add(k, v)} | ||
|
||
val reqOut: DefaultHttpRequest = new DefaultHttpRequest(reqIn.getProtocolVersion, reqIn.getMethod, reqIn.getUri) | ||
reqOut.setChunked(reqIn.isChunked) | ||
reqOut.setContent(reqIn.getContent) | ||
|
||
val headers = for {entry <- reqIn.headers().asScala} yield (entry.getKey, entry.getValue) | ||
headers.foreach((copyHeader(reqOut.headers)_).tupled) | ||
|
||
filter(reqOut) | ||
} | ||
|
||
def apply(): Filter[Req, Res, Req, Res] = Filter.mk(mkCloneRequest) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.twitter.diffy.proxy | ||
|
||
import com.twitter.finagle.Filter | ||
import com.twitter.finagle.http.Request | ||
import com.twitter.util.Future | ||
import org.jboss.netty.handler.codec.http.{HttpResponse, HttpHeaders, HttpRequest} | ||
import scala.collection.JavaConverters._ | ||
|
||
object RefineHttpHeadersByLabel { | ||
val knownLabels: Seq[String] = Seq("primary", "secondary", "candidate") | ||
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. These constants should be maintained in DifferenceProxy.scala to ensure that the labels always match. The current implementation requires these labels to be maintained in two places. 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. Good idea. |
||
|
||
def substitute(inclusions: Seq[String], exclusion: Seq[String])(req: HttpRequest) : HttpRequest = { | ||
|
||
def orFn[T](f: T => Boolean, g: T => Boolean): T => Boolean = { t => f(t) || g(t) } | ||
def constFalse[T](t: T): Boolean = false | ||
|
||
def renameHeaders(headers: HttpHeaders)(nameOld: String, nameNew: String): Unit = { | ||
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. you don't really need headers as an arg since it's closured |
||
val temp = headers.get(nameOld) | ||
headers.remove(nameOld) | ||
headers.set(nameNew, temp) | ||
} | ||
|
||
|
||
val removeExclusion = exclusion. | ||
map(_+"_"). | ||
map( x => (_:String).startsWith(x)). | ||
foldLeft(constFalse[String]_)(orFn) | ||
|
||
val reqOut = Request.apply(req) | ||
val headers: HttpHeaders = reqOut.headers | ||
//k => Option[Function[k => k]] | ||
val incomingHeaders = headers.names().asScala.toSet | ||
|
||
val rewrites = for { | ||
name <- incomingHeaders | ||
inclusion <- inclusions if name.startsWith(inclusion + "_") | ||
} yield name->name.substring(inclusion.length+1) | ||
|
||
incomingHeaders.filter( removeExclusion).foreach( headers.remove ) | ||
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. You can ignore this but I am leaving it in to share my thought process through the review. incomingHeaders foreach { name =>
val shouldExclude = exclusion exists { ex => name.startsWith(s"$ex_") }
if (shouldExclude) {
headers.remove(name)
}
val shouldInclude = inclusion exists { in => name.startsWith(s"$in_") }
if (shouldInclude) {
renameHeaders(headers)(name, name.substring(inclusion.length + 1))
}
} 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. My reflections - Since the header set is mutable, we should apply all the changes in a single operation. |
||
rewrites.foreach((renameHeaders(headers) _ ).tupled) | ||
|
||
reqOut | ||
} | ||
|
||
def apply(label:String): Filter[HttpRequest, HttpResponse, HttpRequest, HttpResponse] = { | ||
def mkFilter(inclusions: Seq[String], exclusion: Seq[String]) | ||
(req:HttpRequest, filter: (HttpRequest => Future[HttpResponse]) ) = | ||
filter(substitute(inclusions, exclusion)(req)) | ||
|
||
val (inclusions, exclusion) = knownLabels.partition(_.equalsIgnoreCase(label)) | ||
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. The expected behavior is that "inclusions" should always be equal to "Seq(label)". You can simplify this code by using that observation and defining "exclusions" as "knownLabels filter { !_.equalsIgnoreCase(label) }" and eliminating inclusions by directly using label instead. 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. Will do. |
||
Filter.mk( mkFilter(inclusions, exclusion)) | ||
} | ||
|
||
} |
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.
Taking a step back, RefineHttpHeadersByLabel might be better written as two separate filters:
RewriteHeadersForLabel(label)
RemoveHeadersForLabels(labels)
and then composing them as:
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.
I see the desire to partition the logic. I'd prefer not to perform multiple runs through the http header set.
Let's see if we can have one filter, but aggregate the header effects.