@@ -19,6 +19,8 @@ pub const HttpMethod = enum {
1919 }
2020};
2121
22+ const ParserState = enum { headers , body };
23+
2224pub const Header = struct {
2325 name : []const u8 ,
2426 value : []const u8 ,
@@ -76,11 +78,10 @@ pub fn parseContent(allocator: std.mem.Allocator, content: []const u8) !ArrayLis
7678
7779 var current_request = HttpRequest .init (allocator );
7880 errdefer current_request .deinit (allocator );
81+ var state : ? ParserState = null ;
7982
8083 var lines = std .mem .splitScalar (u8 , content , '\n ' );
8184 var line_index : usize = 0 ;
82- var in_headers = false ;
83- var in_body = false ;
8485 var body_buffer = ArrayList (u8 ).init (allocator );
8586 defer body_buffer .deinit ();
8687
@@ -89,25 +90,23 @@ pub fn parseContent(allocator: std.mem.Allocator, content: []const u8) !ArrayLis
8990 const trimmed_line = std .mem .trim (u8 , line , & std .ascii .whitespace );
9091
9192 if (trimmed_line .len == 0 ) {
92- if (in_headers ) {
93- in_headers = false ;
94- in_body = true ;
93+ if (state == .headers ) {
94+ state = .body ;
9595 }
9696 continue ;
9797 }
9898
9999 if (std .mem .startsWith (u8 , trimmed_line , "###" )) {
100100 if (current_request .method != null ) {
101101 // if we're in the body, dupe it and clear it
102- if (in_body and body_buffer .items .len > 0 ) {
102+ if (state == .body and body_buffer .items .len > 0 ) {
103103 current_request .body = try allocator .dupe (u8 , body_buffer .items );
104104 body_buffer .clearRetainingCapacity ();
105105 }
106106
107107 try requests .append (current_request );
108108 current_request = HttpRequest .init (allocator );
109- in_body = false ;
110- in_headers = false ;
109+ state = null ;
111110 }
112111 continue ;
113112 }
@@ -116,18 +115,18 @@ pub fn parseContent(allocator: std.mem.Allocator, content: []const u8) !ArrayLis
116115 continue ;
117116 }
118117
119- if (! in_headers and ! in_body and current_request .method == null ) {
118+ if (state == null and current_request .method == null ) {
120119 var tokens = std .mem .tokenizeScalar (u8 , trimmed_line , ' ' );
121120 const method_str = tokens .next () orelse return error .InvalidRequestMissingMethod ;
122121 const url = tokens .next () orelse return error .InvalidRequestMissingURL ;
123122
124123 current_request .method = HttpMethod .fromString (method_str );
125124 current_request .url = try allocator .dupe (u8 , url );
126- in_headers = true ;
125+ state = .headers ;
127126 continue ;
128127 }
129128
130- if (in_headers ) {
129+ if (state == .headers ) {
131130 if (std .mem .indexOf (u8 , trimmed_line , ":" )) | colon_pos | {
132131 const header_name = std .mem .trim (u8 , trimmed_line [0.. colon_pos ], & std .ascii .whitespace );
133132 const header_value = std .mem .trim (u8 , trimmed_line [colon_pos + 1 .. ], & std .ascii .whitespace );
@@ -141,14 +140,14 @@ pub fn parseContent(allocator: std.mem.Allocator, content: []const u8) !ArrayLis
141140 }
142141 }
143142
144- if (in_body ) {
143+ if (state == .body ) {
145144 try body_buffer .appendSlice (trimmed_line );
146145 try body_buffer .append ('\n ' );
147146 }
148147 }
149148
150149 if (current_request .method != null ) {
151- if (in_body and body_buffer .items .len > 0 ) {
150+ if (state == .body and body_buffer .items .len > 0 ) {
152151 current_request .body = try allocator .dupe (u8 , body_buffer .items );
153152 }
154153
0 commit comments