@@ -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 += "\t success\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 += "\t success\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 += "\t failed" ;
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 += "\t failed" ;
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