@@ -97,16 +97,35 @@ private static HttpRequest ConvertToNzbRequest(HttpRequestMessage httpRequest)
9797 Method = httpRequest . Method
9898 } ;
9999
100- foreach ( KeyValuePair < string , IEnumerable < string > > header in httpRequest . Headers )
100+ List < string > requestHeaderKeys = new ( ) ;
101+ List < string > contentHeaderKeys = new ( ) ;
102+
103+ string allRequestHeaders = httpRequest . Headers . ToString ( ) ;
104+ foreach ( string line in allRequestHeaders . Split ( new [ ] { "\r \n " , "\n " } , StringSplitOptions . RemoveEmptyEntries ) )
101105 {
102- nzbRequest . Headers [ header . Key ] = string . Join ( ", " , header . Value ) ;
106+ int colonIndex = line . IndexOf ( ':' ) ;
107+ if ( colonIndex > 0 )
108+ {
109+ string headerName = line . Substring ( 0 , colonIndex ) . Trim ( ) ;
110+ string headerValue = line . Substring ( colonIndex + 1 ) . Trim ( ) ;
111+ requestHeaderKeys . Add ( headerName ) ;
112+ nzbRequest . Headers [ headerName ] = headerValue ;
113+ }
103114 }
104115
105116 if ( httpRequest . Content != null )
106117 {
107- foreach ( KeyValuePair < string , IEnumerable < string > > header in httpRequest . Content . Headers )
118+ string allContentHeaders = httpRequest . Content . Headers . ToString ( ) ;
119+ foreach ( string line in allContentHeaders . Split ( new [ ] { "\r \n " , "\n " } , StringSplitOptions . RemoveEmptyEntries ) )
108120 {
109- nzbRequest . Headers [ header . Key ] = string . Join ( ", " , header . Value ) ;
121+ int colonIndex = line . IndexOf ( ':' ) ;
122+ if ( colonIndex > 0 )
123+ {
124+ string headerName = line . Substring ( 0 , colonIndex ) . Trim ( ) ;
125+ string headerValue = line . Substring ( colonIndex + 1 ) . Trim ( ) ;
126+ contentHeaderKeys . Add ( headerName ) ;
127+ nzbRequest . Headers [ headerName ] = headerValue ;
128+ }
110129 }
111130
112131 byte [ ] contentBytes = httpRequest . Content . ReadAsByteArrayAsync ( ) . GetAwaiter ( ) . GetResult ( ) ;
@@ -116,29 +135,51 @@ private static HttpRequest ConvertToNzbRequest(HttpRequestMessage httpRequest)
116135 }
117136 }
118137
138+ nzbRequest . Cookies [ "__REQUEST_HEADERS__" ] = string . Join ( "|" , requestHeaderKeys ) ;
139+ nzbRequest . Cookies [ "__CONTENT_HEADERS__" ] = string . Join ( "|" , contentHeaderKeys ) ;
140+
119141 return nzbRequest ;
120142 }
121143
122144 private static void ApplyNzbRequestToHttpRequestMessage ( HttpRequest nzbRequest , HttpRequestMessage httpRequest )
123145 {
124- // Apply modified headers back to HttpRequestMessage
125- foreach ( KeyValuePair < string , string > header in nzbRequest . Headers )
146+ HashSet < string > originalRequestHeaders = nzbRequest . Cookies . TryGetValue ( "__REQUEST_HEADERS__" , out string ? valueH )
147+ ? new HashSet < string > ( valueH . Split ( '|' , StringSplitOptions . RemoveEmptyEntries ) , StringComparer . OrdinalIgnoreCase )
148+ : new HashSet < string > ( StringComparer . OrdinalIgnoreCase ) ;
149+
150+ HashSet < string > originalContentHeaders = nzbRequest . Cookies . TryGetValue ( "__CONTENT_HEADERS__" , out string ? valueC )
151+ ? new HashSet < string > ( valueC . Split ( '|' , StringSplitOptions . RemoveEmptyEntries ) , StringComparer . OrdinalIgnoreCase )
152+ : new HashSet < string > ( StringComparer . OrdinalIgnoreCase ) ;
153+
154+ foreach ( ( string key , string value ) in nzbRequest . Headers )
126155 {
127- if ( httpRequest . Headers . Contains ( header . Key ) )
156+ if ( originalRequestHeaders . Contains ( key ) )
128157 {
129- httpRequest . Headers . Remove ( header . Key ) ;
158+ httpRequest . Headers . Remove ( key ) ;
159+ httpRequest . Headers . TryAddWithoutValidation ( key , value ) ;
160+ }
161+ else if ( originalContentHeaders . Contains ( key ) )
162+ {
163+ if ( httpRequest . Content != null )
164+ {
165+ httpRequest . Content . Headers . Remove ( key ) ;
166+ httpRequest . Content . Headers . TryAddWithoutValidation ( key , value ) ;
167+ }
168+ }
169+ else
170+ {
171+ httpRequest . Headers . TryAddWithoutValidation ( key , value ) ;
130172 }
131- httpRequest . Headers . TryAddWithoutValidation ( header . Key , header . Value ) ;
132173 }
133174
134- // Apply cookies
135- if ( nzbRequest . Cookies . Count > 0 )
175+ List < KeyValuePair < string , string > > actualCookies = nzbRequest . Cookies
176+ . Where ( c => ! c . Key . StartsWith ( "__" ) || ! c . Key . EndsWith ( "__" ) )
177+ . ToList ( ) ;
178+
179+ if ( actualCookies . Count > 0 )
136180 {
137- string cookieHeader = string . Join ( "; " , nzbRequest . Cookies . Select ( c => $ "{ c . Key } ={ c . Value } ") ) ;
138- if ( httpRequest . Headers . Contains ( "Cookie" ) )
139- {
140- httpRequest . Headers . Remove ( "Cookie" ) ;
141- }
181+ string cookieHeader = string . Join ( "; " , actualCookies . Select ( c => $ "{ c . Key } ={ c . Value } ") ) ;
182+ httpRequest . Headers . Remove ( "Cookie" ) ;
142183 httpRequest . Headers . Add ( "Cookie" , cookieHeader ) ;
143184 }
144185 }
0 commit comments