11using System ;
2+ using System . Collections . Generic ;
23using System . Linq ;
34using System . Threading . Tasks ;
45using BTCPayServer . Abstractions . Constants ;
@@ -45,6 +46,7 @@ public async Task<IActionResult> GetServerConfig()
4546
4647 SmartContractAddress = serverSettings ? . SmartContractAddress ,
4748 JsonRpcUri = serverSettings ? . JsonRpcUri ? . AbsoluteUri ,
49+ HttpHeaders = HttpHeadersDictionaryToString ( serverSettings ? . HttpHeaders ) ,
4850 } ;
4951 return View ( viewModel ) ;
5052 }
@@ -69,7 +71,8 @@ public async Task<IActionResult> GetServerConfig(TronUSDtLikeServerConfigViewMod
6971 var serverSettings = new TronUSDtLikeServerSettings ( )
7072 {
7173 SmartContractAddress = viewModel . SmartContractAddress is null or "" ? null : viewModel . SmartContractAddress ,
72- JsonRpcUri = viewModel . JsonRpcUri is null or "" ? null : new Uri ( viewModel . JsonRpcUri )
74+ JsonRpcUri = viewModel . JsonRpcUri is null or "" ? null : new Uri ( viewModel . JsonRpcUri ) ,
75+ HttpHeaders = ParseHttpHeadersString ( viewModel . HttpHeaders )
7376 } ;
7477
7578 await settingsRepository . UpdateSetting ( serverSettings , USDtPlugin . ServerSettingsKey ( currentConfiguration ) ) ;
@@ -81,4 +84,43 @@ public async Task<IActionResult> GetServerConfig(TronUSDtLikeServerConfigViewMod
8184
8285 return RedirectToAction ( "GetServerConfig" ) ;
8386 }
87+
88+ /// <summary>
89+ /// Parses a newline-separated string of "Header-Name: Value" pairs into a dictionary.
90+ /// </summary>
91+ private static Dictionary < string , string > ? ParseHttpHeadersString ( string ? headersString )
92+ {
93+ if ( string . IsNullOrWhiteSpace ( headersString ) )
94+ return null ;
95+
96+ var headers = new Dictionary < string , string > ( StringComparer . OrdinalIgnoreCase ) ;
97+ var lines = headersString . Split ( [ '\r ' , '\n ' ] , StringSplitOptions . RemoveEmptyEntries ) ;
98+
99+ foreach ( var line in lines )
100+ {
101+ var colonIndex = line . IndexOf ( ':' ) ;
102+ if ( colonIndex <= 0 ) continue ;
103+
104+ var headerName = line [ ..colonIndex ] . Trim ( ) ;
105+ var headerValue = line [ ( colonIndex + 1 ) ..] . Trim ( ) ;
106+
107+ if ( ! string . IsNullOrEmpty ( headerName ) && ! string . IsNullOrEmpty ( headerValue ) )
108+ {
109+ headers [ headerName ] = headerValue ;
110+ }
111+ }
112+
113+ return headers . Count > 0 ? headers : null ;
114+ }
115+
116+ /// <summary>
117+ /// Converts a dictionary of headers to a newline-separated string of "Header-Name: Value" pairs.
118+ /// </summary>
119+ private static string ? HttpHeadersDictionaryToString ( Dictionary < string , string > ? headers )
120+ {
121+ if ( headers == null || headers . Count == 0 )
122+ return null ;
123+
124+ return string . Join ( Environment . NewLine , headers . Select ( h => $ "{ h . Key } : { h . Value } ") ) ;
125+ }
84126}
0 commit comments