From caf85faa1c62eaea0d9a614ff9e8283e60af9d33 Mon Sep 17 00:00:00 2001 From: Juuso Lapinlampi Date: Tue, 29 Sep 2015 16:09:38 +0000 Subject: Return Content-Type: text/csv with csv upload output Previously, CSV would return the wrong filetype text/plain and fallthrough. RFC 4180 registers text/csv MIME type. RFC 7111 is closely related and says to use text/csv to launch the correct application. --- static/classes/Response.class.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/static/classes/Response.class.php b/static/classes/Response.class.php index 3afcfdb..a83cb6a 100755 --- a/static/classes/Response.class.php +++ b/static/classes/Response.class.php @@ -31,6 +31,9 @@ class Response { switch ($response_type) { case 'csv': + header('Content-Type: text/csv; charset=UTF-8'); + $this->type = $response_type; + break; case 'gyazo': header('Content-Type: text/plain; charset=UTF-8'); $this->type = $response_type; -- cgit v1.2.3 From e39a920f6d41f8d90470f15c4a58f6abebbace92 Mon Sep 17 00:00:00 2001 From: Juuso Lapinlampi Date: Tue, 29 Sep 2015 16:19:58 +0000 Subject: Deprecate Gyazo response, fallthrough to text/plain A majority of the Gyazo code is just copy-pasted text/plain protocol. This will put an end to that insanity. OK ewhal@ --- static/classes/Response.class.php | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/static/classes/Response.class.php b/static/classes/Response.class.php index a83cb6a..c8ddfa2 100755 --- a/static/classes/Response.class.php +++ b/static/classes/Response.class.php @@ -35,9 +35,8 @@ class Response $this->type = $response_type; break; case 'gyazo': - header('Content-Type: text/plain; charset=UTF-8'); - $this->type = $response_type; - break; + // Deprecated API since version 2.0.0 + // fallthrough case 'text': header('Content-Type: text/plain; charset=UTF-8'); $this->type = $response_type; @@ -61,9 +60,6 @@ class Response case 'csv': $response = $this->csv_error($desc); break; - case 'gyazo': - $response = $this->gyazo_error($code, $desc); - break; case 'text': $response = $this->text_error($code, $desc); break; @@ -90,9 +86,6 @@ class Response case 'csv': $response = $this->csv_success($files); break; - case 'gyazo': - $response = $this->gyazo_success($files); - break; case 'text': $response = $this->text_success($files); break; @@ -123,16 +116,6 @@ class Response return $result; } - private static function gyazo_error($code, $description) - { - return 'ERROR: ('.$code.') '.$description; - } - - private static function gyazo_success($files) - { - return POMF_URL.$files[0]['url']; - } - private static function text_error($code, $description) { return 'ERROR: ('.$code.') '.$description; -- cgit v1.2.3 From d35246bf1bc1b8a2aacf038ba4835dd12fcad4ed Mon Sep 17 00:00:00 2001 From: Juuso Lapinlampi Date: Tue, 29 Sep 2015 16:23:23 +0000 Subject: Improve consistency by resorting Response.send() This restores the typical sorting order: - csv - (gyazo) - text - html - default (json) --- static/classes/Response.class.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/static/classes/Response.class.php b/static/classes/Response.class.php index c8ddfa2..48d9311 100755 --- a/static/classes/Response.class.php +++ b/static/classes/Response.class.php @@ -80,15 +80,15 @@ class Response $response = null; switch ($this->type) { - case 'html': - $response = $this->html_success($files); - break; case 'csv': $response = $this->csv_success($files); break; case 'text': $response = $this->text_success($files); break; + case 'html': + $response = $this->html_success($files); + break; default: $response = $this->json_success($files); break; -- cgit v1.2.3 From 2ca5b6b743487477fdaf045a7d4abb605780a300 Mon Sep 17 00:00:00 2001 From: Juuso Lapinlampi Date: Tue, 29 Sep 2015 17:51:09 +0000 Subject: Sort upload responses alphabetically This commit improves readability by swapping text and html response source line order for alphabetical sorting. --- static/classes/Response.class.php | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/static/classes/Response.class.php b/static/classes/Response.class.php index 48d9311..25d101c 100755 --- a/static/classes/Response.class.php +++ b/static/classes/Response.class.php @@ -37,14 +37,14 @@ class Response case 'gyazo': // Deprecated API since version 2.0.0 // fallthrough - case 'text': - header('Content-Type: text/plain; charset=UTF-8'); - $this->type = $response_type; - break; case 'html': header('Content-Type: text/html; charset=UTF-8'); $this->type = $response_type; break; + case 'text': + header('Content-Type: text/plain; charset=UTF-8'); + $this->type = $response_type; + break; default: header('Content-Type: application/json; charset=UTF-8'); $this->type = 'json'; @@ -60,12 +60,12 @@ class Response case 'csv': $response = $this->csv_error($desc); break; - case 'text': - $response = $this->text_error($code, $desc); - break; case 'html': $response = $this->html_error($code, $desc); break; + case 'text': + $response = $this->text_error($code, $desc); + break; default: $response = $this->json_error($code, $desc); break; @@ -83,12 +83,12 @@ class Response case 'csv': $response = $this->csv_success($files); break; - case 'text': - $response = $this->text_success($files); - break; case 'html': $response = $this->html_success($files); break; + case 'text': + $response = $this->text_success($files); + break; default: $response = $this->json_success($files); break; @@ -116,29 +116,29 @@ class Response return $result; } - private static function text_error($code, $description) + private static function html_error($code, $description) { - return 'ERROR: ('.$code.') '.$description; + return '

ERROR: ('.$code.') '.$description.'

'; } - private static function text_success($files) + private static function html_success($files) { foreach ($files as $file) { - $result .= $file['url']."\n"; + $result .= ''.$file['url'].'
'; } return $result; } - private static function html_error($code, $description) + private static function text_error($code, $description) { - return '

ERROR: ('.$code.') '.$description.'

'; + return 'ERROR: ('.$code.') '.$description; } - private static function html_success($files) + private static function text_success($files) { foreach ($files as $file) { - $result .= ''.$file['url'].'
'; + $result .= $file['url']."\n"; } return $result; -- cgit v1.2.3 From fb23dde10af35eea3b0f0b14459630305618ac3a Mon Sep 17 00:00:00 2001 From: Juuso Lapinlampi Date: Tue, 29 Sep 2015 21:53:01 +0000 Subject: Remove default undefined behavior from Response class Let's say some user decides to use output "xml". Unexpectedly, the user would instead receive JSON output. This is undefined behavior in its own way and violates the principle of least surprise. Problems could arise if we added "xml" later with actual application/xml or text/xml content. Right now upload.php overrides an empty output argument to json instead, so this seems safe to do. Any non-existent output will now return a 400 Bad Request error. --- static/classes/Response.class.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/static/classes/Response.class.php b/static/classes/Response.class.php index 25d101c..cb34ab4 100755 --- a/static/classes/Response.class.php +++ b/static/classes/Response.class.php @@ -45,9 +45,13 @@ class Response header('Content-Type: text/plain; charset=UTF-8'); $this->type = $response_type; break; - default: + case 'json': header('Content-Type: application/json; charset=UTF-8'); - $this->type = 'json'; + $this->type = $response_type; + break; + default: + http_response_code(400); + echo 'Invalid response type. Valid options are: csv, html, text, json.'; break; } } @@ -66,7 +70,7 @@ class Response case 'text': $response = $this->text_error($code, $desc); break; - default: + case 'json': $response = $this->json_error($code, $desc); break; } @@ -89,7 +93,7 @@ class Response case 'text': $response = $this->text_success($files); break; - default: + case 'json': $response = $this->json_success($files); break; } -- cgit v1.2.3 From 6d8b20475b4d744764410178854451101468aef7 Mon Sep 17 00:00:00 2001 From: Juuso Lapinlampi Date: Tue, 29 Sep 2015 22:02:55 +0000 Subject: Sort Response code lines alphabetically again This commit improves readability. --- static/classes/Response.class.php | 47 +++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/static/classes/Response.class.php b/static/classes/Response.class.php index cb34ab4..bfc708a 100755 --- a/static/classes/Response.class.php +++ b/static/classes/Response.class.php @@ -41,14 +41,14 @@ class Response header('Content-Type: text/html; charset=UTF-8'); $this->type = $response_type; break; - case 'text': - header('Content-Type: text/plain; charset=UTF-8'); - $this->type = $response_type; - break; case 'json': header('Content-Type: application/json; charset=UTF-8'); $this->type = $response_type; break; + case 'text': + header('Content-Type: text/plain; charset=UTF-8'); + $this->type = $response_type; + break; default: http_response_code(400); echo 'Invalid response type. Valid options are: csv, html, text, json.'; @@ -67,12 +67,12 @@ class Response case 'html': $response = $this->html_error($code, $desc); break; - case 'text': - $response = $this->text_error($code, $desc); - break; case 'json': $response = $this->json_error($code, $desc); break; + case 'text': + $response = $this->text_error($code, $desc); + break; } http_response_code($code); @@ -90,12 +90,12 @@ class Response case 'html': $response = $this->html_success($files); break; - case 'text': - $response = $this->text_success($files); - break; case 'json': $response = $this->json_success($files); break; + case 'text': + $response = $this->text_success($files); + break; } http_response_code(200); @@ -134,20 +134,6 @@ class Response return $result; } - private static function text_error($code, $description) - { - return 'ERROR: ('.$code.') '.$description; - } - - private static function text_success($files) - { - foreach ($files as $file) { - $result .= $file['url']."\n"; - } - - return $result; - } - private static function json_error($code, $description) { return json_encode(array( @@ -164,4 +150,17 @@ class Response 'files' => $files, )); } + private static function text_error($code, $description) + { + return 'ERROR: ('.$code.') '.$description; + } + + private static function text_success($files) + { + foreach ($files as $file) { + $result .= $file['url']."\n"; + } + + return $result; + } } -- cgit v1.2.3 From 7596cf0620ebe8f84360ed30ec0376763e92a612 Mon Sep 17 00:00:00 2001 From: Juuso Lapinlampi Date: Tue, 29 Sep 2015 22:07:41 +0000 Subject: Sort Response default case options alphabetically This commit improves readability and compliments commit 6d8b204. --- static/classes/Response.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/static/classes/Response.class.php b/static/classes/Response.class.php index bfc708a..fe25664 100755 --- a/static/classes/Response.class.php +++ b/static/classes/Response.class.php @@ -51,7 +51,7 @@ class Response break; default: http_response_code(400); - echo 'Invalid response type. Valid options are: csv, html, text, json.'; + echo 'Invalid response type. Valid options are: csv, html, json, text.'; break; } } -- cgit v1.2.3 From daac77a1962d8f256b3e7c5a0b46c10c833f688a Mon Sep 17 00:00:00 2001 From: Juuso Lapinlampi Date: Sun, 4 Oct 2015 12:36:39 +0000 Subject: Fix Gyazo response falling through back to text again This fixes a silly mistake where Gyazo would accidentally use the HTML response instead of text as intended. This bug was introduced in commit 2ca5b6b. --- static/classes/Response.class.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/static/classes/Response.class.php b/static/classes/Response.class.php index fe25664..02b92db 100755 --- a/static/classes/Response.class.php +++ b/static/classes/Response.class.php @@ -34,9 +34,6 @@ class Response header('Content-Type: text/csv; charset=UTF-8'); $this->type = $response_type; break; - case 'gyazo': - // Deprecated API since version 2.0.0 - // fallthrough case 'html': header('Content-Type: text/html; charset=UTF-8'); $this->type = $response_type; @@ -45,6 +42,9 @@ class Response header('Content-Type: application/json; charset=UTF-8'); $this->type = $response_type; break; + case 'gyazo': + // Deprecated API since version 2.0.0 + // fallthrough case 'text': header('Content-Type: text/plain; charset=UTF-8'); $this->type = $response_type; -- cgit v1.2.3 From cc464ee39baaae2b12eda71739a0e7214dcf3854 Mon Sep 17 00:00:00 2001 From: Juuso Lapinlampi Date: Sun, 4 Oct 2015 15:32:15 +0000 Subject: Use CRLF linebreaks for CSV responses RFC 4180 specifies that CSV files should use CRLF (a carriage-return and line-feed pair, e.g. \r\n). --- static/classes/Response.class.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/static/classes/Response.class.php b/static/classes/Response.class.php index 02b92db..e44652a 100755 --- a/static/classes/Response.class.php +++ b/static/classes/Response.class.php @@ -104,17 +104,17 @@ class Response private static function csv_error($description) { - return "error\n".$description."\n"; + return "error\r\n".$description."\r\n"; } private static function csv_success($files) { - $result = "name,url,hash,size\n"; + $result = "name,url,hash,size\r\n"; foreach ($files as $file) { $result .= $file['name'].','. $file['url'].','. $file['hash'].','. - $file['size']."\n"; + $file['size']."\r\n"; } return $result; -- cgit v1.2.3 From 363f107191c2a2042a38b0f36026eb21282e6044 Mon Sep 17 00:00:00 2001 From: Juuso Lapinlampi Date: Sun, 4 Oct 2015 15:54:59 +0000 Subject: Wrap CSV response values in double quotes (") While double quotes are mostly optional in implementations, some CSV linters want double quotes for whatever the reason. Assumably, it reduces the amount of bugs and avoids possible parsing errors. This also has very little relevance for not trusting the user input, but better be safe than sorry. This commit is not very pretty. In a future commit, this issue will be addressed with arrays and fputcsv(). --- static/classes/Response.class.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/static/classes/Response.class.php b/static/classes/Response.class.php index e44652a..4e28678 100755 --- a/static/classes/Response.class.php +++ b/static/classes/Response.class.php @@ -104,17 +104,17 @@ class Response private static function csv_error($description) { - return "error\r\n".$description."\r\n"; + return '"error"'."\r\n"."\"$description\""."\r\n"; } private static function csv_success($files) { - $result = "name,url,hash,size\r\n"; + $result = '"name","url","hash","size"'."\r\n"; foreach ($files as $file) { - $result .= $file['name'].','. - $file['url'].','. - $file['hash'].','. - $file['size']."\r\n"; + $result .= '"'.$file['name'].'"'.','. + '"'.$file['url'].'"'.','. + '"'.$file['hash'].'"'.','. + '"'.$file['size'].'"'."\r\n"; } return $result; -- cgit v1.2.3 From 17dd0a062cc8d2b8075f3f5e18da7246e8c8885a Mon Sep 17 00:00:00 2001 From: Juuso Lapinlampi Date: Sun, 4 Oct 2015 16:55:46 +0000 Subject: Fix empty Gyazo response with more fallthroughs Gyazo didn't fallthrough correctly to text, because the type would be set to Gyazo while the Gyazo case didn't exist. PHP is so ambiguous sometimes. This adds the missing fallthroughs that were removed in commit e39a920. It should now correctly fallthrough ("fallback") to text response. --- static/classes/Response.class.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/static/classes/Response.class.php b/static/classes/Response.class.php index 02b92db..7c8bd22 100755 --- a/static/classes/Response.class.php +++ b/static/classes/Response.class.php @@ -70,6 +70,9 @@ class Response case 'json': $response = $this->json_error($code, $desc); break; + case 'gyazo': + // Deprecated API since version 2.0.0 + // fallthrough case 'text': $response = $this->text_error($code, $desc); break; @@ -93,6 +96,9 @@ class Response case 'json': $response = $this->json_success($files); break; + case 'gyazo': + // Deprecated API since version 2.0.0 + // fallthrough case 'text': $response = $this->text_success($files); break; -- cgit v1.2.3 From 760b1dda71e706a9014b85d34025874e9ef7bbeb Mon Sep 17 00:00:00 2001 From: Juuso Lapinlampi Date: Sun, 4 Oct 2015 17:06:23 +0000 Subject: Clarify Gyazo deprecation warning to use text response It is a little unclear what the deprecation warning wants the developer to do. The fallthrough comment to text response may not be immediately obvious to the reader. This commit helps to understand the intention and to avoid bugs such as commit daac77a. --- static/classes/Response.class.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/static/classes/Response.class.php b/static/classes/Response.class.php index 7c8bd22..cd9229b 100755 --- a/static/classes/Response.class.php +++ b/static/classes/Response.class.php @@ -43,7 +43,7 @@ class Response $this->type = $response_type; break; case 'gyazo': - // Deprecated API since version 2.0.0 + // Deprecated API since version 2.0.0, use text response instead // fallthrough case 'text': header('Content-Type: text/plain; charset=UTF-8'); @@ -71,7 +71,7 @@ class Response $response = $this->json_error($code, $desc); break; case 'gyazo': - // Deprecated API since version 2.0.0 + // Deprecated API since version 2.0.0, use text response instead // fallthrough case 'text': $response = $this->text_error($code, $desc); @@ -97,7 +97,7 @@ class Response $response = $this->json_success($files); break; case 'gyazo': - // Deprecated API since version 2.0.0 + // Deprecated API since version 2.0.0, use text response instead // fallthrough case 'text': $response = $this->text_success($files); -- cgit v1.2.3 From f6579d0716fc05901d95d4247e0f5c0364ab3d38 Mon Sep 17 00:00:00 2001 From: Juuso Lapinlampi Date: Sun, 4 Oct 2015 19:46:24 +0000 Subject: JSON-ify the default invalid response type error Code re-use is good. The previous implementation doesn't reuse existing text_error() functions and also forgoes using text/plain header with the text implementation. This fixes consistency where JSON is assumed to be the default output. It is a common practice among APIs to default all errors to JSON, such as the Stripe and Tumblr APIs. --- static/classes/Response.class.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/static/classes/Response.class.php b/static/classes/Response.class.php index cd9229b..a4e19b5 100755 --- a/static/classes/Response.class.php +++ b/static/classes/Response.class.php @@ -50,8 +50,9 @@ class Response $this->type = $response_type; break; default: - http_response_code(400); - echo 'Invalid response type. Valid options are: csv, html, json, text.'; + header('Content-Type: application/json; charset=UTF-8'); + $this->type = 'json'; + $this->error(400, 'Invalid response type. Valid options are: csv, html, json, text.'); break; } } -- cgit v1.2.3 From b94ac9a9bc957623687bf3cd21548569b96d4a01 Mon Sep 17 00:00:00 2001 From: Juuso Lapinlampi Date: Wed, 6 Jan 2016 13:23:26 +0000 Subject: Fix gyazo response fallthrough to text response The gyazo response would return an empty response, because the construct didn't actually fallthrough to text properly. Force gyazo's response type to be 'text' to fix the issue. Accordingly, the 'gyazo' cases for send() and error() now become dead code. Remove them. OK ewhal@ --- static/classes/Response.class.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/static/classes/Response.class.php b/static/classes/Response.class.php index 3518133..81e7fb4 100755 --- a/static/classes/Response.class.php +++ b/static/classes/Response.class.php @@ -43,8 +43,10 @@ class Response $this->type = $response_type; break; case 'gyazo': - // Deprecated API since version 2.0.0, use text response instead - // fallthrough + // Deprecated API since version 2.0.0, fallback to similar text API + header('Content-Type: text/plain; charset=UTF-8'); + $this->type = 'text'; + break; case 'text': header('Content-Type: text/plain; charset=UTF-8'); $this->type = $response_type; @@ -71,9 +73,6 @@ class Response case 'json': $response = $this->json_error($code, $desc); break; - case 'gyazo': - // Deprecated API since version 2.0.0, use text response instead - // fallthrough case 'text': $response = $this->text_error($code, $desc); break; @@ -97,9 +96,6 @@ class Response case 'json': $response = $this->json_success($files); break; - case 'gyazo': - // Deprecated API since version 2.0.0, use text response instead - // fallthrough case 'text': $response = $this->text_success($files); break; -- cgit v1.2.3