1
0
Fork 0
mirror of https://github.com/Jackett/Jackett synced 2024-12-29 11:17:22 +00:00

core: fix form encoding. resolves #4346 resolves #3061 (#6994)

This commit is contained in:
Diego Heras 2020-02-08 07:03:03 +01:00 committed by GitHub
parent 77a2264164
commit f770596354
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 10 deletions

View file

@ -214,7 +214,7 @@ namespace Jackett.Common.Utils.Clients
else if (webRequest.Type == RequestType.POST)
{
if (webRequest.PostData != null)
request.Content = new FormUrlEncodedContent(webRequest.PostData);
request.Content = FormUrlEncodedContentWithEncoding(webRequest.PostData, webRequest.Encoding);
request.Method = HttpMethod.Post;
}
else

View file

@ -234,7 +234,7 @@ namespace Jackett.Common.Utils.Clients
else if (webRequest.Type == RequestType.POST)
{
if (webRequest.PostData != null)
request.Content = new FormUrlEncodedContent(webRequest.PostData);
request.Content = FormUrlEncodedContentWithEncoding(webRequest.PostData, webRequest.Encoding);
request.Method = HttpMethod.Post;
}
else

View file

@ -230,7 +230,7 @@ namespace Jackett.Common.Utils.Clients
else if (webRequest.Type == RequestType.POST)
{
if (webRequest.PostData != null)
request.Content = new FormUrlEncodedContent(webRequest.PostData);
request.Content = FormUrlEncodedContentWithEncoding(webRequest.PostData, webRequest.Encoding);
request.Method = HttpMethod.Post;
}
else
@ -250,7 +250,7 @@ namespace Jackett.Common.Utils.Clients
}
// some cloudflare clients are using a refresh header
// Pull it out manually
// Pull it out manually
if (response.StatusCode == System.Net.HttpStatusCode.ServiceUnavailable && response.Headers.Contains("Refresh"))
{
var refreshHeaders = response.Headers.GetValues("Refresh");

View file

@ -213,7 +213,7 @@ namespace Jackett.Common.Utils.Clients
else if (webRequest.Type == RequestType.POST)
{
if (webRequest.PostData != null)
request.Content = new FormUrlEncodedContent(webRequest.PostData);
request.Content = FormUrlEncodedContentWithEncoding(webRequest.PostData, webRequest.Encoding);
request.Method = HttpMethod.Post;
}
else

View file

@ -1,8 +1,11 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using AutoMapper;
using Jackett.Common.Models.Config;
using Jackett.Common.Services.Interfaces;
@ -184,5 +187,33 @@ namespace Jackett.Common.Utils.Clients
{
// nothing by default
}
/**
* This method does the same as FormUrlEncodedContent but with custom encoding instead of utf-8
* https://stackoverflow.com/a/13832544
*/
protected static ByteArrayContent FormUrlEncodedContentWithEncoding(
IEnumerable<KeyValuePair<string, string>> nameValueCollection, Encoding encoding)
{
// utf-8 / default
if (Encoding.UTF8.Equals(encoding) || encoding == null)
return new FormUrlEncodedContent(nameValueCollection);
// other encodings
var builder = new StringBuilder();
foreach (KeyValuePair<string, string> pair in nameValueCollection)
{
if (builder.Length > 0)
builder.Append('&');
builder.Append(HttpUtility.UrlEncode(pair.Key, encoding));
builder.Append('=');
builder.Append(HttpUtility.UrlEncode(pair.Value, encoding));
}
// HttpRuleParser.DefaultHttpEncoding == "latin1"
var data = Encoding.GetEncoding("latin1").GetBytes(builder.ToString());
var content = new ByteArrayContent(data);
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
return content;
}
}
}

View file

@ -49,11 +49,6 @@ namespace Jackett.Common.Utils
return Encoding.UTF8.GetString(Convert.FromBase64String(str));
}
public static string PostDataFromDict(IEnumerable<KeyValuePair<string, string>> dict)
{
return new FormUrlEncodedContent(dict).ReadAsStringAsync().Result;
}
/// <summary>
/// Convert an array of bytes to a string of hex digits
/// </summary>