package Ġcom . hex agon kt . http . server Ċ Ċ import Ġcom . hex agon kt . http . Method Ċ Ċ private Ġconst Ġval ĠALLOW _ ORIGIN Ġ= Ġ" Access - Control - Allow - Origin " Ċ private Ġconst Ġval ĠALLOW _ CRED ENTIAL S Ġ= Ġ" Access - Control - Allow - Credentials " Ċ private Ġconst Ġval ĠREQUEST _ METHOD Ġ= Ġ" Access - Control - Request - Method " Ċ private Ġconst Ġval ĠEX POSE _ HEADERS Ġ= Ġ" Access - Control - Expose - Headers " Ċ private Ġconst Ġval ĠREQUEST _ HEADERS Ġ= Ġ" Access - Control - Request - Headers " Ċ private Ġconst Ġval ĠALLOW _ HEADERS Ġ= Ġ" Access - Control - Allow - Headers " Ċ private Ġconst Ġval ĠMAX _ AGE Ġ= Ġ" Access - Control - Max - Age " Ċ Ċ internal Ġfun ĠCall . simple Request ( settings : ĠC ors Settings ) Ġ{ ĊĠĠĠ Ġval Ġorigin Ġ= Ġrequest . origin Ġ?: Ġreturn ĊĠĠĠ Ġif Ġ(! settings . allow Origin ( origin )) ĊĠĠĠĠĠĠĠ Ġhalt ( 4 0 3 , Ġ" Not Ġallowed Ġorigin : Ġ$ origin ") ĊĊĠĠĠ Ġval Ġaccess Control Allow Origin Ġ= Ġsettings . access Control Allow Origin ( origin ) ĊĠĠĠ Ġresponse . headers [ ALLOW _ ORIGIN ] Ġ= Ġaccess Control Allow Origin ĊĠĠĠ Ġif Ġ( access Control Allow Origin Ġ!= Ġ"* ") ĊĠĠĠĠĠĠĠ Ġresponse . headers [" V ary "] Ġ= Ġ" Origin " ĊĊĠĠĠ Ġif Ġ( settings . support Credentials ) ĊĠĠĠĠĠĠĠ Ġresponse . headers [ ALLOW _ CRED ENTIAL S ] Ġ= Ġtrue ĊĊĠĠĠ Ġval Ġaccess Control Request Method Ġ= Ġrequest . headers [ REQUEST _ METHOD ] ĊĠĠĠ Ġif Ġ( request . method Ġ== ĠMethod . OPTIONS Ġ&& Ġaccess Control Request Method Ġ!= Ġnull ) ĊĠĠĠĠĠĠĠ Ġreturn ĊĊĠĠĠ Ġif Ġ( request . method Ġ! in Ġsettings . allowed Methods ) ĊĠĠĠĠĠĠĠ Ġhalt ( 4 0 3 , Ġ" Not Ġallowed Ġmethod : Ġ${ request . method }") ĊĊĠĠĠ Ġif Ġ( settings . ex posed Headers . isNotEmpty ()) Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġrequest Header Names Ġ= Ġrequest . headers Values . keys . toSet () ĊĠĠĠĠĠĠĠ Ġval Ġrequest Headers Ġ= Ġrequest Header Names . filter Ġ{ Ġit Ġin Ġsettings . ex posed Headers Ġ} ĊĊĠĠĠĠĠĠĠ Ġresponse . headers [ EXPOSE _ HEADERS ] Ġ= Ġrequest Headers . join ToString (",") ĊĠĠĠ Ġ} Ċ } Ċ Ċ internal Ġfun ĠCall . pre Flight Request ( settings : ĠC ors Settings ) Ġ{ ĊĊĠĠĠ Ġval Ġmethod Header Ġ= Ġrequest . headers [ REQUEST _ METHOD ] ĊĠĠĠ Ġval Ġrequest Method Ġ= Ġmethod Header ĊĠĠĠĠĠĠĠ Ġ?: Ġhalt ( 4 0 3 , Ġ" Access - Control - Request - Method Ġrequired Ġheader Ġnot Ġfound ") ĊĊĠĠĠ Ġval Ġmethod Ġ= ĠMethod . valueOf ( request Method ) ĊĠĠĠ Ġif Ġ( method Ġ! in Ġsettings . allowed Methods ) ĊĠĠĠĠĠĠĠ Ġhalt ( 4 0 3 , Ġ" Not Ġallowed Ġmethod : Ġ$ method ") ĊĊĠĠĠ Ġval Ġaccess Control Request Headers Ġ= Ġrequest . headers Values [ REQUEST _ HEADERS ] ĊĠĠĠĠĠĠĠ Ġ? . first OrNull () ĊĊĠĠĠ Ġif Ġ( access Control Request Headers Ġ!= Ġnull ) Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġallowed Headers Ġ= Ġaccess Control Request Headers ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. split (",") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. map Ġ{ Ġit . trim () Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. all Ġ{ Ġit Ġin Ġsettings . allowed Headers Ġ} ĊĊĠĠĠĠĠĠĠ Ġif Ġ(! allowed Headers Ġ&& Ġsettings . allowed Headers . isNotEmpty ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġhalt ( 4 0 3 , Ġ" Not Ġallowed Ġheaders ") ĊĊĠĠĠĠĠĠĠ Ġval Ġheaders Ġ= Ġsettings . allowed Headers ĊĠĠĠĠĠĠĠ Ġval Ġrequest Headers Ġ= Ġif Ġ( headers . isEmpty ()) Ġrequest . headers Values . keys . toSet () Ġelse Ġheaders ĊĠĠĠĠĠĠĠ Ġresponse . headers [ ALLOW _ HEADERS ] Ġ= Ġrequest Headers . join ToString (",") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġresponse . headers [ REQUEST _ METHOD ] Ġ= Ġsettings . allowed Methods . join ToString (",") ĊĊĠĠĠ Ġif Ġ( settings . pre Flight Max Age Ġ> Ġ 0 ) ĊĠĠĠĠĠĠĠ Ġresponse . headers [ MAX _ AGE ] Ġ= Ġsettings . pre Flight Max Age ĊĊĠĠĠ Ġresponse . status Ġ= Ġsettings . pre Flight Status Ċ } Ċ