|
| 1 | +module fasthttp |
| 2 | + |
| 3 | +const empty_space = u8(` `) |
| 4 | +const cr_char = u8(`\r`) |
| 5 | +const lf_char = u8(`\n`) |
| 6 | + |
| 7 | +// libc memchr is AVX2-accelerated via glibc IFUNC |
| 8 | +@[inline] |
| 9 | +fn find_byte(buf &u8, len int, c u8) int { |
| 10 | + unsafe { |
| 11 | + p := C.memchr(buf, c, len) |
| 12 | + if p == voidptr(nil) { |
| 13 | + return -1 |
| 14 | + } |
| 15 | + return int(&u8(p) - buf) |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +// parse_http1_request_line parses the request line of an HTTP/1.1 request. |
| 20 | +// spec: https://datatracker.ietf.org/doc/rfc9112/ |
| 21 | +// request-line is the start-line for for requests |
| 22 | +// According to RFC 9112, the request line is structured as: |
| 23 | +// `request-line = method SP request-target SP HTTP-version` |
| 24 | +// where: |
| 25 | +// METHOD is the HTTP method (e.g., GET, POST) |
| 26 | +// SP is a single space character |
| 27 | +// REQUEST-TARGET is the path or resource being requested |
| 28 | +// HTTP-VERSION is the version of HTTP being used (e.g., HTTP/1.1) |
| 29 | +// CRLF is a carriage return followed by a line feed |
| 30 | +// returns the position after the CRLF on success |
| 31 | +@[direct_array_access] |
| 32 | +pub fn parse_http1_request_line(mut req HttpRequest) !int { |
| 33 | + unsafe { |
| 34 | + buf := &req.buffer[0] |
| 35 | + len := req.buffer.len |
| 36 | + |
| 37 | + if len < 12 { |
| 38 | + return error('Too short') |
| 39 | + } |
| 40 | + |
| 41 | + // METHOD |
| 42 | + pos1 := find_byte(buf, len, empty_space) |
| 43 | + if pos1 <= 0 { |
| 44 | + return error('Invalid method') |
| 45 | + } |
| 46 | + req.method = Slice{0, pos1} |
| 47 | + |
| 48 | + // PATH - skip any extra spaces |
| 49 | + mut pos2 := pos1 + 1 |
| 50 | + for pos2 < len && buf[pos2] == empty_space { |
| 51 | + pos2++ |
| 52 | + } |
| 53 | + if pos2 >= len { |
| 54 | + return error('Missing path') |
| 55 | + } |
| 56 | + |
| 57 | + path_start := pos2 |
| 58 | + space_pos := find_byte(buf + pos2, len - pos2, empty_space) |
| 59 | + cr_pos := find_byte(buf + pos2, len - pos2, cr_char) |
| 60 | + |
| 61 | + if space_pos < 0 && cr_pos < 0 { |
| 62 | + return error('Invalid request line') |
| 63 | + } |
| 64 | + |
| 65 | + // pick earliest delimiter |
| 66 | + mut path_len := 0 |
| 67 | + mut delim_pos := 0 |
| 68 | + if space_pos >= 0 && (cr_pos < 0 || space_pos < cr_pos) { |
| 69 | + path_len = space_pos |
| 70 | + delim_pos = pos2 + space_pos |
| 71 | + } else { |
| 72 | + path_len = cr_pos |
| 73 | + delim_pos = pos2 + cr_pos |
| 74 | + } |
| 75 | + |
| 76 | + req.path = Slice{path_start, path_len} |
| 77 | + |
| 78 | + // VERSION |
| 79 | + if buf[delim_pos] == cr_char { |
| 80 | + // No HTTP version specified |
| 81 | + req.version = Slice{delim_pos, 0} |
| 82 | + } else { |
| 83 | + version_start := delim_pos + 1 |
| 84 | + cr := find_byte(buf + version_start, len - version_start, cr_char) |
| 85 | + if cr < 0 { |
| 86 | + return error('Invalid HTTP request line: Missing CR') |
| 87 | + } |
| 88 | + req.version = Slice{version_start, cr} |
| 89 | + delim_pos = version_start + cr |
| 90 | + } |
| 91 | + |
| 92 | + // Validate CRLF |
| 93 | + if delim_pos + 1 >= len || buf[delim_pos + 1] != lf_char { |
| 94 | + return error('Invalid CRLF') |
| 95 | + } |
| 96 | + |
| 97 | + return delim_pos + 2 // Return position after CRLF |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// decode_http_request parses a raw HTTP request from the given byte buffer |
| 102 | +pub fn decode_http_request(buffer []u8) !HttpRequest { |
| 103 | + mut req := HttpRequest{ |
| 104 | + buffer: buffer |
| 105 | + } |
| 106 | + |
| 107 | + // header_start is the byte index immediately after the request line's \r\n |
| 108 | + header_start := parse_http1_request_line(mut req)! |
| 109 | + |
| 110 | + // Find the end of the header block (\r\n\r\n) |
| 111 | + mut body_start := -1 |
| 112 | + for i := header_start; i <= buffer.len - 4; i++ { |
| 113 | + if buffer[i] == cr_char && buffer[i + 1] == lf_char && buffer[i + 2] == cr_char |
| 114 | + && buffer[i + 3] == lf_char { |
| 115 | + body_start = i + 4 |
| 116 | + |
| 117 | + // The header fields slice covers everything from header_start |
| 118 | + // up to (but not including) the final double CRLF |
| 119 | + req.header_fields = Slice{ |
| 120 | + start: header_start |
| 121 | + len: i - header_start |
| 122 | + } |
| 123 | + break |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + if body_start != -1 { |
| 128 | + req.body = Slice{ |
| 129 | + start: body_start |
| 130 | + len: buffer.len - body_start |
| 131 | + } |
| 132 | + } else { |
| 133 | + // If no body delimiter found, assume headers go to end or body is missing |
| 134 | + req.header_fields = Slice{header_start, buffer.len - header_start - 2} |
| 135 | + req.body = Slice{0, 0} |
| 136 | + } |
| 137 | + |
| 138 | + return req |
| 139 | +} |
| 140 | + |
| 141 | +// Helper function to convert Slice to string for debugging |
| 142 | +fn (slice Slice) to_string(buffer []u8) string { |
| 143 | + if slice.len <= 0 { |
| 144 | + return '' |
| 145 | + } |
| 146 | + return buffer[slice.start..slice.start + slice.len].bytestr() |
| 147 | +} |
0 commit comments