<?php
$lane=getenv('NR_LANE');$port=$lane==='B'?18933:18934;$r=json_decode(file_get_contents(__DIR__.'/private/request.json'),true);$file=__DIR__.'/private/http-attempts-retry1.json';if(is_file($file))throw new RuntimeException('No lists HTTP replay');$attempts=[];$save=static function()use(&$attempts,$file){file_put_contents($file,json_encode($attempts,JSON_PRETTY_PRINT));chmod($file,0600);};
foreach($r['cases']as $c){$attempts[]=['label'=>$c['label'],'path'=>$c['path'],'params'=>$c['params'],'complete'=>false];$i=count($attempts)-1;$save();$ctx=stream_context_create(['http'=>['method'=>'POST','header'=>"Content-Type: application/x-www-form-urlencoded\r\nToken: ".$r['token']."\r\nAccept: application/json, text/plain, */*\r\nCache-Control: max-age=28880\r\nX-CDX-Case: ".$c['label']."\r\n",'content'=>http_build_query($c['params']),'ignore_errors'=>true,'follow_location'=>0,'timeout'=>30]]);$body=file_get_contents('http://127.0.0.1:'.$port.$c['path'],false,$ctx);$attempts[$i]['complete']=$body!==false;$attempts[$i]['headers']=$http_response_header??[];$attempts[$i]['body_base64']=$body===false?null:base64_encode($body);$save();if($body===false||!is_file(__DIR__.'/private/wire-'.$c['label'].'.json'))throw new RuntimeException('No blind retry');$w=json_decode(file_get_contents(__DIR__.'/private/wire-'.$c['label'].'.json'),true);if(!$w['own_source_unchanged'])throw new RuntimeException('Own source changed');}
echo json_encode(['lane'=>$lane,'HTTP'=>count($attempts),'complete'=>count(array_filter($attempts,static function($a){return $a['complete'];}))]),"\n";
