You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by gi...@git.apache.org on 2017/08/18 15:19:54 UTC

[GitHub] akrabat commented on a change in pull request #2646: Content type header improvements

akrabat commented on a change in pull request #2646: Content type header improvements
URL: https://github.com/apache/incubator-openwhisk/pull/2646#discussion_r133985120
 
 

 ##########
 File path: core/controller/src/main/scala/whisk/core/controller/WebActions.scala
 ##########
 @@ -265,30 +265,33 @@ protected[core] object WhiskWebActionsApi extends Directives {
     }
 
     /**
-     * Finds the content-type in the header list and maps it to a known media type. If it is not
-     * recognized, construct a failure with appropriate message.
+     * Finds the content-type in the header list and ensures that it is a valid format. If it is not
+     * valid, construct a failure with appropriate message.
+     * If the content-type header is missing, then return the supplied defaultType
      */
     private def findContentTypeInHeader(headers: List[RawHeader], transid: TransactionId, defaultType: MediaType): Try[MediaType] = {
         headers.find(_.lowercaseName == `Content-Type`.lowercaseName) match {
             case Some(header) =>
                 MediaType.parse(header.value) match {
-                    case Right(mediaType: MediaType) =>
-                        // lookup the media type specified in the content header to see if it is a recognized type
-                        MediaTypes.getForKey(mediaType.mainType -> mediaType.subType).map(Success(_)).getOrElse {
-                            // this is a content-type that is not recognized, reject it
-                            Failure(RejectRequest(BadRequest, Messages.httpUnknownContentType)(transid))
-                        }
+                    case Right(mediaType: MediaType) => Success(mediaType) // mediaType is a valid format
                     case _ => Failure(RejectRequest(BadRequest, Messages.httpUnknownContentType)(transid))
                 }
             case None => Success(defaultType)
         }
     }
 
     private def interpretHttpResponseAsJson(code: StatusCode, headers: List[RawHeader], js: JsValue, transid: TransactionId) = {
-        findContentTypeInHeader(headers, transid, `application/json`) match {
-            case Success(mediaType) if (mediaType == `application/json`) =>
+        findContentTypeInHeader(headers, transid, `application/json`).flatMap { mediaType =>
+            // A response is JSON if it's content type is "application/json" or ends in "+json"
+            if (MediaType.unapply(mediaType).getOrElse("").endsWith("+json") || mediaType == `application/json`) {
 
 Review comment:
   I wasn't sure if I could use `mediaType.value` here as I couldn't work out if it's guaranteed to be a string. I'm pretty sure that `MediaType.unapply(mediaType).getOrElse("")` is safer though may be overkill?
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services