Skip to content

Commit

Permalink
finatra-http: Get rid of MediaType
Browse files Browse the repository at this point in the history
Problem

MediaType is one of our last guava dependencies, so we'll be able to start
removing guava once we can get rid of it.

Solution

Replace MediaType with a String everywhere the finatra API requires a MediaType,
and rely on method in finagle's MediaType to check equality for media types and
handle utf8 charsets.

JIRA Issues: CSL-5672

Differential Revision: https://phabricator.twitter.biz/D308761
  • Loading branch information
mosesn authored and jenkins committed May 6, 2019
1 parent 29948ea commit ec0953f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ Note that ``PHAB_ID=#`` and ``RB_ID=#`` correspond to associated messages in com
Unreleased
----------

New Features
~~~~~~~~~~~~

* finagle-http: Add two new methods to `com.twitter.finagle.http.MediaType`,
`MediaType#typeEquals` for checking if two media types have the same type and
subtype, ignoring their charset, and `MediaType#addUtf8Charset` for easily
setting a utf-8 charset. ``PHAB_ID=D308761``

Runtime Behavior Changes
~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,49 @@ object MediaType {
val Csv = "application/csv"
val Gif = "image/gif"
val Html = "text/html"
val HtmlUtf8 = "text/html; charset=utf-8"
val Iframe = "application/iframe"
val Javascript = "application/javascript"
val Jpeg = "image/jpeg"
val Json = "application/json"
val JsonUtf8 = "application/json; charset=utf-8"
val JsonPatch = "application/json-patch+json"
val MultipartForm = "multipart/form-data"
val OctetStream = "application/octet-stream"
val PlainText = "text/plain"
val PlainTextUtf8 = "text/plain; charset=utf-8"
val Png = "image/png"
val Rss = "application/rss+xml"
val Txt = "text/plain"
val WwwForm = "application/x-www-form-urlencoded"
val Xls = "application/vnd.ms-excel"
val Xml = "application/xml"
val XmlUtf8 = "application/xml; charset=utf-8"
val Zip = "application/zip"

/**
* Adds a utf-8 charset parameter to a media type string.
*
* Note that if a charset is already set that it will add a second one,
* it does not inspect the provided media type.
*/
def addUtf8Charset(mediaType: String): String =
mediaType + "; charset=utf-8"

/**
* Checks equality for two media types, based on their type and subtype.
*
* This ignores media type parameters and suffixes.
*/
def typeEquals(left: String, right: String): Boolean = {
val leftIndex = {
val index = left.indexOf(';')
if (index == -1) left.length else index
}
val rightIndex = {
val index = right.indexOf(';')
if (index == -1) right.length else index
}
(leftIndex == rightIndex) && left.regionMatches(0, right, 0, leftIndex)
}
}

0 comments on commit ec0953f

Please sign in to comment.