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 c855e66690b8b91ab2cbe302d484fcc8e386da72..e92077779c8abce3eb16d17d13649b1d26a53bec 100644 (file)
@@ -158,9 +158,9 @@ class Command_Connect
             Log::err($res->getBody());
             exit(2);
         }
             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);
             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');
             $tokenParams = json_decode($res->getBody(), true);
         } else {
             Log::err('Wrong content type in auth verification response');
index eb66692a9c9b06f8231b738e1cc18bc8af5ab88c..8d76e77022fdd659e91087abb91a4b48a205f72d 100644 (file)
@@ -29,7 +29,7 @@ class Command_Targets
         $req->req->getUrl()->setQueryVariable('q', 'syndicate-to');
         $res = $req->send();
 
         $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);
         }
             Log::err('response data are not of type application/json');
             exit(2);
         }
index 3d7221dc4cd972b4e0bb00cee103a534e76f4ed6..dce163f42a2c51f470cb9fe13000b262f4cec9fe 100644 (file)
@@ -108,7 +108,7 @@ class Request
         );
 
         $shown = false;
         );
 
         $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');
             $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