Skip to content

Commit 2e92e70

Browse files
committed
minor fix v1.0.2
fix #1 fix #2 fix #3
1 parent bcadf3b commit 2e92e70

File tree

6 files changed

+211
-173
lines changed

6 files changed

+211
-173
lines changed

RawPrintingHTTPServer/Program.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,26 @@ static void Main()
1313
{
1414
Application.EnableVisualStyles();
1515
Application.SetCompatibleTextRenderingDefault(false);
16-
Form1 frm = new Form1();
16+
frm = new Form1();
1717
frm.Hide();
1818
frm.start();
19+
Application.ApplicationExit += new EventHandler(OnApplicationExit);
1920
Application.Run();
2021
}
22+
23+
private static Form1 frm;
24+
25+
private static void OnApplicationExit(object sender, EventArgs e)
26+
{
27+
try
28+
{
29+
if (frm != null && !frm.IsDisposed)
30+
{
31+
frm.stop();
32+
frm.Close();
33+
}
34+
}
35+
catch { }
36+
}
2137
}
2238
}

RawPrintingHTTPServer/RawPrintingHTTPServer.cs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
namespace RawPrintingHTTPServer
88
{
9+
enum ResponseCode {
10+
OK = 0,
11+
NotFound = 1,
12+
Forbidden = 2
13+
}
914
class RawPrintingHTTPServer
1015
{
1116
private HttpListener _listener;
@@ -86,7 +91,7 @@ private Task _listen()
8691
}
8792
else
8893
{
89-
if (req.Url.AbsolutePath == "/permissions")
94+
if (req.Url.AbsolutePath == "/permissions" && req.HttpMethod == "GET")
9095
{
9196
resp.AppendHeader("Access-Control-Allow-Origin", origin);
9297
} else
@@ -108,41 +113,44 @@ private Task _listen()
108113
{
109114
accesslog += "\tfailed\tsame origin";
110115
ServerConfig.appendLog(accesslog);
111-
resp.StatusCode = (int)HttpStatusCode.Forbidden;
112-
resp.StatusDescription = "FORBIDDEN";
113-
resp.Close();
116+
responseForbidden(resp);
114117
}
115118
else
116119
{
117-
bool is404 = false;
120+
ResponseCode respCode = ResponseCode.OK;
118121
if (req.Url.AbsolutePath == "/")
119122
{
120-
is404 = _home.handle(req, resp, accesslog);
123+
respCode = _home.handle(req, resp, accesslog);
121124
}
122125
else if (req.Url.AbsolutePath == "/permissions")
123126
{
124-
is404 = _permissions.handle(req, resp, accesslog, origin);
127+
respCode = _permissions.handle(req, resp, accesslog, origin);
125128
}
126129
else if (req.Url.AbsolutePath == "/printers")
127130
{
128-
is404 = _printers.handle(req, resp, accesslog);
131+
respCode = _printers.handle(req, resp, accesslog);
129132
}
130133
else if (req.Url.AbsolutePath == "/settings")
131134
{
132-
is404 = _settings.handle(req, resp, accesslog);
135+
respCode = _settings.handle(req, resp, accesslog);
133136
}
134137
else
135138
{
136-
is404 = true;
139+
respCode = ResponseCode.NotFound;
137140
}
138141

139-
if (is404)
142+
switch(respCode)
140143
{
141-
accesslog += "\tfailed\tnot found";
142-
ServerConfig.appendLog(accesslog);
143-
resp.StatusCode = (int)HttpStatusCode.NotFound;
144-
resp.StatusDescription = "NOT FOUND";
145-
resp.Close();
144+
case ResponseCode.NotFound:
145+
accesslog += "\tfailed\tnot found";
146+
ServerConfig.appendLog(accesslog);
147+
resp.StatusCode = (int)HttpStatusCode.NotFound;
148+
resp.StatusDescription = "NOT FOUND";
149+
resp.Close();
150+
break;
151+
case ResponseCode.Forbidden:
152+
responseForbidden(resp);
153+
break;
146154
}
147155
}
148156
}
@@ -214,6 +222,13 @@ public void Stop()
214222
}
215223
}
216224

