Split off content type comments
authorChristian Weiske <cweiske@cweiske.de>
Sun, 27 Aug 2017 20:39:01 +0000 (22:39 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Mon, 28 Aug 2017 04:07:40 +0000 (06:07 +0200)
Resolves: https://github.com/cweiske/shpub/pull/6

src/shpub/Command/Connect.php
src/shpub/Command/Targets.php
src/shpub/Request.php
src/shpub/Util.php [new file with mode: 0644]

index c855e66..e920777 100644 (file)
@@ -158,9 +158,9 @@ class Command_Connect
             Log::err($res->getBody());
             exit(2);
         }
-        if ($res->getHeader('content-type') == 'application/x-www-form-urlencoded') {
+        if (Util::getMimeType($res) == 'application/x-www-form-urlencoded') {
             parse_str($res->getBody(), $tokenParams);
-        } elseif ($res->getHeader('content-type') == 'application/json') {
+        } elseif (Util::getMimeType($res) == 'application/json') {
             $tokenParams = json_decode($res->getBody(), true);
         } else {
             Log::err('Wrong content type in auth verification response');
index eb66692..8d76e77 100644 (file)
@@ -29,7 +29,7 @@ class Command_Targets
         $req->req->getUrl()->setQueryVariable('q', 'syndicate-to');
         $res = $req->send();
 
-        if ($res->getHeader('content-type') != 'application/json') {
+        if (Util::getMimeType($res) != 'application/json') {
             Log::err('response data are not of type application/json');
             exit(2);
         }
index 3d7221d..dce163f 100644 (file)
@@ -108,7 +108,7 @@ class Request
         );
 
         $shown = false;
-        if ($res->getHeader('content-type') == 'application/json') {
+        if (Util::getMimeType($res) == 'application/json') {
             $errData = json_decode($res->getBody());
             if (!isset($errData->error)) {
                 Log::err('Error response does not contain "error" property');
diff --git a/src/shpub/Util.php b/src/shpub/Util.php
new file mode 100644 (file)
index 0000000..d0df03f
--- /dev/null
@@ -0,0 +1,18 @@
+<?php
+namespace shpub;
+
+class Util
+{
+    /**
+     * Get the MIME content type from a HTTP response
+     *
+     * @param object $res HTTP response
+     *
+     * @return string MIME type without comments
+     */
+    public static function getMimeType(\HTTP_Request2_Response $res)
+    {
+        list($type, ) = explode(';', $res->getHeader('content-type'));
+        return trim($type);
+    }
+}
\ No newline at end of file