public static bool TryParseQueryString(string str, out PrintTask result, bool obfused = false)
 {
     try
     {
         var data      = new Dictionary <string, string>();
         var seperator = new char[] { '=' };
         foreach (var field in str.Split('&'))
         {
             var kv = field.Split(seperator, 2);
             data[HttpUtility.UrlDecode(kv[0])] = kv.Length > 1 ? HttpUtility.UrlDecode(kv[1]) : "";
         }
         result = new PrintTask();
         if (obfused)
         {
             if (data.TryGetValue("authinfo", out var authinfo))
             {
                 var bytes = Convert.FromBase64String(authinfo);
                 for (int i = 0; i < bytes.Length; i += 2)
                 {
                     bytes[i + 1] ^= (byte)(0xff & ~bytes[i]);
                     bytes[i]     ^= bytes[i + 1];
                 }
                 var start = bytes[0] == 0 ? 1 : 0;
                 authinfo = Encoding.UTF8.GetString(bytes, start, bytes.Length - start);
                 var splitted = authinfo.Split(new[] { '|' }, 2);
                 if (splitted.Length == 2)
                 {
                     result.Username     = splitted[0];
                     result.UserNickname = splitted[1];
                 }
             }
         }
         else
         {
             if (data.TryGetValue("username", out var username))
             {
                 result.Username = username;
             }
             if (data.TryGetValue("nickname", out var nickname))
             {
                 result.UserNickname = nickname;
             }
         }
         if (data.TryGetValue("content", out var content))
         {
             result.Content = content;
         }
         return(true);
     }
     catch (Exception)
     {
         result = null;
         return(false);
     }
 }
 public static bool TryParseJson(string str, out PrintTask result)
 {
     try
     {
         result = JsonConvert.DeserializeObject <PrintTask>(str);
         return(result != null);
     }
     catch (Exception)
     {
         result = null;
         return(false);
     }
 }
示例#3
0
        private void testPrintButton_Click(object sender, RoutedEventArgs e)
        {
            if (!_isPrinterConfigured)
            {
                MessageBox.Show("Please setup printing configurations first", "Cannot Start",
                                MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            var testTask = new PrintTask();

            testTask.Username     = "******";
            testTask.UserNickname = "Test Nickname";
            testTask.Content      = Enumerable.Range(0, 20).Select(x => "This is a very long line ").Aggregate((a, b) => a + b) + "\n";
            testTask.Content     += "Here are punctuations\n\"" + @"'[]{}!@#$%^&*()_-+=,./<>?;':\|`~" + "\n";
            testTask.Content     += Enumerable.Range(0, 20).Select(
                x => Enumerable.Range(0, 30).Select(y => "Go to next page ").Aggregate((a, b) => a + b) + "\n"
                ).Aggregate((a, b) => a + b);
            PrintHandler(testTask);
        }
示例#4
0
        //private void LoadPrintFormPage()
        //{
        //	string filePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
        //	filePath = Path.Combine(Path.GetDirectoryName(filePath), "print.html");
        //	string content = File.OpenText(filePath).ReadToEnd();
        //	_printFormRawPage = Encoding.UTF8.GetBytes(content);
        //}

        private void HandleRequest(HttpListenerContext context)
        {
            var request  = context.Request;
            var response = context.Response;

            if (request.HttpMethod == "POST")
            {
                try
                {
                    //var buffer = new byte[request.ContentLength64 + 10];
                    //int len;
                    //len = request.InputStream.Read(buffer, 0, (int)request.ContentLength64);
                    //var str = Encoding.UTF8.GetString(buffer, 0, len);
                    var str = new StreamReader(request.InputStream).ReadToEnd();
                    if (PrintTask.TryParseQueryString(str, out PrintTask printTask, true))
                    {
                        _printQueue.Add(printTask);
                        response.Redirect(RedirectPath);
                    }
                    else
                    {
                        response.StatusCode = 400;
                    }
                }