Skip to content

Commit 357a7a1

Browse files
committedJan 29, 2020
Fixed the "System.UriFormatException: Invalid URI: The Uri string is too long." exception when submitting a longer hardware report.
1 parent 49367c2 commit 357a7a1

File tree

4 files changed

+47
-11
lines changed

4 files changed

+47
-11
lines changed
 

‎GUI/CrashForm.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ This Source Code Form is subject to the terms of the Mozilla Public
44
License, v. 2.0. If a copy of the MPL was not distributed with this
55
file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
7-
Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
7+
Copyright (C) 2009-2020 Michael Möller <mmoeller@openhardwaremonitor.org>
88
99
*/
1010

11-
11+
using OpenHardwareMonitor.Utilities;
1212
using System;
1313
using System.IO;
1414
using System.Net;
@@ -59,10 +59,10 @@ private void sendButton_Click(object sender, EventArgs e) {
5959

6060
string report =
6161
"type=crash&" +
62-
"version=" + Uri.EscapeDataString(version.ToString()) + "&" +
63-
"report=" + Uri.EscapeDataString(reportTextBox.Text) + "&" +
64-
"comment=" + Uri.EscapeDataString(commentTextBox.Text) + "&" +
65-
"email=" + Uri.EscapeDataString(emailTextBox.Text);
62+
"version=" + HttpUtility.UrlEncode(version.ToString()) + "&" +
63+
"report=" + HttpUtility.UrlEncode(reportTextBox.Text) + "&" +
64+
"comment=" + HttpUtility.UrlEncode(commentTextBox.Text) + "&" +
65+
"email=" + HttpUtility.UrlEncode(emailTextBox.Text);
6666
byte[] byteArray = Encoding.UTF8.GetBytes(report);
6767
request.ContentLength = byteArray.Length;
6868

‎GUI/ReportForm.cs

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ This Source Code Form is subject to the terms of the Mozilla Public
44
License, v. 2.0. If a copy of the MPL was not distributed with this
55
file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
7-
Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
7+
Copyright (C) 2009-2020 Michael Möller <mmoeller@openhardwaremonitor.org>
88
99
*/
1010

11+
using OpenHardwareMonitor.Utilities;
1112
using System;
1213
using System.Drawing;
1314
using System.IO;
@@ -47,10 +48,10 @@ private void sendButton_Click(object sender, EventArgs e) {
4748

4849
string report =
4950
"type=hardware&" +
50-
"version=" + Uri.EscapeDataString(version.ToString()) + "&" +
51-
"report=" + Uri.EscapeDataString(reportTextBox.Text) + "&" +
52-
"comment=" + Uri.EscapeDataString(commentTextBox.Text) + "&" +
53-
"email=" + Uri.EscapeDataString(emailTextBox.Text);
51+
"version=" + HttpUtility.UrlEncode(version.ToString()) + "&" +
52+
"report=" + HttpUtility.UrlEncode(reportTextBox.Text) + "&" +
53+
"comment=" + HttpUtility.UrlEncode(commentTextBox.Text) + "&" +
54+
"email=" + HttpUtility.UrlEncode(emailTextBox.Text);
5455
byte[] byteArray = Encoding.UTF8.GetBytes(report);
5556
request.ContentLength = byteArray.Length;
5657

‎OpenHardwareMonitor.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
<Compile Include="GUI\UserRadioGroup.cs" />
126126
<Compile Include="Properties\AssemblyVersion.cs" />
127127
<Compile Include="Utilities\HttpServer.cs" />
128+
<Compile Include="Utilities\HttpUtility.cs" />
128129
<Compile Include="Utilities\Logger.cs" />
129130
<Compile Include="Utilities\PersistentSettings.cs" />
130131
<Compile Include="Properties\AssemblyInfo.cs" />

‎Utilities/HttpUtility.cs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
3+
This Source Code Form is subject to the terms of the Mozilla Public
4+
License, v. 2.0. If a copy of the MPL was not distributed with this
5+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
7+
Copyright (C) 2020 Michael Möller <mmoeller@openhardwaremonitor.org>
8+
9+
*/
10+
11+
using System;
12+
using System.Text;
13+
14+
namespace OpenHardwareMonitor.Utilities {
15+
16+
public class HttpUtility {
17+
public static string UrlEncode(string s) {
18+
19+
int maxLength = 32765;
20+
var sb = new StringBuilder();
21+
int imax = s.Length / maxLength;
22+
23+
for (int i = 0; i <= imax; i++) {
24+
sb.Append(
25+
Uri.EscapeDataString(i < imax
26+
? s.Substring(maxLength * i, maxLength)
27+
: s.Substring(maxLength * i)));
28+
}
29+
30+
return sb.ToString();
31+
}
32+
33+
}
34+
}

0 commit comments

Comments
 (0)