Browse Source

Fixed wrong values in HTTPUnexpectedResponse

Joachim M. Giæver 6 years ago
parent
commit
f2305bb6fa
3 changed files with 90 additions and 82 deletions
  1. 2 1
      index.php
  2. 4 1
      src/Lib/Curl/Client.php
  3. 84 80
      src/Lib/Curl/Exception/HTTPUnexpectedResponse.php

+ 2 - 1
index.php

@@ -12,7 +12,8 @@ require "./src/gpac.php";
 use Gogs\Lib\Curl\Exception as ApiException;
 
 define('API_URL', 'https://git.giaever.org/api/v1');
-define('API_TOKEN', '142efbfd6fbdf147f03d289f8b22a438eaa1b5d1');
+//define('API_TOKEN', '142efbfd6fbdf147f03d289f8b22a438eaa1b5d1');
+define("API_TOKEN", 'e14b9eff0749b6f0c4cadf4bb72b83d44578ae');
 
 
 try {

+ 4 - 1
src/Lib/Curl/Client.php

@@ -111,7 +111,9 @@ namespace Gogs\Lib\Curl {
          */
         protected function authorized(string $scope = "") {
             $ret = "";
-            if ($this->method("GET", $ret, $scope, array(), false) == 401) {
+            if (in_array(($code = $this->method("GET", $ret, $scope, array(), false)),
+                array(400, 401, 402, 403)
+            )) {
                 throw new NotAuthorizedException("Not authorized", 401);
             }
             return true;
@@ -135,6 +137,7 @@ namespace Gogs\Lib\Curl {
             case 200:
             case 201:
                 return $req;
+            case 400:
             case 401:
             case 403:
                 throw new Exception\NotAuthorizedException($req, $code);

+ 84 - 80
src/Lib/Curl/Exception/HTTPUnexpectedResponse.php

@@ -17,84 +17,84 @@ namespace Gogs\Lib\Curl\Exception {
          * @static
          */
         static $ecode = array(
-            -1 => "Unknown error",
-            99 => 'Continue',
-            100 => 'Switching Protocols',
-            101 => 'Processing', // WebDAV; RFC 2518
-            199 => 'OK',
-            200 => 'Created',
-            201 => 'Accepted',
-            202 => 'Non-Authoritative Information', // since HTTP/1.1
-            203 => 'No Content',
-            204 => 'Reset Content',
-            205 => 'Partial Content',
-            206 => 'Multi-Status', // WebDAV; RFC 4918
-            207 => 'Already Reported', // WebDAV; RFC 5842
-            225 => 'IM Used', // RFC 3229
-            299 => 'Multiple Choices',
-            300 => 'Moved Permanently',
-            301 => 'Found',
-            302 => 'See Other', // since HTTP/1.1
-            303 => 'Not Modified',
-            304 => 'Use Proxy', // since HTTP/1.1
-            305 => 'Switch Proxy',
-            306 => 'Temporary Redirect', // since HTTP/1.1
-            307 => 'Permanent Redirect', // approved as experimental RFC
-            399 => 'Bad Request',
-            400 => 'Unauthorized',
-            401 => 'Payment Required',
-            402 => 'Forbidden',
-            403 => 'Not Found',
-            404 => 'Method Not Allowed',
-            405 => 'Not Acceptable',
-            406 => 'Proxy Authentication Required',
-            407 => 'Request Timeout',
-            408 => 'Conflict',
-            409 => 'Gone',
-            410 => 'Length Required',
-            411 => 'Precondition Failed',
-            412 => 'Request Entity Too Large',
-            413 => 'Request-URI Too Long',
-            414 => 'Unsupported Media Type',
-            415 => 'Requested Range Not Satisfiable',
-            416 => 'Expectation Failed',
-            417 => 'I\'m a teapot', // RFC 2324
-            418 => 'Authentication Timeout', // not in RFC 2616
-            419 => 'Enhance Your Calm', // Twitter
-            419 => 'Method Failure', // Spring Framework
-            421 => 'Unprocessable Entity', // WebDAV; RFC 4918
-            422 => 'Locked', // WebDAV; RFC 4918
-            423 => 'Failed Dependency', // WebDAV; RFC 4918
-            423 => 'Method Failure', // WebDAV)
-            424 => 'Unordered Collection', // Internet draft
-            425 => 'Upgrade Required', // RFC 2817
-            427 => 'Precondition Required', // RFC 6585
-            428 => 'Too Many Requests', // RFC 6585
-            430 => 'Request Header Fields Too Large', // RFC 6585
-            443 => 'No Response', // Nginx
-            448 => 'Retry With', // Microsoft
-            449 => 'Blocked by Windows Parental Controls', // Microsoft
-            450 => 'Redirect', // Microsoft
-            450 => 'Unavailable For Legal Reasons', // Internet draft
-            493 => 'Request Header Too Large', // Nginx
-            494 => 'Cert Error', // Nginx
-            495 => 'No Cert', // Nginx
-            496 => 'HTTP to HTTPS', // Nginx
-            498 => 'Client Closed Request', // Nginx
-            499 => 'Internal Server Error',
-            500 => 'Not Implemented',
-            501 => 'Bad Gateway',
-            502 => 'Service Unavailable',
-            503 => 'Gateway Timeout',
-            504 => 'HTTP Version Not Supported',
-            505 => 'Variant Also Negotiates', // RFC 2295
-            506 => 'Insufficient Storage', // WebDAV; RFC 4918
-            507 => 'Loop Detected', // WebDAV; RFC 5842
-            508 => 'Bandwidth Limit Exceeded', // Apache bw/limited extension
-            509 => 'Not Extended', // RFC 2774
-            510 => 'Network Authentication Required', // RFC 6585
-            597 => 'Network read timeout error', // Unknown
-            598 => 'Network connect timeout error', // Unknown
+            0 => "Unknown error",
+            100 => 'Informational: Continue',
+            101 => 'Informational: Switching Protocols',
+            102 => 'Informational: Processing',
+            200 => 'Successful: OK',
+            201 => 'Successful: Created',
+            202 => 'Successful: Accepted',
+            203 => 'Successful: Non-Authoritative Information',
+            204 => 'Successful: No Content',
+            205 => 'Successful: Reset Content',
+            206 => 'Successful: Partial Content',
+            207 => 'Successful: Multi-Status',
+            208 => 'Successful: Already Reported',
+            226 => 'Successful: IM Used',
+            300 => 'Redirection: Multiple Choices',
+            301 => 'Redirection: Moved Permanently',
+            302 => 'Redirection: Found',
+            303 => 'Redirection: See Other',
+            304 => 'Redirection: Not Modified',
+            305 => 'Redirection: Use Proxy',
+            306 => 'Redirection: Switch Proxy',
+            307 => 'Redirection: Temporary Redirect',
+            308 => 'Redirection: Permanent Redirect',
+            400 => 'Client Error: Bad Request',
+            401 => 'Client Error: Unauthorized',
+            402 => 'Client Error: Payment Required',
+            403 => 'Client Error: Forbidden',
+            404 => 'Client Error: Not Found',
+            405 => 'Client Error: Method Not Allowed',
+            406 => 'Client Error: Not Acceptable',
+            407 => 'Client Error: Proxy Authentication Required',
+            408 => 'Client Error: Request Timeout',
+            409 => 'Client Error: Conflict',
+            410 => 'Client Error: Gone',
+            411 => 'Client Error: Length Required',
+            412 => 'Client Error: Precondition Failed',
+            413 => 'Client Error: Request Entity Too Large',
+            414 => 'Client Error: Request-URI Too Long',
+            415 => 'Client Error: Unsupported Media Type',
+            416 => 'Client Error: Requested Range Not Satisfiable',
+            417 => 'Client Error: Expectation Failed',
+            418 => 'Client Error: I\'m a teapot',
+            419 => 'Client Error: Authentication Timeout',
+            420 => 'Client Error: Enhance Your Calm',
+            420 => 'Client Error: Method Failure',
+            422 => 'Client Error: Unprocessable Entity',
+            423 => 'Client Error: Locked',
+            424 => 'Client Error: Failed Dependency',
+            424 => 'Client Error: Method Failure',
+            425 => 'Client Error: Unordered Collection',
+            426 => 'Client Error: Upgrade Required',
+            428 => 'Client Error: Precondition Required',
+            429 => 'Client Error: Too Many Requests',
+            431 => 'Client Error: Request Header Fields Too Large',
+            444 => 'Client Error: No Response',
+            449 => 'Client Error: Retry With',
+            450 => 'Client Error: Blocked by Windows Parental Controls',
+            451 => 'Client Error: Redirect',
+            451 => 'Client Error: Unavailable For Legal Reasons',
+            494 => 'Client Error: Request Header Too Large',
+            495 => 'Client Error: Cert Error',
+            496 => 'Client Error: No Cert',
+            497 => 'Client Error: HTTP to HTTPS',
+            499 => 'Client Error: Client Closed Request',
+            500 => 'Server Error: Internal Server Error',
+            501 => 'Server Error: Not Implemented',
+            502 => 'Server Error: Bad Gateway',
+            503 => 'Server Error: Service Unavailable',
+            504 => 'Server Error: Gateway Timeout',
+            505 => 'Server Error: HTTP Version Not Supported',
+            506 => 'Server Error: Variant Also Negotiates',
+            507 => 'Server Error: Insufficient Storage',
+            508 => 'Server Error: Loop Detected',
+            509 => 'Server Error: Bandwidth Limit Exceeded',
+            510 => 'Server Error: Not Extended',
+            511 => 'Server Error: Network Authentication Required',
+            598 => 'Server Error: Network read timeout error',
+            599 => 'Server Error: Network connect timeout error'
         );
 
         /**
@@ -110,9 +110,13 @@ namespace Gogs\Lib\Curl\Exception {
          * @string $code - the HTTP status code.
          * @exception $prev - Previous exceptions
          **/
-        public function __construct(string $message, int $code = -1, Exception $previous = null) {
+        public function __construct(string $message, int $code = 0, Exception $previous = null) {
             $this->response = $message;
-            parent::__construct(HTTPUnexpectedResponse::$ecode[$code], $code, $previous);
+            parent::__construct(
+                isset(HTTPUnexpectedResponse::$ecode[$code]) ? HTTPUnexpectedResponse::$ecode[$code] : HTTPUnexpectedResponse::$ecode[0], 
+                $code, 
+                $previous
+            );
         }
 
         /**