|
| 1 | +/* |
| 2 | +Copyright (C) 2025 Robyn (robyn@mamallama.dev) |
| 3 | +
|
| 4 | + This program is free software: you can redistribute it and/or modify |
| 5 | + it under the terms of the GNU General Public License as published by |
| 6 | + the Free Software Foundation, either version 3 of the License, or |
| 7 | + (at your option) any later version. |
| 8 | +*/ |
| 9 | + |
| 10 | +using System.Collections.Generic; |
| 11 | +using System.Collections.Specialized; |
| 12 | +using System.IO; |
| 13 | +using System.Net; |
| 14 | + |
| 15 | +namespace SharpWebserver.Extensions; |
| 16 | +public static class RequestExtensions |
| 17 | +{ |
| 18 | + public static string GetFirstOrDefaultForKey(this NameValueCollection query, string key) |
| 19 | + { |
| 20 | + if (query.GetValues(key) is not string[] values) |
| 21 | + return string.Empty; |
| 22 | + |
| 23 | + return values[0]; |
| 24 | + } |
| 25 | + |
| 26 | + public static Dictionary<string, string> GetFirstOrDefaultForAll(this NameValueCollection query) |
| 27 | + { |
| 28 | + var keys = query.AllKeys; |
| 29 | + var data = new Dictionary<string, string>(); |
| 30 | + |
| 31 | + foreach (var item in keys) |
| 32 | + { |
| 33 | + if (item is null) |
| 34 | + continue; |
| 35 | + |
| 36 | + data[item] = query.GetFirstOrDefaultForKey(item); |
| 37 | + } |
| 38 | + |
| 39 | + return data; |
| 40 | + } |
| 41 | + |
| 42 | + public static Dictionary<string, string>? GetDecodedFormDataFirstOrDefault(this HttpListenerRequest request) |
| 43 | + { |
| 44 | + var formData = new Dictionary<string, string>(); |
| 45 | + |
| 46 | + //name=value1&name=value2&name=value3 |
| 47 | + |
| 48 | + if (request.ContentType != "application/x-www-form-urlencoded" || request.ContentLength64 == 0) |
| 49 | + return null; |
| 50 | + |
| 51 | + using StreamReader data = new(request.InputStream); |
| 52 | + //data.BaseStream.Seek(0, SeekOrigin.Begin); |
| 53 | + |
| 54 | + string input = data.ReadToEnd(); |
| 55 | + string[] pairs = input.Split('&'); |
| 56 | + |
| 57 | + foreach (var item in pairs) |
| 58 | + { |
| 59 | + var kvp = item.Split('='); |
| 60 | + |
| 61 | + if (kvp.Length != 2) |
| 62 | + continue; |
| 63 | + |
| 64 | + if (formData.TryGetValue(kvp[0], out var _)) |
| 65 | + continue; |
| 66 | + else |
| 67 | + formData[kvp[0]] = kvp[1]; |
| 68 | + } |
| 69 | + |
| 70 | + return formData; |
| 71 | + } |
| 72 | + |
| 73 | + public static Dictionary<string, string[]>? GetDecodedFormData(this HttpListenerRequest request) |
| 74 | + { |
| 75 | + var formData = new Dictionary<string, List<string>>(); |
| 76 | + |
| 77 | + //name=value1&name=value2&name=value3 |
| 78 | + |
| 79 | + if (request.ContentType != "application/x-www-form-urlencoded" || request.ContentLength64 == 0) |
| 80 | + return null; |
| 81 | + |
| 82 | + using StreamReader data = new(request.InputStream); |
| 83 | + //data.BaseStream.Seek(0, SeekOrigin.Begin); |
| 84 | + |
| 85 | + string input = data.ReadToEnd(); |
| 86 | + string[] pairs = input.Split('&'); |
| 87 | + |
| 88 | + foreach (var item in pairs) |
| 89 | + { |
| 90 | + Logger.LogTrace("Splitting pair", [ |
| 91 | + ("Input", item) |
| 92 | + ]); |
| 93 | + |
| 94 | + var kvp = item.Split('='); |
| 95 | + |
| 96 | + if (kvp.Length != 2) |
| 97 | + continue; |
| 98 | + |
| 99 | + List<string> addingList; |
| 100 | + |
| 101 | + if (formData.TryGetValue(kvp[0], out var strings)) |
| 102 | + addingList = strings; |
| 103 | + else |
| 104 | + addingList = []; |
| 105 | + |
| 106 | + addingList.Add(kvp[1]); |
| 107 | + formData[kvp[0]] = addingList; |
| 108 | + } |
| 109 | + |
| 110 | + Dictionary<string, string[]> items = []; |
| 111 | + |
| 112 | + //Convert to array |
| 113 | + foreach (var item in formData) |
| 114 | + items[item.Key] = [.. item.Value]; |
| 115 | + |
| 116 | + return items; |
| 117 | + } |
| 118 | +} |
0 commit comments