-
Notifications
You must be signed in to change notification settings - Fork 367
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") | ||
|
||
|
|
||
| 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 = { | ||
|
||
| 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 ) | ||
|
||
| 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)) | ||
|
||
| Filter.mk( mkFilter(inclusions, exclusion)) | ||
| } | ||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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.