|
|
@@ -11,19 +11,90 @@ namespace Astralis {
|
|
|
public Enumerable<string> path_components { get; private set; }
|
|
|
|
|
|
public Enumerable<HttpHeader> headers { get; private set; }
|
|
|
+ public Enumerable<KeyValuePair<string, string>> query_parameters { get; private set; }
|
|
|
+ public Enumerable<KeyValuePair<string, string>> cookies { get; private set; }
|
|
|
|
|
|
- public HttpRequest(string url, string method, string version) {
|
|
|
+ public HttpRequest(Connection connection, string url, string method, string version) {
|
|
|
this.raw_path = url;
|
|
|
this.method = method;
|
|
|
this.version = version;
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+ // Parse path components
|
|
|
var path_parts = this.raw_path.split("?");
|
|
|
this.path_components = Wrap.array<string>(path_parts[0].split("/"))
|
|
|
.select<string>(p => Uri.unescape_string (p) ?? "")
|
|
|
.where (p => p != "")
|
|
|
.to_immutable_buffer();
|
|
|
+
|
|
|
+ // Parse query parameters from URL
|
|
|
+ this.query_parameters = parse_query_parameters(path_parts.length > 1 ? path_parts[1] : "");
|
|
|
+
|
|
|
+ // Parse headers from connection
|
|
|
+ this.headers = parse_headers(connection);
|
|
|
+
|
|
|
+ // Parse cookies from connection
|
|
|
+ this.cookies = parse_cookies(connection);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Enumerable<KeyValuePair<string, string>> parse_query_parameters(string query_string) {
|
|
|
+ if (query_string == null || query_string == "") {
|
|
|
+ return Wrap.array<KeyValuePair<string, string>>(new KeyValuePair<string, string>[0]);
|
|
|
+ }
|
|
|
+
|
|
|
+ var pairs = query_string.split("&");
|
|
|
+ var result = new KeyValuePair<string, string>[0];
|
|
|
+
|
|
|
+ foreach (var pair in pairs) {
|
|
|
+ var kv = pair.split("=", 2);
|
|
|
+ if (kv.length == 2) {
|
|
|
+ var key = Uri.unescape_string(kv[0]) ?? "";
|
|
|
+ var value = Uri.unescape_string(kv[1]) ?? "";
|
|
|
+ result += new KeyValuePair<string, string>(key, value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return Wrap.array<KeyValuePair<string, string>>(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Enumerable<HttpHeader> parse_headers(Connection connection) {
|
|
|
+ var headers = new HttpHeader[0];
|
|
|
+
|
|
|
+ // Common HTTP headers to look for
|
|
|
+ string[] common_headers = {
|
|
|
+ "Accept", "Accept-Encoding", "Accept-Language", "Authorization",
|
|
|
+ "Cache-Control", "Connection", "Content-Encoding", "Content-Length",
|
|
|
+ "Content-Type", "Cookie", "Host", "If-Modified-Since",
|
|
|
+ "If-None-Match", "Referer", "User-Agent"
|
|
|
+ };
|
|
|
+
|
|
|
+ foreach (var header_name in common_headers) {
|
|
|
+ var value = MHD.lookup_connection_value(connection, MHD.ValueKind.HEADER_KIND, header_name);
|
|
|
+ if (value != null) {
|
|
|
+ headers += new HttpHeader(header_name, value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return Wrap.array<HttpHeader>(headers);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Enumerable<KeyValuePair<string, string>> parse_cookies(Connection connection) {
|
|
|
+ var cookies = new KeyValuePair<string, string>[0];
|
|
|
+
|
|
|
+ var cookie_header = MHD.lookup_connection_value(connection, MHD.ValueKind.HEADER_KIND, "Cookie");
|
|
|
+ if (cookie_header != null) {
|
|
|
+ var cookie_pairs = cookie_header.split(";");
|
|
|
+ foreach (var pair in cookie_pairs) {
|
|
|
+ var trimmed = pair.strip();
|
|
|
+ var kv = trimmed.split("=", 2);
|
|
|
+ if (kv.length == 2) {
|
|
|
+ var key = kv[0].strip();
|
|
|
+ var value = kv[1].strip();
|
|
|
+ cookies += new KeyValuePair<string, string>(key, value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return Wrap.array<KeyValuePair<string, string>>(cookies);
|
|
|
}
|
|
|
}
|
|
|
|