225+
public void responseForbidden(HttpListenerResponse resp)
226+
{
227+
resp.StatusCode = (int)HttpStatusCode.Forbidden;
228+
resp.StatusDescription = "FORBIDDEN";
229+
resp.Close();
230+
}
231+
217232
public void response(HttpListenerResponse resp, byte[] data, string contentType)
218233
{
219234
resp.ContentType = contentType;

RawPrintingHTTPServer/handlers/HomeHandler.cs

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@ public HomeHandler(RawPrintingHTTPServer server)
1818

1919
private bool WritePrintJobFile(string printjobname, byte[] bindata)
2020
{
21-
string filePath = ServerConfig.basePath + "\\" + printjobname + ".prn";
21+
string replaceSlashWith = "_";
22+
string sanitized = printjobname.Replace("\\", replaceSlashWith).Replace("/", replaceSlashWith);
23+
string filePath = ServerConfig.basePath + "\\" + sanitized + ".prn";
2224
using (FileStream sw = new FileStream(filePath, FileMode.Create))
2325
{
2426
sw.Write(bindata, 0, bindata.Length);
2527
}
2628
return true;
2729
}
2830

29-
public bool handle(HttpListenerRequest req, HttpListenerResponse resp, string accesslog)
31+
public ResponseCode handle(HttpListenerRequest req, HttpListenerResponse resp, string accesslog)
3032
{
3133
if (req.HttpMethod == "POST")
3234
{
@@ -38,65 +40,67 @@ public bool handle(HttpListenerRequest req, HttpListenerResponse resp, string ac
3840
}
3941
else
4042
{
41-
return true;
43+
return ResponseCode.NotFound;
4244
}
4345
}
4446

45-
private bool _handlePost(HttpListenerRequest req, HttpListenerResponse resp, string accesslog)
47+
private ResponseCode _handlePost(HttpListenerRequest req, HttpListenerResponse resp, string accesslog)
4648
{
47-
if (req.HasEntityBody)
49+
if (!req.HasEntityBody)
4850
{
49-
PrintJobResponse printjobresp = new PrintJobResponse();
50-
try
51+
return ResponseCode.NotFound;
52+
}
53+
54+
PrintJobResponse printjobresp = new PrintJobResponse();
55+
try
56+
{
57+
using (Stream body = req.InputStream)
5158
{
52-
using (Stream body = req.InputStream)
59+
Encoding encoding = req.ContentEncoding;
60+
using (StreamReader reader = new StreamReader(body, encoding))
5361
{
54-
Encoding encoding = req.ContentEncoding;
55-
using (StreamReader reader = new StreamReader(body, encoding))
56-
{
57-
string json = reader.ReadToEnd();
58-
PrintJobPostBody printjob = ServerConfig.fromJSON<PrintJobPostBody>(json);
59-
body.Close();
60-
reader.Close();
62+
string json = reader.ReadToEnd();
63+
PrintJobPostBody printjob = ServerConfig.fromJSON<PrintJobPostBody>(json);
64+
body.Close();
65+
reader.Close();
6166

62-
byte[] bindata = printjob.DataToByteArray();
67+
byte[] bindata = printjob.DataToByteArray();
6368

64-
bool success = false;
65-
if (server.config.testingMode == 1)
66-
{
67-
success = WritePrintJobFile(printjob.id, bindata);
68-
} else if (server.config.testingMode == 0)
69-
{
70-
success = RawPrintingHelper.SendBytesToPrinter(printjob.printer, bindata, printjob.id);
71-
} else
72-
{
73-
success = RawPrintingHelper.SendBytesToPrinter(printjob.printer, bindata, printjob.id) && WritePrintJobFile(printjob.id, bindata);
74-
}
75-
76-
accesslog += "\tsuccess\t" + printjob.id + "\t" + printjob.printer;
77-
ServerConfig.appendLog(accesslog);
78-
printjobresp.success = true;
79-
printjobresp.data = printjob.id;
69+
bool success = false;
70+
if (server.config.testingMode == 1)
71+
{
72+
success = WritePrintJobFile(printjob.id, bindata);
73+
}
74+
else if (server.config.testingMode == 0)
75+
{
76+
success = RawPrintingHelper.SendBytesToPrinter(printjob.printer, bindata, printjob.id);
8077
}
78+
else
79+
{
80+
success = RawPrintingHelper.SendBytesToPrinter(printjob.printer, bindata, printjob.id) && WritePrintJobFile(printjob.id, bindata);
81+
}
82+
83+
accesslog += "\tsuccess\t" + printjob.id + "\t" + printjob.printer;
84+
ServerConfig.appendLog(accesslog);
85+
printjobresp.success = true;
86+
printjobresp.data = printjob.id;
8187
}
8288
}
83-
catch (Exception e)
84-
{
85-
printjobresp.success = false;
86-
printjobresp.data = e.Message;
87-
accesslog += "\tfailed";
88-
ServerConfig.appendLog(accesslog);
89-
}
90-
server.responseJSON(resp, printjobresp);
9189
}
92-
else
90+
catch (Exception e)
9391
{
94-
return true;
92+
ServerConfig.appendLog("Error: " + e.Message + "\n" + e.StackTrace);
93+
printjobresp.success = false;
94+
printjobresp.data = "";
95+
accesslog += "\tfailed";
96+
ServerConfig.appendLog(accesslog);
9597
}
96-
return false;
98+
server.responseJSON(resp, printjobresp);
99+
100+
return ResponseCode.OK;
97101
}
98102

99-
private bool _handleGet(HttpListenerRequest req, HttpListenerResponse resp, string accesslog)
103+
private ResponseCode _handleGet(HttpListenerRequest req, HttpListenerResponse resp, string accesslog)
100104
{
101105
string html = "<html>";
102106
html += "<head><style>";
@@ -185,7 +189,7 @@ private bool _handleGet(HttpListenerRequest req, HttpListenerResponse resp, stri
185189
html += "</html>";
186190
ServerConfig.appendLog(accesslog);
187191
server.responseHTML(resp, html);
188-
return false;
192+
return ResponseCode.OK;
189193
}
190194
}
191195
}

0 commit comments

Comments
 (0)