jeudi 25 avril 2019

net::ERR_EMPTY_RESPONSE while ajax - Laravel 5.8

I have a laravel 5.8 site, I have these

routes

Route::get('/', function () {return view('welcome'); });
Route::get('fingerprinting','FingerPrintingController@init');
Route::post('fingerprinting/store','FingerPrintingController@store');
Route::get('fingerprinting/tasks','FingerPrintingController@tasks');

view

<!DOCTYPE html>
<html lang="en">

<head>
    <title>pass-through</title>
    <meta name="csrf-token" content="">
</head>

<body>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <script type="text/javascript">

        var model            = getIPhoneModel()
        var width            = window.screen.width
        var height           = window.screen.height
        var devicePixelRatio = window.devicePixelRatio

        function getIPhoneModel() {
            // Create a canvas element which can be used to retrieve information about the GPU.
            var canvas = document.createElement("canvas");
            if (canvas) {
                var context = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
                if (context) {
                    var info = context.getExtension("WEBGL_debug_renderer_info");
                    if (info) {
                        var renderer = context.getParameter(info.UNMASKED_RENDERER_WEBGL);
                    }
                }
            }

            // iPhone X
            if ((height / width == 812 / 375) && (devicePixelRatio == 3)) {
                return "iPhone X";
            } else if ((height / width == 896 / 414) && (devicePixelRatio == 3)) {
                return "iPhone XS Max";
            } else if ((height / width == 896 / 414) && (devicePixelRatio == 2)) {
                return "iPhone XR";
            } else if ((height / width == 1024 / 768) && (devicePixelRatio == 2)) {
                return "iPad 4";
            }
            else if ((height / width == 736 / 414) && (devicePixelRatio == 3)) {
                switch (renderer) {
                    default:
                    return "iPhone 6 Plus, 6s Plus, 7 Plus or 8 Plus";
                    case "Apple A8 GPU":
                    return "iPhone 6 Plus";
                    case "Apple A9 GPU":
                    return "iPhone 6s Plus";
                    case "Apple A10 GPU":
                    return "iPhone 7 Plus";
                    case "Apple A11 GPU":
                    return "iPhone 8 Plus";
                }
                // iPhone 6+/6s+/7+ and 8+ in zoom mode
            } else if ((height / width == 667 / 375) && (devicePixelRatio == 3)) {
                switch(renderer) {
                    default:
                    return "iPhone 6 Plus, 6s Plus, 7 Plus or 8 Plus (display zoom)";
                    case "Apple A8 GPU":
                    return "iPhone 6 Plus (display zoom)";
                    case "Apple A9 GPU":
                    return "iPhone 6s Plus (display zoom)";
                    case "Apple A10 GPU":
                    return "iPhone 7 Plus (display zoom)";
                    case "Apple A11 GPU":
                    return "iPhone 8 Plus (display zoom)";
                }
                // iPhone 6/6s/7 and 8
            } else if ((height / width == 667 / 375) && (devicePixelRatio == 2)) {
                switch(renderer) {
                    default:
                    return "iPhone 6, 6s, 7 or 8";
                    case "Apple A8 GPU":
                    return "iPhone 6";
                    case "Apple A9 GPU":
                    return "iPhone 6s";
                    case "Apple A10 GPU":
                    return "iPhone 7";
                    case "Apple A11 GPU":
                    return "iPhone 8";
                }
                // iPhone 5/5C/5s/SE or 6/6s/7 and 8 in zoom mode
            } else if ((height / width == 1.775) && (devicePixelRatio == 2)) {
                switch(renderer) {
                    default:
                    return "iPhone 5, 5C, 5S, SE or 6, 6s, 7 and 8 (display zoom)";
                    case "PowerVR SGX 543":
                    return "iPhone 5 or 5c";
                    case "Apple A7 GPU":
                    return "iPhone 5s";
                    case "Apple A8 GPU":
                    return "iPhone 6 (display zoom)";
                    case "Apple A9 GPU":
                    return "iPhone SE or 6s (display zoom)";
                    case "Apple A10 GPU":
                    return "iPhone 7 (display zoom)";
                    case "Apple A11 GPU":
                    return "iPhone 8 (display zoom)";
                }
                // iPhone 4/4s
            } else if ((height / width == 1.5) && (devicePixelRatio == 2)) {
                switch(renderer) {
                    default:
                    return "iPhone 4 or 4s";
                    case "PowerVR SGX 535":
                    return "iPhone 4";
                    case "PowerVR SGX 543":
                    return "iPhone 4s";
                }
                // iPhone 1/3G/3GS
            } else if ((height / width == 1.5) && (devicePixelRatio == 1)) {
                switch(renderer) {
                    default:
                    return "iPhone 1, 3G or 3GS";
                    case "ALP0298C05":
                    return "iPhone 3GS";
                    case "S5L8900":
                    return "iPhone 1, 3G";
                }
            } else {
                return "Not an iPhone";
            }
        }


        console.log(model,width,height,devicePixelRatio);

        var currentUrl = window.location.href;
        var newUrl = currentUrl.replace("fingerprinting", "fingerprinting/tasks");

        // alert(newUrl);

        $.ajax({
            method: 'POST', // Type of response and matches what we said in the route
            url: "fingerprinting/store",
            data: {'cpe_mac' : '{!! $cpe_mac !!}', 'device_mac' : '{!! $device_mac !!}','original_uri':'{!! $original_uri !!}', 'model' : model, 'ip': '{!! $ip !!}', 'port': '{!! $port !!}', 'vlan': '{!! $vlan !!}'   }, // a JSON object to send back
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            success: function(response){ // What to do if we succeed
                console.log(response);
                window.location.href = newUrl;
            },
            error: function(jqXHR, textStatus, errorThrown) { // What to do if we fail
                console.log(JSON.stringify(jqXHR));
                console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
            }
        });


    </script>

