11const std = @import ("std" );
2+ const http = std .http ;
23
34const Allocator = std .mem .Allocator ;
45const ArrayList = std .ArrayList ;
56
6- pub const HttpMethod = enum {
7- GET ,
8- POST ,
9- PUT ,
10- DELETE ,
11- PATCH ,
12- HEAD ,
13- OPTIONS ,
14- TRACE ,
15- CONNECT ,
16-
17- pub fn fromString (method_str : []const u8 ) ? HttpMethod {
18- return std .meta .stringToEnum (HttpMethod , method_str ) orelse null ;
19- }
20- };
21-
227const ParserState = enum { headers , body };
238
249pub const Header = struct {
@@ -27,7 +12,7 @@ pub const Header = struct {
2712};
2813
2914pub const HttpRequest = struct {
30- method : ? HttpMethod ,
15+ method : ? http.Method ,
3116 url : []const u8 ,
3217 headers : ArrayList (Header ),
3318 body : ? []const u8 ,
@@ -120,7 +105,7 @@ pub fn parseContent(allocator: std.mem.Allocator, content: []const u8) !ArrayLis
120105 const method_str = tokens .next () orelse return error .InvalidRequestMissingMethod ;
121106 const url = tokens .next () orelse return error .InvalidRequestMissingURL ;
122107
123- current_request .method = HttpMethod . fromString ( method_str );
108+ current_request .method = std . meta . stringToEnum ( http . Method , method_str ) orelse null ;
124109 current_request .url = try allocator .dupe (u8 , url );
125110 state = .headers ;
126111 continue ;
@@ -183,8 +168,8 @@ test "HttpParser from String Contents" {
183168 requests .deinit ();
184169 }
185170
186- try std .testing .expectEqual (HttpMethod .GET , requests .items [0 ].method );
187- try std .testing .expectEqual (HttpMethod .POST , requests .items [1 ].method );
171+ try std .testing .expectEqual (http . Method .GET , requests .items [0 ].method );
172+ try std .testing .expectEqual (http . Method .POST , requests .items [1 ].method );
188173 try std .testing .expectEqualStrings ("https://api.example.com" , requests .items [0 ].url );
189174 try std .testing .expectEqualStrings ("https://api.example.com/users" , requests .items [1 ].url );
190175 try std .testing .expectEqualStrings ("Authorization" , requests .items [0 ].headers .items [1 ].name );
0 commit comments