예제 #1
0
        private bool _handlePost(HttpListenerRequest req, HttpListenerResponse resp, string accesslog)
        {
            if (req.HasEntityBody)
            {
                PrintJobResponse printjobresp = new PrintJobResponse();
                try
                {
                    using (Stream body = req.InputStream)
                    {
                        Encoding encoding = req.ContentEncoding;
                        using (StreamReader reader = new StreamReader(body, encoding))
                        {
                            string           json     = reader.ReadToEnd();
                            PrintJobPostBody printjob = ServerConfig.fromJSON <PrintJobPostBody>(json);
                            body.Close();
                            reader.Close();

                            byte[] bindata = printjob.DataToByteArray();

                            bool success = false;
                            if (server.config.testingMode == 1)
                            {
                                success = WritePrintJobFile(printjob.id, bindata);
                            }
                            else if (server.config.testingMode == 0)
                            {
                                success = RawPrintingHelper.SendBytesToPrinter(printjob.printer, bindata, printjob.id);
                            }
                            else
                            {
                                success = RawPrintingHelper.SendBytesToPrinter(printjob.printer, bindata, printjob.id) && WritePrintJobFile(printjob.id, bindata);
                            }

                            accesslog += "\tsuccess\t" + printjob.id;
                            ServerConfig.appendLog(accesslog);
                            printjobresp.success = true;
                            printjobresp.data    = printjob.id;
                        }
                    }
                }
                catch (Exception e)
                {
                    printjobresp.success = false;
                    printjobresp.data    = e.Message;
                    accesslog           += "\tfailed";
                    ServerConfig.appendLog(accesslog);
                }
                byte[] data = Encoding.UTF8.GetBytes(ServerConfig.toJSON(printjobresp));
                resp.ContentType     = "application/json";
                resp.ContentEncoding = Encoding.UTF8;
                resp.ContentLength64 = data.LongLength;
                resp.OutputStream.Write(data, 0, data.Length);
                resp.Close();
            }
            else
            {
                return(true);
            }
            return(false);
        }