HttpContext.vala 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using MHD;
  2. using Invercargill;
  3. using Invercargill.DataStructures;
  4. namespace Astralis {
  5. public class HttpRequest : Object {
  6. // Basic request information
  7. public string raw_path { get; private set; }
  8. public Method method { get; private set; }
  9. public string version { get; private set; }
  10. // Path and routing
  11. public Enumerable<string> path_components { get; private set; }
  12. public string query_string { get; private set; }
  13. // Request data collections
  14. public Catalogue<string, string> headers { get; private set; }
  15. public Catalogue<string, string> query_params { get; private set; }
  16. public Catalogue<string, string> cookies { get; private set; }
  17. // Request body
  18. public AsyncInput request_body { get; private set; }
  19. // Content information
  20. public string content_type { get; private set; }
  21. public int64 content_length { get; private set; }
  22. public string? content_encoding { get; private set; }
  23. // Client information
  24. public string? remote_address { get; private set; }
  25. public string? user_agent { get; private set; }
  26. public string? referer { get; private set; }
  27. public HttpRequest(
  28. string url,
  29. string method,
  30. string version,
  31. Catalogue<string, string> headers,
  32. AsyncInput request_body,
  33. Catalogue<string, string> query,
  34. Catalogue<string, string> cookies,
  35. string? remote_address
  36. ) {
  37. this.raw_path = url;
  38. this.method = Method.from_string(method);
  39. this.version = version;
  40. this.headers = headers;
  41. this.request_body = request_body;
  42. this.query_params = query;
  43. this.cookies = cookies;
  44. this.remote_address = remote_address;
  45. // Parse path components
  46. var path_parts = this.raw_path.split("?");
  47. this.path_components = Wrap.array<string>(path_parts[0].split("/"))
  48. .select<string>(p => Uri.unescape_string(p) ?? "")
  49. .where(p => p != "")
  50. .to_immutable_buffer();
  51. // Extract query string
  52. this.query_string = path_parts.length > 1 ? path_parts[1] : "";
  53. // Extract common headers
  54. this.content_type = get_header("Content-Type") ?? "";
  55. this.content_length = parse_content_length(get_header("Content-Length"));
  56. this.content_encoding = get_header("Content-Encoding");
  57. this.user_agent = get_header("User-Agent");
  58. this.referer = get_header("Referer");
  59. }
  60. // Header access methods
  61. public string? get_header(string name) {
  62. string? value;
  63. if (headers.try_get_any(name, out value)) {
  64. return value;
  65. }
  66. return null;
  67. }
  68. public string get_header_or_default(string name, string default_value) {
  69. string? value;
  70. if (headers.try_get_any(name, out value)) {
  71. return value;
  72. }
  73. return default_value;
  74. }
  75. public Enumerable<string> get_header_all(string name) {
  76. return headers.get_or_empty(name);
  77. }
  78. public bool has_header(string name) {
  79. return headers.has(name);
  80. }
  81. // Query parameter access methods
  82. public string? get_query(string name) {
  83. string? value;
  84. if (query_params.try_get_any(name, out value)) {
  85. return value;
  86. }
  87. return null;
  88. }
  89. public string get_query_or_default(string name, string default_value) {
  90. string? value;
  91. if (query_params.try_get_any(name, out value)) {
  92. return value;
  93. }
  94. return default_value;
  95. }
  96. public Enumerable<string> get_query_all(string name) {
  97. return query_params.get_or_empty(name);
  98. }
  99. public bool has_query(string name) {
  100. return query_params.has(name);
  101. }
  102. // Cookie access methods
  103. public string? get_cookie(string name) {
  104. string? value;
  105. if (cookies.try_get_any(name, out value)) {
  106. return value;
  107. }
  108. return null;
  109. }
  110. public string get_cookie_or_default(string name, string default_value) {
  111. string? value;
  112. if (cookies.try_get_any(name, out value)) {
  113. return value;
  114. }
  115. return default_value;
  116. }
  117. public Enumerable<string> get_cookie_all(string name) {
  118. return cookies.get_or_empty(name);
  119. }
  120. public bool has_cookie(string name) {
  121. return cookies.has(name);
  122. }
  123. public bool is_form_urlencoded() {
  124. return content_type.contains("application/x-www-form-urlencoded");
  125. }
  126. public bool is_json() {
  127. return content_type.contains("application/json");
  128. }
  129. public bool is_multipart() {
  130. return content_type.contains("multipart/form-data");
  131. }
  132. // Helper method
  133. private static int64 parse_content_length(string? value) {
  134. if (value == null) return 0;
  135. return int64.parse(value);
  136. }
  137. }
  138. public class HttpContext : Object {
  139. public HttpRequest request { get; private set; }
  140. internal HttpContext(HttpRequest request) {
  141. this.request = request;
  142. }
  143. }
  144. }