This repository has been archived by the owner on Dec 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathViewHtmlReport.aspx.cs
executable file
·118 lines (99 loc) · 3.89 KB
/
ViewHtmlReport.aspx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* ViewHtmlReport.aspx.cs
*
* Authors:
* Rolf Bjarne Kvinge ([email protected])
*
* Copyright 2009 Novell, Inc. (http://www.novell.com)
*
* See the LICENSE file included with the distribution for details.
*
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Web;
using System.Web.UI;
using log4net;
using MonkeyWrench;
using MonkeyWrench.Web.WebServices;
public partial class ViewHtmlReport : System.Web.UI.Page
{
private static readonly ILog log = LogManager.GetLogger (typeof (ViewHtmlReport));
private static void FixupHtml (string filename, int workfile_id, string md5, TextWriter writer)
{
string [] find;
string [] replace;
string line;
string name = !string.IsNullOrEmpty (md5) ? "md5" : "workfile_id";
string value = !string.IsNullOrEmpty (md5) ? md5 : workfile_id.ToString ();
find = new string [] {
"img src=\"",
"img src='",
"a href=\"",
"a href='" };
replace = new string []{
string.Format ("img src=\"ViewHtmlReport.aspx?{1}={0}&filename=", value, name),
string.Format ("img src='ViewHtmlReport.aspx?{1}={0}&filename=", value, name),
string.Format ("a href=\"ViewHtmlReport.aspx?{1}={0}&filename=", value, name),
string.Format ("a href='ViewHtmlReport.aspx?{1}={0}&filename=", value, name)};
using (FileStream fs_reader = new FileStream (filename, FileMode.Open, FileAccess.Read, FileShare.Read)) {
using (StreamReader reader = new StreamReader (fs_reader)) {
while (null != (line = reader.ReadLine ())) {
for (int i = 0; i < find.Length; i++)
line = line.Replace (find [i], replace [i]);
// undo any changes for relative links
line = line.Replace (string.Format ("ViewHtmlReport.aspx?workfile_id={0}&filename=#", workfile_id), "#");
// undo any changes for javascript links
line = line.Replace (string.Format ("ViewHtmlReport.aspx?workfile_id={0}&filename=javascript", workfile_id), "javascript");
// undo any changes for external links
line = line.Replace (string.Format ("ViewHtmlReport.aspx?workfile_id={0}&filename=http://", workfile_id), "http://");
writer.Write (line);
writer.Write ('\n');
}
}
}
}
protected void Page_Load (object sender, EventArgs e)
{
string md5 = Request ["md5"];
string filename = Request ["filename"];
int workfile_id;
int.TryParse (Request ["workfile_id"], out workfile_id);
if (string.IsNullOrEmpty (filename)) {
// send html file
string tmp_html_filename = null;
Response.ContentType = "text/html";
try {
tmp_html_filename = Path.GetTempFileName ();
using (WebClient web = new WebClient ()) {
web.Headers.Add ("Accept-Encoding", "gzip");
web.DownloadFile (Utilities.CreateWebServiceDownloadUrl (Request, workfile_id, false), tmp_html_filename);
if (web.ResponseHeaders ["Content-Encoding"] == "gzip")
MonkeyWrench.FileUtilities.GZUncompress (tmp_html_filename);
FixupHtml (tmp_html_filename, workfile_id, md5, Response.Output);
}
} catch (HttpException ex) {
log.ErrorFormat ("ViewHtmlReport: Exception while download html: {0} (redirected to login page)", ex);
Response.Redirect ("Login.aspx", false);
} catch (WebException ex) {
log.ErrorFormat ("ViewHtmlReport: Exception while download html: {0} (redirected to login page)", ex);
Response.Redirect ("Login.aspx", false);
} finally {
try {
File.Delete (tmp_html_filename);
} catch (Exception ex) {
log.ErrorFormat ("{0}", ex);
// Ignore any exceptions.
}
}
return;
}
string url = Utilities.CreateWebServiceDownloadUrl (Request, workfile_id, true);
if (!string.IsNullOrEmpty (md5))
url += "&md5=" + md5;
url += "&filename=" + HttpUtility.UrlEncode (filename);
Response.Redirect (url, false);
}
}