@@ -36,13 +36,21 @@ pub const Assertion = struct {
3636 assertion_type : AssertionType ,
3737};
3838
39+ pub const HttpVersion = enum {
40+ @"HTTP/1.0" ,
41+ @"HTTP/1.1" ,
42+ @"HTTP/2" ,
43+ @"HTTP/3" ,
44+ };
45+
3946pub const HttpRequest = struct {
4047 // TODO: Null HTTP METHOD SHOULD NOT BE CRITICAL TO PROPERLY PARSING
4148 method : ? http.Method ,
4249 url : []const u8 ,
4350 headers : ArrayList (http .Header ),
4451 body : ? []u8 ,
4552 assertions : ArrayList (Assertion ),
53+ version : HttpVersion ,
4654 // TODO: Add a name for the request if needed.
4755
4856 pub fn init () HttpRequest {
@@ -52,6 +60,7 @@ pub const HttpRequest = struct {
5260 .headers = .empty ,
5361 .body = null ,
5462 .assertions = .empty ,
63+ .version = .@"HTTP/1.1" ,
5564 };
5665 }
5766
@@ -136,8 +145,12 @@ pub fn parseContent(allocator: Allocator, content: []const u8) !ArrayList(HttpRe
136145 var tokens = std .mem .tokenizeScalar (u8 , trimmed_line , ' ' );
137146 const method_str = tokens .next () orelse return error .InvalidRequestMissingMethod ;
138147 const url = tokens .next () orelse return error .InvalidRequestMissingURL ;
148+ const version = tokens .next ();
139149 current_request .method = std .meta .stringToEnum (http .Method , method_str ) orelse null ;
140150 current_request .url = try allocator .dupe (u8 , url );
151+ if (version ) | v | {
152+ current_request .version = std .meta .stringToEnum (HttpVersion , v ) orelse return error .InvalidHttpVersion ;
153+ }
141154 state = .headers ;
142155 continue ;
143156 }
@@ -171,7 +184,7 @@ pub fn parseContent(allocator: Allocator, content: []const u8) !ArrayList(HttpRe
171184
172185test "HttpParser from String Contents" {
173186 const test_http_contents =
174- \\GET https://api.example.com
187+ \\GET https://api.example.com HTTP/3
175188 \\Accept: */*
176189 \\Authorization: Bearer ABC123
177190 \\
@@ -199,6 +212,7 @@ test "HttpParser from String Contents" {
199212 try std .testing .expectEqual (http .Method .POST , requests .items [1 ].method );
200213 try std .testing .expectEqualStrings ("https://api.example.com" , requests .items [0 ].url );
201214 try std .testing .expectEqualStrings ("https://api.example.com/users" , requests .items [1 ].url );
215+ try std .testing .expectEqual (HttpVersion .@"HTTP/3" , requests .items [0 ].version );
202216 try std .testing .expectEqualStrings ("Authorization" , requests .items [0 ].headers .items [1 ].name );
203217 try std .testing .expectEqualStrings ("Bearer ABC123" , requests .items [0 ].headers .items [1 ].value );
204218 try std .testing .expectEqualStrings ("Authorization" , requests .items [1 ].headers .items [1 ].name );
0 commit comments