From a35b58eb4741ce43b8582b69d7addd71b71041bf Mon Sep 17 00:00:00 2001 From: shibafu Date: Thu, 14 Mar 2019 00:43:31 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=E6=94=B9=E8=A1=8C=E3=82=B3=E3=83=BC?= =?UTF-8?q?=E3=83=89=E3=82=92=E6=AD=A3=E8=A6=8F=E5=8C=96=E3=81=99=E3=82=8B?= =?UTF-8?q?=E5=87=A6=E7=90=86=E3=82=92=E3=83=9F=E3=83=89=E3=83=AB=E3=82=A6?= =?UTF-8?q?=E3=82=A7=E3=82=A2=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Kernel.php | 1 + app/Http/Middleware/NormalizeLineEnding.php | 30 +++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 app/Http/Middleware/NormalizeLineEnding.php diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 93bf68b..95714cf 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -35,6 +35,7 @@ class Kernel extends HttpKernel \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, + \App\Http\Middleware\NormalizeLineEnding::class, ], 'api' => [ diff --git a/app/Http/Middleware/NormalizeLineEnding.php b/app/Http/Middleware/NormalizeLineEnding.php new file mode 100644 index 0000000..e0ec87e --- /dev/null +++ b/app/Http/Middleware/NormalizeLineEnding.php @@ -0,0 +1,30 @@ +input() as $key => $value) { + $newInput[$key] = str_replace(["\r\n", "\r"], "\n", $value); + } + $request->replace($newInput); + + return $next($request); + } +} From df3826a6d47c1a3a5bd44c25a1ac339d76342a0e Mon Sep 17 00:00:00 2001 From: shibafu Date: Thu, 14 Mar 2019 00:47:52 +0900 Subject: [PATCH 2/3] =?UTF-8?q?=E3=82=B3=E3=83=B3=E3=83=88=E3=83=AD?= =?UTF-8?q?=E3=83=BC=E3=83=A9=E5=86=85=E3=81=A7=E5=AE=9F=E8=A3=85=E3=81=95?= =?UTF-8?q?=E3=82=8C=E3=81=A6=E3=81=84=E3=81=9F=E6=94=B9=E8=A1=8C=E3=82=B3?= =?UTF-8?q?=E3=83=BC=E3=83=89=E3=81=AE=E6=AD=A3=E8=A6=8F=E5=8C=96=E3=82=92?= =?UTF-8?q?=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/EjaculationController.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/app/Http/Controllers/EjaculationController.php b/app/Http/Controllers/EjaculationController.php index 4490677..d2292e0 100644 --- a/app/Http/Controllers/EjaculationController.php +++ b/app/Http/Controllers/EjaculationController.php @@ -30,9 +30,6 @@ class EjaculationController extends Controller public function store(Request $request) { $inputs = $request->all(); - if ($request->has('note')) { - $inputs['note'] = str_replace(["\r\n", "\r"], "\n", $inputs['note']); - } $validator = Validator::make($inputs, [ 'date' => 'required|date_format:Y/m/d', @@ -113,9 +110,6 @@ class EjaculationController extends Controller $ejaculation = Ejaculation::findOrFail($id); $inputs = $request->all(); - if ($request->has('note')) { - $inputs['note'] = str_replace(["\r\n", "\r"], "\n", $inputs['note']); - } $validator = Validator::make($inputs, [ 'date' => 'required|date_format:Y/m/d', From 9e786a54694398d96331bd98c61014f07decd4bb Mon Sep 17 00:00:00 2001 From: shibafu Date: Thu, 14 Mar 2019 00:56:17 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Middleware/NormalizeLineEndingTest.php | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 tests/Unit/Http/Middleware/NormalizeLineEndingTest.php diff --git a/tests/Unit/Http/Middleware/NormalizeLineEndingTest.php b/tests/Unit/Http/Middleware/NormalizeLineEndingTest.php new file mode 100644 index 0000000..7d16f0b --- /dev/null +++ b/tests/Unit/Http/Middleware/NormalizeLineEndingTest.php @@ -0,0 +1,80 @@ +replace([ + 'test' => "foo\r\nbar" + ]); + + $middleware = new NormalizeLineEnding(); + + $middleware->handle($request, function (Request $request) { + $this->assertEquals("foo\nbar", $request->input('test')); + }); + } + + public function testCRtoLF() + { + $request = Request::create('/'); + $request->replace([ + 'test' => "foo\rbar" + ]); + + $middleware = new NormalizeLineEnding(); + + $middleware->handle($request, function (Request $request) { + $this->assertEquals("foo\nbar", $request->input('test')); + }); + } + + public function testLFtoLF() + { + $request = Request::create('/'); + $request->replace([ + 'test' => "foo\nbar" + ]); + + $middleware = new NormalizeLineEnding(); + + $middleware->handle($request, function (Request $request) { + $this->assertEquals("foo\nbar", $request->input('test')); + }); + } + + public function testArrayRequest() + { + $request = Request::create('/'); + $request->replace([ + 'test' => "foo\r\nbar", + 'hash' => [ + 'yuzuki' => "yuzuki\r\nyukari", + 'miku' => "hatsune\r\nmiku", + ], + 'array' => [ + "kagamine\r\nrin", + "kagamine\r\nlen" + ] + ]); + + $middleware = new NormalizeLineEnding(); + + $middleware->handle($request, function (Request $request) { + $this->assertEquals("foo\nbar", $request->input('test')); + $this->assertEquals("yuzuki\nyukari", $request->input('hash.yuzuki')); + $this->assertEquals("hatsune\nmiku", $request->input('hash.miku')); + $this->assertEquals("kagamine\nrin", $request->input('array.0')); + $this->assertEquals("kagamine\nlen", $request->input('array.1')); + }); + } +}