</body>

</html>


Controller

<?php
namespace App\Http\Controllers;

use Redirect, Response;
use Illuminate\Support\Facades\Input;
use App\Fingerprinting;
use App\CURL;

class FingerPrintingController extends Controller {


    public function init() {

        if(Input::get('key') != 'NDN2M3ItYmVudS1uZXR3b3Jrcy01NUMtcDBydDQh') {
            return Redirect::to('/')->with('error', 'Key is not match');
        }

        $inputs       = Input::all();

        $ip           = $inputs['ip'];
        $port         = $inputs['port'];
        $vlan         = $inputs['vlan'];
        $cpe_mac      = $inputs['cpe_mac'];
        $device_mac   = $inputs['device_mac'];
        $original_uri = $inputs['original_uri'];

        $APP_URL = env('APP_URL');

        return view('iphone.index', get_defined_vars());
    }

    public function store() {

        $inputs       = Input::all();
        $ip           = $inputs['ip'];
        $port         = $inputs['port'];
        $vlan         = $inputs['vlan'];
        $cpe_mac      = $inputs['cpe_mac'];
        $device_mac   = $inputs['device_mac'];
        $original_uri = $inputs['original_uri'];
        $model        = $inputs['model'];

        $fingerprinting = Fingerprinting::where('device_mac',$device_mac)->where('cpe_mac',$cpe_mac)->first();

        if($fingerprinting == null) {
            $fingerprinting = new Fingerprinting;
        }

        $fingerprinting->ip           = $ip;
        $fingerprinting->port         = $port;
        $fingerprinting->vlan         = $vlan;
        $fingerprinting->cpe_mac      = $cpe_mac;
        $fingerprinting->device_mac   = $device_mac;
        $fingerprinting->model        = $model;
        $fingerprinting->original_uri = $original_uri;
        $fingerprinting->save();

        return Response::json($fingerprinting);

    }

    public function tasks() {

        $inputs       = Input::all();
        $ip           = $inputs['ip'];
        $port         = $inputs['port'];
        $vlan         = $inputs['vlan'];
        $cpe_mac      = $inputs['cpe_mac'];
        $device_mac   = $inputs['device_mac'];
        $original_uri = $inputs['original_uri'];

        $fingerprinting = Fingerprinting::where('device_mac',$device_mac)->where('cpe_mac',$cpe_mac)->first();
        $original_url   = $fingerprinting->original_uri;

        //Make a PUT
        $data                 = [];
        $data['device_model'] = $fingerprinting->model;
        $data                 = json_encode($data);
        $url                  = 'http://'.$ip.':'.$port.'/vrg/vcpe/'.$cpe_mac.'/vlan/'.$vlan.'/device/'.$device_mac.'/fingerprint_update';
        $result               = CURL::post($url,$data);

        return Redirect::to(urldecode($original_url));
    }

}


Test

I go to this url in my browser

http://localhost:8006/fingerprinting?cpe_mac=B4B6761454F6&device_mac=AABBCCCDDEEFF&original_uri=https%3A%2F%2Fwww.apple.com%2F&ip=172.19.242.48&port=1234&vlan=856&key=NDN2M3ItYmVudS1uZXR3b3Jrcy01NUMtcDBydDQh

I kept getting

Provisional headers are shown
Access-Control-Request-Headers: x-csrf-token
Access-Control-Request-Method: POST
DNT: 1
Origin: http://localhost:8006
Referer: http://localhost:8006/fingerprinting?cpe_mac=B4B6761454F6&device_mac=AABBCCCDDEEFF&original_uri=https%3A%2F%2Fwww.apple.com%2F&ip=172.19.242.48&port=1234&vlan=856&key=NDN2M3ItYmVudS1uZXR3b3Jrcy01NUMtcDBydDQh
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36

In console, I see this

How do I prevent this issue ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire