-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to rewrite arbitrary headers
- Loading branch information
Showing
2 changed files
with
38 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package fakehttp.interceptor | ||
|
||
import org.jboss.netty.handler.codec.http._ | ||
|
||
/** | ||
* This trait adds header rewriting to the interceptor. | ||
* | ||
* To use, define a header with prefix "X-Fakehttp-Header-Rewrite-{header}". The value of this header will be written | ||
* into the "{header}" header. These X-Fakehttp-Header-Rewrite-* headers will then be removed. | ||
* | ||
* @author darren | ||
* | ||
*/ | ||
trait HeaderRewriting extends Interceptor { | ||
|
||
import HeaderRewriting._ | ||
import scala.collection.JavaConversions._ | ||
|
||
override abstract def intercept(request: HttpRequest): InterceptResult = { | ||
request.getHeaderNames filter { | ||
_.startsWith(PREFIX) | ||
} foreach { header => | ||
val value = request.getHeader(header) | ||
request.setHeader(header.substring(PREFIX_LENGTH), value) | ||
System.err.println("Setting header [%s] to [%s]".format(header.substring(PREFIX_LENGTH), value)) | ||
request.removeHeader(header) | ||
} | ||
|
||
super.intercept(request) | ||
} | ||
|
||
} | ||
|
||
object HeaderRewriting { | ||
val PREFIX = "X-Fakehttp-Header-Rewrite-" | ||
val PREFIX_LENGTH = PREFIX.length | ||
} |